Skip to content

Commit 9a1fff7

Browse files
authored
fix: nextjs 14.2.24 cannot auto create local storage (#4249) (#4250)
1 parent de87639 commit 9a1fff7

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

projects/app/src/pages/chat/share.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ const OutLink = (props: Props) => {
248248
<Flex
249249
h={'full'}
250250
gap={4}
251-
{...(isEmbed
252-
? { p: '0 !important', insertProps: { borderRadius: '0', boxShadow: 'none' } }
253-
: { p: [0, 5] })}
251+
{...(isEmbed ? { p: '0 !important', borderRadius: '0', boxShadow: 'none' } : { p: [0, 5] })}
254252
>
255253
{(!quoteData || isPc) && (
256254
<PageContainer flex={'1 0 0'} w={0} isLoading={loading} p={'0 !important'}>
@@ -316,12 +314,12 @@ const OutLink = (props: Props) => {
316314

317315
const Render = (props: Props) => {
318316
const { shareId, authToken, customUid, appId } = props;
319-
const { localUId } = useShareChatStore();
317+
const { localUId, setLocalUId, loaded } = useShareChatStore();
320318
const { source, chatId, setSource, setAppId, setOutLinkAuthData } = useChatStore();
321319
const { setUserDefaultLng } = useI18nLng();
322320

323321
const chatHistoryProviderParams = useMemo(() => {
324-
return { shareId, outLinkUid: authToken || customUid || localUId };
322+
return { shareId, outLinkUid: authToken || customUid || localUId || '' };
325323
}, [authToken, customUid, localUId, shareId]);
326324
const chatRecordProviderParams = useMemo(() => {
327325
return {
@@ -338,20 +336,32 @@ const Render = (props: Props) => {
338336
setUserDefaultLng(true);
339337
});
340338

341-
// Set outLinkAuthData
339+
// Set default localUId
342340
useEffect(() => {
343-
setOutLinkAuthData({
344-
shareId,
345-
outLinkUid: chatHistoryProviderParams.outLinkUid
346-
});
341+
if (loaded) {
342+
if (!localUId) {
343+
setLocalUId(`shareChat-${Date.now()}-${getNanoid(24)}`);
344+
}
345+
}
346+
}, [loaded, localUId, setLocalUId]);
347+
348+
// Init outLinkAuthData
349+
useEffect(() => {
350+
if (chatHistoryProviderParams.outLinkUid) {
351+
setOutLinkAuthData({
352+
shareId,
353+
outLinkUid: chatHistoryProviderParams.outLinkUid
354+
});
355+
}
347356
return () => {
348357
setOutLinkAuthData({});
349358
};
350-
}, [chatHistoryProviderParams.outLinkUid, shareId]);
359+
}, [chatHistoryProviderParams.outLinkUid, setOutLinkAuthData, shareId]);
360+
351361
// Watch appId
352362
useEffect(() => {
353363
setAppId(appId);
354-
}, [appId]);
364+
}, [appId, setAppId]);
355365

356366
return source === ChatSourceEnum.share ? (
357367
<ChatContextProvider params={chatHistoryProviderParams}>

projects/app/src/web/core/chat/storeShareChat.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { create } from 'zustand';
22
import { devtools, persist } from 'zustand/middleware';
33
import { immer } from 'zustand/middleware/immer';
4-
import { customAlphabet } from 'nanoid';
5-
const nanoid = customAlphabet(
6-
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWSYZ1234567890_',
7-
24
8-
);
94

105
type State = {
11-
localUId: string;
6+
localUId?: string;
7+
setLocalUId: (localUId: string) => void;
128
loaded: boolean;
139
};
1410

1511
export const useShareChatStore = create<State>()(
1612
devtools(
1713
persist(
1814
immer((set, get) => ({
19-
localUId: `shareChat-${Date.now()}-${nanoid()}`,
15+
localUId: undefined,
16+
setLocalUId(localUId: string) {
17+
set({ localUId });
18+
},
2019
loaded: false
2120
})),
2221
{

0 commit comments

Comments
 (0)