Added localstorage support

pull/4/head
Antonio De Lucreziis 2 years ago
parent d623df9158
commit 96ca0fef8f

@ -10,6 +10,7 @@ import { Icon } from './components/Icon.jsx'
import { Popup } from './components/Popup.jsx' import { Popup } from './components/Popup.jsx'
import { Toolbar } from './components/Toolbar.jsx' import { Toolbar } from './components/Toolbar.jsx'
import { OptionBar } from './components/OptionBar.jsx' import { OptionBar } from './components/OptionBar.jsx'
import { usePersistentState } from './utils.jsx'
window._ = _ window._ = _
window.dataBuffer = {} window.dataBuffer = {}
@ -68,15 +69,15 @@ async function loadEventi(ids) {
const App = ({}) => { const App = ({}) => {
// Data Sources // Data Sources
const [source, setSource] = useState('magistrale') const [source, setSource] = usePersistentState('orario.source', 'magistrale')
const [eventi, setEventi] = useState([]) const [eventi, setEventi] = useState([])
// View Modes // View Modes
const [mode, setMode] = useState(MODE_COURSE) const [mode, setMode] = usePersistentState('orario.mode', MODE_COURSE)
// Selection // Selection
const [selectedCourses, setSelectedCourses] = useState([]) const [selectedCourses, setSelectedCourses] = usePersistentState('orario.selection', [])
const [hideOtherCourses, setHideOtherCourses] = useState(false) const [hideOtherCourses, setHideOtherCourses] = usePersistentState('orario.hide-other', false)
// Menus // Menus
const [helpVisible, setHelpVisible] = useState(false) const [helpVisible, setHelpVisible] = useState(false)
@ -99,19 +100,18 @@ const App = ({}) => {
if ( if (
selectedCourses.length === 0 || selectedCourses.length === 0 ||
selectedCourses.filter(id => groupIds.has(id)).length === 0 (eventi.length > 0 && selectedCourses.filter(id => groupIds.has(id)).length === 0)
) { ) {
setHideOtherCourses(false) setHideOtherCourses(false)
} }
}, [eventi, selectedCourses.length]) }, [eventi, selectedCourses.length])
const [theme, setTheme] = useState( const [theme, setTheme] = usePersistentState(
localStorage.getItem('theme') ?? 'orario.theme',
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
) )
document.body.classList.toggle('dark-mode', theme === 'dark') document.body.classList.toggle('dark-mode', theme === 'dark')
localStorage.setItem('theme', theme)
return ( return (
<> <>

@ -1,5 +1,7 @@
// Calendar // Calendar
import { useEffect, useState } from 'preact/hooks'
export const WEEK_DAYS = [ export const WEEK_DAYS = [
'Domenica', 'Domenica',
'Lunedì', 'Lunedì',
@ -53,3 +55,19 @@ export const withClasses = classes =>
.filter(([, value]) => value) .filter(([, value]) => value)
.map(([key]) => key) .map(([key]) => key)
.join(' ') .join(' ')
// Hooks
export const usePersistentState = (key, initialValue) => {
const previousValue = localStorage.getItem(key)
const [value, setValue] = useState(
previousValue !== null ? JSON.parse(previousValue) : initialValue
)
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value))
}, [value])
return [value, setValue]
}

Loading…
Cancel
Save