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.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2025-11-07 12:20:30 -08:00
parent 7ed159b395
commit a37e140c6f

View file

@ -2,7 +2,7 @@
import { type CreateMessage, type Message, useChat } from "@ai-sdk/react"; import { type CreateMessage, type Message, useChat } from "@ai-sdk/react";
import { useParams, useRouter } from "next/navigation"; import { useParams, useRouter } from "next/navigation";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo, useRef } from "react";
import ChatInterface from "@/components/chat/ChatInterface"; import ChatInterface from "@/components/chat/ChatInterface";
import { useChatAPI, useChatState } from "@/hooks/use-chat"; import { useChatAPI, useChatState } from "@/hooks/use-chat";
import { useDocumentTypes } from "@/hooks/use-document-types"; import { useDocumentTypes } from "@/hooks/use-document-types";
@ -12,10 +12,16 @@ import { useSearchSourceConnectors } from "@/hooks/use-search-source-connectors"
export default function ResearcherPage() { export default function ResearcherPage() {
const { search_space_id, chat_id } = useParams(); const { search_space_id, chat_id } = useParams();
const router = useRouter(); const router = useRouter();
const hasSetInitialConnectors = useRef(false);
const chatIdParam = Array.isArray(chat_id) ? chat_id[0] : chat_id; const chatIdParam = Array.isArray(chat_id) ? chat_id[0] : chat_id;
const isNewChat = !chatIdParam; const isNewChat = !chatIdParam;
// Reset the flag when chat ID changes
useEffect(() => {
hasSetInitialConnectors.current = false;
}, [chatIdParam]);
const { const {
token, token,
isLoading, isLoading,
@ -163,9 +169,14 @@ export default function ResearcherPage() {
setTopK, setTopK,
]); ]);
// Set all sources as default for new chats // Set all sources as default for new chats (only once on initial mount)
useEffect(() => { 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 // Combine all document types and live search connectors
const allSourceTypes = [ const allSourceTypes = [
...documentTypes.map((dt) => dt.type), ...documentTypes.map((dt) => dt.type),
@ -174,6 +185,7 @@ export default function ResearcherPage() {
if (allSourceTypes.length > 0) { if (allSourceTypes.length > 0) {
setSelectedConnectors(allSourceTypes); setSelectedConnectors(allSourceTypes);
hasSetInitialConnectors.current = true;
} }
} }
}, [ }, [