import { For, Show, createMemo, createSignal } from "solid-js"; import { createResource } from "@/lib/create-resource"; import type { ColumnDef } from "@tanstack/solid-table"; import { Link } from "@tanstack/solid-router"; import { getClassesByUserId, getMyClasses } from "@/api/classes"; import { getSettings } from "@/api/settings"; import type { MarksReport } from "@/api/client"; import { Badge } from "@/components/ui/badge"; import { buttonVariants } from "@/components/ui/button"; import { IconExternalLink } from "@/components/ui/icons"; import { DataTable, DataTableEmpty } from "@/components/ui/data-table"; import { ExamLink } from "@/components/exams/exam-link"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { cn } from "@/lib/cn"; import { examKindLabel } from "@/lib/exam-labels"; import { examWeight } from "@/lib/exam-weight"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; const round = (n: number) => (Math.round(n * 100) / 100).toString(); type MarkRow = MarksReport["courses"][number]["results"][number]; function markWithGrade(mark: number | null, grade?: string | null) { if (mark == null) return grade ?? "—"; return grade ? `${round(mark)} / ${grade}` : round(mark); } function scoreWidth(mark: number | null): string { return `${Math.max(0, Math.min(100, Math.round(mark ?? 0)))}%`; } /** How many sections we bother reading before giving up on naming one. */ const MAX_SECTIONS = 3; export function MarksReportView(props: { report: MarksReport; compact?: boolean }) { const t = useT(); const auth = useAuth(); // The report names each block only by its instance id, so the section's own // name has to be read separately: `/classes/me` for the student reading their // own karne (the one class route a student may call), `/classes/user/{id}` // for a parent or teacher reading someone else's. A refusal is not an error // to show — the button simply keeps its generic label. const [sections] = createResource( () => props.report.user, async (userId) => { try { const page = userId === auth.user()?.id ? await getMyClasses({ limit: MAX_SECTIONS }) : await getClassesByUserId(userId, { limit: MAX_SECTIONS }); return page.items; } catch { return []; } }, { initialValue: [] }, ); // Naming the section is only honest when the student sits in exactly one: // with two, nothing in the payload says which one teaches this course. const sectionName = () => (sections().length === 1 ? sections()[0]!.name : null); const openLabel = () => sectionName() ?? t("instances.open"); const openAria = () => (sectionName() ? `${sectionName()} — ${t("instances.open")}` : t("instances.open")); type CourseBlock = MarksReport["courses"][number]; const courseColumns = createMemo[]>(() => [ { id: "course", header: t("nav.courses"), meta: { headerClass: "w-[42%]" }, cell: (cell) => ( {cell.row.original.course.title} ), }, { id: "instance", header: t("instances.title"), meta: { headerClass: "w-[28%]", cellClass: "text-muted-foreground" }, cell: (cell) => ( {openLabel()} ), }, { id: "average", header: t("marks.courseAvg"), meta: { headerClass: "w-[30%]" }, cell: (cell) => (
{markWithGrade(cell.row.original.average, cell.row.original.average_grade)}
), }, ]); const [settings] = createResource(() => getSettings()); const compact = () => props.compact === true; const resultCount = createMemo(() => props.report.courses.reduce((total, course) => total + course.results.length, 0)); const [tab, setTab] = createSignal<"general" | "byCourse">("general"); const [activeInstanceId, setActiveInstanceId] = createSignal(null); const activeCourse = createMemo(() => { const id = activeInstanceId(); const courses = props.report.courses; return (id ? courses.find((c) => c.instance === id) : undefined) ?? courses[0]; }); const columns = createMemo[]>(() => [ { accessorKey: "title", header: t("marks.exam"), meta: { stickyLeft: true }, cell: (cell) => ( {cell.row.original.title}

{examKindLabel(cell.row.original.kind, t)}

), }, { accessorKey: "kind", header: t("exams.kind"), meta: { headerClass: compact() ? "hidden sm:table-cell" : undefined, cellClass: compact() ? "hidden sm:table-cell" : undefined }, cell: (cell) => {examKindLabel(cell.row.original.kind, t)}, }, { id: "weight", header: t("marks.weight"), meta: { align: "right", divider: "left", cellClass: "" }, cell: (cell) => examWeight(cell.row.original, settings()?.exam_kinds) ?? "—", }, { accessorKey: "mark", header: t("marks.mark"), meta: { align: "right", cellClass: "font-semibold" }, cell: (cell) => markWithGrade(cell.row.original.mark, cell.row.original.grade), }, ]); return (

{t("marks.overall")}

{markWithGrade(props.report.overall_average, props.report.overall_grade)}

{t("nav.courses")}
{props.report.courses.length}
{t("exams.results")}
{resultCount()}
0} fallback={{t("marks.empty")}}> setTab(value === "byCourse" ? "byCourse" : "general")}> {t("marks.tabGeneral")} {t("marks.tabByCourse")}
{(block) => ( )}
{(block) => (
{block.course.title} {openLabel()}

{t("marks.courseAvg")}

{markWithGrade(block.average, block.average_grade)}

0} fallback={{t("exams.noResults")}}>
)}
); }