import { FilesIcon, XIcon } from "lucide-react"; import { useEffect, useMemo, useRef, useState } from "react"; import type { GroupImperativeHandle } from "react-resizable-panels"; import { ConversationEmptyState } from "@/components/ai-elements/conversation"; import { Button } from "@/components/ui/button"; import { ResizableHandle, ResizablePanel, ResizablePanelGroup, } from "@/components/ui/resizable"; import { env } from "@/env"; import { cn } from "@/lib/utils"; import { ArtifactFileDetail, ArtifactFileList, useArtifacts, } from "../artifacts"; import { useThread } from "../messages/context"; const CLOSE_MODE = { chat: 100, artifacts: 0 }; const OPEN_MODE = { chat: 60, artifacts: 40 }; const ChatBox: React.FC<{ children: React.ReactNode; threadId: string }> = ({ children, threadId, }) => { const { thread } = useThread(); const threadIdRef = useRef(threadId); const layoutRef = useRef(null); const { artifacts, open: artifactsOpen, setOpen: setArtifactsOpen, setArtifacts, select: selectArtifact, deselect, selectedArtifact, } = useArtifacts(); const [autoSelectFirstArtifact, setAutoSelectFirstArtifact] = useState(true); useEffect(() => { if (threadIdRef.current !== threadId) { threadIdRef.current = threadId; deselect(); } // Update artifacts from the current thread setArtifacts(thread.values.artifacts); // Deselect if the currently selected artifact no longer exists if ( selectedArtifact && !thread.values.artifacts?.includes(selectedArtifact) ) { deselect(); } if ( env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true" && autoSelectFirstArtifact ) { if (thread?.values?.artifacts?.length > 0) { setAutoSelectFirstArtifact(false); selectArtifact(thread.values.artifacts[0]!); } } }, [ threadId, autoSelectFirstArtifact, deselect, selectArtifact, selectedArtifact, setArtifacts, thread.values.artifacts, ]); const artifactPanelOpen = useMemo(() => { if (env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true") { return artifactsOpen && artifacts?.length > 0; } return artifactsOpen; }, [artifactsOpen, artifacts]); useEffect(() => { if (layoutRef.current) { if (artifactPanelOpen) { layoutRef.current.setLayout(OPEN_MODE); } else { layoutRef.current.setLayout(CLOSE_MODE); } } }, [artifactPanelOpen]); return ( {children}
{selectedArtifact ? ( ) : (
{thread.values.artifacts?.length === 0 ? ( } title="No artifact selected" description="Select an artifact to view its details" /> ) : (

Artifacts

)}
)}
); }; export { ChatBox };