feat(error-book): implement error book module with SM2 spaced repetition

- Add SM2 algorithm implementation with tests for spaced repetition review scheduling

- Add data-access, schema, types, and server actions for error book CRUD

- Add components: add dialog, class overview, filters, item card, stats cards, review buttons, top wrong questions

- Add error-book routes for admin, teacher, parent, and student roles

- Add i18n messages (en, zh-CN) for error book module
This commit is contained in:
SpecialX
2026-06-23 17:36:42 +08:00
parent 396c2c568d
commit bf056399c6
26 changed files with 3613 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
"use client"
import { BarChart3 } from "lucide-react"
import { EmptyState } from "@/shared/components/ui/empty-state"
export default function AdminErrorBookError() {
return (
<div className="p-8">
<EmptyState
icon={BarChart3}
title="加载全校错题分析失败"
description="发生了一些错误,请刷新页面重试。"
action={{ label: "刷新页面", onClick: () => window.location.reload() }}
className="border-none shadow-none"
/>
</div>
)
}

View File

@@ -0,0 +1,23 @@
import { Skeleton } from "@/shared/components/ui/skeleton"
export default function AdminErrorBookLoading() {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div className="space-y-2">
<Skeleton className="h-8 w-[200px]" />
<Skeleton className="h-4 w-[300px]" />
</div>
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
{Array.from({ length: 4 }).map((_, idx) => (
<Skeleton key={idx} className="h-[120px] w-full rounded-md" />
))}
</div>
<div className="grid gap-4 md:grid-cols-2">
{Array.from({ length: 2 }).map((_, idx) => (
<Skeleton key={idx} className="h-[300px] w-full rounded-md" />
))}
</div>
<Skeleton className="h-[400px] w-full rounded-md" />
</div>
)
}

View File

@@ -0,0 +1,113 @@
import type { JSX } from "react"
import { BarChart3 } from "lucide-react"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { EmptyState } from "@/shared/components/ui/empty-state"
import {
getStudentErrorBookSummaries,
getTopWrongQuestionsByStudentIds,
getKnowledgePointWeakness,
getSubjectErrorDistribution,
getStudentNameMap,
getAllStudentIds,
} from "@/modules/error-book/data-access"
import { ClassErrorBookOverview, StudentErrorTable } from "@/modules/error-book/components/class-error-overview"
import { TopWrongQuestions } from "@/modules/error-book/components/top-wrong-questions"
export const dynamic = "force-dynamic"
export default async function AdminErrorBookPage(): Promise<JSX.Element> {
const ctx = await requirePermission(Permissions.ERROR_BOOK_ANALYTICS_READ)
if (ctx.dataScope.type !== "all") {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground"></p>
</div>
<EmptyState
icon={BarChart3}
title="权限不足"
description="您没有权限查看全校错题分析数据。"
className="h-[360px] bg-card"
/>
</div>
)
}
// 通过 data-access 层查询所有学生 ID遵循三层架构app 层不直接访问 DB
const studentIds = await getAllStudentIds()
if (studentIds.length === 0) {
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground"></p>
</div>
<EmptyState
icon={BarChart3}
title="暂无学生数据"
description="系统中还没有学生用户,无法查看错题分析。"
className="h-[360px] bg-card"
/>
</div>
)
}
// 限制查询数量,避免性能问题(取最近活跃的 500 名学生)
const limitedStudentIds = studentIds.slice(0, 500)
const [summaries, topWrongQuestions, weakKps, subjectDist, nameMap] = await Promise.all([
getStudentErrorBookSummaries(limitedStudentIds),
getTopWrongQuestionsByStudentIds(limitedStudentIds, 10),
getKnowledgePointWeakness(limitedStudentIds, 10),
getSubjectErrorDistribution(limitedStudentIds),
getStudentNameMap(limitedStudentIds),
])
const studentsWithErrorBook = summaries.filter((s) => s.totalCount > 0)
const totalErrorItems = summaries.reduce((sum, s) => sum + s.totalCount, 0)
const averageMasteryRate = studentsWithErrorBook.length > 0
? studentsWithErrorBook.reduce((sum, s) => sum + s.masteredRate, 0) / studentsWithErrorBook.length
: 0
const sortedSummaries = [...summaries]
.filter((s) => s.totalCount > 0)
.sort((a, b) => b.totalCount - a.totalCount)
.slice(0, 50)
return (
<div className="flex h-full flex-col space-y-8 p-8">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground">
</p>
</div>
<ClassErrorBookOverview
totalStudents={studentIds.length}
studentsWithErrorBook={studentsWithErrorBook.length}
totalErrorItems={totalErrorItems}
averageMasteryRate={averageMasteryRate}
topWeakKnowledgePoints={weakKps}
subjectDistribution={subjectDist}
/>
<div className="space-y-4">
<h2 className="text-lg font-semibold"> Top 50</h2>
<StudentErrorTable
students={sortedSummaries}
studentNames={nameMap}
basePath="/admin/error-book"
/>
</div>
<TopWrongQuestions questions={topWrongQuestions} />
</div>
)
}