Store the UI theme in the LocalStorage

This commit is contained in:
He4eT 2021-03-24 17:09:57 +05:00
commit 7f143925ac
6 changed files with 28 additions and 25 deletions

View file

@ -5,7 +5,7 @@ import {
buildPlayLinkHref buildPlayLinkHref
} from '~/src/utils/utils.routing' } from '~/src/utils/utils.routing'
export default ({ name, ifdb, url, theme }) => ( export default ({ name, ifdb, url }) => (
<div> <div>
<h4>{name}</h4> <h4>{name}</h4>
<a <a
@ -15,7 +15,7 @@ export default ({ name, ifdb, url, theme }) => (
</a> </a>
<span> | </span> <span> | </span>
<Link <Link
href={buildPlayLinkHref({ url, theme })}> href={buildPlayLinkHref({ url })}>
Play Play
</Link> </Link>
</div> </div>

View file

@ -1,5 +1,5 @@
import { h, render } from 'preact' import { h, render } from 'preact'
import { Route, Router, Redirect, Switch } from 'wouter-preact' import { Route, Router, Switch } from 'wouter-preact'
import { import {
useHashLocation, useHashLocation,
@ -34,17 +34,16 @@ function App () {
themeEngine themeEngine
}} /> }} />
</Route> </Route>
<Route path='/games/:theme'> <Route path='/games/'>
{params => <GamesView {...{ <GamesView />
</Route>
<Route path='/play/:encodedUrl'>
{params => <PlayerView {...{
...themeEngine, ...themeEngine,
...params ...params
}} />} }} />}
</Route> </Route>
<Route path='/games'> <Route path='/play/:encodedUrl/:theme'>
<Redirect
to={`/games/${themeEngine.currentTheme}/`} />
</Route>
<Route path='/play/:theme/:encodedUrl'>
{params => <PlayerView {...{ {params => <PlayerView {...{
...themeEngine, ...themeEngine,
...params ...params

View file

@ -16,19 +16,28 @@ const themes = [
'wasp' 'wasp'
] ]
const LS_THEME_KEY = 'ifplayer/theme'
const DEFAULT_THEME = themes[0] const DEFAULT_THEME = themes[0]
const getSavedTheme = () => {
const savedTheme = localStorage.getItem(LS_THEME_KEY)
return savedTheme || DEFAULT_THEME
}
const assertTheme = theme => const assertTheme = theme =>
themes.includes(theme) themes.includes(theme)
? theme ? theme
: DEFAULT_THEME : getSavedTheme()
export const useThemeEngine = (initialTheme = DEFAULT_THEME) => { export const useThemeEngine = (initialTheme = getSavedTheme()) => {
const [currentTheme, setCurrentTheme] = const [currentTheme, setCurrentTheme] =
useState(initialTheme) useState(initialTheme)
const setTheme = theme => { const setTheme = theme => {
setCurrentTheme(assertTheme(theme)) const newTheme = assertTheme(theme)
setCurrentTheme(newTheme)
localStorage.setItem(LS_THEME_KEY, newTheme)
} }
return { currentTheme, setTheme, themes } return { currentTheme, setTheme, themes }

View file

@ -21,8 +21,8 @@ export const useHashLocation = () => {
return [loc, navigate] return [loc, navigate]
} }
export const buildPlayLinkHref = ({ url, theme }) => export const buildPlayLinkHref = ({ url }) =>
`/#/play/${theme}/${encodeURIComponent(url)}` `/#/play/${encodeURIComponent(url)}`
export const extractView = location => { export const extractView = location => {
const currentView = location.split('/').filter(Boolean)[0] const currentView = location.split('/').filter(Boolean)[0]

View file

@ -1,5 +1,4 @@
import { h } from 'preact' import { h } from 'preact'
import { useEffect } from 'preact/hooks'
import { Link } from 'wouter-preact' import { Link } from 'wouter-preact'
import GameEntry from import GameEntry from
@ -15,9 +14,7 @@ const tutorialGame = {
url: 'http://mirror.ifarchive.org/if-archive/games/zcode/dreamhold.z8' url: 'http://mirror.ifarchive.org/if-archive/games/zcode/dreamhold.z8'
} }
export default function ({ setTheme, theme }) { export default function () {
useEffect(() => setTheme(theme), [theme])
return ( return (
<main className='view games'> <main className='view games'>
@ -32,7 +29,7 @@ export default function ({ setTheme, theme }) {
<p> <p>
Choose one or <Link href='/'> Choose one or <Link href='/'>
go back </Link>. go back</Link>.
</p> </p>
<h3> <h3>
@ -48,8 +45,7 @@ export default function ({ setTheme, theme }) {
<ul> <ul>
<li> <li>
<GameEntry {...{ <GameEntry {...{
...tutorialGame, ...tutorialGame
theme
}} /> }} />
</li> </li>
</ul> </ul>
@ -77,8 +73,7 @@ export default function ({ setTheme, theme }) {
{top2019.map(game => ( {top2019.map(game => (
<li> <li>
<GameEntry {...{ <GameEntry {...{
...game, ...game
theme
}} /> }} />
</li> </li>
))} ))}

View file

@ -64,7 +64,7 @@ export default function ({ themeEngine }) {
</h3> </h3>
<p> <p>
<Link href={`/#/games/${themeEngine.currentTheme}`}> <Link href={'/#/games/'}>
IFDB games IFDB games
</Link> </Link>
</p> </p>