feat(textbooks): 教材模块审计重构 — 跨模块解耦 + 权限 + i18n + 错误边界 + 纯函数抽取

P0 修复:
- 解耦跨模块 UI 依赖:knowledge-point-dialogs 不再直接 import questions,
  改为 renderQuestionCreator render prop 由页面注入
- 接入 usePermission Hook 替换 canEdit 硬编码
- 全模块 i18n 改造:新增 en/zh-CN 翻译文件,替换所有硬编码文案
- Server Action 资源归属校验:新增 verifyChapterBelongsToTextbook/
  verifyKnowledgePointBelongsToTextbook,在 reorder/update/delete/create 中校验

P1 改进:
- 补齐 Error Boundary:4 个 error.tsx + TextbookSectionErrorBoundary 区块包裹
- 抽取纯函数到 utils.ts/graph-layout.ts/constants.ts 并补单测(26 用例全通过)
- 消除重复组件:删除 knowledge-point-panel/create-knowledge-point-dialog
- 修复类型断言:chapter.children! → 守卫式访问
- 图谱 a11y:添加 role/aria-label/aria-pressed
- 统一删除确认:confirm() → AlertDialog
- 数据范围过滤:getTextbooksWithScope 支持学生端按年级过滤

P2 预留:
- TextbookAnalytics 埋点接口 + Provider + Hook

同步 005 架构数据 JSON:补充 getTextbooksWithScope/verify*/ChapterTreeNode 等
This commit is contained in:
SpecialX
2026-06-22 16:25:59 +08:00
parent 45ee1ae43c
commit 22d3f07fcf
35 changed files with 2043 additions and 792 deletions

View File

@@ -0,0 +1,29 @@
"use client"
import { AlertCircle } from "lucide-react"
import { useTranslations } from "next-intl"
import { EmptyState } from "@/shared/components/ui/empty-state"
export default function StudentTextbookDetailError({
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
const t = useTranslations("textbooks")
return (
<div className="flex h-full flex-col items-center justify-center space-y-4 p-8">
<EmptyState
icon={AlertCircle}
title={t("error.loadFailed")}
description={t("error.loadFailedDesc")}
action={{
label: t("error.retry"),
onClick: () => reset(),
}}
className="border-none shadow-none h-auto"
/>
</div>
)
}

View File

@@ -1,4 +1,5 @@
import { notFound } from "next/navigation"
import { getTranslations } from "next-intl/server"
import { BookOpen } from "lucide-react"
@@ -7,6 +8,7 @@ import { TextbookReader } from "@/modules/textbooks/components/textbook-reader"
import { Badge } from "@/shared/components/ui/badge"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { getCurrentStudentUser } from "@/modules/users/data-access"
import { getGradeNameById } from "@/modules/school/data-access"
export const dynamic = "force-dynamic"
@@ -15,6 +17,7 @@ export default async function StudentTextbookDetailPage({
}: {
params: Promise<{ id: string }>
}) {
const t = await getTranslations("textbooks")
const student = await getCurrentStudentUser()
if (!student) return notFound()
@@ -28,6 +31,13 @@ export default async function StudentTextbookDetailPage({
if (!textbook) notFound()
// P1-1 数据范围过滤:校验教材年级与学生年级匹配
// student.gradeId 是 grades 表 id需解析为年级名称后才能与 textbooks.grade 字符串比较
const studentGradeName = student.gradeId ? await getGradeNameById(student.gradeId) : null
if (studentGradeName && textbook.grade && textbook.grade !== studentGradeName) {
notFound()
}
return (
<div className="flex h-[calc(100vh-4rem-3rem)] flex-col overflow-hidden bg-muted/5">
<div className="flex items-center justify-between border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 py-3 px-6 shrink-0 z-10">
@@ -48,13 +58,14 @@ export default async function StudentTextbookDetailPage({
<div className="h-full flex items-center justify-center rounded-lg border border-dashed bg-card">
<EmptyState
icon={BookOpen}
title="No chapters"
description="This textbook has no chapters yet."
title={t("reader.noChapters")}
description={t("reader.noChaptersDesc")}
className="border-none shadow-none"
/>
</div>
) : (
<div className="h-full min-h-0 max-w-[1600px] mx-auto w-full">
{/* 学生端不传 renderQuestionCreator无题目创建权限 */}
<TextbookReader chapters={chapters} knowledgePoints={knowledgePoints} />
</div>
)}

View File

@@ -0,0 +1,29 @@
"use client"
import { AlertCircle } from "lucide-react"
import { useTranslations } from "next-intl"
import { EmptyState } from "@/shared/components/ui/empty-state"
export default function StudentTextbooksError({
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
const t = useTranslations("textbooks")
return (
<div className="flex h-full flex-col items-center justify-center space-y-4 p-8">
<EmptyState
icon={AlertCircle}
title={t("error.loadFailed")}
description={t("error.loadFailedDesc")}
action={{
label: t("error.retry"),
onClick: () => reset(),
}}
className="border-none shadow-none h-auto"
/>
</div>
)
}

View File

@@ -1,16 +1,18 @@
import { BookOpen, UserX } from "lucide-react"
import { getTranslations } from "next-intl/server"
import { getTextbooks } from "@/modules/textbooks/data-access"
import { getTextbooksWithScope } from "@/modules/textbooks/data-access"
import { TextbookCard } from "@/modules/textbooks/components/textbook-card"
import { TextbookFilters } from "@/modules/textbooks/components/textbook-filters"
import { getCurrentStudentUser } from "@/modules/users/data-access"
import { getGradeNameById } from "@/modules/school/data-access"
import { EmptyState } from "@/shared/components/ui/empty-state"
export const dynamic = "force-dynamic"
type SearchParams = { [key: string]: string | string[] | undefined }
const getParam = (params: SearchParams, key: string) => {
const getParam = (params: SearchParams, key: string): string | undefined => {
const v = params[key]
return Array.isArray(v) ? v[0] : v
}
@@ -20,28 +22,39 @@ export default async function StudentTextbooksPage({
}: {
searchParams: Promise<SearchParams>
}) {
const t = await getTranslations("textbooks")
const [student, sp] = await Promise.all([getCurrentStudentUser(), searchParams])
if (!student) {
return (
<div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
<EmptyState title="No user found" description="Create a student user to see textbooks." icon={UserX} />
<div className="space-y-8">
<EmptyState
title={t("student.noUser")}
description={t("student.noUserDesc")}
icon={UserX}
/>
</div>
)
}
const q = getParam(sp, "q") || undefined
const subject = getParam(sp, "subject") || undefined
const grade = getParam(sp, "grade") || undefined
const q = getParam(sp, "q")
const subject = getParam(sp, "subject")
const grade = getParam(sp, "grade")
const textbooks = await getTextbooks(q, subject, grade)
// P1-1 数据范围过滤:学生端强制按学生所在年级过滤
// student.gradeId 是 grades 表 id需通过 getGradeNameById 解析为年级名称(如 "Grade 7"
// 才能与 textbooks.grade 字符串字段匹配
const studentGradeName = student.gradeId ? await getGradeNameById(student.gradeId) : null
const textbooks = await getTextbooksWithScope(q, subject, grade, {
grade: studentGradeName ?? undefined,
})
const hasFilters = Boolean(q || (subject && subject !== "all") || (grade && grade !== "all"))
return (
<div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
<div className="space-y-8">
<div>
<h2 className="text-2xl font-bold tracking-tight">Textbooks</h2>
<p className="text-muted-foreground">Browse your course textbooks.</p>
<h2 className="text-2xl font-bold tracking-tight">{t("student.list.title")}</h2>
<p className="text-muted-foreground">{t("student.list.subtitle")}</p>
</div>
<TextbookFilters />
@@ -49,19 +62,31 @@ export default async function StudentTextbooksPage({
{textbooks.length === 0 ? (
<EmptyState
icon={BookOpen}
title={hasFilters ? "No textbooks match your filters" : "No textbooks yet"}
description={hasFilters ? "Try clearing filters or adjusting keywords." : "No textbooks are available right now."}
action={hasFilters ? { label: "Clear filters", href: "/student/learning/textbooks" } : undefined}
title={hasFilters ? t("student.list.empty.withFilters") : t("student.list.empty.withoutFilters")}
description={
hasFilters
? t("student.list.empty.withFiltersDesc")
: t("student.list.empty.withoutFiltersDesc")
}
action={
hasFilters
? { label: t("list.clearFilters"), href: "/student/learning/textbooks" }
: undefined
}
className="bg-card"
/>
) : (
<div className="grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
{textbooks.map((textbook) => (
<TextbookCard key={textbook.id} textbook={textbook} hrefBase="/student/learning/textbooks" hideActions />
<TextbookCard
key={textbook.id}
textbook={textbook}
hrefBase="/student/learning/textbooks"
hideActions
/>
))}
</div>
)}
</div>
)
}