Skip to content

Commit

Permalink
🐛 fix: fix mobile route (lobehub#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Sep 9, 2023
1 parent 444d9ec commit d5e03b6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
5 changes: 0 additions & 5 deletions src/layout/AppMobileLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Flexbox } from 'react-layout-kit';

import SafeSpacing from '@/components/SafeSpacing';
import MobileTabBar from '@/features/MobileTabBar';
import { useOnFinishHydrationSession, useSessionStore } from '@/store/session';

const useStyles = createStyles(({ css, cx, stylish }) => ({
container: cx(
Expand Down Expand Up @@ -49,10 +48,6 @@ const AppMobileLayout = memo<AppMobileLayoutProps>(
({ children, showTabBar, navBar, style, className }) => {
const { styles, cx } = useStyles();

useOnFinishHydrationSession(() => {
useSessionStore.setState({ isMobile: true });
}, []);

return (
<>
<Head>
Expand Down
8 changes: 8 additions & 0 deletions src/pages/chat/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import { useSessionStore } from '@/store/session';
import { useEffectAfterSessionHydrated } from '@/store/session/hooks';
import { agentSelectors } from '@/store/session/selectors';
import { genSiteHeadTitle } from '@/utils/genSiteHeadTitle';

Expand All @@ -22,6 +23,13 @@ const Chat = memo(() => {
const pageTitle = genSiteHeadTitle([avatar, title].filter(Boolean).join(' '));
const RenderLayout = mobile ? MobileLayout : DesktopLayout;

useEffectAfterSessionHydrated(
(store) => {
store.setState({ isMobile: mobile });
},
[mobile],
);

return (
<>
<Head>
Expand Down
1 change: 1 addition & 0 deletions src/store/session/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useEffectAfterHydrated';
export * from './useOnFinishHydrationSession';
export * from './useSessionChatInit';
export * from './useSessionHydrated';
22 changes: 22 additions & 0 deletions src/store/session/hooks/useEffectAfterHydrated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react';

import { useSessionStore } from '../store';

export const useEffectAfterSessionHydrated = (
fn: (session: typeof useSessionStore) => void,
deps: any[] = [],
) => {
useEffect(() => {
const hasRehydrated = useSessionStore.persist.hasHydrated();

if (hasRehydrated) {
// 等价 useEffect 多次触发
fn(useSessionStore);
} else {
// 等价于 useEffect 第一次触发
useSessionStore.persist.onFinishHydration(() => {
fn(useSessionStore);
});
}
}, deps);
};
8 changes: 2 additions & 6 deletions src/store/session/hooks/useOnFinishHydrationSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import { SessionStore, useSessionStore } from '../store';
/**
* 当 Session 水合完毕后才会执行的 useEffect
* @param fn
* @param deps
*/
export const useOnFinishHydrationSession = (
fn: (state: SessionStore) => void,
deps: any[] = [],
) => {
export const useOnFinishHydrationSession = (fn: (state: SessionStore) => void) => {
useEffect(() => {
// 只有当水合完毕后再开始做操作
useSessionStore.persist.onFinishHydration(() => {
fn(useSessionStore.getState());
});
}, deps);
}, []);
};
11 changes: 5 additions & 6 deletions src/store/session/hooks/useSessionHydrated.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { useEffect, useState } from 'react';
import { useState } from 'react';

import { useSessionStore } from '../store';
import { useOnFinishHydrationSession } from './useOnFinishHydrationSession';

export const useSessionHydrated = () => {
// 根据 sessions 是否有值来判断是否已经初始化
const hasInited = !!Object.values(useSessionStore.getState().sessions).length;

const [isInit, setInit] = useState(hasInited);

useEffect(() => {
useSessionStore.persist.onFinishHydration(() => {
if (!isInit) setInit(true);
});
}, []);
useOnFinishHydrationSession(() => {
if (!isInit) setInit(true);
});

return isInit;
};

0 comments on commit d5e03b6

Please sign in to comment.