Extract themeEngine

This commit is contained in:
He4eT 2021-02-24 23:35:40 +05:00
commit c9f42e497a
5 changed files with 30 additions and 28 deletions

View file

@ -1,15 +1,16 @@
import { h } from 'preact'
export default function ({ themeList, currentTheme, setTheme }) {
const options = themeList.map(theme => (
export default function ({ themeEngine }) {
const options = themeEngine.themes.map(theme => (
<option value={theme}>
{theme}
</option>))
return (
<select
value={currentTheme}
onChange={({ target }) => setTheme(target.value)}>
value={themeEngine.currentTheme}
onChange={({ target }) =>
themeEngine.setTheme(target.value)}>
{options}
</select>)
}

View file

@ -9,7 +9,6 @@ const INITIAL_STATUS = {
}
export default function ({url}) {
console.log(123, url)
const [status, setStatus] = useState(INITIAL_STATUS)
const [vm, setVM] = useState(null)

View file

@ -1,12 +1,9 @@
import { h, render } from 'preact'
import { useState } from 'preact/hooks'
import { Route, Router, Switch } from 'wouter-preact'
import { useHashLocation } from '~/src/utils/utils.routing'
import {
DEFAULT_THEME,
themeList,
assertTheme
useThemeEngine
} from '~/src/themes/themes'
import HomeView from '~/src/views/HomeView'
@ -15,26 +12,20 @@ import PlayerView from '~/src/views/PlayerView'
import '~/src/style/base.css'
function App () {
const [currentTheme, setCurrentTheme] =
useState(DEFAULT_THEME)
const setTheme = theme =>
setCurrentTheme(assertTheme(theme))
const themeEngine = useThemeEngine()
return (
<Router hook={useHashLocation}>
<div className={['app', currentTheme].join(' ')}>
<div className={['app', themeEngine.currentTheme].join(' ')}>
<Switch>
<Route path='/'>
<HomeView {...{
setTheme,
themeList,
currentTheme
themeEngine
}} />
</Route>
<Route path='/play/:theme/:encodedUrl'>
{params => <PlayerView {...{
setTheme,
setTheme: themeEngine.setTheme,
...params
}} />}
</Route>

View file

@ -1,6 +1,8 @@
import { useState, useCallback } from 'preact/hooks'
import '~/src/style/themes.css'
export const themeList = [
const themes = [
'default',
'default-dark',
'nord',
@ -9,9 +11,20 @@ export const themeList = [
'_raw'
]
export const DEFAULT_THEME = themeList[0]
const DEFAULT_THEME = themes[0]
export const assertTheme = theme =>
themeList.includes(theme)
const assertTheme = theme =>
themes.includes(theme)
? theme
: DEFAULT_THEME
export const useThemeEngine = (defaultTheme = DEFAULT_THEME) => {
const [currentTheme, setCurrentTheme] =
useState(DEFAULT_THEME)
const setTheme = useCallback(theme => {
setCurrentTheme(assertTheme(theme))
}, [currentTheme]);
return { currentTheme, setTheme, themes };
}

View file

@ -14,7 +14,7 @@ const playButton = (name, url, theme) => (
Play "{name}"
</Link>)
export default function ({ themeList, currentTheme, setTheme }) {
export default function ({ themeEngine }) {
const [targetName, setTargetName] = useState(null)
const [targetUrl, setTargetUrl] = useState(null)
@ -28,9 +28,7 @@ export default function ({ themeList, currentTheme, setTheme }) {
<ul>
<li>
<ThemeSelector {...{
currentTheme,
themeList,
setTheme
themeEngine
}} />
</li>
<li>
@ -47,7 +45,7 @@ export default function ({ themeList, currentTheme, setTheme }) {
</li>
</ul>
{ targetUrl
? playButton(targetName, targetUrl, currentTheme)
? playButton(targetName, targetUrl, themeEngine.currentTheme)
: null }
</main>)
}