DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
Block 9: ModuleTree nav component with mastery indicators, fetch progress from API
whf2620bf01b4be
diff --git a/web/app/(workspace)/learning/[bookId]/page.tsx b/web/app/(workspace)/learning/[bookId]/page.tsxindex 0f1ec2e3e..befce85c1 100644--- a/web/app/(workspace)/learning/[bookId]/page.tsx+++ b/web/app/(workspace)/learning/[bookId]/page.tsx@@ -4,7 +4,8 @@ import { useParams } from "next/navigation";import { useEffect, useState, useRef, useCallback } from "react";import { Lightbulb, Loader2 } from "lucide-react";import { useTranslation } from "react-i18next";-import { wsUrl } from "@/lib/api";+import { apiUrl, wsUrl } from "@/lib/api";+import ModuleTree from "@/components/learning/ModuleTree";interface StreamEvent {type: string;@@ -45,6 +46,31 @@ export default function LearningBookPage() {const [error, setError] = useState<string | null>(null);const wsRef = useRef<WebSocket | null>(null);+ interface ModuleData {+ id: string;+ name: string;+ order: number;+ knowledge_points: { id: string; name: string; type: string }[];+ pass_threshold: number;+ }+ const [modules, setModules] = useState<ModuleData[]>([]);+ const [masteryLevels, setMasteryLevels] = useState<Record<string, number>>({});+ const [currentModuleId, setCurrentModuleId] = useState<string>("");++ const fetchProgress = useCallback(() => {+ fetch(apiUrl(`/api/v1/learning/progress/${params.bookId}`), { credentials: "include" })+ .then((r) => r.json())+ .then((data) => {+ setModules(data.modules ?? []);+ setMasteryLevels(data.mastery_levels ?? {});+ setCurrentModuleId(data.current_module_id ?? "");+ })+ .catch(() => {});+ }, [params.bookId]);++ const fetchProgressRef = useRef(fetchProgress);+ fetchProgressRef.current = fetchProgress;+const connect = useCallback(() => {const ws = new WebSocket(wsUrl("/api/v1/ws"));wsRef.current = ws;@@ -95,8 +121,12 @@ export default function LearningBookPage() {setStages(prev => prev.map(s =>s.stage === currentStageRef.current ? { ...s, status: "completed" } : s));+ const endedStage = currentStageRef.current;currentStageRef.current = "";setCurrentStage("");+ if (["plan", "diagnostic_phase1", "diagnostic_phase2", "review", "module_test"].includes(endedStage)) {+ fetchProgressRef.current();+ }} else if (evt.type === "error") {setError(evt.content);setStages(prev => prev.map(s =>@@ -105,6 +135,11 @@ export default function LearningBookPage() {}};+ // Fetch learning progress for module tree+ useEffect(() => {+ fetchProgress();+ }, [fetchProgress]);+useEffect(() => {connect();return () => { wsRef.current?.close(); };@@ -112,12 +147,26 @@ export default function LearningBookPage() {return (<div className="flex h-full">- {/* Stage sidebar */}- <div className="w-64 border-r border-[var(--border)] p-4 overflow-y-auto">- <h2 className="text-sm font-semibold mb-3 flex items-center gap-2">- <Lightbulb className="w-4 h-4 text-[var(--primary)]" />- Learning Stages- </h2>+ {/* 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>+ <ModuleTree+ modules={modules}+ masteryLevels={masteryLevels}+ currentModuleId={currentModuleId}+ currentStage={currentStage}+ />+ </div>+ )}+ {/* Stage progress */}+ <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+ </h2>{connecting && <Loader2 className="w-4 h-4 animate-spin text-[var(--muted-foreground)]" />}{stages.map((s) => (<div key={s.stage} className="flex items-center gap-2 py-1 text-sm">@@ -130,6 +179,7 @@ export default function LearningBookPage() {</span></div>))}+ </div></div>{/* Content area */}<div className="flex-1 p-8 overflow-y-auto">diff --git a/web/components/learning/ModuleTree.tsx b/web/components/learning/ModuleTree.tsxnew file mode 100644index 000000000..0f0fe3817--- /dev/null+++ b/web/components/learning/ModuleTree.tsx@@ -0,0 +1,124 @@+"use client";++import { useState, useEffect } from "react";+import { ChevronRight, ChevronDown, Circle } from "lucide-react";++interface KnowledgePoint {+ id: string;+ name: string;+ type: string;+}++interface Module {+ id: string;+ name: string;+ order: number;+ knowledge_points: KnowledgePoint[];+ pass_threshold: number;+}++interface ModuleTreeProps {+ modules: Module[];+ masteryLevels: Record<string, number>;+ currentModuleId: string;+ currentStage: string;+}++function masteryColor(mastery: number, threshold: number): string {+ if (mastery >= threshold) return "text-green-500";+ if (mastery > 0) return "text-yellow-500";+ return "text-[var(--muted-foreground)]";+}++function ModuleNode({+ module,+ masteryLevels,+ isCurrent,+}: {+ module: Module;+ masteryLevels: Record<string, number>;+ isCurrent: boolean;+}) {+ const [expanded, setExpanded] = useState(isCurrent);++ useEffect(() => {+ if (isCurrent) setExpanded(true);+ }, [isCurrent]);++ const mastered = module.knowledge_points.filter(+ (kp) => (masteryLevels[kp.id] ?? 0) >= module.pass_threshold+ ).length;+ const total = module.knowledge_points.length;+ const passRate = total > 0 ? Math.round((mastered / total) * 100) : 0;++ return (+ <div>+ <button+ onClick={() => setExpanded((prev) => !prev)}+ className={`flex items-center gap-1.5 w-full px-2 py-1.5 text-left text-sm rounded-md transition-colors ${+ isCurrent+ ? "bg-[var(--primary)]/10 text-[var(--primary)]"+ : "text-[var(--foreground)] hover:bg-[var(--accent)]"+ }`}+ >+ {expanded ? (+ <ChevronDown className="w-3.5 h-3.5 shrink-0" />+ ) : (+ <ChevronRight className="w-3.5 h-3.5 shrink-0" />+ )}+ <span className="truncate flex-1">{module.name}</span>+ <span className="text-xs text-[var(--muted-foreground)] shrink-0">+ {passRate}%+ </span>+ </button>+ {expanded && (+ <div className="ml-4 border-l border-[var(--border)] pl-2">+ {module.knowledge_points.map((kp) => {+ const mastery = masteryLevels[kp.id] ?? 0;+ return (+ <div+ key={kp.id}+ className="flex items-center gap-1.5 px-2 py-1 text-sm text-[var(--muted-foreground)]"+ >+ <Circle+ className={`w-2 h-2 shrink-0 fill-current ${masteryColor(mastery, module.pass_threshold)}`}+ />+ <span className="truncate">{kp.name}</span>+ </div>+ );+ })}+ </div>+ )}+ </div>+ );+}++export default function ModuleTree({+ modules,+ masteryLevels,+ currentModuleId,+ currentStage,+}: ModuleTreeProps) {+ const sorted = [...modules].sort((a, b) => a.order - b.order);++ if (sorted.length === 0) {+ return (+ <div className="text-sm text-[var(--muted-foreground)] px-2 py-1">+ No modules loaded+ </div>+ );+ }++ return (+ <div className="space-y-0.5">+ {sorted.map((mod) => (+ <ModuleNode+ key={mod.id}+ module={mod}+ masteryLevels={masteryLevels}+ isCurrent={mod.id === currentModuleId}+ />+ ))}+ </div>+ );+}