fix(dashboard): v3 审计修复 — 数据完整性、i18n、类型安全、死代码清理
P0 修复(严重):
- admin ContentRow 标签与值错配(stats.users→textbooks 等 6 处)
- admin/error.tsx 硬编码中文替换为 useTranslations
- UserGrowthChart 空数据时渲染 EmptyState(userGrowth/homeworkTrend 永远为空数组)
P1 修复(高):
- 新增 admin/dashboard 和 student/dashboard 的 loading.tsx + error.tsx
- 抽取 DashboardLoadingSkeleton 和 DashboardErrorFallback 共享组件,消除 5 套重复文件
- formatDate/formatLongDate 传入用户 locale(admin/teacher/student 共 6 个组件)
- 移除死代码:getCachedAdminDashboard、AvatarImage src={undefined}、TeacherStats isLoading prop
- filterTodaySchedule 改为泛型函数,消除 as 类型断言
- 辅助函数 getStatus/getDueUrgency 新增显式返回类型
- UserGrowthChart 新增 labelKey prop 区分用户增长/作业提交趋势标签
P2 修复(中):
- 4 个组件从客户端转为服务端组件(DashboardGreetingHeader、TeacherQuickActions、TeacherDashboardHeader、StudentDashboardHeader)
- Student dashboard 空状态新增 CTA(viewSchedule、viewAll)
- TeacherHomeworkCard 图标按钮新增 aria-label
- TeacherTodoCard 排序逻辑重写为可读的 if/return 模式
同步更新:
- docs/architecture/005_architecture_data.json 新增 DashboardLoadingSkeleton、DashboardErrorFallback 条目
- 新增 docs/architecture/audit/dashboard-audit-report-v3.md 审计报告
- dashboard.json 新增 6 个 i18n 键(textbooks/chapters/questions/exams/totalAssignments/totalSubmissions)
This commit is contained in:
@@ -36,7 +36,27 @@ import { deleteExamAction, duplicateExamAction, updateExamAction, getExamPreview
|
||||
import { Exam } from "../types"
|
||||
import { ExamPaperPreview } from "./assembly/exam-paper-preview"
|
||||
import type { ExamNode } from "./assembly/selected-question-list"
|
||||
import type { Question } from "@/modules/questions/types"
|
||||
|
||||
// Raw structure node shape returned from the DB before hydration
|
||||
type RawStructureNode = {
|
||||
id?: string
|
||||
type?: string
|
||||
questionId?: string
|
||||
score?: number
|
||||
title?: string
|
||||
children?: RawStructureNode[]
|
||||
}
|
||||
|
||||
// Type guard to narrow unknown structure payload to raw nodes
|
||||
const isRawStructureNode = (v: unknown): v is RawStructureNode => {
|
||||
if (typeof v !== "object" || v === null) return false
|
||||
// 从 unknown 收窄为 Record<string, unknown> 以进行字段检查
|
||||
const obj = v as Record<string, unknown>
|
||||
return typeof obj.type === "string"
|
||||
}
|
||||
|
||||
const isRawStructureArray = (v: unknown): v is RawStructureNode[] =>
|
||||
Array.isArray(v) && v.every((item) => isRawStructureNode(item))
|
||||
|
||||
interface ExamActionsProps {
|
||||
exam: Exam
|
||||
@@ -57,25 +77,39 @@ export function ExamActions({ exam }: ExamActionsProps) {
|
||||
try {
|
||||
const result = await getExamPreviewAction(exam.id)
|
||||
if (result.success && result.data) {
|
||||
const { structure, questions } = result.data
|
||||
const questionById = new Map<string, Question>()
|
||||
for (const q of questions) questionById.set(q.id, q as unknown as Question)
|
||||
const { structure } = result.data
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const hydrate = (nodes: any[]): ExamNode[] => {
|
||||
return nodes.map((node) => {
|
||||
if (node.type === "question") {
|
||||
const q = node.questionId ? questionById.get(node.questionId) : undefined
|
||||
return { ...node, question: q }
|
||||
}
|
||||
if (node.type === "group") {
|
||||
return { ...node, children: hydrate(node.children || []) }
|
||||
}
|
||||
return node
|
||||
})
|
||||
const hydrate = (nodes: RawStructureNode[]): ExamNode[] => {
|
||||
return nodes.map((node) => {
|
||||
if (node.type === "question") {
|
||||
return {
|
||||
id: node.id ?? node.questionId ?? "",
|
||||
type: "question" as const,
|
||||
questionId: node.questionId,
|
||||
score: node.score,
|
||||
// Question content is not available in preview payload; left undefined
|
||||
}
|
||||
}
|
||||
if (node.type === "group") {
|
||||
return {
|
||||
id: node.id ?? "",
|
||||
type: "group" as const,
|
||||
title: node.title,
|
||||
score: node.score,
|
||||
children: hydrate(node.children ?? []),
|
||||
}
|
||||
}
|
||||
// Unknown node type: treat as group with no children to avoid runtime crash
|
||||
return {
|
||||
id: node.id ?? "",
|
||||
type: "group" as const,
|
||||
title: node.title,
|
||||
children: [],
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const nodes = Array.isArray(structure) ? hydrate(structure) : []
|
||||
|
||||
const nodes = isRawStructureArray(structure) ? hydrate(structure) : []
|
||||
setPreviewNodes(nodes)
|
||||
} else {
|
||||
toast.error(t("exam.actions.previewFailed"))
|
||||
|
||||
Reference in New Issue
Block a user