feat(textbooks): 知识图谱功能全面重构 — 前置依赖 + dagre 布局 + React Flow 可视化 + 师生双视角
将教材模块图谱从基本无用状态升级为完整知识图谱可视化系统。 数据层:新增 knowledgePointPrerequisites 表(复合主键+双外键 cascade);新增 data-access-graph.ts(server-only)知识点关联聚合、学生/班级掌握度查询;utils.ts 新增 hasCycleAfterAddingEdge(DFS 循环依赖检测)。 业务层:3 个新 Server Action(getKnowledgeGraphDataAction 三视图模式、createPrerequisiteAction 含循环检测、deletePrerequisiteAction);graph-layout.ts 重写为 dagre 分层有向图布局。 视图层:knowledge-graph.tsx 重写为 React Flow 主组件(全书视图+搜索高亮+关联节点高亮+章节着色);4 个新组件(graph-kp-node/graph-prerequisite-edge/graph-toolbar/graph-node-detail-panel);use-graph-data.ts 派生值模式避免 effect 中 setState。 架构:严格三层架构,客户端通过 Server Action 间接访问 server-only 数据层;权限校验+ i18n 全覆盖;架构文档 004/005 同步。 测试:utils.test.ts 新增 5 个循环检测测试,graph-layout.test.ts 重写 5 个 dagre 布局测试,全部 30 个教材模块单元测试通过。 附带提交 drizzle/0005 error-book 迁移文件以保持 journal 一致性。
This commit is contained in:
182
src/modules/textbooks/components/graph-node-detail-panel.tsx
Normal file
182
src/modules/textbooks/components/graph-node-detail-panel.tsx
Normal file
@@ -0,0 +1,182 @@
|
||||
"use client"
|
||||
|
||||
import { useTranslations } from "next-intl"
|
||||
import Link from "next/link"
|
||||
import { X, ExternalLink, Plus, Trash2 } from "lucide-react"
|
||||
import { Button } from "@/shared/components/ui/button"
|
||||
import { Badge } from "@/shared/components/ui/badge"
|
||||
import { ScrollArea } from "@/shared/components/ui/scroll-area"
|
||||
import { Separator } from "@/shared/components/ui/separator"
|
||||
import type { KpWithRelations, MasteryInfo } from "../types"
|
||||
|
||||
interface GraphNodeDetailPanelProps {
|
||||
kp: KpWithRelations
|
||||
mastery: MasteryInfo | null
|
||||
prerequisites: { id: string; name: string; description: string | null }[]
|
||||
successors: { id: string; name: string; description: string | null }[]
|
||||
canEdit: boolean
|
||||
textbookId: string
|
||||
onClose: () => void
|
||||
onJumpToKp: (kpId: string) => void
|
||||
onAddPrerequisite: () => void
|
||||
onRemovePrerequisite: (prereqId: string) => void
|
||||
}
|
||||
|
||||
export function GraphNodeDetailPanel({
|
||||
kp,
|
||||
mastery,
|
||||
prerequisites,
|
||||
successors,
|
||||
canEdit,
|
||||
onClose,
|
||||
onJumpToKp,
|
||||
onAddPrerequisite,
|
||||
onRemovePrerequisite,
|
||||
}: GraphNodeDetailPanelProps) {
|
||||
const t = useTranslations("textbooks")
|
||||
|
||||
const correctRate = mastery && mastery.totalQuestions > 0
|
||||
? Math.round((mastery.correctQuestions / mastery.totalQuestions) * 100)
|
||||
: null
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full border-l bg-background">
|
||||
<div className="flex items-center justify-between p-3 border-b shrink-0">
|
||||
<h3 className="text-sm font-semibold truncate">{t("graph.detail.title")}</h3>
|
||||
<Button variant="ghost" size="sm" className="h-7 w-7 p-0" onClick={onClose}>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<ScrollArea className="flex-1 min-h-0">
|
||||
<div className="p-3 space-y-4">
|
||||
{/* 知识点名称 */}
|
||||
<div>
|
||||
<h4 className="text-base font-medium">{kp.name}</h4>
|
||||
{kp.chapterTitle && (
|
||||
<p className="text-xs text-muted-foreground mt-1">{kp.chapterTitle}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 描述 */}
|
||||
<div>
|
||||
<h5 className="text-xs font-medium text-muted-foreground mb-1">
|
||||
{t("graph.detail.title")}
|
||||
</h5>
|
||||
<p className="text-sm">
|
||||
{kp.description || t("graph.detail.noDescription")}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 掌握度 */}
|
||||
{mastery && (
|
||||
<>
|
||||
<Separator />
|
||||
<div>
|
||||
<h5 className="text-xs font-medium text-muted-foreground mb-2">
|
||||
{t("graph.node.mastery")}
|
||||
</h5>
|
||||
<div className="space-y-1 text-sm">
|
||||
<div className="flex justify-between">
|
||||
<span>{t("graph.detail.correctRate")}</span>
|
||||
<span>{correctRate !== null ? `${correctRate}%` : t("graph.detail.masteryNotAssessed")}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span>{t("graph.detail.totalQuestions")}</span>
|
||||
<span>{mastery.totalQuestions}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 关联题目 */}
|
||||
<Separator />
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h5 className="text-xs font-medium text-muted-foreground">
|
||||
{t("graph.node.questions")} ({kp.questionCount})
|
||||
</h5>
|
||||
{kp.questionCount > 0 && (
|
||||
<Button asChild variant="ghost" size="sm" className="h-7 text-xs">
|
||||
<Link href={`/teacher/questions?kp=${kp.id}`}>
|
||||
<ExternalLink className="h-3 w-3 mr-1" />
|
||||
{t("graph.detail.viewAllQuestions")}
|
||||
</Link>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 前置知识点 */}
|
||||
<Separator />
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<h5 className="text-xs font-medium text-muted-foreground">
|
||||
{t("graph.node.prerequisite")} ({prerequisites.length})
|
||||
</h5>
|
||||
{canEdit && (
|
||||
<Button variant="ghost" size="sm" className="h-7 text-xs" onClick={onAddPrerequisite}>
|
||||
<Plus className="h-3 w-3 mr-1" />
|
||||
{t("graph.detail.addPrerequisite")}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
{prerequisites.length === 0 ? (
|
||||
<p className="text-xs text-muted-foreground">{t("graph.detail.noPrerequisites")}</p>
|
||||
) : (
|
||||
<div className="space-y-1">
|
||||
{prerequisites.map((p) => (
|
||||
<div key={p.id} className="flex items-center justify-between gap-2">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 text-xs justify-start flex-1"
|
||||
onClick={() => onJumpToKp(p.id)}
|
||||
>
|
||||
{p.name}
|
||||
</Button>
|
||||
{canEdit && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-7 w-7 p-0 text-muted-foreground hover:text-destructive"
|
||||
onClick={() => onRemovePrerequisite(p.id)}
|
||||
>
|
||||
<Trash2 className="h-3 w-3" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 后置知识点 */}
|
||||
<Separator />
|
||||
<div>
|
||||
<h5 className="text-xs font-medium text-muted-foreground mb-2">
|
||||
{t("graph.node.successor")} ({successors.length})
|
||||
</h5>
|
||||
{successors.length === 0 ? (
|
||||
<p className="text-xs text-muted-foreground">{t("graph.detail.noSuccessors")}</p>
|
||||
) : (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{successors.map((s) => (
|
||||
<Badge
|
||||
key={s.id}
|
||||
variant="outline"
|
||||
className="cursor-pointer hover:bg-accent text-xs"
|
||||
onClick={() => onJumpToKp(s.id)}
|
||||
>
|
||||
{s.name}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user