fix: move handling to clearTopic

This commit is contained in:
Tristan Zhang 2025-10-14 22:12:29 +08:00
parent 59a50c346b
commit 499ad5fabf
2 changed files with 26 additions and 28 deletions

View File

@ -61,7 +61,7 @@ const Messages: React.FC<MessagesProps> = ({ assistant, topic, setActiveTopic, o
const [isLoadingMore, setIsLoadingMore] = useState(false)
const [isProcessingContext, setIsProcessingContext] = useState(false)
const { addTopic } = useAssistant(assistant.id)
const { addTopic, updateTopic, assistant: currentAssistant } = useAssistant(assistant.id)
const { showPrompt, messageNavigation } = useSettings()
const { t } = useTranslation()
const dispatch = useAppDispatch()
@ -105,15 +105,28 @@ const Messages: React.FC<MessagesProps> = ({ assistant, topic, setActiveTopic, o
const clearTopic = useCallback(
async (data: Topic) => {
if (data && data.id !== topic.id) {
await clearTopicMessages(data.id)
return
}
const targetTopic = data && data.id !== topic.id ? data : topic
const isCurrentTopic = targetTopic.id === topic.id
await clearTopicMessages()
setDisplayMessages([])
if (!isCurrentTopic) {
await clearTopicMessages(data.id)
} else {
await clearTopicMessages()
setDisplayMessages([])
}
if (currentAssistant?.topics.length === 1) {
const updatedTopic = {
...targetTopic,
name: t('chat.default.topic.name'),
isNameManuallyEdited: false
}
updateTopic(updatedTopic)
if (isCurrentTopic) {
setActiveTopic(updatedTopic)
}
}
},
[clearTopicMessages, topic.id]
[clearTopicMessages, topic, currentAssistant?.topics.length, updateTopic, setActiveTopic, t]
)
useEffect(() => {

View File

@ -127,26 +127,11 @@ export const Topics: React.FC<Props> = ({ assistant: _assistant, activeTopic, se
deleteTimerRef.current = setTimeout(() => setDeletingTopicId(null), 2000)
}, [])
const onClearMessages = useCallback(
(topic: Topic) => {
// window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true)
store.dispatch(setGenerating(false))
EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic)
if (assistant.topics.length === 1) {
const updatedTopic = {
...topic,
name: t('chat.default.topic.name'),
isNameManuallyEdited: false
}
updateTopic(updatedTopic)
if (topic.id === activeTopic.id) {
setActiveTopic(updatedTopic)
}
}
},
[assistant.topics.length, t, updateTopic, activeTopic.id, setActiveTopic]
)
const onClearMessages = useCallback((topic: Topic) => {
// window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true)
store.dispatch(setGenerating(false))
EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic)
}, [])
const handleConfirmDelete = useCallback(
async (topic: Topic, e: React.MouseEvent) => {