Add the V2 web app: design system, shell, study flows, AI, settings, sharing
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
eae109c166
commit
74a98ac1f1
52 changed files with 11976 additions and 1 deletions
110
web/src/features/Explain.tsx
Normal file
110
web/src/features/Explain.tsx
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { CornerDownLeft, Sparkles, Square } from 'lucide-react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { errorMessage, streamEvents } from '@/lib/api';
|
||||
import { Markdown } from '@/ui/Markdown';
|
||||
import { Button, Callout, Spinner } from '@/ui';
|
||||
|
||||
type ExplainBody =
|
||||
| { kind: 'question'; stem: string; options: { text: string; correct: boolean }[]; selected?: string[]; rationale?: string }
|
||||
| { kind: 'card'; front: string; back: string }
|
||||
| { kind: 'text'; text: string };
|
||||
|
||||
type Event = { type: 'text'; delta: string } | { type: 'error'; message: string } | { type: 'done' };
|
||||
|
||||
/** "Explain with AI": streams a short explanation and accepts follow-up questions. */
|
||||
export function Explain({ body, label = 'Explain with AI', className }: { body: ExplainBody; label?: string; className?: string }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [turns, setTurns] = useState<{ ask?: string; text: string }[]>([]);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [ask, setAsk] = useState('');
|
||||
const abort = useRef<AbortController | null>(null);
|
||||
const key = JSON.stringify(body);
|
||||
|
||||
useEffect(() => {
|
||||
// A different question: start over.
|
||||
abort.current?.abort();
|
||||
setOpen(false);
|
||||
setTurns([]);
|
||||
setError('');
|
||||
setAsk('');
|
||||
}, [key]);
|
||||
useEffect(() => () => abort.current?.abort(), []);
|
||||
|
||||
const run = async (question?: string) => {
|
||||
setOpen(true);
|
||||
setBusy(true);
|
||||
setError('');
|
||||
setTurns((t) => [...t, { ask: question, text: '' }]);
|
||||
const controller = new AbortController();
|
||||
abort.current = controller;
|
||||
try {
|
||||
await streamEvents<Event>(
|
||||
'/api/ai/explain',
|
||||
{ ...body, ask: question },
|
||||
(e) => {
|
||||
if (e.type === 'text') setTurns((t) => t.map((x, i) => (i === t.length - 1 ? { ...x, text: x.text + e.delta } : x)));
|
||||
else if (e.type === 'error') setError(e.message);
|
||||
},
|
||||
controller.signal,
|
||||
);
|
||||
} catch (e) {
|
||||
if ((e as Error)?.name !== 'AbortError') setError(errorMessage(e));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (!open) {
|
||||
return (
|
||||
<Button size="sm" variant="ghost" icon={<Sparkles />} className={className} onClick={() => run()}>
|
||||
{label}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<div className={className}>
|
||||
<div className="rounded-xl border border-accent/25 bg-accent-soft/50 p-4">
|
||||
<div className="eyebrow mb-2 flex items-center gap-1.5 text-accent">
|
||||
<Sparkles className="size-3.5" /> AI explanation
|
||||
</div>
|
||||
<div className="space-y-3">
|
||||
{turns.map((t, i) => (
|
||||
<div key={i}>
|
||||
{t.ask && <div className="mb-1 text-[13px] font-medium text-ink-2">{t.ask}</div>}
|
||||
{t.text ? <Markdown className="text-[14.5px]">{t.text}</Markdown> : busy && i === turns.length - 1 ? <Spinner className="size-4" label="Thinking" /> : null}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{error && (
|
||||
<Callout tone="bad" className="mt-3 py-2">
|
||||
{error}
|
||||
</Callout>
|
||||
)}
|
||||
<form
|
||||
className="mt-3 flex gap-2"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
if (busy) return abort.current?.abort();
|
||||
const question = ask.trim();
|
||||
if (!question) return;
|
||||
setAsk('');
|
||||
void run(question);
|
||||
}}
|
||||
>
|
||||
<input
|
||||
value={ask}
|
||||
onChange={(e) => setAsk(e.target.value)}
|
||||
placeholder="Ask a follow-up…"
|
||||
aria-label="Follow-up question"
|
||||
className="h-9 min-w-0 flex-1 rounded-[9px] border border-line-strong bg-surface px-3 text-sm outline-none focus:border-accent"
|
||||
/>
|
||||
<Button type="submit" size="sm" variant={busy ? 'secondary' : 'primary'} icon={busy ? <Square className="fill-current" /> : <CornerDownLeft />} aria-label={busy ? 'Stop' : 'Ask'}>
|
||||
{busy ? 'Stop' : 'Ask'}
|
||||
</Button>
|
||||
</form>
|
||||
<p className="mt-2 text-[11px] text-ink-3">AI can be wrong. Check anything important against your course material.</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue