freezmo/components/ReviewClient.tsx
Martin Dvorak 2103f9fc69 Smart Review: instant loading feedback, no more dead-feeling tap
Reported: tapping Review due could sit for many seconds with no loading
screen. The flow itself is local and fast (counts are hydrated on the
library; <1.5s even at 6x CPU throttle with 3,000 answers), so the
visible fault was missing feedback: /review is a dynamic route and
until its payload arrived nothing on screen changed.

- LinkLoadingScreen: the review card links render a full-screen
  loading screen from the link's own useLinkStatus pending state, the
  instant the tap happens (measured 58 ms with the route request held
  open). The links also prefetch their payload.
- Route loading boundaries (loading.tsx) for /review, /stats and
  /exam, sharing a RouteLoading screen (app icon + spinner +
  localized message); ReviewClient's bare skeleton replaced with it.
- Health probes: per-key 30s cache + in-flight sharing in use-health.ts
  (every page mounts its own gate; a session now fires one probe
  instead of one per navigation). Manual re-check still forces.
- Czech fix: exam heading 'Zkouškové režim' -> 'Zkouškový režim'.

Verified by the rebuilt suites (the /tmp harness was wiped overnight):
review-loading 15/15 (stalled-request loading screen at 58 ms, session
arrival after release, fast normal path, mistakes path, end-to-end
grading, probe cache) and navigation 78/78 (tab bar, labels 360/390
EN/CS, routing, deep links, wizard loading lock). Lint + build clean.
SW freezmo-v30.
2026-10-11 14:35:16 +00:00

111 lines
3.8 KiB
TypeScript

"use client";
import { useEffect, useMemo, useState, useSyncExternalStore } from "react";
import { useRouter } from "next/navigation";
import { SessionFeed, type SessionItem } from "@/components/SessionFeed";
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(
() => (mistakes ? mistakeItems(answers, deckId) : dueItems(srs)),
[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>
)}
/>
);
}