Skip to content

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

Block 3: frontend API client + multi-book page redesign with tab panel, delete/redo

whf4aaef7cc4189
diff --git a/web/app/(workspace)/learning/page.tsx b/web/app/(workspace)/learning/page.tsx
index 7cb0b417f..f9ce9fcfb 100644
--- a/web/app/(workspace)/learning/page.tsx
+++ b/web/app/(workspace)/learning/page.tsx
@@ -2,37 +2,35 @@
import { useEffect, useState, useCallback } from "react";
import { useRouter } from "next/navigation";
-import { BookOpen, Loader2, Play } from "lucide-react";
+import {
+ BookOpen,
+ Loader2,
+ Play,
+ Plus,
+ RotateCcw,
+ Trash2,
+} from "lucide-react";
import { useTranslation } from "react-i18next";
-import { fetchProgress } from "@/lib/learning-api";
-
-interface ModuleSummary {
- id: string;
- name: string;
- order: number;
- pass_threshold: number;
- knowledge_points: { id: string; name: string }[];
-}
+import {
+ fetchAllProgress,
+ deleteProgress,
+ redoProgress,
+ type ProgressSummary,
+} from "@/lib/learning-api";
+
+type Tab = "current" | "completed" | "all";
export default function LearningPage() {
const router = useRouter();
const { t } = useTranslation();
- const [modules, setModules] = useState<ModuleSummary[]>([]);
- const [masteryLevels, setMasteryLevels] = useState<Record<string, number>>({});
+ const [items, setItems] = useState<ProgressSummary[]>([]);
const [loading, setLoading] = useState(true);
+ const [tab, setTab] = useState<Tab>("all");
const [toast, setToast] = useState("");
- useEffect(() => {
- if (!toast) return;
- const timer = setTimeout(() => setToast(""), 3500);
- return () => clearTimeout(timer);
- }, [toast]);
-
const loadProgress = useCallback(async () => {
try {
- const data = await fetchProgress("default");
- setModules(data.modules ?? []);
- setMasteryLevels(data.mastery_levels ?? {});
+ setItems(await fetchAllProgress());
} catch {
setToast(t("guidedLearning.connectionError"));
} finally {
@@ -44,10 +42,23 @@ export default function LearningPage() {
loadProgress();
}, [loadProgress]);
- const masteredCount = (mod: ModuleSummary) =>
- mod.knowledge_points.filter(
- (kp) => (masteryLevels[kp.id] ?? 0) >= mod.pass_threshold,
- ).length;
+ useEffect(() => {
+ if (!toast) return;
+ const timer = setTimeout(() => setToast(""), 3500);
+ return () => clearTimeout(timer);
+ }, [toast]);
+
+ const filtered = items.filter((it) => {
+ if (tab === "current") return it.current_stage !== "completed";
+ if (tab === "completed") return it.current_stage === "completed";
+ return true;
+ });
+
+ const tabs: { key: Tab; label: string }[] = [
+ { key: "current", label: t("guidedLearning.tabCurrent") },
+ { key: "completed", label: t("guidedLearning.tabCompleted") },
+ { key: "all", label: t("guidedLearning.tabAll") },
+ ];
if (loading) {
return (
@@ -64,69 +75,132 @@ export default function LearningPage() {
{toast}
</div>
)}
+
+ {/* Header */}
<div className="flex items-center justify-between mb-6">
<div>
<h1 className="text-2xl font-bold">{t("guidedLearning.title")}</h1>
<p className="text-[var(--muted-foreground)] text-sm mt-1">
- {modules.length > 0
- ? `${modules.length} ${t("guidedLearning.modules")}`
- : t("guidedLearning.description")}
+ {t("guidedLearning.description")}
</p>
</div>
<button
onClick={() => router.push("/learning/default")}
className="flex items-center gap-2 px-4 py-2 rounded-lg bg-[var(--primary)] text-[var(--primary-foreground)] text-sm font-medium hover:opacity-90 transition-opacity"
>
- <Play className="w-4 h-4" />
- {t("guidedLearning.startLearning")}
+ <Plus className="w-4 h-4" />
+ {t("guidedLearning.createModule")}
</button>
</div>
- {modules.length === 0 ? (
+ {/* Tabs */}
+ <div className="flex gap-1 mb-6 border-b border-[var(--border)]">
+ {tabs.map((t) => (
+ <button
+ key={t.key}
+ onClick={() => setTab(t.key)}
+ className={`px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px ${
+ tab === t.key
+ ? "border-[var(--primary)] text-[var(--foreground)]"
+ : "border-transparent text-[var(--muted-foreground)] hover:text-[var(--foreground)]"
+ }`}
+ >
+ {t.label}
+ </button>
+ ))}
+ </div>
+
+ {/* Content */}
+ {filtered.length === 0 ? (
<div className="flex flex-col items-center justify-center py-20 text-[var(--muted-foreground)]">
<BookOpen className="w-12 h-12 mb-4 opacity-50" />
<p className="text-lg font-medium">{t("guidedLearning.noModules")}</p>
- <p className="text-sm mt-1">
- {t("guidedLearning.description")}
- </p>
+ <p className="text-sm mt-1">{t("guidedLearning.description")}</p>
</div>
) : (
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
- {modules
- .sort((a, b) => a.order - b.order)
- .map((mod) => {
- const mastered = masteredCount(mod);
- const total = mod.knowledge_points.length;
- const pct = total > 0 ? Math.round((mastered / total) * 100) : 0;
-
- return (
- <div
- key={mod.id}
- className="p-5 rounded-xl border border-[var(--border)] bg-[var(--card)]"
- >
- <h3 className="font-semibold text-[var(--foreground)] truncate">
- {mod.name}
- </h3>
- <p className="text-sm text-[var(--muted-foreground)] mt-1">
- {t("guidedLearning.moduleProgress", { count: total })}
- </p>
- {total > 0 && (
- <div className="mt-3">
- <div className="flex items-center justify-between text-xs text-[var(--muted-foreground)] mb-1">
- <span>{t("guidedLearning.masteredProgress", { mastered, total })}</span>
- <span>{pct}%</span>
- </div>
- <div className="w-full h-1.5 rounded-full bg-[var(--muted)]">
- <div
- className="h-full rounded-full bg-[var(--primary)] transition-all"
- style={{ width: `${pct}%` }}
- />
- </div>
+ {filtered
+ .sort((a, b) => (b.updated_at ?? 0) - (a.updated_at ?? 0))
+ .map((card) => (
+ <div
+ key={card.book_id}
+ className="p-5 rounded-xl border border-[var(--border)] bg-[var(--card)]"
+ >
+ <h3 className="font-semibold text-[var(--foreground)] truncate">
+ {card.book_id}
+ </h3>
+ <p className="text-sm text-[var(--muted-foreground)] mt-1">
+ {card.modules_count} {t("guidedLearning.modules")} ·{" "}
+ {card.kp_count} {t("guidedLearning.knowledgePoints")}
+ </p>
+
+ {/* Mastery bar */}
+ {card.kp_count > 0 && (
+ <div className="mt-3">
+ <div className="flex items-center justify-between text-xs text-[var(--muted-foreground)] mb-1">
+ <span>{t("guidedLearning.mastered")}</span>
+ <span>{card.mastered_pct}%</span>
+ </div>
+ <div className="w-full h-1.5 rounded-full bg-[var(--muted)]">
+ <div
+ className="h-full rounded-full bg-[var(--primary)] transition-all"
+ style={{ width: `${card.mastered_pct}%` }}
+ />
</div>
- )}
+ </div>
+ )}
+
+ {/* Timestamp */}
+ {card.updated_at > 0 && (
+ <p className="text-xs text-[var(--muted-foreground)] mt-2">
+ {new Date(card.updated_at * 1000).toLocaleDateString()}
+ </p>
+ )}
+
+ {/* Actions */}
+ <div className="flex items-center gap-2 mt-3 pt-3 border-t border-[var(--border)]">
+ <button
+ onClick={() => router.push(`/learning/${card.book_id}`)}
+ className="flex items-center gap-1 px-3 py-1.5 text-xs rounded-lg bg-[var(--primary)] text-[var(--primary-foreground)] hover:opacity-90"
+ >
+ <Play className="w-3.5 h-3.5" />
+ {t("guidedLearning.continueLearning")}
+ </button>
+
+ <button
+ onClick={async () => {
+ if (!confirm(t("guidedLearning.redoConfirm"))) return;
+ try {
+ await redoProgress(card.book_id);
+ loadProgress();
+ } catch {
+ setToast(t("guidedLearning.redoFailed"));
+ }
+ }}
+ className="flex items-center gap-1 px-3 py-1.5 text-xs rounded-lg border border-[var(--border)] text-[var(--muted-foreground)] hover:text-[var(--foreground)] hover:bg-[var(--background)]/60"
+ >
+ <RotateCcw className="w-3.5 h-3.5" />
+ {t("guidedLearning.redo")}
+ </button>
+
+ <button
+ onClick={async () => {
+ if (!confirm(t("guidedLearning.deleteConfirm"))) return;
+ try {
+ await deleteProgress(card.book_id);
+ loadProgress();
+ } catch {
+ setToast(t("guidedLearning.deleteFailed"));
+ }
+ }}
+ className="flex items-center gap-1 px-3 py-1.5 text-xs rounded-lg text-red-500 hover:bg-red-500/10"
+ >
+ <Trash2 className="w-3.5 h-3.5" />
+ {t("guidedLearning.delete")}
+ </button>
</div>
- );
- })}
+ </div>
+ ))}
</div>
)}
</div>
diff --git a/web/lib/learning-api.ts b/web/lib/learning-api.ts
index ec253dd2f..06384672e 100644
--- a/web/lib/learning-api.ts
+++ b/web/lib/learning-api.ts
@@ -41,3 +41,30 @@ export async function initModules(bookId: string, modules: ModuleInit[]) {
if (!res.ok) throw new Error(`Failed to init modules: ${res.status}`);
return res.json();
}
+
+export interface ProgressSummary {
+ book_id: string;
+ modules_count: number;
+ kp_count: number;
+ current_stage: string;
+ mastered_pct: number;
+ updated_at: number;
+}
+
+export async function fetchAllProgress(): Promise<ProgressSummary[]> {
+ const res = await apiFetch(apiUrl("/api/v1/learning/progress"));
+ if (!res.ok) throw new Error(`Failed to fetch all progress: ${res.status}`);
+ return res.json();
+}
+
+export async function deleteProgress(bookId: string) {
+ const res = await apiFetch(apiUrl(`/api/v1/learning/progress/${encodeURIComponent(bookId)}`), { method: "DELETE" });
+ if (!res.ok) throw new Error(`Failed to delete progress: ${res.status}`);
+ return res.json();
+}
+
+export async function redoProgress(bookId: string) {
+ const res = await apiFetch(apiUrl(`/api/v1/learning/progress/${encodeURIComponent(bookId)}/redo`), { method: "POST" });
+ if (!res.ok) throw new Error(`Failed to redo progress: ${res.status}`);
+ return res.json();
+}
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index 6ca241ea1..ea458727b 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -1624,6 +1624,18 @@
"guidedLearning.inputPlaceholder": "Type your answer...",
"guidedLearning.submit": "Submit",
"guidedLearning.noModulesLoaded": "No modules loaded",
+ "guidedLearning.createModule": "Create Module",
+ "guidedLearning.tabCurrent": "In Progress",
+ "guidedLearning.tabCompleted": "Completed",
+ "guidedLearning.tabAll": "All",
+ "guidedLearning.mastered": "Mastered",
+ "guidedLearning.continueLearning": "Continue Learning",
+ "guidedLearning.redo": "Redo",
+ "guidedLearning.redoConfirm": "This will reset your progress but keep modules. Continue?",
+ "guidedLearning.redoFailed": "Failed to redo progress",
+ "guidedLearning.delete": "Delete",
+ "guidedLearning.deleteConfirm": "This will permanently delete all progress and modules. Continue?",
+ "guidedLearning.deleteFailed": "Failed to delete progress",
"guidedLearning.stage.diagnostic_phase1": "Diagnostic Phase 1",
"guidedLearning.stage.diagnostic_phase2": "Diagnostic Phase 2",
"guidedLearning.stage.metacognitive_intro": "Metacognitive Intro",
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index c560fa7ed..27dcd8bea 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -1624,6 +1624,18 @@
"guidedLearning.inputPlaceholder": "输入你的答案...",
"guidedLearning.submit": "提交",
"guidedLearning.noModulesLoaded": "暂无模块",
+ "guidedLearning.createModule": "创建模块",
+ "guidedLearning.tabCurrent": "当前学习",
+ "guidedLearning.tabCompleted": "已完成",
+ "guidedLearning.tabAll": "全部",
+ "guidedLearning.mastered": "已掌握",
+ "guidedLearning.continueLearning": "继续学习",
+ "guidedLearning.redo": "重做",
+ "guidedLearning.redoConfirm": "将重置学习进度但保留模块结构,确认继续?",
+ "guidedLearning.redoFailed": "重做失败",
+ "guidedLearning.delete": "删除",
+ "guidedLearning.deleteConfirm": "将永久删除所有进度和模块,确认继续?",
+ "guidedLearning.deleteFailed": "删除失败",
"guidedLearning.stage.diagnostic_phase1": "诊断阶段一",
"guidedLearning.stage.diagnostic_phase2": "诊断阶段二",
"guidedLearning.stage.metacognitive_intro": "元认知导入",