From a37e140c6fa2f65c78dcebadbd8c31f02b65721b Mon Sep 17 00:00:00 2001 From: "DESKTOP-RTLN3BA\\$punk" Date: Fri, 7 Nov 2025 12:20:30 -0800 Subject: [PATCH] feat: enhance chat initialization logic in ResearcherPage - Introduced a ref to track if initial connectors have been set for new chats. - Updated the effect to reset the flag when the chat ID changes, ensuring connectors are set only once on initial mount. - Improved the condition for setting default sources for new chats to prevent unnecessary updates. --- .../researcher/[[...chat_id]]/page.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx index a30619717..1b6df4174 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/researcher/[[...chat_id]]/page.tsx @@ -2,7 +2,7 @@ import { type CreateMessage, type Message, useChat } from "@ai-sdk/react"; import { useParams, useRouter } from "next/navigation"; -import { useEffect, useMemo } from "react"; +import { useEffect, useMemo, useRef } from "react"; import ChatInterface from "@/components/chat/ChatInterface"; import { useChatAPI, useChatState } from "@/hooks/use-chat"; import { useDocumentTypes } from "@/hooks/use-document-types"; @@ -12,10 +12,16 @@ import { useSearchSourceConnectors } from "@/hooks/use-search-source-connectors" export default function ResearcherPage() { const { search_space_id, chat_id } = useParams(); const router = useRouter(); + const hasSetInitialConnectors = useRef(false); const chatIdParam = Array.isArray(chat_id) ? chat_id[0] : chat_id; const isNewChat = !chatIdParam; + // Reset the flag when chat ID changes + useEffect(() => { + hasSetInitialConnectors.current = false; + }, [chatIdParam]); + const { token, isLoading, @@ -163,9 +169,14 @@ export default function ResearcherPage() { setTopK, ]); - // Set all sources as default for new chats + // Set all sources as default for new chats (only once on initial mount) useEffect(() => { - if (isNewChat && selectedConnectors.length === 0 && documentTypes.length > 0) { + if ( + isNewChat && + !hasSetInitialConnectors.current && + selectedConnectors.length === 0 && + documentTypes.length > 0 + ) { // Combine all document types and live search connectors const allSourceTypes = [ ...documentTypes.map((dt) => dt.type), @@ -174,6 +185,7 @@ export default function ResearcherPage() { if (allSourceTypes.length > 0) { setSelectedConnectors(allSourceTypes); + hasSetInitialConnectors.current = true; } } }, [