58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { create } from 'zustand';
|
|
|
|
export type Mode = 'edit' | 'view'
|
|
|
|
export type ColorScheme = 'light' | 'dark' | 'auto'
|
|
|
|
export interface AppState {
|
|
colorScheme: ColorScheme,
|
|
mapTab: Record<string, {
|
|
year: number | null,
|
|
region: number | null,
|
|
district: number | null
|
|
}>,
|
|
currentTab: string | null;
|
|
}
|
|
|
|
export const useAppStore = create<AppState>(() => ({
|
|
colorScheme: 'auto',
|
|
currentTab: null,
|
|
mapTab: {}
|
|
}))
|
|
|
|
const getColorScheme = () => {
|
|
useAppStore.getState().colorScheme
|
|
}
|
|
|
|
const setColorScheme = (colorScheme: ColorScheme) => {
|
|
useAppStore.setState(() => ({ colorScheme: colorScheme }))
|
|
localStorage.setItem('colorScheme', colorScheme.toString())
|
|
}
|
|
|
|
const getCurrentTab = () => useAppStore.getState().currentTab
|
|
const setCurrentTab = (id: string | null) => useAppStore.setState(() => ({ currentTab: id }))
|
|
|
|
const setMapTabYear = (id: string, year: number | null) =>
|
|
useAppStore.setState((state) => {
|
|
return {
|
|
mapTab: {
|
|
...state.mapTab,
|
|
[id]: { ...state.mapTab[id], year: year }
|
|
}
|
|
}
|
|
})
|
|
|
|
const deleteMapTab = (id: string) =>
|
|
useAppStore.setState((state) => {
|
|
const { [id]: _, ...remainingTabs } = state.mapTab;
|
|
return { mapTab: remainingTabs };
|
|
})
|
|
|
|
export {
|
|
deleteMapTab,
|
|
getCurrentTab,
|
|
setCurrentTab,
|
|
setMapTabYear,
|
|
getColorScheme,
|
|
setColorScheme
|
|
} |