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 { EmptyInline } from "@/components/ui/empty-inline"; import { ExamLink } from "@/components/exams/exam-link"; import { SearchableSelect } from "@/components/ui/searchable-select"; 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 { usePreferences, useT } from "@/stores/preferences-context"; import { formatDecimal } from "@/lib/format"; type MarkRow = MarksReport["courses"][number]["results"][number]; 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; identity?: { name: string; studentNumber?: string | null; classes: string[] }; }) { const t = useT(); const { locale } = usePreferences(); // "68,2 · Bant 3": the grade is the school's band label, and "68,2 / 3" // read as a score out of 3. const markWithGrade = (mark: number | null, grade?: string | null) => { const band = grade ? t("marks.bandLabel", { grade }) : null; if (mark == null) return band ?? "—"; const value = formatDecimal(mark, locale()); return band ? `${value} · ${band}` : value; }; 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", accessorFn: (row) => row.course.title, header: t("nav.courses"), size: 260, cell: (cell) => ( {cell.row.original.course.title} ), }, { id: "instance", header: t("instances.title"), size: 170, meta: { cellClass: "text-muted-foreground" }, cell: (cell) => ( {openLabel()} ), }, // The average and its band are two values, so two columns: "68,2 · Bant 3" // in one cell was the one row taller and wider than its neighbours. { id: "average", accessorFn: (row) => row.average ?? undefined, header: t("marks.courseAvg"), size: 160, cell: (cell) => (
{formatDecimal(cell.row.original.average!, locale())}
), }, { id: "band", accessorFn: (row) => row.average_grade || undefined, header: t("marks.band"), size: 110, }, ]); 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 courseOptions = createMemo(() => props.report.courses.map((block) => ({ value: block.instance, label: block.course.title, }))); const columns = createMemo[]>(() => [ { accessorKey: "title", header: t("marks.exam"), // The title is the one long value; four equal columns cut it to "2. Dönem B…". size: 240, meta: { stickyLeft: true }, cell: (cell) => ( {cell.row.original.title} ), }, // The kind is its own column at every width; the compact view used to hide // it and stack it under the title as a second line instead. { id: "kind", size: 110, accessorFn: (row) => examKindLabel(row.kind, t), header: t("exams.kind"), cell: (cell) => {examKindLabel(cell.row.original.kind, t)}, }, { id: "weight", size: 90, header: t("marks.weight"), meta: { align: "right", divider: "left", cellClass: "" }, cell: (cell) => examWeight(cell.row.original, settings()?.exam_kinds) ?? "—", }, { id: "mark", size: 100, accessorFn: (row) => row.mark ?? undefined, header: t("marks.mark"), meta: { align: "right", cellClass: "font-semibold" }, cell: (cell) => formatDecimal(cell.row.original.mark!, locale()), }, { id: "grade", size: 120, accessorFn: (row) => row.grade || undefined, header: t("marks.band"), meta: { align: "right" }, }, ]); return (
{(identity) => (

{identity().name}

{(className) => {className}} {t("roster.studentNumber")}: {identity().studentNumber}
)}

{t("marks.overall")}

{props.report.overall_average == null ? "—" : formatDecimal(props.report.overall_average, locale())}

{t("marks.bandLabel", { grade: props.report.overall_grade! })}
{t("nav.courses")}
{props.report.courses.length}
{t("marks.examCount")}
{resultCount()}
0} fallback={
}> setTab(value === "byCourse" ? "byCourse" : "general")}> {t("marks.tabGeneral")} {t("marks.tabByCourse")}
{(block) => (
{block.course.title} {openLabel()}

{t("marks.courseAvg")}

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

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