fix: use functional state update in handleConversationIdInfoChange to fix stale closure

The handleConversationIdInfoChange callback captured conversationIdInfo
from its closure and spread it when calling setConversationIdInfo. This
caused stale state issues when the callback was invoked after
conversationIdInfo had changed but before the callback was re-created.

Changes:
- Use functional update form (prev => ...) in setConversationIdInfo to
  always read the latest state, matching the pattern already used by
  removeConversationIdInfo
- Remove conversationIdInfo from the dependency array since it's no
  longer captured in the closure
- Add missing handleChangeConversation to handleNewConversation deps
  in embedded-chatbot/hooks.tsx

Fixes #33362

Signed-off-by: majiayu000 <1835304752@qq.com>
This commit is contained in:
majiayu000 2026-03-12 23:56:38 +08:00
parent 4717168fe2
commit 91446d2556
No known key found for this signature in database
GPG Key ID: 29C2AE46ADC3B14B
2 changed files with 25 additions and 21 deletions

View File

@ -180,18 +180,20 @@ export const useChatWithHistory = (installedAppInfo?: InstalledApp) => {
const currentConversationId = useMemo(() => conversationIdInfo?.[appId || '']?.[userId || 'DEFAULT'] || '', [appId, conversationIdInfo, userId])
const handleConversationIdInfoChange = useCallback((changeConversationId: string) => {
if (appId) {
let prevValue = conversationIdInfo?.[appId || '']
if (typeof prevValue === 'string')
prevValue = {}
setConversationIdInfo({
...conversationIdInfo,
[appId || '']: {
...prevValue,
[userId || 'DEFAULT']: changeConversationId,
},
setConversationIdInfo((prev) => {
let prevValue = prev?.[appId || '']
if (typeof prevValue === 'string')
prevValue = {}
return {
...prev,
[appId || '']: {
...prevValue,
[userId || 'DEFAULT']: changeConversationId,
},
}
})
}
}, [appId, conversationIdInfo, setConversationIdInfo, userId])
}, [appId, setConversationIdInfo, userId])
const [newConversationId, setNewConversationId] = useState('')
const chatShouldReloadKey = useMemo(() => {

View File

@ -146,18 +146,20 @@ export const useEmbeddedChatbot = (appSourceType: AppSourceType, tryAppId?: stri
const currentConversationId = useMemo(() => conversationIdInfo?.[appId || '']?.[userId || 'DEFAULT'] || conversationId || '', [appId, conversationIdInfo, userId, conversationId])
const handleConversationIdInfoChange = useCallback((changeConversationId: string) => {
if (appId) {
let prevValue = conversationIdInfo?.[appId || '']
if (typeof prevValue === 'string')
prevValue = {}
setConversationIdInfo({
...conversationIdInfo,
[appId || '']: {
...prevValue,
[userId || 'DEFAULT']: changeConversationId,
},
setConversationIdInfo((prev) => {
let prevValue = prev?.[appId || '']
if (typeof prevValue === 'string')
prevValue = {}
return {
...prev,
[appId || '']: {
...prevValue,
[userId || 'DEFAULT']: changeConversationId,
},
}
})
}
}, [appId, conversationIdInfo, setConversationIdInfo, userId])
}, [appId, setConversationIdInfo, userId])
const [newConversationId, setNewConversationId] = useState('')
const chatShouldReloadKey = useMemo(() => {
@ -441,7 +443,7 @@ export const useEmbeddedChatbot = (appSourceType: AppSourceType, tryAppId?: stri
handleChangeConversation('')
handleNewConversationInputsChange(await getProcessedInputsFromUrlParams())
setClearChatList(true)
}, [isTryApp, setShowNewConversationItemInList, handleNewConversationInputsChange, setClearChatList])
}, [isTryApp, setShowNewConversationItemInList, handleChangeConversation, handleNewConversationInputsChange, setClearChatList])
const handleNewConversationCompleted = useCallback((newConversationId: string) => {
setNewConversationId(newConversationId)