forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 fix: fix mobile route (lobehub#165)
- Loading branch information
Showing
6 changed files
with
38 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |