umami/src/store/app.ts

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-05-20 18:02:08 +02:00
import { create } from 'zustand';
import {
DATE_RANGE_CONFIG,
DEFAULT_DATE_RANGE,
DEFAULT_LOCALE,
DEFAULT_THEME,
LOCALE_CONFIG,
THEME_CONFIG,
TIMEZONE_CONFIG,
} from 'lib/constants';
2022-08-29 05:20:54 +02:00
import { getItem } from 'next-basics';
import { getTimezone } from 'lib/date';
2024-03-19 18:38:16 +01:00
function getDefaultTheme() {
return typeof window !== 'undefined'
? window?.matchMedia('(prefers-color-scheme: dark)')?.matches
? 'dark'
: 'light'
: 'light';
}
const initialState = {
locale: getItem(LOCALE_CONFIG) || DEFAULT_LOCALE,
2024-03-19 18:38:16 +01:00
theme: getItem(THEME_CONFIG) || getDefaultTheme() || DEFAULT_THEME,
timezone: getItem(TIMEZONE_CONFIG) || getTimezone(),
dateRange: getItem(DATE_RANGE_CONFIG) || DEFAULT_DATE_RANGE,
shareToken: null,
user: null,
config: null,
};
const store = create(() => ({ ...initialState }));
2024-02-24 05:31:35 +01:00
export function setTheme(theme: string) {
store.setState({ theme });
}
export function setTimezone(timezone: string) {
store.setState({ timezone });
}
2024-02-24 05:31:35 +01:00
export function setLocale(locale: string) {
store.setState({ locale });
}
2024-02-24 05:31:35 +01:00
export function setShareToken(shareToken: string) {
store.setState({ shareToken });
}
2024-02-24 05:31:35 +01:00
export function setUser(user: object) {
store.setState({ user });
}
2024-02-24 05:31:35 +01:00
export function setConfig(config: object) {
store.setState({ config });
}
2024-03-18 20:24:37 +01:00
export function setDateRange(dateRange: string | object) {
store.setState({ dateRange });
}
export default store;