Restore study gestures and shuffle progress; harden note autosave and record V1 comparison

This commit is contained in:
Elijah 2026-11-11 20:00:17 -07:00
parent 4bf121728b
commit d7e19db468
8 changed files with 236 additions and 37 deletions

View file

@ -99,6 +99,7 @@ export function FlipCard({
footer,
leaving,
burst,
onSwipe,
}: {
front: string;
back: string;
@ -113,15 +114,19 @@ export function FlipCard({
/** Plays the swipe-away animation used when a card is graded. */
leaving?: 'left' | 'right' | null;
burst?: { streak: number; tier: StreakTier; id: number } | null;
/** Horizontal touch gestures grade an answer without interfering with vertical scrolling. */
onSwipe?: (known: boolean) => void;
}) {
const [showHint, setShowHint] = useState(false);
const touch = useRef<{ x: number; y: number } | null>(null);
const suppressClick = useRef(false);
useEffect(() => setShowHint(false), [cardKey]);
const face = 'flip-face flex min-h-[max(320px,min(60vh,680px))] flex-col rounded-[24px] border border-line bg-surface shadow-2';
// Short answers read best large and centred; long ones as normal text.
const size = (text: string) =>
text.length < 60 ? 'text-[30px] sm:text-[38px] lg:text-[46px] leading-tight text-center' : text.length < 220 ? 'text-[22px] sm:text-[26px] lg:text-[30px] leading-snug text-center' : 'text-[17px] sm:text-[19px] lg:text-[21px]';
return (
<div key={cardKey} className={cx('flip-scene relative w-full animate-in', leaving === 'left' && 'swipe-left', leaving === 'right' && 'swipe-right')}>
<div key={cardKey} className={cx('flip-scene relative w-full', !leaving && 'animate-in', leaving === 'left' && 'swipe-left', leaving === 'right' && 'swipe-right')}>
{/* The whole card is the flip target, on both sides. Space, Enter and the arrow keys do the same. */}
<div
className="flip-card rounded-[24px] outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-4 focus-visible:ring-offset-paper"
@ -129,7 +134,31 @@ export function FlipCard({
role="button"
tabIndex={0}
aria-label={flipped ? 'Flashcard, answer side. Activate to show the question.' : 'Flashcard, question side. Activate to show the answer.'}
onClick={onFlip}
style={{ touchAction: onSwipe ? 'pan-y' : undefined }}
onTouchStart={(e) => {
const target = e.target as HTMLElement;
if (!onSwipe || target.closest('button,a,input,textarea,select')) return;
const p = e.touches[0];
touch.current = { x: p.clientX, y: p.clientY };
suppressClick.current = false;
}}
onTouchCancel={() => { touch.current = null; }}
onTouchEnd={(e) => {
const start = touch.current;
touch.current = null;
if (!start || !onSwipe) return;
const p = e.changedTouches[0];
const dx = p.clientX - start.x;
const dy = p.clientY - start.y;
if (Math.abs(dx) >= 80 && Math.abs(dx) > Math.abs(dy) * 1.5) {
suppressClick.current = true;
onSwipe(dx > 0);
}
}}
onClick={() => {
if (suppressClick.current) { suppressClick.current = false; return; }
onFlip();
}}
>
<div className={cx(face, 'flip-front')} inert={flipped ? true : undefined}>
<div className="flex items-center justify-between px-6 pt-5">
@ -324,6 +353,14 @@ export function CramSession({
reset();
onState(freshCramState(cards, shuffled));
};
const shuffleRemaining = () => {
if (busy) return;
const remaining = state.order.slice(state.index);
const ordered = state.shuffled
? cards.filter((c) => remaining.includes(c.id)).map((c) => c.id)
: shuffle(remaining);
onState({ ...state, shuffled: !state.shuffled, order: [...state.order.slice(0, state.index), ...ordered] });
};
const studyUnknown = () => {
reset();
onState(freshCramState(cards.filter((c) => state.unknown.includes(c.id)), true, state.round + 1));
@ -357,7 +394,7 @@ export function CramSession({
<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)}>
<IconButton label={state.shuffled ? 'Put remaining cards in order' : 'Shuffle remaining cards'} active={state.shuffled} disabled={busy || done} onClick={shuffleRemaining}>
<Shuffle />
</IconButton>
</>
@ -390,7 +427,7 @@ export function CramSession({
</div>
</div>
) : (
<FlipCard cardKey={`${state.round}:${state.index}:${card.id}`} front={card.front} back={card.back} hint={card.hint} flipped={flipped} onFlip={flip} leaving={leaving} burst={burst} footer={renderExtra?.(card)} label={state.known.includes(card.id) ? 'Question · marked known' : state.unknown.includes(card.id) ? 'Question · still learning' : undefined} />
<FlipCard cardKey={`${state.round}:${state.index}:${card.id}`} front={card.front} back={card.back} hint={card.hint} flipped={flipped} onFlip={flip} onSwipe={seenAnswer && !busy ? grade : undefined} leaving={leaving} burst={burst} footer={renderExtra?.(card)} label={state.known.includes(card.id) ? 'Question · marked known' : state.unknown.includes(card.id) ? 'Question · still learning' : undefined} />
)}
</main>
{!done && card && (