import { X } from "lucide-react" import { useEffect, useRef, useState } from "react" import LoadingDots from "../../ui/LoadingDots" import ChatInput from "./ChatInput" import ChatMessage from "./ChatMessage" import ContextDisplay from "./ContextDisplay" import { handleSend, handleStopGeneration } from "./lib/chatUtils" interface Message { role: "user" | "assistant" content: string context?: string } export default function AIChat({ activeFileContent, activeFileName, onClose, }: { activeFileContent: string activeFileName: string onClose: () => void }) { const [messages, setMessages] = useState([]) const [input, setInput] = useState("") const [isGenerating, setIsGenerating] = useState(false) const chatContainerRef = useRef(null) const abortControllerRef = useRef(null) const [context, setContext] = useState(null) const [isContextExpanded, setIsContextExpanded] = useState(false) const [isLoading, setIsLoading] = useState(false) useEffect(() => { scrollToBottom() }, [messages]) const scrollToBottom = () => { if (chatContainerRef.current) { setTimeout(() => { chatContainerRef.current?.scrollTo({ top: chatContainerRef.current.scrollHeight, behavior: "smooth", }) }, 100) } } return (
CHAT
{activeFileName}
{messages.map((message, messageIndex) => ( ))} {isLoading && }
handleSend( input, context, messages, setMessages, setInput, setIsContextExpanded, setIsGenerating, setIsLoading, abortControllerRef, activeFileContent ) } handleStopGeneration={() => handleStopGeneration(abortControllerRef)} />
) }