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
2 changed files with 28 additions and 19 deletions

View File

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

View File

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