22
33import { type CreateMessage , type Message , useChat } from "@ai-sdk/react" ;
44import { useParams , useRouter } from "next/navigation" ;
5- import { useEffect , useMemo } from "react" ;
5+ import { useEffect , useMemo , useRef } from "react" ;
66import ChatInterface from "@/components/chat/ChatInterface" ;
77import { useChatAPI , useChatState } from "@/hooks/use-chat" ;
88import { useDocumentTypes } from "@/hooks/use-document-types" ;
@@ -12,10 +12,16 @@ import { useSearchSourceConnectors } from "@/hooks/use-search-source-connectors"
1212export default function ResearcherPage ( ) {
1313 const { search_space_id, chat_id } = useParams ( ) ;
1414 const router = useRouter ( ) ;
15+ const hasSetInitialConnectors = useRef ( false ) ;
1516
1617 const chatIdParam = Array . isArray ( chat_id ) ? chat_id [ 0 ] : chat_id ;
1718 const isNewChat = ! chatIdParam ;
1819
20+ // Reset the flag when chat ID changes
21+ useEffect ( ( ) => {
22+ hasSetInitialConnectors . current = false ;
23+ } , [ chatIdParam ] ) ;
24+
1925 const {
2026 token,
2127 isLoading,
@@ -163,9 +169,14 @@ export default function ResearcherPage() {
163169 setTopK ,
164170 ] ) ;
165171
166- // Set all sources as default for new chats
172+ // Set all sources as default for new chats (only once on initial mount)
167173 useEffect ( ( ) => {
168- if ( isNewChat && selectedConnectors . length === 0 && documentTypes . length > 0 ) {
174+ if (
175+ isNewChat &&
176+ ! hasSetInitialConnectors . current &&
177+ selectedConnectors . length === 0 &&
178+ documentTypes . length > 0
179+ ) {
169180 // Combine all document types and live search connectors
170181 const allSourceTypes = [
171182 ...documentTypes . map ( ( dt ) => dt . type ) ,
@@ -174,6 +185,7 @@ export default function ResearcherPage() {
174185
175186 if ( allSourceTypes . length > 0 ) {
176187 setSelectedConnectors ( allSourceTypes ) ;
188+ hasSetInitialConnectors . current = true ;
177189 }
178190 }
179191 } , [
0 commit comments