diff options
Diffstat (limited to 'src/store')
| -rw-r--r-- | src/store/app.ts | 50 | ||||
| -rw-r--r-- | src/store/cache.ts | 9 | ||||
| -rw-r--r-- | src/store/dashboard.ts | 22 | ||||
| -rw-r--r-- | src/store/version.ts | 55 | ||||
| -rw-r--r-- | src/store/websites.ts | 35 |
5 files changed, 171 insertions, 0 deletions
diff --git a/src/store/app.ts b/src/store/app.ts new file mode 100644 index 0000000..bb54e56 --- /dev/null +++ b/src/store/app.ts @@ -0,0 +1,50 @@ +import { create } from 'zustand'; +import { + DATE_RANGE_CONFIG, + DEFAULT_DATE_RANGE_VALUE, + DEFAULT_LOCALE, + DEFAULT_THEME, + LOCALE_CONFIG, + THEME_CONFIG, + TIMEZONE_CONFIG, +} from '@/lib/constants'; +import { getTimezone } from '@/lib/date'; +import { getItem } from '@/lib/storage'; + +const initialState = { + locale: getItem(LOCALE_CONFIG) || process.env.defaultLocale || DEFAULT_LOCALE, + theme: getItem(THEME_CONFIG) || DEFAULT_THEME, + timezone: getItem(TIMEZONE_CONFIG) || getTimezone(), + dateRangeValue: getItem(DATE_RANGE_CONFIG) || DEFAULT_DATE_RANGE_VALUE, + shareToken: null, + user: null, + config: null, +}; + +const store = create(() => ({ ...initialState })); + +export function setTimezone(timezone: string) { + store.setState({ timezone }); +} + +export function setLocale(locale: string) { + store.setState({ locale }); +} + +export function setShareToken(shareToken: string) { + store.setState({ shareToken }); +} + +export function setUser(user: object) { + store.setState({ user }); +} + +export function setConfig(config: object) { + store.setState({ config }); +} + +export function setDateRangeValue(dateRangeValue: string) { + store.setState({ dateRangeValue }); +} + +export const useApp = store; diff --git a/src/store/cache.ts b/src/store/cache.ts new file mode 100644 index 0000000..8ac9384 --- /dev/null +++ b/src/store/cache.ts @@ -0,0 +1,9 @@ +import { create } from 'zustand'; + +const store = create(() => ({})); + +export function setValue(key: string, value: any) { + store.setState({ [key]: value }); +} + +export const useCache = store; diff --git a/src/store/dashboard.ts b/src/store/dashboard.ts new file mode 100644 index 0000000..93f59ed --- /dev/null +++ b/src/store/dashboard.ts @@ -0,0 +1,22 @@ +import { create } from 'zustand'; +import { DASHBOARD_CONFIG, DEFAULT_WEBSITE_LIMIT } from '@/lib/constants'; +import { getItem, setItem } from '@/lib/storage'; + +export const initialState = { + showCharts: true, + limit: DEFAULT_WEBSITE_LIMIT, + websiteOrder: [], + websiteActive: [], + editing: false, + isEdited: false, +}; + +const store = create(() => ({ ...initialState, ...getItem(DASHBOARD_CONFIG) })); + +export function saveDashboard(settings) { + store.setState(settings); + + setItem(DASHBOARD_CONFIG, store.getState()); +} + +export const useDashboard = store; diff --git a/src/store/version.ts b/src/store/version.ts new file mode 100644 index 0000000..95367af --- /dev/null +++ b/src/store/version.ts @@ -0,0 +1,55 @@ +import { produce } from 'immer'; +import semver from 'semver'; +import { create } from 'zustand'; +import { CURRENT_VERSION, UPDATES_URL, VERSION_CHECK } from '@/lib/constants'; +import { getItem } from '@/lib/storage'; + +const initialState = { + current: CURRENT_VERSION, + latest: null, + hasUpdate: false, + checked: false, + releaseUrl: null, +}; + +const store = create(() => ({ ...initialState })); + +export async function checkVersion() { + const { current } = store.getState(); + + const data = await fetch(`${UPDATES_URL}?v=${current}`, { + method: 'GET', + headers: { + Accept: 'application/json', + }, + }).then(res => { + if (res.ok) { + return res.json(); + } + + return null; + }); + + if (!data) { + return; + } + + store.setState( + produce(state => { + const { latest, url } = data; + const lastCheck = getItem(VERSION_CHECK); + + const hasUpdate = !!(latest && lastCheck?.version !== latest && semver.gt(latest, current)); + + state.current = current; + state.latest = latest; + state.hasUpdate = hasUpdate; + state.checked = true; + state.releaseUrl = url; + + return state; + }), + ); +} + +export const useVersion = store; diff --git a/src/store/websites.ts b/src/store/websites.ts new file mode 100644 index 0000000..4ddcab0 --- /dev/null +++ b/src/store/websites.ts @@ -0,0 +1,35 @@ +import { produce } from 'immer'; +import { create } from 'zustand'; +import type { DateRange } from '@/lib/types'; + +const store = create(() => ({})); + +export function setWebsiteDateRange(websiteId: string, dateRange: DateRange) { + store.setState( + produce(state => { + if (!state[websiteId]) { + state[websiteId] = {}; + } + + state[websiteId].dateRange = { ...dateRange, modified: Date.now() }; + + return state; + }), + ); +} + +export function setWebsiteDateCompare(websiteId: string, dateCompare: string) { + store.setState( + produce(state => { + if (!state[websiteId]) { + state[websiteId] = {}; + } + + state[websiteId].dateCompare = dateCompare; + + return state; + }), + ); +} + +export const useWebsites = store; |