From 7549972048835c5842d674d0e44adc4fe1668504 Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 17 Jul 2025 11:48:25 +0800 Subject: [PATCH] hotfix: enhance assistant topic validation in useActiveTopic hook (#8213) fix: enhance assistant topic validation in useActiveTopic hook Updated the useActiveTopic hook to ensure that the assistant and its topics are properly validated before accessing properties. This prevents potential errors when data is not fully loaded. --- src/renderer/src/hooks/useTopic.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/hooks/useTopic.ts b/src/renderer/src/hooks/useTopic.ts index e8b9077690..abb22cc921 100644 --- a/src/renderer/src/hooks/useTopic.ts +++ b/src/renderer/src/hooks/useTopic.ts @@ -34,7 +34,14 @@ export function useActiveTopic(assistantId: string, topic?: Topic) { useEffect(() => { // activeTopic not in assistant.topics - if (assistant && assistant.topics.length > 0 && !find(assistant.topics, { id: activeTopic?.id })) { + // 确保 assistant 和 assistant.topics 存在,避免在数据未完全加载时访问属性 + if ( + assistant && + assistant.topics && + Array.isArray(assistant.topics) && + assistant.topics.length > 0 && + !find(assistant.topics, { id: activeTopic?.id }) + ) { setActiveTopic(assistant.topics[0]) } }, [activeTopic?.id, assistant])