- axe is clean across every screen in both themes, phone and desktop. Fixed: the hidden file inputs (aria-hidden + tabIndex -1 — they are opened by labelled buttons, so they should not be in the accessibility tree), the heading order (deck/homework/smart-review cards are h2 under the page h1; LearningSection takes a level so it stays h2 on the library and h3 inside the settings dialog) and landmarks around the study and review feeds. - tests/contrast.mjs (new diagnostic): samples rendered pixels, because axe cannot judge text on blurred glass — it reports those nodes as incomplete. It skips disabled controls (1.4.3 exempts them) and content behind an open sheet. Result: no below-minimum text on any scanned screen. - The report control exists in the homework guide too: under each "Try it yourself" exercise, labelled "Report exercise", without a skip (there is no queue to skip). lib/question-reports now takes deckId/deckTitle directly. - The rail's two creation actions are buttons now (.nav-action) rather than quiet nav rows — once the library is full they were the only way to start something on desktop and read as just more navigation. "Make my homework" is "New homework" wherever it is an action (rail, empty state, share chooser, manifest shortcut); the page title and mode switch keep their wording. Version 0.1.6 (three task bumps), SW -> freezmo-v52.
174 lines
5.2 KiB
TypeScript
174 lines
5.2 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Reported questions. Two copies: one in this browser — the source of truth
|
|
* for skipping, works offline and Undo can take it back — and one appended to
|
|
* the server's `reports.jsonl` (see /api/reports) so the operator can inspect
|
|
* what was flagged.
|
|
*
|
|
* Reports are deliberately not part of the backup/sync payload: the server
|
|
* file is the cross-device record, the local list is just this browser's skip
|
|
* state.
|
|
*/
|
|
|
|
import { z } from "zod";
|
|
|
|
import type { Activity } from "./schemas";
|
|
|
|
const STORAGE_KEY = "freezmo.reports.v1";
|
|
const LIMIT = 200;
|
|
const EMPTY: QuestionReport[] = [];
|
|
|
|
export const questionReportSchema = z.object({
|
|
deckId: z.string().min(1).max(80),
|
|
deckTitle: z.string().max(200).optional(),
|
|
activityId: z.string().min(1).max(80),
|
|
type: z.string().min(1).max(40),
|
|
question: z.string().min(1).max(600),
|
|
at: z.number(),
|
|
});
|
|
export type QuestionReport = z.infer<typeof questionReportSchema>;
|
|
|
|
type Listener = () => void;
|
|
|
|
let reports: QuestionReport[] | null = null;
|
|
let cached: QuestionReport[] = EMPTY;
|
|
const listeners = new Set<Listener>();
|
|
|
|
function read(): QuestionReport[] {
|
|
if (typeof window === "undefined") return EMPTY;
|
|
try {
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return EMPTY;
|
|
const parsed: unknown = JSON.parse(raw);
|
|
if (!Array.isArray(parsed)) return EMPTY;
|
|
const valid: QuestionReport[] = [];
|
|
for (const entry of parsed) {
|
|
const result = questionReportSchema.safeParse(entry);
|
|
if (result.success) valid.push(result.data);
|
|
else console.warn("[freezmo] skipping invalid report", result.error.issues);
|
|
}
|
|
return valid;
|
|
} catch (error) {
|
|
console.warn("[freezmo] could not read reports", error);
|
|
return EMPTY;
|
|
}
|
|
}
|
|
|
|
function ensureLoaded(): void {
|
|
if (reports !== null) return;
|
|
reports = read();
|
|
cached = reports;
|
|
}
|
|
|
|
function write(next: QuestionReport[]): void {
|
|
reports = next;
|
|
cached = next;
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
|
} catch (error) {
|
|
console.warn("[freezmo] could not save reports", error);
|
|
}
|
|
listeners.forEach((listener) => listener());
|
|
}
|
|
|
|
export const reportStore = {
|
|
subscribe(listener: Listener): () => void {
|
|
ensureLoaded();
|
|
listeners.add(listener);
|
|
const onStorage = (event: StorageEvent) => {
|
|
if (event.key === STORAGE_KEY) {
|
|
reports = null;
|
|
ensureLoaded();
|
|
listeners.forEach((l) => l());
|
|
}
|
|
};
|
|
window.addEventListener("storage", onStorage);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
window.removeEventListener("storage", onStorage);
|
|
};
|
|
},
|
|
getSnapshot(): QuestionReport[] {
|
|
ensureLoaded();
|
|
return cached;
|
|
},
|
|
getServerSnapshot(): QuestionReport[] {
|
|
return EMPTY;
|
|
},
|
|
};
|
|
|
|
/** Short text of an activity, for the report payload. */
|
|
export function activityText(activity: Activity): string {
|
|
switch (activity.type) {
|
|
case "flashcard":
|
|
return activity.front;
|
|
case "multiple_choice":
|
|
return activity.question;
|
|
case "true_false":
|
|
return activity.statement;
|
|
case "fill_blank":
|
|
return activity.sentence;
|
|
case "matching":
|
|
return activity.instruction;
|
|
case "ordering":
|
|
return activity.instruction;
|
|
case "short_answer":
|
|
return activity.question;
|
|
}
|
|
}
|
|
|
|
export function isReported(deckId: string, activityId: string): boolean {
|
|
return reportStore.getSnapshot().some((report) => report.deckId === deckId && report.activityId === activityId);
|
|
}
|
|
|
|
/**
|
|
* The deck's activities minus the reported ones. Never returns an empty list:
|
|
* a deck where everything was reported stays studiable (and regenerable).
|
|
*/
|
|
export function withoutReported(deckId: string, activities: Activity[]): Activity[] {
|
|
const kept = activities.filter((activity) => !isReported(deckId, activity.id));
|
|
return kept.length > 0 ? kept : activities;
|
|
}
|
|
|
|
/** Adds (or refreshes) a report and sends a copy to the server, best effort. */
|
|
export function reportQuestion(input: {
|
|
/** A deck id, or the homework record's id for a homework exercise. */
|
|
deckId: string;
|
|
deckTitle?: string;
|
|
activity: Activity;
|
|
}): QuestionReport {
|
|
const entry: QuestionReport = {
|
|
deckId: input.deckId,
|
|
deckTitle: input.deckTitle,
|
|
activityId: input.activity.id,
|
|
type: input.activity.type,
|
|
question: activityText(input.activity).slice(0, 600),
|
|
at: Date.now(),
|
|
};
|
|
ensureLoaded();
|
|
const others = reportStore
|
|
.getSnapshot()
|
|
.filter((report) => !(report.deckId === entry.deckId && report.activityId === entry.activityId));
|
|
write([entry, ...others].slice(0, LIMIT));
|
|
|
|
// The operator's copy: appended under the shared data dir by /api/reports.
|
|
// Offline or a server hiccup never blocks the skip.
|
|
void fetch("/api/reports", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(entry),
|
|
}).catch(() => {});
|
|
|
|
return entry;
|
|
}
|
|
|
|
/** Undo: forget the report locally (the server file is append-only). */
|
|
export function unreportQuestion(deckId: string, activityId: string): void {
|
|
ensureLoaded();
|
|
write(
|
|
reportStore
|
|
.getSnapshot()
|
|
.filter((report) => !(report.deckId === deckId && report.activityId === activityId)),
|
|
);
|
|
}
|