import React, { useState, useRef, useEffect } from "react"; interface Message { role: "user" | "assistant"; content: string; } interface ChatInterfaceProps { pipelineId: string | null; onFileUpload: (file: File) => Promise; } const TypingIndicator = () => (
); const LoadingState = () => (
Processing your document...
); const ErrorState = ({ onRetry }: { onRetry: () => void }) => (

Document Upload Failed

There was an error processing your document. Please try again.

); const API_BASE_URL = process.env.REACT_APP_API_BASE_URL || ""; const ChatInterface: React.FC = ({ pipelineId, onFileUpload, }) => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const [isUploading, setIsUploading] = useState(false); const [uploadError, setUploadError] = useState(false); const messagesEndRef = useRef(null); const fileInputRef = useRef(null); useEffect(() => { if (pipelineId) { setIsUploading(false); setMessages([ { role: "assistant", content: "👋 Your document has been processed! You can now ask questions about its content.", }, ]); } }, [pipelineId]); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleFileUpload = async ( event: React.ChangeEvent ) => { const file = event.target.files?.[0]; if (file) { try { setIsUploading(true); setUploadError(false); await onFileUpload(file); } catch (error) { console.error("Error uploading file:", error); setUploadError(true); } finally { setIsUploading(false); } } }; const handleRetry = () => { setUploadError(false); // Reset file input if (fileInputRef.current) { fileInputRef.current.value = ""; } }; const sendMessage = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || !pipelineId) return; const userMessage = input.trim(); setInput(""); setMessages((prev) => [...prev, { role: "user", content: userMessage }]); setIsLoading(true); try { const response = await fetch(`${API_BASE_URL}/api/chat/${pipelineId}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ query: userMessage }), }); const data = await response.json(); setMessages((prev) => [ ...prev, { role: "assistant", content: data.response }, ]); } catch (error) { console.error("Error sending message:", error); setMessages((prev) => [ ...prev, { role: "assistant", content: "Error: Failed to get response" }, ]); } finally { setIsLoading(false); } }; return (
{isUploading ? ( ) : uploadError ? ( ) : !pipelineId ? (

Upload your document

) : (
{messages.map((message, index) => (
{message.content}
))} {isLoading && } {messages.length === 0 && !isLoading && (

No messages yet

Start by asking a question about your document

)}
setInput(e.target.value)} placeholder="Ask a question about your document..." className="flex-1 p-3 bg-transparent text-white placeholder-gray-400 focus:outline-none" disabled={isLoading} />
)}
); }; export default ChatInterface;