refactor: add persist for settings store

This commit is contained in:
moonrailgun 2023-12-08 22:05:34 +08:00
parent dc3bbe0b12
commit 674952da59
4 changed files with 23 additions and 7 deletions

View File

@ -16,8 +16,8 @@ import { WebsitePage } from './pages/Website';
import { useGlobalConfig } from './hooks/useConfig'; import { useGlobalConfig } from './hooks/useConfig';
import { useInjectWebsiteScript } from './hooks/useInjectWebsiteScript'; import { useInjectWebsiteScript } from './hooks/useInjectWebsiteScript';
import { ConfigProvider, theme } from 'antd'; import { ConfigProvider, theme } from 'antd';
import { useGlobalStateStore } from './store/global';
import clsx from 'clsx'; import clsx from 'clsx';
import { useSettingsStore } from './store/settings';
export const AppRoutes: React.FC = React.memo(() => { export const AppRoutes: React.FC = React.memo(() => {
const { info } = useUserStore(); const { info } = useUserStore();
@ -54,7 +54,7 @@ export const AppRoutes: React.FC = React.memo(() => {
AppRoutes.displayName = 'AppRoutes'; AppRoutes.displayName = 'AppRoutes';
export const App: React.FC = React.memo(() => { export const App: React.FC = React.memo(() => {
const colorScheme = useGlobalStateStore((state) => state.colorScheme); const colorScheme = useSettingsStore((state) => state.colorScheme);
const algorithm = const algorithm =
colorScheme === 'dark' ? theme.darkAlgorithm : theme.defaultAlgorithm; colorScheme === 'dark' ? theme.darkAlgorithm : theme.defaultAlgorithm;

View File

@ -1,13 +1,13 @@
import React from 'react'; import React from 'react';
import { useGlobalStateStore } from '../store/global';
import { LuMoon, LuSun } from 'react-icons/lu'; import { LuMoon, LuSun } from 'react-icons/lu';
import { useEvent } from '../hooks/useEvent'; import { useEvent } from '../hooks/useEvent';
import { Button } from 'antd'; import { Button } from 'antd';
import { useSettingsStore } from '../store/settings';
export const ColorSchemeSwitcher: React.FC = React.memo(() => { export const ColorSchemeSwitcher: React.FC = React.memo(() => {
const colorScheme = useGlobalStateStore((state) => state.colorScheme); const colorScheme = useSettingsStore((state) => state.colorScheme);
const handleSwitchColorScheme = useEvent(() => { const handleSwitchColorScheme = useEvent(() => {
useGlobalStateStore.setState({ useSettingsStore.setState({
colorScheme: colorScheme === 'dark' ? 'light' : 'dark', colorScheme: colorScheme === 'dark' ? 'light' : 'dark',
}); });
}); });

View File

@ -18,12 +18,10 @@ interface GlobalState {
dateRange: DateRange; dateRange: DateRange;
startDate: Dayjs | null; startDate: Dayjs | null;
endDate: Dayjs | null; endDate: Dayjs | null;
colorScheme: 'light' | 'dark';
} }
export const useGlobalStateStore = create<GlobalState>(() => ({ export const useGlobalStateStore = create<GlobalState>(() => ({
dateRange: DateRange.Last24Hours, dateRange: DateRange.Last24Hours,
startDate: null, startDate: null,
endDate: null, endDate: null,
colorScheme: 'light',
})); }));

View File

@ -0,0 +1,18 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
interface SettingsState {
colorScheme: 'light' | 'dark';
}
export const useSettingsStore = create<SettingsState>()(
persist(
() =>
({
colorScheme: 'light' as const,
} as SettingsState),
{
name: 'settings',
}
)
);