Answer history and schedules - Every answer is recorded (deck, activity, topic, correct/wrong, time) in a versioned IndexedDB log (freezmo-learning v1, stores answers + srs), mirrored in memory for synchronous React reads and capped at 20k answers (oldest pruned). StudyFeed records as it grades. - Light SM-2 schedule per activity: 1 day, 3 days, then interval x ease; wrong answers return in ten minutes with the ease nudged down. Review, mistakes, stats - Library 'Smart review' card: due count, mistakes count (latest answer wrong — clears itself when answered correctly), today's goal progress. Review sessions freeze their item list at start. - New /review (due across decks, ?mistakes=1 global or ?deck= per deck) built on a shared SessionFeed with the study feed's snap/keyboard mechanics, wake lock and a results slide. - New /stats: day streak, five-week calendar, accuracy per subject and per deck, weakest topics (>=3 answers), daily-goal progress. - Daily goal configurable in Settings (10/20/30/50). Exam mode - New /exam: pick decks, question count, optional timer; no hints (a NoHints context hides hint rows and the self-check escape); results with score, per-deck and per-topic breakdown; wrong answers land in the mistakes set through the normal history. Reminders (conditional) - VAPID-gated: GET/POST/DELETE /api/push and POST /api/push/test with the standard route pattern, RATE_LIMIT_PUSH, zod, auth before body. Subscriptions stored server-side as endpoint + keys only (PUSH_DIR). UI hidden entirely without keys; iOS install note; service worker push/notificationclick handlers; scripts/send-reminders.mjs for a cron/systemd timer; web-push added as the one server dependency. Verified: new phase-2 suite 42/42 (history/SRS shapes from a real study run, mistakes clearing, review session, stats math, exam timer and breakdowns, VAPID-less hidden state plus the configured subscribe/validate/test/delete path against a second instance) plus regression 74/74, mobile 52/52, wizard 80/80, declutter 116/116, animation 8/8, native 35/35, phase1 38/38, i18n 25/25. Key parity 709/709. Lint + build clean. SW freezmo-v26.
200 lines
7.4 KiB
TypeScript
200 lines
7.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
import { ApiError, gradeAnswer } from "@/lib/client-api";
|
|
import { useI18n } from "@/lib/i18n";
|
|
import { modelErrorCopy } from "@/lib/model-errors";
|
|
import type { Activity } from "@/lib/schemas";
|
|
import { cn } from "@/lib/utils";
|
|
import { Button, Spinner } from "@/components/ui";
|
|
import { ModelErrorActions } from "@/components/ModelErrorActions";
|
|
|
|
import {
|
|
ActivityHeader,
|
|
FeedbackPanel,
|
|
Hint,
|
|
PromptCard,
|
|
QuestionText,
|
|
useNoHints,
|
|
type ActivityComponentProps,
|
|
} from "./shared";
|
|
|
|
type ShortAnswerActivity = Extract<Activity, { type: "short_answer" }>;
|
|
|
|
type GradeState =
|
|
| { status: "idle" }
|
|
| { status: "grading" }
|
|
| { status: "graded"; correct: boolean; feedback: string; missing: string[] }
|
|
| { status: "error"; message: string; kind?: string }
|
|
| { status: "self-check" };
|
|
|
|
export function ShortAnswer({ activity, active, language, onAnswered, onNext, isLast, nextLabel }: ActivityComponentProps<ShortAnswerActivity>) {
|
|
const { t } = useI18n();
|
|
const noHints = useNoHints();
|
|
const [value, setValue] = useState("");
|
|
const [state, setState] = useState<GradeState>({ status: "idle" });
|
|
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (active && state.status === "idle") {
|
|
const timer = window.setTimeout(() => textareaRef.current?.focus(), 350);
|
|
return () => window.clearTimeout(timer);
|
|
}
|
|
}, [active, state.status]);
|
|
|
|
const grade = async () => {
|
|
if (!value.trim() || state.status === "grading") return;
|
|
setState({ status: "grading" });
|
|
try {
|
|
const result = await gradeAnswer({
|
|
question: activity.question,
|
|
referenceAnswer: activity.referenceAnswer,
|
|
keyPoints: activity.keyPoints,
|
|
userAnswer: value.trim(),
|
|
language,
|
|
});
|
|
setState({
|
|
status: "graded",
|
|
correct: result.correct,
|
|
feedback: result.feedback,
|
|
missing: result.missing,
|
|
});
|
|
onAnswered(result.correct, { userAnswer: value.trim() });
|
|
} catch (error) {
|
|
setState({
|
|
status: "error",
|
|
message:
|
|
error instanceof ApiError
|
|
? error.message
|
|
: t("short.gradeFailed"),
|
|
kind: error instanceof ApiError ? error.kind : undefined,
|
|
});
|
|
}
|
|
};
|
|
|
|
const selfCheck = (correct: boolean) => {
|
|
setState({ status: "graded", correct, feedback: t("short.selfChecked"), missing: [] });
|
|
onAnswered(correct, { userAnswer: value.trim() });
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<ActivityHeader type="short_answer" topic={activity.topic} />
|
|
<PromptCard>
|
|
<QuestionText>{activity.question}</QuestionText>
|
|
</PromptCard>
|
|
|
|
<label className="sr-only" htmlFor={`answer-${activity.id}`}>
|
|
{t("short.yourAnswer")}
|
|
</label>
|
|
<textarea
|
|
id={`answer-${activity.id}`}
|
|
ref={textareaRef}
|
|
value={value}
|
|
onChange={(event) => setValue(event.target.value)}
|
|
onKeyDown={(event) => {
|
|
event.stopPropagation();
|
|
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
event.preventDefault();
|
|
void grade();
|
|
}
|
|
}}
|
|
disabled={state.status === "grading" || state.status === "graded"}
|
|
rows={3}
|
|
placeholder={t("short.placeholder")}
|
|
className="w-full resize-none rounded-2xl border-2 border-[var(--glass-border)] bg-[var(--surface-strong)] px-4 py-3 text-[15px] font-medium text-frost-950 shadow-sm outline-none transition placeholder:text-frost-400 focus:border-[var(--focus)] disabled:opacity-70 dark:border-[var(--glass-border)] dark:bg-[var(--surface-strong)] dark:text-frost-50 dark:placeholder:text-frost-400"
|
|
/>
|
|
|
|
{state.status === "idle" ? (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<Hint>{t("short.ctrlEnter")}</Hint>
|
|
<Button size="lg" onClick={() => void grade()} disabled={!value.trim()}>
|
|
{t("short.submit")}
|
|
</Button>
|
|
</div>
|
|
) : null}
|
|
|
|
{state.status === "grading" ? (
|
|
<div className="flex items-center gap-3 rounded-2xl border border-white/60 bg-white/50 backdrop-blur-xl px-4 py-3 text-sm font-semibold text-frost-800 dark:border-white/15 dark:bg-white/10 dark:text-frost-100">
|
|
<Spinner className="size-5" />
|
|
{t("short.grading")}
|
|
</div>
|
|
) : null}
|
|
|
|
{state.status === "error" ? (
|
|
<div className="rounded-2xl border-2 border-amber-300 bg-amber-50 p-4 text-sm text-amber-900 dark:border-amber-400/40 dark:bg-amber-400/10 dark:text-amber-100">
|
|
<p className="font-bold">{modelErrorCopy(state.kind)?.title ?? t("short.couldNotGrade")}</p>
|
|
<p className="mt-1">{modelErrorCopy(state.kind)?.body ?? state.message}</p>
|
|
<div className="mt-3 flex flex-wrap gap-2">
|
|
<Button size="sm" onClick={() => void grade()}>
|
|
{t("common.retry")}
|
|
</Button>
|
|
<ModelErrorActions kind={state.kind} />
|
|
{!noHints ? (
|
|
<Button size="sm" variant="secondary" onClick={() => setState({ status: "self-check" })}>
|
|
{t("short.showAnswer")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
|
|
{state.status === "self-check" ? (
|
|
<div className="card space-y-3 p-4">
|
|
<p className="text-sm font-bold text-frost-900 dark:text-frost-100">{t("short.modelAnswer")}</p>
|
|
<p className="text-sm leading-relaxed text-frost-700 dark:text-frost-200">{activity.referenceAnswer}</p>
|
|
<p className="text-xs text-frost-500 dark:text-frost-400">{t("short.keyPoints")} {activity.keyPoints.join(" · ")}</p>
|
|
<div className="grid grid-cols-2 gap-3">
|
|
<Button variant="secondary" onClick={() => selfCheck(false)}>
|
|
{t("short.missedIt")}
|
|
</Button>
|
|
<Button variant="success" onClick={() => selfCheck(true)}>
|
|
{t("short.hadRight")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
|
|
{state.status === "graded" ? (
|
|
<>
|
|
<div
|
|
className={cn(
|
|
"rounded-2xl border-2 p-3 text-sm font-semibold",
|
|
state.correct
|
|
? "border-emerald-300 bg-emerald-50 text-emerald-900 dark:border-emerald-400/40 dark:bg-emerald-400/10 dark:text-emerald-100"
|
|
: "border-rose-300 bg-rose-50 text-rose-900 dark:border-rose-400/40 dark:bg-rose-400/10 dark:text-rose-100",
|
|
)}
|
|
>
|
|
<p className="font-extrabold uppercase tracking-wide">
|
|
{state.correct ? t("short.accepted") : t("short.needsWork")}
|
|
</p>
|
|
<p className="mt-1 font-normal">{state.feedback}</p>
|
|
{state.missing.length ? (
|
|
<ul className="mt-2 list-inside list-disc font-normal">
|
|
{state.missing.map((item) => (
|
|
<li key={item}>{item}</li>
|
|
))}
|
|
</ul>
|
|
) : null}
|
|
{!state.correct ? (
|
|
<p className="mt-2 font-normal">
|
|
<span className="font-bold">{t("short.modelAnswer")}: </span>
|
|
{activity.referenceAnswer}
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
|
|
<FeedbackPanel
|
|
correct={state.correct}
|
|
explanation={activity.explanation}
|
|
xp={activity.xp}
|
|
isLast={isLast}
|
|
nextLabel={nextLabel}
|
|
onNext={onNext}
|
|
/>
|
|
</>
|
|
) : null}
|
|
</>
|
|
);
|
|
}
|