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:
Elijah 2026-11-11 08:53:01 -07:00
parent eae109c166
commit 74a98ac1f1
52 changed files with 11976 additions and 1 deletions

View file

@ -0,0 +1,290 @@
import { ArrowLeft, ArrowRight, Check, Lightbulb, RotateCcw, Shuffle, X } from 'lucide-react';
import { useEffect, useMemo, useState, type ReactNode } from 'react';
import { cx, plural, shuffle, useHotkeys } from '@/lib/util';
import { Markdown } from '@/ui/Markdown';
import { Badge, Button, IconButton, Kbd, ProgressBar } from '@/ui';
import { Ring } from './charts';
/* ------------------------------ the card ------------------------------ */
export function FlipCard({
front,
back,
hint,
flipped,
onFlip,
label,
cardKey,
footer,
}: {
front: string;
back: string;
hint?: string;
flipped: boolean;
onFlip: () => void;
label?: ReactNode;
/** Changing this replays the entrance animation and resets the hint. */
cardKey: string;
footer?: ReactNode;
}) {
const [showHint, setShowHint] = useState(false);
useEffect(() => setShowHint(false), [cardKey]);
const face = 'flip-face flex min-h-[320px] flex-col rounded-[22px] border border-line bg-surface shadow-2 sm:min-h-[380px]';
// Short answers read best large and centred; long ones as normal text.
const size = (text: string) => (text.length < 60 ? 'text-[28px] sm:text-[34px] leading-tight text-center' : text.length < 220 ? 'text-[21px] sm:text-[24px] leading-snug text-center' : 'text-[17px] sm:text-[18px]');
return (
<div key={cardKey} className="flip w-full animate-in">
<div className="flip-inner" data-flipped={flipped}>
<div className={cx(face, 'flip-front')}>
<button type="button" onClick={onFlip} aria-label="Show answer" className="flex flex-1 cursor-pointer flex-col rounded-[22px] text-left">
<div className="flex items-center justify-between px-5 pt-4">
<span className="eyebrow">{label ?? 'Front'}</span>
<span className="text-xs text-ink-3">Tap to flip</span>
</div>
<div className="grid flex-1 place-items-center px-6 py-6 sm:px-10">
<Markdown serif className={cx('max-h-[52vh] w-full overflow-y-auto', size(front))}>
{front}
</Markdown>
</div>
</button>
{hint && (
<div className="border-t border-line px-5 py-3">
{showHint ? (
<div className="flex items-start gap-2 text-sm text-ink-2">
<Lightbulb className="mt-0.5 size-4 shrink-0 text-warn" />
<Markdown>{hint}</Markdown>
</div>
) : (
<button type="button" onClick={() => setShowHint(true)} className="flex items-center gap-1.5 text-[13px] font-medium text-ink-2 hover:text-ink">
<Lightbulb className="size-4" /> Show hint
</button>
)}
</div>
)}
</div>
<div className={cx(face, 'flip-back')}>
<button type="button" onClick={onFlip} aria-label="Show question" className="flex cursor-pointer items-center justify-between rounded-t-[22px] px-5 pt-4 text-left">
<span className="eyebrow">Back</span>
<span className="max-w-[70%] truncate text-xs text-ink-3">{front.replace(/[#*_`>]/g, '').slice(0, 80)}</span>
</button>
<div className="grid flex-1 place-items-center px-6 py-6 sm:px-10" aria-live="polite">
<Markdown serif className={cx('max-h-[52vh] w-full overflow-y-auto', size(back))}>
{back}
</Markdown>
</div>
{footer && <div className="border-t border-line px-5 py-3">{footer}</div>}
</div>
</div>
</div>
);
}
export function StudyTopBar({ title, subtitle, onExit, right, progress, exitLabel = 'Exit' }: { title: string; subtitle?: ReactNode; onExit: () => void; right?: ReactNode; progress?: number; exitLabel?: string }) {
return (
<header className="sticky top-0 z-20 border-b border-line bg-paper/95 backdrop-blur">
<div className="mx-auto flex h-14 max-w-3xl items-center gap-2 px-3 sm:px-5">
<IconButton label={exitLabel} onClick={onExit}>
<X />
</IconButton>
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-semibold">{title}</div>
{subtitle && <div className="truncate text-xs text-ink-3">{subtitle}</div>}
</div>
{right}
</div>
{progress !== undefined && <ProgressBar value={progress} tone="ink" className="h-[3px] rounded-none" label="Progress" />}
</header>
);
}
/* ------------------------------ browse / cram ------------------------------ */
export interface CramState {
order: string[];
index: number;
known: string[];
unknown: string[];
shuffled: boolean;
/** Which pass this is: 1 = whole deck, 2+ = cards still being learned. */
round: number;
}
export interface CramCard {
id: string;
front: string;
back: string;
hint?: string;
}
export function freshCramState(cards: CramCard[], shuffled: boolean, round = 1): CramState {
const ids = cards.map((c) => c.id);
return { order: shuffled ? shuffle(ids) : ids, index: 0, known: [], unknown: [], shuffled, round };
}
/** Drops cards that no longer exist and clamps the position, so a saved session survives deck edits. */
export function reconcileCramState(state: CramState | null | undefined, cards: CramCard[]): CramState | null {
if (!state || !Array.isArray(state.order)) return null;
const valid = new Set(cards.map((c) => c.id));
const order = state.order.filter((id) => valid.has(id));
if (order.length === 0) return null;
return {
order,
index: Math.max(0, Math.min(state.index ?? 0, order.length)),
known: (state.known ?? []).filter((id) => valid.has(id)),
unknown: (state.unknown ?? []).filter((id) => valid.has(id)),
shuffled: Boolean(state.shuffled),
round: state.round ?? 1,
};
}
export function CramSession({
title,
subtitle,
cards,
state,
onState,
onExit,
onGrade,
exitLabel,
renderExtra,
}: {
title: string;
subtitle?: string;
cards: CramCard[];
state: CramState;
onState: (s: CramState) => void;
onExit: () => void;
/** Called when a card is graded (used for activity tracking). */
onGrade?: (cardId: string, known: boolean) => void;
exitLabel?: string;
renderExtra?: (card: CramCard) => ReactNode;
}) {
const [flipped, setFlipped] = useState(false);
const byId = useMemo(() => new Map(cards.map((c) => [c.id, c])), [cards]);
const total = state.order.length;
const done = state.index >= total;
const card = done ? undefined : byId.get(state.order[state.index]);
useEffect(() => setFlipped(false), [state.index, state.round]);
const grade = (known: boolean) => {
if (!card) return;
onGrade?.(card.id, known);
onState({
...state,
index: state.index + 1,
known: known ? [...state.known.filter((x) => x !== card.id), card.id] : state.known.filter((x) => x !== card.id),
unknown: known ? state.unknown.filter((x) => x !== card.id) : [...state.unknown.filter((x) => x !== card.id), card.id],
});
};
const move = (delta: number) => onState({ ...state, index: Math.max(0, Math.min(total, state.index + delta)) });
const restart = (shuffled = state.shuffled) => onState(freshCramState(cards, shuffled));
const studyUnknown = () => {
const pool = cards.filter((c) => state.unknown.includes(c.id));
onState({ ...freshCramState(pool, true, state.round + 1) });
};
useHotkeys({
space: (e) => {
e.preventDefault();
if (!done) setFlipped((f) => !f);
},
Enter: () => !done && setFlipped((f) => !f),
ArrowRight: () => move(1),
ArrowLeft: () => move(-1),
'1': () => flipped && grade(false),
'2': () => flipped && grade(true),
});
return (
<div className="flex min-h-dvh flex-col bg-paper">
<StudyTopBar
title={title}
subtitle={subtitle ?? (state.round > 1 ? `Round ${state.round}: still learning` : 'Browse')}
onExit={onExit}
exitLabel={exitLabel}
progress={total ? Math.min(1, state.index / total) : 0}
right={
<>
<span className="num text-[13px] font-medium text-ink-2">
{Math.min(state.index + 1, total)}/{total}
</span>
<IconButton label={state.shuffled ? 'Shuffled. Restart in order' : 'Shuffle and restart'} active={state.shuffled} onClick={() => restart(!state.shuffled)}>
<Shuffle />
</IconButton>
</>
}
/>
<main className="mx-auto flex w-full max-w-2xl flex-1 flex-col px-4 pt-6 pb-36 sm:px-5 sm:pt-10">
{done || !card ? (
<div className="card m-auto w-full animate-in p-7 text-center sm:p-10">
<div className="mx-auto mb-4 w-fit">
<Ring value={total ? state.known.length / (state.known.length + state.unknown.length || 1) : 0} size={104} stroke={9} tone="good">
<span className="display num text-[28px]">{state.known.length}</span>
</Ring>
</div>
<h2 className="display text-[28px]">{state.unknown.length === 0 ? 'You know them all' : 'Round complete'}</h2>
<p className="mt-2 text-[15px] text-ink-2">
{state.known.length} known{state.unknown.length > 0 ? `, ${state.unknown.length} still learning` : ''} out of {plural(total, 'card')}.
</p>
<div className="mt-6 flex flex-wrap justify-center gap-2">
{state.unknown.length > 0 && (
<Button variant="primary" size="lg" icon={<RotateCcw />} onClick={studyUnknown}>
Study the {state.unknown.length} again
</Button>
)}
<Button size="lg" icon={<RotateCcw />} onClick={() => restart()}>
Start over
</Button>
<Button size="lg" variant="ghost" onClick={onExit}>
Done
</Button>
</div>
</div>
) : (
<>
<FlipCard cardKey={`${state.round}:${card.id}`} front={card.front} back={card.back} hint={card.hint} flipped={flipped} onFlip={() => setFlipped((f) => !f)} footer={renderExtra?.(card)} />
<div className="mt-4 flex justify-center gap-1.5">
{state.known.includes(card.id) && <Badge tone="good">Marked known</Badge>}
{state.unknown.includes(card.id) && <Badge tone="warn">Still learning</Badge>}
</div>
</>
)}
</main>
{!done && card && (
<footer className="safe-bottom fixed inset-x-0 bottom-0 z-20 border-t border-line bg-paper/95 backdrop-blur">
<div className="mx-auto flex h-[76px] max-w-2xl items-center gap-2 px-3 sm:px-5">
<IconButton label="Previous card" onClick={() => move(-1)} disabled={state.index === 0}>
<ArrowLeft />
</IconButton>
{flipped ? (
<>
<Button size="lg" className="flex-1 border-warn/40 text-warn hover:bg-warn-soft" icon={<RotateCcw />} onClick={() => grade(false)}>
Still learning
<span className="hidden sm:inline">
<Kbd>1</Kbd>
</span>
</Button>
<Button size="lg" className="flex-1 border-good/40 text-good hover:bg-good-soft" icon={<Check />} onClick={() => grade(true)}>
Know it
<span className="hidden sm:inline">
<Kbd>2</Kbd>
</span>
</Button>
</>
) : (
<Button size="lg" variant="primary" className="flex-1" onClick={() => setFlipped(true)}>
Show answer
<span className="hidden sm:inline">
<Kbd>Space</Kbd>
</span>
</Button>
)}
<IconButton label="Skip card" onClick={() => move(1)}>
<ArrowRight />
</IconButton>
</div>
</footer>
)}
</div>
);
}