freezmo/components/ReviewClient.tsx
Martin Dvorak 9cea13496a Report question: skip a broken card and tell the server
A generated matching card arrived on a phone with three identical
definitions (the generation guards now drop those), but there was no way
to get past a bad card from inside a session. The feed chrome (deck
study, review, mistakes and exam) now has the red "!" control: one tap
records the question, skips to the next card and shows an Undo toast.

- lib/question-reports.ts: the local list (freezmo.reports.v1, zod on
  read, capped 200) is the skip state — reported cards leave every
  session type, count as "not attempted" in the results instead of
  wrong, and Undo takes a report back. A deck where everything is
  reported stays studiable so it can be regenerated.
- POST /api/reports appends one JSON line per report to
  REPORTS_DIR/reports.jsonl (shared/freezmo/reports in production, the
  dev data dir in dev). Deliberately write-only: the question text is
  readable on the server (that is the point — prompt tuning) but never
  over HTTP, since the app has no auth. Best effort: offline reports stay
  local.
- The button: a red outlined rounded square with "!", the chrome's
  control size, in both feed headers (hidden on the results slide).
- config: REPORTS_DIR + RATE_LIMIT_REPORTS (20); the deploy creates the
  shared reports dir; the dev unit and AGENTS.md note the new server
  data location.
- Suites: phase-1 (append + validation + no GET), phase-2 (button, toast,
  server POST, local copy, skip counter, hidden next session, Undo, exam
  pool). SW -> freezmo-v38.
2026-10-12 07:12:55 +00:00

116 lines
4 KiB
TypeScript

"use client";
import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
import { useRouter } from "next/navigation";
import { SessionFeed, type SessionItem } from "@/components/SessionFeed";
import { isReported } from "@/lib/question-reports";
import { RouteLoading } from "@/components/RouteLoading";
import { Button } from "@/components/ui";
import { haptic } from "@/lib/haptics";
import { ensureLearningHydrated, learningStore, recordAnswer } from "@/lib/history";
import { useI18n } from "@/lib/i18n";
import { dueItems, mistakeItems, type ReviewItem } from "@/lib/learning";
/**
* Review / mistakes session: built from due items (SRS) across every deck, or
* from the activities whose latest answer was wrong. Every answer is recorded,
* so mistakes clear themselves once answered correctly.
*/
export function ReviewClient({ mistakes, deckId }: { mistakes: boolean; deckId?: string }) {
const { t } = useI18n();
const router = useRouter();
const answers = useSyncExternalStore(
learningStore.subscribe,
learningStore.getAnswersSnapshot,
learningStore.getServerAnswers,
);
const srs = useSyncExternalStore(
learningStore.subscribe,
learningStore.getSrsSnapshot,
learningStore.getServerSrs,
);
const [ready, setReady] = useState(false);
/** Frozen at session start: answering must not shrink the live queue. */
const [session, setSession] = useState<ReviewItem[] | null>(null);
useEffect(() => {
void ensureLearningHydrated().then(() => setReady(true));
}, []);
const items = useMemo(
// Reported questions are out of rotation (see lib/question-reports).
() =>
(mistakes ? mistakeItems(answers, deckId) : dueItems(srs)).filter(
(item) => !isReported(item.deck.id, item.activity.id),
),
[mistakes, deckId, answers, srs],
);
useEffect(() => {
if (!ready || session || items.length === 0) return;
setSession(items);
}, [ready, session, items]);
const title = mistakes
? deckId
? t("review.mistakesDeck")
: t("review.mistakesTitle")
: t("review.title");
if (!ready) {
return <RouteLoading messageKey="review.preparing" />;
}
if (!session) {
return (
<main className="frost-bg grid min-h-dvh place-items-center px-4">
<div className="card max-w-md space-y-3 p-6 text-center">
<h1 className="text-xl font-extrabold text-[var(--text)]">{title}</h1>
<p className="text-sm leading-relaxed text-[var(--text-muted)]">
{mistakes ? t("review.noMistakes") : t("review.nothingDue")}
</p>
<Button size="lg" className="touch-target" onClick={() => router.push("/")}>
{t("review.done")}
</Button>
</div>
</main>
);
}
const sessionItems: SessionItem[] = session.map(({ deck, activity }) => ({ deck, activity }));
return (
<SessionFeed
title={title}
items={sessionItems}
testId="review-feed"
onExit={() => router.push("/")}
onAnswered={(item, correct) => {
haptic(correct ? "success" : "error");
recordAnswer({
deckId: item.deck.id,
deckTitle: item.deck.title,
activityId: item.activity.id,
type: item.activity.type,
topic: item.activity.topic,
correct,
});
}}
renderResults={({ correct, total, score }) => (
<div className="card space-y-4 p-5 text-center">
<p className="text-[11px] font-semibold uppercase tracking-wider text-[var(--text-subtle)]">{title}</p>
<p className="text-[42px] font-extrabold leading-none text-[var(--text)]">{score}%</p>
<p className="text-sm font-medium text-[var(--text-muted)]">
{t("results.gotOf", { correct, total, retry: "" })}
</p>
<div className="flex flex-wrap justify-center gap-2">
<Button size="lg" className="touch-target" onClick={() => router.push("/")}>
{t("review.done")}
</Button>
</div>
</div>
)}
/>
);
}