File size: 9,019 Bytes
a1aef88 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 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<string[]>([]);
const [question, setQuestion] = useState('');
const [messages, setMessages] = useState<Message[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const chatEnd = useRef<HTMLDivElement>(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 (
<Panel title="No Data Loaded">
<p className="text-sm text-text-muted">
Load a dataset first from the{' '}
<button onClick={() => setPage('dashboard')} className="text-accent hover:underline">
Dashboard
</button>.
</p>
</Panel>
);
}
return (
<div className="flex h-full gap-4">
{/* Sidebar config */}
<div className="w-72 shrink-0 space-y-4">
<Panel title="Configuration">
<div className="space-y-3">
<div>
<label className="mb-1 block text-xs text-text-muted">Gemini API Key</label>
<div className="relative">
<Key className="absolute left-2.5 top-2 h-3.5 w-3.5 text-text-muted" />
<input
type="password"
value={geminiKey}
onChange={(e) => 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"
/>
</div>
</div>
<div>
<label className="mb-1 block text-xs text-text-muted">
Target Columns{selectedCols.length > 0 && ` (${selectedCols.length})`}
</label>
<div className="flex flex-wrap gap-1.5">
{columns.map((col) => (
<button
key={col}
onClick={() => toggleCol(col)}
className={`rounded-md border px-2 py-1 text-xs transition-colors ${
selectedCols.includes(col)
? 'border-accent bg-accent-dim/30 text-accent-bright'
: 'border-border bg-raised text-text-secondary hover:border-border-bright'
}`}
>
{col}
</button>
))}
</div>
</div>
</div>
</Panel>
<Panel title="Quick Prompts">
<div className="space-y-1.5">
{[
'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) => (
<button
key={prompt}
onClick={() => setQuestion(prompt)}
className="w-full rounded border border-border/50 bg-raised px-3 py-2 text-left text-xs text-text-secondary transition-colors hover:border-accent hover:bg-elevated"
>
{prompt}
</button>
))}
</div>
</Panel>
</div>
{/* Chat area */}
<div className="flex flex-1 flex-col rounded-lg border border-border bg-surface">
{/* Header */}
<div className="flex items-center gap-2 border-b border-border px-4 py-3">
<Sparkles className="h-4 w-4 text-accent" />
<span className="text-sm font-semibold text-text-primary">AI Intelligence Query</span>
<span className="text-xs text-text-muted">Powered by Gemini</span>
</div>
{/* Messages */}
<div className="flex-1 overflow-y-auto p-4">
{messages.length === 0 && (
<div className="flex h-full flex-col items-center justify-center gap-3 text-center">
<div className="rounded-full bg-elevated p-4">
<Sparkles className="h-8 w-8 text-accent/50" />
</div>
<p className="text-sm text-text-muted">
Ask questions about your data using natural language.
</p>
<p className="text-xs text-text-muted">
Select one or more columns and type your question below.
</p>
</div>
)}
{messages.map((msg, i) => (
<div
key={i}
className={`mb-4 flex gap-3 ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
{msg.role === 'assistant' && (
<div className="mt-1 shrink-0 rounded-full bg-accent-dim/30 p-1.5">
<Bot className="h-3.5 w-3.5 text-accent" />
</div>
)}
<div
className={`max-w-[75%] rounded-lg px-4 py-2.5 text-sm ${
msg.role === 'user'
? 'bg-accent-dim text-white'
: 'border border-border bg-raised text-text-primary'
}`}
>
<div className="whitespace-pre-wrap">{msg.content}</div>
<p className="mt-1 text-[10px] opacity-50">
{msg.timestamp.toLocaleTimeString()}
</p>
</div>
{msg.role === 'user' && (
<div className="mt-1 shrink-0 rounded-full bg-purple/20 p-1.5">
<User className="h-3.5 w-3.5 text-purple" />
</div>
)}
</div>
))}
{loading && (
<div className="flex items-center gap-2 text-xs text-text-muted">
<div className="flex gap-1">
<div className="h-1.5 w-1.5 animate-bounce rounded-full bg-accent [animation-delay:-0.3s]" />
<div className="h-1.5 w-1.5 animate-bounce rounded-full bg-accent [animation-delay:-0.15s]" />
<div className="h-1.5 w-1.5 animate-bounce rounded-full bg-accent" />
</div>
Analyzing data...
</div>
)}
<div ref={chatEnd} />
</div>
{/* Error */}
{error && (
<div className="mx-4 mb-2 flex items-center gap-2 rounded-md bg-danger/10 px-3 py-2 text-xs text-danger">
<AlertTriangle className="h-3.5 w-3.5" /> {error}
</div>
)}
{/* Input */}
<div className="border-t border-border p-3">
<div className="flex items-center gap-2">
<input
value={question}
onChange={(e) => 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"
/>
<button
onClick={handleSend}
disabled={loading || !geminiKey}
className="rounded-md bg-accent-dim p-2 text-white transition-colors hover:bg-accent disabled:opacity-50"
>
<Send className="h-4 w-4" />
</button>
</div>
</div>
</div>
</div>
);
}
|