fix: nextjs 14.2.24 cannot auto create local storage (#4249) (#4250)

This commit is contained in:
Archer 2025-03-20 11:40:47 +08:00 committed by GitHub
parent de87639fce
commit 9a1fff74fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 19 deletions

View File

@ -248,9 +248,7 @@ const OutLink = (props: Props) => {
<Flex <Flex
h={'full'} h={'full'}
gap={4} gap={4}
{...(isEmbed {...(isEmbed ? { p: '0 !important', borderRadius: '0', boxShadow: 'none' } : { p: [0, 5] })}
? { p: '0 !important', insertProps: { borderRadius: '0', boxShadow: 'none' } }
: { p: [0, 5] })}
> >
{(!quoteData || isPc) && ( {(!quoteData || isPc) && (
<PageContainer flex={'1 0 0'} w={0} isLoading={loading} p={'0 !important'}> <PageContainer flex={'1 0 0'} w={0} isLoading={loading} p={'0 !important'}>
@ -316,12 +314,12 @@ const OutLink = (props: Props) => {
const Render = (props: Props) => { const Render = (props: Props) => {
const { shareId, authToken, customUid, appId } = props; const { shareId, authToken, customUid, appId } = props;
const { localUId } = useShareChatStore(); const { localUId, setLocalUId, loaded } = useShareChatStore();
const { source, chatId, setSource, setAppId, setOutLinkAuthData } = useChatStore(); const { source, chatId, setSource, setAppId, setOutLinkAuthData } = useChatStore();
const { setUserDefaultLng } = useI18nLng(); const { setUserDefaultLng } = useI18nLng();
const chatHistoryProviderParams = useMemo(() => { const chatHistoryProviderParams = useMemo(() => {
return { shareId, outLinkUid: authToken || customUid || localUId }; return { shareId, outLinkUid: authToken || customUid || localUId || '' };
}, [authToken, customUid, localUId, shareId]); }, [authToken, customUid, localUId, shareId]);
const chatRecordProviderParams = useMemo(() => { const chatRecordProviderParams = useMemo(() => {
return { return {
@ -338,20 +336,32 @@ const Render = (props: Props) => {
setUserDefaultLng(true); setUserDefaultLng(true);
}); });
// Set outLinkAuthData // Set default localUId
useEffect(() => { useEffect(() => {
setOutLinkAuthData({ if (loaded) {
shareId, if (!localUId) {
outLinkUid: chatHistoryProviderParams.outLinkUid setLocalUId(`shareChat-${Date.now()}-${getNanoid(24)}`);
}); }
}
}, [loaded, localUId, setLocalUId]);
// Init outLinkAuthData
useEffect(() => {
if (chatHistoryProviderParams.outLinkUid) {
setOutLinkAuthData({
shareId,
outLinkUid: chatHistoryProviderParams.outLinkUid
});
}
return () => { return () => {
setOutLinkAuthData({}); setOutLinkAuthData({});
}; };
}, [chatHistoryProviderParams.outLinkUid, shareId]); }, [chatHistoryProviderParams.outLinkUid, setOutLinkAuthData, shareId]);
// Watch appId // Watch appId
useEffect(() => { useEffect(() => {
setAppId(appId); setAppId(appId);
}, [appId]); }, [appId, setAppId]);
return source === ChatSourceEnum.share ? ( return source === ChatSourceEnum.share ? (
<ChatContextProvider params={chatHistoryProviderParams}> <ChatContextProvider params={chatHistoryProviderParams}>

View File

@ -1,14 +1,10 @@
import { create } from 'zustand'; import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware'; import { devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer'; import { immer } from 'zustand/middleware/immer';
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet(
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ1234567890_',
24
);
type State = { type State = {
localUId: string; localUId?: string;
setLocalUId: (localUId: string) => void;
loaded: boolean; loaded: boolean;
}; };
@ -16,7 +12,10 @@ export const useShareChatStore = create<State>()(
devtools( devtools(
persist( persist(
immer((set, get) => ({ immer((set, get) => ({
localUId: `shareChat-${Date.now()}-${nanoid()}`, localUId: undefined,
setLocalUId(localUId: string) {
set({ localUId });
},
loaded: false loaded: false
})), })),
{ {