Block 12: i18n strings (en/zh) + error toast for learning pages

This commit is contained in:
whf
2026-05-14 05:31:34 +08:00
parent 92ae45e361
commit 14a7d07438
4 changed files with 70 additions and 17 deletions

View File

@@ -44,6 +44,7 @@ export default function LearningBookPage() {
const currentStageRef = useRef<string>("");
const [connecting, setConnecting] = useState(true);
const [error, setError] = useState<string | null>(null);
const [toast, setToast] = useState("");
const wsRef = useRef<WebSocket | null>(null);
interface ModuleData {
@@ -95,7 +96,7 @@ export default function LearningBookPage() {
} catch { /* ignore parse errors */ }
};
ws.onerror = () => setError("Connection error");
ws.onerror = () => setError(t("guidedLearning.connectionError"));
ws.onclose = () => setConnecting(true);
}, [params.bookId]);
@@ -129,12 +130,19 @@ export default function LearningBookPage() {
}
} else if (evt.type === "error") {
setError(evt.content);
setToast(t("guidedLearning.stageFailed"));
setStages(prev => prev.map(s =>
s.stage === currentStageRef.current ? { ...s, status: "error" } : s
));
}
};
useEffect(() => {
if (!toast) return;
const timer = setTimeout(() => setToast(""), 3500);
return () => clearTimeout(timer);
}, [toast]);
// Fetch learning progress for module tree
useEffect(() => {
fetchProgress();
@@ -146,13 +154,18 @@ export default function LearningBookPage() {
}, [connect]);
return (
<div className="flex h-full">
<div className="flex h-full relative">
{toast && (
<div className="fixed top-4 right-4 z-50 rounded-lg bg-red-500/90 px-4 py-2 text-sm text-white shadow-lg">
{toast}
</div>
)}
{/* Sidebar */}
<div className="w-64 border-r border-[var(--border)] p-4 overflow-y-auto flex flex-col gap-4">
{/* Module tree */}
{modules.length > 0 && (
<div>
<h2 className="text-sm font-semibold mb-2">Modules</h2>
<h2 className="text-sm font-semibold mb-2">{t("guidedLearning.modules")}</h2>
<ModuleTree
modules={modules}
masteryLevels={masteryLevels}
@@ -165,7 +178,7 @@ export default function LearningBookPage() {
<div>
<h2 className="text-sm font-semibold mb-3 flex items-center gap-2">
<Lightbulb className="w-4 h-4 text-[var(--primary)]" />
Learning Stages
{t("guidedLearning.stages")}
</h2>
{connecting && <Loader2 className="w-4 h-4 animate-spin text-[var(--muted-foreground)]" />}
{stages.map((s) => (
@@ -190,7 +203,7 @@ export default function LearningBookPage() {
</div>
) : (
<div className="flex items-center justify-center h-full text-[var(--muted-foreground)]">
{connecting ? <Loader2 className="w-8 h-8 animate-spin" /> : "Ready to start learning"}
{connecting ? <Loader2 className="w-8 h-8 animate-spin" /> : t("guidedLearning.ready")}
</div>
)}
</div>

View File

@@ -3,6 +3,7 @@
import { useEffect, useState, useCallback } from "react";
import { useRouter } from "next/navigation";
import { BookOpen, Loader2, Play } from "lucide-react";
import { useTranslation } from "react-i18next";
import { fetchProgress } from "@/lib/learning-api";
interface ModuleSummary {
@@ -15,9 +16,17 @@ interface ModuleSummary {
export default function LearningPage() {
const router = useRouter();
const { t } = useTranslation();
const [modules, setModules] = useState<ModuleSummary[]>([]);
const [masteryLevels, setMasteryLevels] = useState<Record<string, number>>({});
const [loading, setLoading] = useState(true);
const [toast, setToast] = useState("");
useEffect(() => {
if (!toast) return;
const timer = setTimeout(() => setToast(""), 3500);
return () => clearTimeout(timer);
}, [toast]);
const loadProgress = useCallback(async () => {
try {
@@ -25,11 +34,11 @@ export default function LearningPage() {
setModules(data.modules ?? []);
setMasteryLevels(data.mastery_levels ?? {});
} catch {
// No progress yet — show empty state
setToast(t("guidedLearning.connectionError"));
} finally {
setLoading(false);
}
}, []);
}, [t]);
useEffect(() => {
loadProgress();
@@ -50,13 +59,18 @@ export default function LearningPage() {
return (
<div className="p-8 max-w-4xl mx-auto">
{toast && (
<div className="fixed top-4 right-4 z-50 rounded-lg bg-red-500/90 px-4 py-2 text-sm text-white shadow-lg">
{toast}
</div>
)}
<div className="flex items-center justify-between mb-6">
<div>
<h1 className="text-2xl font-bold">Learning Modules</h1>
<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} module${modules.length > 1 ? "s" : ""} available`
: "Start a learning session to generate modules"}
? `${modules.length} ${t("guidedLearning.modules")}`
: t("guidedLearning.description")}
</p>
</div>
<button
@@ -64,16 +78,16 @@ export default function LearningPage() {
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" />
Start Learning
{t("guidedLearning.startLearning")}
</button>
</div>
{modules.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">No modules yet</p>
<p className="text-lg font-medium">{t("guidedLearning.noModules")}</p>
<p className="text-sm mt-1">
Click &quot;Start Learning&quot; to begin — modules will be created automatically.
{t("guidedLearning.description")}
</p>
</div>
) : (
@@ -94,12 +108,12 @@ export default function LearningPage() {
{mod.name}
</h3>
<p className="text-sm text-[var(--muted-foreground)] mt-1">
{total} knowledge point{total !== 1 ? "s" : ""}
{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>{mastered}/{total} mastered</span>
<span>{t("guidedLearning.masteredProgress", { mastered, total })}</span>
<span>{pct}%</span>
</div>
<div className="w-full h-1.5 rounded-full bg-[var(--muted)]">

View File

@@ -1605,5 +1605,18 @@
"Co-Writer tooltip": "AI-powered document editor.",
"Book tooltip": "Create AI-powered books.",
"Knowledge tooltip": "Manage learning personas.",
"Space tooltip": "Your personal learning library."
"Space tooltip": "Your personal learning library.",
"guidedLearning.title": "Guided Learning",
"guidedLearning.description": "Framework v1.8.2 — structured mastery-based learning",
"guidedLearning.noModules": "No modules yet",
"guidedLearning.startLearning": "Start Learning",
"guidedLearning.modules": "Modules",
"guidedLearning.knowledgePoints": "knowledge points",
"guidedLearning.mastered": "mastered",
"guidedLearning.stages": "Learning Stages",
"guidedLearning.ready": "Ready to start learning",
"guidedLearning.connectionError": "Connection error",
"guidedLearning.stageFailed": "Stage execution failed. Progress saved, will retry this stage.",
"guidedLearning.moduleProgress": "{{count}} knowledge point(s)",
"guidedLearning.masteredProgress": "{{mastered}}/{{total}} mastered"
}

View File

@@ -1605,5 +1605,18 @@
"Co-Writer tooltip": "AI 驱动的协作文档编辑器。",
"Book tooltip": "生成交互式 AI 书籍。",
"Knowledge tooltip": "管理文档与教学人设。",
"Space tooltip": "你的个人学习空间。"
"Space tooltip": "你的个人学习空间。",
"guidedLearning.title": "引导式学习",
"guidedLearning.description": "框架 v1.8.2 — 结构化掌握式学习",
"guidedLearning.noModules": "暂无模块",
"guidedLearning.startLearning": "开始学习",
"guidedLearning.modules": "学习模块",
"guidedLearning.knowledgePoints": "个知识点",
"guidedLearning.mastered": "已掌握",
"guidedLearning.stages": "学习阶段",
"guidedLearning.ready": "准备开始学习",
"guidedLearning.connectionError": "连接错误",
"guidedLearning.stageFailed": "阶段执行失败。进度已保存,将重试该阶段。",
"guidedLearning.moduleProgress": "{{count}} 个知识点",
"guidedLearning.masteredProgress": "已掌握 {{mastered}}/{{total}}"
}