SurfSense/surfsense_web/components/chat/ChatInterface.tsx

99 lines
3.2 KiB
TypeScript
Raw Normal View History

"use client";
2025-07-27 10:41:15 -07:00
import { type ChatHandler, ChatSection as LlamaIndexChatSection } from "@llamaindex/chat-ui";
2025-10-22 16:35:15 +02:00
import { useParams } from "next/navigation";
import { createContext, useCallback, useEffect, useState } from "react";
2025-10-23 19:48:10 +02:00
import type { ChatDetails } from "@/app/dashboard/[search_space_id]/chats/chats-client";
import type { PodcastItem } from "@/app/dashboard/[search_space_id]/podcasts/podcasts-client";
2025-10-22 14:41:47 +02:00
import type { ResearchMode } from "@/components/chat";
2025-07-27 10:41:15 -07:00
import { ChatInputUI } from "@/components/chat/ChatInputGroup";
import { ChatMessagesUI } from "@/components/chat/ChatMessages";
import { useChatAPI } from "@/hooks/use-chat";
2025-07-27 10:41:15 -07:00
import type { Document } from "@/hooks/use-documents";
2025-10-23 19:48:10 +02:00
import { ChatPanelContainer } from "./ChatPanel/ChatPanelContainer";
interface ChatInterfaceProps {
2025-07-25 18:06:48 -07:00
handler: ChatHandler;
onDocumentSelectionChange?: (documents: Document[]) => void;
selectedDocuments?: Document[];
onConnectorSelectionChange?: (connectorTypes: string[]) => void;
selectedConnectors?: string[];
searchMode?: "DOCUMENTS" | "CHUNKS";
onSearchModeChange?: (mode: "DOCUMENTS" | "CHUNKS") => void;
}
2025-07-25 18:06:48 -07:00
2025-10-22 15:35:44 +02:00
interface ChatInterfaceContext {
isChatPannelOpen: boolean;
setIsChatPannelOpen: (value: boolean) => void;
2025-10-23 01:28:07 +02:00
chat_id: string;
chatDetails: ChatDetails | null;
2025-10-22 15:35:44 +02:00
}
export const chatInterfaceContext = createContext<ChatInterfaceContext | null>(null);
export default function ChatInterface({
2025-07-25 18:06:48 -07:00
handler,
onDocumentSelectionChange,
selectedDocuments = [],
onConnectorSelectionChange,
selectedConnectors = [],
searchMode,
onSearchModeChange,
}: ChatInterfaceProps) {
const { chat_id, search_space_id } = useParams();
const [chatDetails, setChatDetails] = useState<ChatDetails | null>(null);
2025-10-22 14:41:47 +02:00
const [isChatPannelOpen, setIsChatPannelOpen] = useState(false);
const [podcast, setPodcast] = useState<PodcastItem | null>(null);
2025-10-22 15:35:44 +02:00
const contextValue = {
isChatPannelOpen,
setIsChatPannelOpen,
2025-10-23 01:28:07 +02:00
chat_id: typeof chat_id === "string" ? chat_id : chat_id ? chat_id[0] : "",
podcast,
setPodcast,
chatDetails,
2025-10-22 15:35:44 +02:00
};
2025-10-22 14:41:47 +02:00
const { fetchChatDetails } = useChatAPI({
token: localStorage.getItem("surfsense_bearer_token"),
search_space_id: search_space_id as string,
});
const getChat = useCallback(
async (id: string) => {
const chat = await fetchChatDetails(id);
setChatDetails(chat);
},
[fetchChatDetails]
);
useEffect(() => {
const id = typeof chat_id === "string" ? chat_id : chat_id ? chat_id[0] : "";
if (!id) return;
getChat(id);
}, [chat_id, search_space_id]);
2025-07-25 18:06:48 -07:00
return (
2025-10-22 15:35:44 +02:00
<chatInterfaceContext.Provider value={contextValue}>
<LlamaIndexChatSection handler={handler} className="flex h-full">
<div className="flex gap-4 flex-1 w-full">
<div className="flex grow-1 flex-col">
<ChatMessagesUI />
<div className="border-t p-4">
<ChatInputUI
onDocumentSelectionChange={onDocumentSelectionChange}
selectedDocuments={selectedDocuments}
onConnectorSelectionChange={onConnectorSelectionChange}
selectedConnectors={selectedConnectors}
searchMode={searchMode}
onSearchModeChange={onSearchModeChange}
/>
</div>
2025-10-22 14:41:47 +02:00
</div>
2025-10-23 01:28:07 +02:00
<ChatPanelContainer />
2025-07-25 18:06:48 -07:00
</div>
2025-10-22 15:35:44 +02:00
</LlamaIndexChatSection>
</chatInterfaceContext.Provider>
2025-07-25 18:06:48 -07:00
);
}