"use client"; import * as React from "react"; interface Message { id: string; content: string; sender: "user" | "ai"; timestamp: Date; type?: "text" | "error" | "system"; } interface AgentConfig { name: string; description: string; greeting: string; personality: string; updatedAt: string; } interface MobileFullscreenChatProps { isOpen: boolean; onClose: () => void; theme?: "light" | "dark"; apiEndpoint?: string; initialMessage?: string; } export const MobileFullscreenChat: React.FC = ({ isOpen, onClose, theme = "light", apiEndpoint = "/api/rag/answer", initialMessage = "Hello! How can I help you today?", }) => { const [messages, setMessages] = React.useState([ { id: "1", content: initialMessage, sender: "ai", timestamp: new Date(), type: "text", }, ]); const [inputValue, setInputValue] = React.useState(""); const [isLoading, setIsLoading] = React.useState(false); const [agentConfig, setAgentConfig] = React.useState({ name: "AI Assistant", description: "Your helpful AI assistant", greeting: initialMessage, personality: "friendly and helpful", updatedAt: new Date().toISOString(), }); const messagesEndRef = React.useRef(null); // Scroll to bottom when messages change React.useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); // Load agent config on mount React.useEffect(() => { if (isOpen) { loadAgentConfig(); } }, [isOpen]); const loadAgentConfig = async () => { try { const response = await fetch("/api/rag/agent-config"); if (response.ok) { const config = await response.json(); setAgentConfig(config); } } catch (error) { console.error("Failed to load agent config:", error); } }; const sendMessage = async () => { if (!inputValue.trim() || isLoading) return; const userMessage: Message = { id: Date.now().toString(), content: inputValue.trim(), sender: "user", timestamp: new Date(), type: "text", }; setMessages((prev) => [...prev, userMessage]); setInputValue(""); setIsLoading(true); try { const response = await fetch(apiEndpoint, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ question: userMessage.content, sessionId: `mobile-chat-${Date.now()}`, }), }); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const data = await response.json(); const aiMessage: Message = { id: (Date.now() + 1).toString(), content: data.answer || "I'm sorry, I couldn't generate a response.", sender: "ai", timestamp: new Date(), type: "text", }; setMessages((prev) => [...prev, aiMessage]); } catch (error) { console.error("Failed to send message:", error); const errorMessage: Message = { id: (Date.now() + 1).toString(), content: "Sorry, I encountered an error. Please try again.", sender: "ai", timestamp: new Date(), type: "error", }; setMessages((prev) => [...prev, errorMessage]); } finally { setIsLoading(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); sendMessage(); } }; if (!isOpen) return null; const themeClasses = theme === "dark" ? "bg-gray-900 text-white" : "bg-white text-gray-900"; return (
{/* Header */}

{agentConfig.name}

{agentConfig.description}

{/* Messages */}
{messages.map((message) => (

{message.content}

{message.timestamp.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", })}
))} {isLoading && (
)}
{/* Input */}