Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(learner): align progress page with reading records

XZHe2c77cf602c8
diff --git a/web/app/(utility)/settings/progress/page.tsx b/web/app/(utility)/settings/progress/page.tsx
index 440a6ea8f..260c3cd23 100644
--- a/web/app/(utility)/settings/progress/page.tsx
+++ b/web/app/(utility)/settings/progress/page.tsx
@@ -3,32 +3,101 @@
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
-import { listLearningRecords, type LearningRecord } from "@/lib/learning-records-api";
+import {
+ listLearningRecords,
+ type LearningRecords,
+} from "@/lib/learning-records-api";
-function label(record: LearningRecord): string {
- return String(record.title ?? record.material_title ?? record.material_id ?? "Learning activity");
-}
-
-function isCompleted(record: LearningRecord): boolean {
- return record.completed === true;
-}
-
-function score(record: LearningRecord): number | null {
- const value = Number(record.score);
- const total = Number(record.total);
- return Number.isFinite(value) && Number.isFinite(total) && total > 0 ? value / total : null;
+function percent(value: number): number {
+ return Math.round(value * 100);
}
export default function LearningProgressPage() {
const { t } = useTranslation();
- const [records, setRecords] = useState<LearningRecord[] | null>(null);
+ const [records, setRecords] = useState<LearningRecords | null>(null);
const [error, setError] = useState(false);
useEffect(() => { void listLearningRecords().then(setRecords).catch(() => setError(true)); }, []);
- const completed = records?.filter(isCompleted).length ?? 0;
- const scores = records?.map(score).filter((value): value is number => value !== null) ?? [];
- const averageScore = scores.length ? Math.round((scores.reduce((sum, value) => sum + value, 0) / scores.length) * 100) : null;
+ const progress = records?.progress ?? [];
+ const activities = records?.activities ?? [];
+ const hasRecords = progress.length > 0 || activities.length > 0;
- return <main className="mx-auto max-w-3xl px-6 py-8"><h1 className="text-xl font-semibold">{t("Learning progress")}</h1><p className="mt-1 text-sm text-[var(--muted-foreground)]">{t("Review your own reading and learning activity.")}</p>{error ? <p className="mt-8 text-sm text-red-600">{t("Unable to load learning progress")}</p> : records === null ? <p className="mt-8 text-sm text-[var(--muted-foreground)]">{t("Loading progress")}</p> : records.length === 0 ? <p className="mt-8 text-sm text-[var(--muted-foreground)]">{t("No learning activity yet")}</p> : <><div className="mt-6 grid grid-cols-2 gap-3"><div className="rounded-md border border-[var(--border)] p-4"><span className="block text-xs text-[var(--muted-foreground)]">{t("Activities")}</span><strong className="mt-1 block text-xl">{records.length}</strong></div><div className="rounded-md border border-[var(--border)] p-4"><span className="block text-xs text-[var(--muted-foreground)]">{t("Completed")}</span><strong className="mt-1 block text-xl">{completed}</strong></div>{averageScore !== null && <div className="rounded-md border border-[var(--border)] p-4"><span className="block text-xs text-[var(--muted-foreground)]">{t("Average score")}</span><strong className="mt-1 block text-xl">{averageScore}%</strong></div>}</div><ul className="mt-6 grid gap-3">{records.map((record, index) => <li key={String(record.event_id ?? record.id ?? index)} className="rounded-md border border-[var(--border)] p-4"><span className="font-medium">{label(record)}</span><span className="mt-1 block text-xs text-[var(--muted-foreground)]">{String(record.action ?? record.kind ?? "")}</span></li>)}</ul></>}</main>;
+ return (
+ <main className="mx-auto max-w-3xl px-6 py-8">
+ <h1 className="text-xl font-semibold">{t("Learning progress")}</h1>
+ <p className="mt-1 text-sm text-[var(--muted-foreground)]">
+ {t("Review your own reading and learning activity.")}
+ </p>
+ {error ? (
+ <p className="mt-8 text-sm text-red-600">{t("Unable to load learning progress")}</p>
+ ) : records === null ? (
+ <p className="mt-8 text-sm text-[var(--muted-foreground)]">{t("Loading progress")}</p>
+ ) : !hasRecords ? (
+ <p className="mt-8 text-sm text-[var(--muted-foreground)]">{t("No learning activity yet")}</p>
+ ) : (
+ <>
+ <div className="mt-6 grid grid-cols-2 gap-3">
+ <div className="rounded-md border border-[var(--border)] p-4">
+ <span className="block text-xs text-[var(--muted-foreground)]">{t("Materials")}</span>
+ <strong className="mt-1 block text-xl">{progress.length}</strong>
+ </div>
+ <div className="rounded-md border border-[var(--border)] p-4">
+ <span className="block text-xs text-[var(--muted-foreground)]">{t("Learning actions")}</span>
+ <strong className="mt-1 block text-xl">{activities.length}</strong>
+ </div>
+ </div>
+ <section className="mt-6">
+ <h2 className="text-sm font-semibold">{t("Reading progress")}</h2>
+ {progress.length === 0 ? (
+ <p className="mt-3 text-sm text-[var(--muted-foreground)]">{t("No reading progress yet")}</p>
+ ) : (
+ <ul className="mt-3 grid gap-3">
+ {progress.map((record) => (
+ <li key={record.material_id} className="rounded-md border border-[var(--border)] p-4">
+ <div className="flex items-center justify-between gap-3">
+ <span className="min-w-0 truncate font-medium">{record.material_id}</span>
+ <span className="text-xs text-[var(--muted-foreground)]">
+ {percent(record.furthest_percentage)}%
+ </span>
+ </div>
+ <div
+ className="mt-3 h-1.5 overflow-hidden rounded-full bg-[var(--border)]"
+ role="progressbar"
+ aria-valuemin={0}
+ aria-valuemax={100}
+ aria-valuenow={percent(record.furthest_percentage)}
+ aria-label={t("Furthest reading progress")}
+ >
+ <div
+ className="h-full rounded-full bg-[var(--primary)]"
+ style={{ width: `${Math.min(100, Math.max(0, percent(record.furthest_percentage)))}%` }}
+ />
+ </div>
+ </li>
+ ))}
+ </ul>
+ )}
+ </section>
+ <section className="mt-6">
+ <h2 className="text-sm font-semibold">{t("Learning actions")}</h2>
+ {activities.length === 0 ? (
+ <p className="mt-3 text-sm text-[var(--muted-foreground)]">{t("No learning actions yet")}</p>
+ ) : (
+ <ul className="mt-3 grid gap-3">
+ {activities.map((record) => (
+ <li key={record.activity_id} className="rounded-md border border-[var(--border)] p-4">
+ <span className="font-medium">{record.material_id}</span>
+ <span className="mt-1 block text-xs text-[var(--muted-foreground)]">
+ {record.action} · {record.extension_id} · {t("Locator")} {record.locator}
+ </span>
+ </li>
+ ))}
+ </ul>
+ )}
+ </section>
+ </>
+ )}
+ </main>
+ );
}
diff --git a/web/lib/learning-records-api.ts b/web/lib/learning-records-api.ts
index e86ce981b..dbf0cf18c 100644
--- a/web/lib/learning-records-api.ts
+++ b/web/lib/learning-records-api.ts
@@ -1,11 +1,31 @@
import { apiFetch, apiUrl } from "@/lib/api";
-export interface LearningRecord {
- [key: string]: unknown;
+export interface ReadingProgressRecord {
+ material_id: string;
+ latest_locator: number;
+ latest_percentage: number;
+ furthest_locator: number;
+ furthest_percentage: number;
+ updated_at: number;
}
-export async function listLearningRecords(): Promise<LearningRecord[]> {
- const response = await apiFetch(apiUrl("/api/v1/learning/records"));
+export interface ReadingActivityRecord {
+ activity_id: string;
+ material_id: string;
+ extension_id: string;
+ action: string;
+ locator: number;
+ result_type: "card" | "quiz" | "feedback" | "browser_speech";
+ created_at: number;
+}
+
+export interface LearningRecords {
+ progress: ReadingProgressRecord[];
+ activities: ReadingActivityRecord[];
+}
+
+export async function listLearningRecords(): Promise<LearningRecords> {
+ const response = await apiFetch(apiUrl("/api/mastery-paths/reading/records"));
if (!response.ok) throw new Error("Failed to load learning progress");
- return response.json() as Promise<LearningRecord[]>;
+ return response.json() as Promise<LearningRecords>;
}
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index ea72027d8..8c50e700c 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -4045,5 +4045,16 @@
"The Invidious instance could not verify your account. Check the instance and reconnect.": "The Invidious instance could not verify your account. Check the instance and reconnect.",
"Your DeepTutor login expired during authorization. Sign in, then start a new Invidious connection.": "Your DeepTutor login expired during authorization. Sign in, then start a new Invidious connection.",
"The account could not be connected. Please start a new connection.": "The account could not be connected. Please start a new connection.",
- "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate.": "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate."
+ "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate.": "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate.",
+ "Learning progress": "Learning progress",
+ "Review your own reading and learning activity.": "Review your own reading and learning activity.",
+ "Unable to load learning progress": "Unable to load learning progress",
+ "Loading progress": "Loading progress",
+ "No learning activity yet": "No learning activity yet",
+ "Learning actions": "Learning actions",
+ "Reading progress": "Reading progress",
+ "No reading progress yet": "No reading progress yet",
+ "Furthest reading progress": "Furthest reading progress",
+ "No learning actions yet": "No learning actions yet",
+ "Locator": "Locator"
}
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index 702df3f39..def6dbfe3 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -4045,5 +4045,16 @@
"The Invidious instance could not verify your account. Check the instance and reconnect.": "Invidious 实例暂时无法验证账号,请检查实例后重新关联。",
"Your DeepTutor login expired during authorization. Sign in, then start a new Invidious connection.": "授权期间 DeepTutor 登录已失效,请先登录 DeepTutor,再重新关联 Invidious。",
"The account could not be connected. Please start a new connection.": "账号关联失败,请重新发起关联。",
- "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate.": "请使用 Invidious 账号登录,确认只读授权后会自动返回此页。DeepTutor 与 Invidious 使用各自的登录账号。"
+ "Sign in with your Invidious account, then approve read-only access to return here. Your DeepTutor login is separate.": "请使用 Invidious 账号登录,确认只读授权后会自动返回此页。DeepTutor 与 Invidious 使用各自的登录账号。",
+ "Learning progress": "学习进度",
+ "Review your own reading and learning activity.": "查看你自己的阅读与学习活动。",
+ "Unable to load learning progress": "无法加载学习进度",
+ "Loading progress": "正在加载进度",
+ "No learning activity yet": "还没有学习活动",
+ "Learning actions": "学习操作",
+ "Reading progress": "阅读进度",
+ "No reading progress yet": "还没有阅读进度",
+ "Furthest reading progress": "最远阅读进度",
+ "No learning actions yet": "还没有学习操作",
+ "Locator": "位置"
}
diff --git a/web/tests/e2e/learning-progress.audit.ts b/web/tests/e2e/learning-progress.audit.ts
new file mode 100644
index 000000000..0b0a5a3f4
--- /dev/null
+++ b/web/tests/e2e/learning-progress.audit.ts
@@ -0,0 +1,114 @@
+import { expect, test } from "@playwright/test";
+
+test("learning progress uses the account reading records contract", async ({
+ page,
+}, testInfo) => {
+ const requestedPaths: string[] = [];
+
+ await page.route("**/api/**", async (route) => {
+ const request = route.request();
+ const path = new URL(request.url()).pathname;
+ requestedPaths.push(path);
+ const json = (payload: unknown, status = 200) =>
+ route.fulfill({ status, json: payload });
+
+ if (path === "/api/auth/status") {
+ return json({
+ enabled: false,
+ authenticated: true,
+ role: "admin",
+ is_admin: true,
+ });
+ }
+ if (path === "/api/settings/ui") return json({ language: "en" });
+ if (path === "/api/settings") {
+ return json({
+ ui: { theme: "system", language: "en", response_language: "en" },
+ catalog: {
+ version: 1,
+ connections: [],
+ services: {
+ llm: { active_profile_id: null, active_model_id: null, profiles: [] },
+ task: { active_profile_id: null, active_model_id: null, profiles: [] },
+ embedding: {
+ active_profile_id: null,
+ active_model_id: null,
+ profiles: [],
+ },
+ search: { active_profile_id: null, profiles: [] },
+ tts: { active_profile_id: null, active_model_id: null, profiles: [] },
+ stt: { active_profile_id: null, active_model_id: null, profiles: [] },
+ imagegen: {
+ active_profile_id: null,
+ active_model_id: null,
+ profiles: [],
+ },
+ videogen: {
+ active_profile_id: null,
+ active_model_id: null,
+ profiles: [],
+ },
+ },
+ },
+ });
+ }
+ if (path === "/api/mastery-paths/reading/records") {
+ return json({
+ progress: [
+ {
+ material_id: "rm_lesson",
+ latest_locator: 3,
+ latest_percentage: 0.3,
+ furthest_locator: 5,
+ furthest_percentage: 0.5,
+ updated_at: 1,
+ },
+ ],
+ activities: [
+ {
+ activity_id: "racc_lesson",
+ material_id: "rm_lesson",
+ extension_id: "guided_learning",
+ action: "guide",
+ locator: 4,
+ result_type: "card",
+ created_at: 2,
+ },
+ ],
+ });
+ }
+ return json({});
+ });
+
+ await page.goto("/settings/progress", { waitUntil: "domcontentloaded" });
+
+ await expect(page.getByRole("heading", { name: "Learning progress" })).toBeVisible();
+ await expect(page.getByText("rm_lesson").first()).toBeVisible();
+ await expect(page.getByRole("progressbar").first()).toHaveAttribute(
+ "aria-valuenow",
+ "50",
+ );
+ await expect(page.getByText(/guide · guided_learning · Locator 4/)).toBeVisible();
+
+ await page.setViewportSize({ width: 1280, height: 800 });
+ await page.screenshot({
+ path: testInfo.outputPath("desktop.png"),
+ fullPage: true,
+ });
+
+ await page.setViewportSize({ width: 390, height: 844 });
+ await expect
+ .poll(() =>
+ page.evaluate(
+ () => document.documentElement.scrollWidth - document.documentElement.clientWidth,
+ ),
+ )
+ .toBeLessThanOrEqual(1);
+ await page.screenshot({
+ path: testInfo.outputPath("mobile.png"),
+ fullPage: true,
+ });
+
+ expect(requestedPaths).toContain("/api/mastery-paths/reading/records");
+ expect(requestedPaths).not.toContain("/api/v1/learning/records");
+});
diff --git a/web/tests/learning-records-api.test.ts b/web/tests/learning-records-api.test.ts
new file mode 100644
index 000000000..62d58b421
--- /dev/null
+++ b/web/tests/learning-records-api.test.ts
@@ -0,0 +1,69 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { listLearningRecords } from "../lib/learning-records-api";
+
+test("learning records request the account reading summary", async () => {
+ const originalFetch = globalThis.fetch;
+ let requestedUrl = "";
+ let requestedInit: RequestInit | undefined;
+ globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
+ requestedUrl = String(input);
+ requestedInit = init;
+ return new Response(
+ JSON.stringify({
+ progress: [],
+ activities: [],
+ }),
+ { status: 200, headers: { "Content-Type": "application/json" } },
+ );
+ };
+
+ try {
+ const records = await listLearningRecords();
+ assert.equal(requestedUrl, "/api/mastery-paths/reading/records");
+ assert.equal(requestedInit?.method, undefined);
+ assert.deepEqual(requestedInit?.credentials, "include");
+ assert.deepEqual(records, { progress: [], activities: [] });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});
+
+test("learning records preserve the backend's two-part response shape", async () => {
+ const originalFetch = globalThis.fetch;
+ const payload = {
+ progress: [
+ {
+ material_id: "rm_one",
+ latest_locator: 3,
+ latest_percentage: 0.3,
+ furthest_locator: 5,
+ furthest_percentage: 0.5,
+ updated_at: 1,
+ },
+ ],
+ activities: [
+ {
+ activity_id: "racc_one",
+ material_id: "rm_one",
+ extension_id: "guided_learning",
+ action: "guide",
+ locator: 4,
+ result_type: "card",
+ created_at: 2,
+ },
+ ],
+ };
+ globalThis.fetch = async () =>
+ new Response(JSON.stringify(payload), {
+ status: 200,
+ headers: { "Content-Type": "application/json" },
+ });
+
+ try {
+ assert.deepEqual(await listLearningRecords(), payload);
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+});