import { useState, useRef, useEffect } from 'react'; import { Send, Key, AlertTriangle, Sparkles, User, Bot } from 'lucide-react'; import { api } from '../../api/client'; import { useStore } from '../../store/useStore'; import { Panel } from '../common/Panel'; interface Message { role: 'user' | 'assistant'; content: string; timestamp: Date; } export function QueryPage() { const { data, dataLoaded, geminiKey, setGeminiKey, setPage } = useStore(); const [selectedCols, setSelectedCols] = useState([]); const [question, setQuestion] = useState(''); const [messages, setMessages] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const chatEnd = useRef(null); useEffect(() => { chatEnd.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const columns = data?.columns ?? []; const toggleCol = (c: string) => setSelectedCols((p) => (p.includes(c) ? p.filter((x) => x !== c) : [...p, c])); const handleSend = async () => { if (!question.trim() && !selectedCols.length) return; if (!geminiKey) { setError('Enter your Gemini API key to use AI queries.'); return; } if (!selectedCols.length) { setError('Select at least one column to query.'); return; } const userMsg: Message = { role: 'user', content: question || 'Summarize this data', timestamp: new Date() }; setMessages((prev) => [...prev, userMsg]); setQuestion(''); setError(null); setLoading(true); try { const res = await api.queryGemini(question || '', selectedCols, geminiKey); const assistantMsg: Message = { role: 'assistant', content: res.response, timestamp: new Date() }; setMessages((prev) => [...prev, assistantMsg]); } catch (e: unknown) { setError(e instanceof Error ? e.message : 'Query failed'); } finally { setLoading(false); } }; if (!dataLoaded) { return (

Load a dataset first from the{' '} .

); } return (
{/* Sidebar config */}
setGeminiKey(e.target.value)} placeholder="Enter API key..." className="w-full rounded border border-border bg-deep py-1.5 pl-8 pr-3 text-xs text-text-primary placeholder:text-text-muted focus:border-accent focus:outline-none" />
{columns.map((col) => ( ))}
{[ 'Summarize the data in bullet points', 'What are the most common patterns?', 'Identify any anomalies or outliers', 'What correlations exist in this data?', 'Provide a statistical overview', ].map((prompt) => ( ))}
{/* Chat area */}
{/* Header */}
AI Intelligence Query Powered by Gemini
{/* Messages */}
{messages.length === 0 && (

Ask questions about your data using natural language.

Select one or more columns and type your question below.

)} {messages.map((msg, i) => (
{msg.role === 'assistant' && (
)}
{msg.content}

{msg.timestamp.toLocaleTimeString()}

{msg.role === 'user' && (
)}
))} {loading && (
Analyzing data...
)}
{/* Error */} {error && (
{error}
)} {/* Input */}
setQuestion(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && !e.shiftKey && handleSend()} placeholder="Ask about your data..." className="flex-1 rounded-md border border-border bg-deep px-3 py-2 text-sm text-text-primary placeholder:text-text-muted focus:border-accent focus:outline-none" />
); }