Skip to content

Commit

Permalink
♻️ refactor: 收敛切换 SideBar 方法为 useSwitchSideBarOnInit
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Aug 12, 2023
1 parent 8359b62 commit bbad38f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
9 changes: 2 additions & 7 deletions src/pages/chat/[id]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Flexbox } from 'react-layout-kit';
import { shallow } from 'zustand/shallow';

import AppLayout from '@/layout/AppLayout';
import { useGlobalStore } from '@/store/global';
import { useSwitchSideBarOnInit } from '@/store/global';
import { useSessionStore } from '@/store/session';

import { Sessions } from '../SessionList';
Expand All @@ -31,12 +31,7 @@ const ChatLayout = memo<PropsWithChildren>(({ children }) => {
toggleTopic();
}, [id]);

useEffect(() => {
const hasRehydrated = useGlobalStore.persist.hasHydrated();
if (hasRehydrated) {
useGlobalStore.setState({ sidebarKey: 'chat' });
}
}, []);
useSwitchSideBarOnInit('chat');

return (
<AppLayout>
Expand Down
9 changes: 2 additions & 7 deletions src/pages/settings/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Flexbox } from 'react-layout-kit';

import SideBar from '@/features/SideBar';
import { createI18nNext } from '@/locales/create';
import { useGlobalStore } from '@/store/global';
import { useSwitchSideBarOnInit } from '@/store/global';
import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';

const initI18n = createI18nNext('setting');
Expand All @@ -18,12 +18,7 @@ const SettingLayout = memo<{ children: ReactNode }>(({ children }) => {
initI18n.finally();
}, []);

useEffect(() => {
const hasRehydrated = useGlobalStore.persist.hasHydrated();
if (hasRehydrated) {
useGlobalStore.setState({ sidebarKey: 'settings' });
}
}, []);
useSwitchSideBarOnInit('settings');

return (
<>
Expand Down
2 changes: 2 additions & 0 deletions src/store/global/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useHydrated';
export * from './useSwitchSideBarOnInit';
17 changes: 17 additions & 0 deletions src/store/global/hooks/useSwitchSideBarOnInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect } from 'react';

import { SidebarTabKey } from '../initialState';
import { useGlobalStore } from '../store';

/**
* 切换侧边栏选项
* @desc 只会在应用初始化时(且水合后)执行一次
*/
export const useSwitchSideBarOnInit = (key: SidebarTabKey) => {
useEffect(() => {
const hasRehydrated = useGlobalStore.persist.hasHydrated();
if (hasRehydrated) {
useGlobalStore.getState().switchSideBar(key);
}
}, []);
};
1 change: 1 addition & 0 deletions src/store/global/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './hooks';
export * from './selectors';
export * from './store';
13 changes: 8 additions & 5 deletions src/store/global/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ const createStore: StateCreator<GlobalStore, [['zustand/devtools', never]]> = (.
});

// =============== persist 本地缓存中间件配置 ============ //

const LOBE_SETTINGS = 'LOBE_SETTINGS';

const persistOptions: PersistOptions<GlobalStore> = {
name: LOBE_SETTINGS,
type GlobalPersist = Pick<GlobalStore, 'preference' | 'settings'>;

const persistOptions: PersistOptions<GlobalStore, GlobalPersist> = {
name: 'LOBE_SETTINGS',
partialize: (s) => ({
preference: s.preference,
settings: s.settings,
}),
skipHydration: true,
};

Expand Down

0 comments on commit bbad38f

Please sign in to comment.