Files
NextEdu/src/app/(dashboard)/teacher/grades/stats/page.tsx
SpecialX 21142f9b99 feat(app): add error/loading boundaries across all dashboard routes and new routes
- Add error.tsx and loading.tsx boundaries for admin, parent, student, teacher routes

- Add admin announcements edit, audit-logs overview, curriculum-map, invitation-codes, permissions, questions, roles routes

- Add admin elective detail and components, files, course-plans, users, scheduling boundaries

- Add messages group-compose route

- Add parent course-plans, elective, grades report-card, practice routes

- Add student course-plans, elective detail, error-book dialogs, grades report-card, learning study-path, leave, schedule boundaries

- Add teacher attendance report, classes boundaries, course-plans boundaries, elective, exams analytics/edit-rich/all/create/new, grades report-card, homework boundaries, leave, lesson-plans calendar

- Add auth loading, onboarding loading, api cron
2026-07-03 10:26:25 +08:00

125 lines
4.1 KiB
TypeScript

import type { JSX } from "react"
import { getTeacherClasses } from "@/modules/classes/data-access"
import { getClassGradeStatsWithMeta, getClassRanking } from "@/modules/grades/data-access"
import { getSubjectOptions } from "@/modules/school/data-access"
import { ClassGradeReport } from "@/modules/grades/components/class-grade-report"
import { ExportButton } from "@/modules/grades/components/export-button"
import { StatsClassSelector } from "@/modules/grades/components/stats-class-selector"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { getParam, type SearchParams } from "@/shared/lib/search-params"
import { BarChart3 } from "lucide-react"
import { getTranslations } from "next-intl/server"
export const dynamic = "force-dynamic"
export default async function StatsPage({
searchParams,
}: {
searchParams: Promise<SearchParams>
}): Promise<JSX.Element> {
const ctx = await requirePermission(Permissions.GRADE_RECORD_READ)
const sp = await searchParams
const t = await getTranslations("grades")
const classId = getParam(sp, "classId")
const subjectId = getParam(sp, "subjectId")
const [classes, allSubjects] = await Promise.all([
getTeacherClasses(),
getSubjectOptions(),
])
if (classes.length === 0) {
return (
<div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
<div>
<h1 className="text-2xl font-bold tracking-tight">{t("title.stats")}</h1>
<p className="text-muted-foreground">{t("page.stats.description")}</p>
</div>
<EmptyState
title={t("page.stats.noClassesTitle")}
description={t("page.stats.noClassesDescription")}
icon={BarChart3}
className="border-none shadow-none"
/>
</div>
)
}
// P3 修复:对 class_taught scope 过滤可选班级
const allowedClassIds =
ctx.dataScope.type === "class_taught" ? ctx.dataScope.classIds : null
const scopedClasses = allowedClassIds
? classes.filter((c) => allowedClassIds.includes(c.id))
: classes
if (scopedClasses.length === 0) {
return (
<div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
<div>
<h1 className="text-2xl font-bold tracking-tight">{t("title.stats")}</h1>
<p className="text-muted-foreground">{t("page.stats.description")}</p>
</div>
<EmptyState
title={t("page.stats.noAccessTitle")}
description={t("page.stats.noAccessDescription")}
icon={BarChart3}
className="border-none shadow-none"
/>
</div>
)
}
const targetClassId = classId ?? scopedClasses[0].id
const targetSubjectId = subjectId && subjectId !== "all" ? subjectId : undefined
// P3 修复:传递 scope 到 data-access 层
const [stats, ranking] = await Promise.all([
getClassGradeStatsWithMeta(
targetClassId,
targetSubjectId,
undefined,
ctx.dataScope,
ctx.userId
),
getClassRanking(
targetClassId,
targetSubjectId,
undefined,
ctx.dataScope,
ctx.userId
),
])
const classOptions = scopedClasses.map((c) => ({ id: c.id, name: c.name }))
const subjectOptions = allSubjects.map((s) => ({ id: s.id, name: s.name }))
return (
<div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold tracking-tight">{t("title.stats")}</h1>
<p className="text-muted-foreground">{t("page.stats.description")}</p>
</div>
<ExportButton
classId={targetClassId}
subjectId={targetSubjectId}
variant="outline"
label={t("page.stats.exportLabel")}
/>
</div>
<StatsClassSelector
classes={classOptions}
subjects={subjectOptions}
currentClassId={targetClassId}
currentSubjectId={subjectId ?? "all"}
/>
<ClassGradeReport stats={stats} ranking={ranking} />
</div>
)
}