mirror of https://github.com/langgenius/dify.git
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:
parent
4717168fe2
commit
91446d2556
|
|
@ -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(() => {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue