hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { For, Show, createMemo, createSignal } from "solid-js"; |
| 2 | import { createResource } from "@/lib/create-resource"; |
| 3 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 4 | import { Link } from "@tanstack/solid-router"; |
| 5 | import { getClassesByUserId, getMyClasses } from "@/api/classes"; |
| 6 | import { getSettings } from "@/api/settings"; |
| 7 | import type { MarksReport } from "@/api/client"; |
| 8 | import { Badge } from "@/components/ui/badge"; |
| 9 | import { buttonVariants } from "@/components/ui/button"; |
| 10 | import { IconExternalLink } from "@/components/ui/icons"; |
| 11 | import { DataTable, DataTableEmpty } from "@/components/ui/data-table"; |
| 12 | import { EmptyInline } from "@/components/ui/empty-inline"; |
| 13 | import { ExamLink } from "@/components/exams/exam-link"; |
| 14 | import { SearchableSelect } from "@/components/ui/searchable-select"; |
| 15 | import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; |
| 16 | import { cn } from "@/lib/cn"; |
| 17 | import { examKindLabel } from "@/lib/exam-labels"; |
| 18 | import { examWeight } from "@/lib/exam-weight"; |
| 19 | import { useAuth } from "@/stores/auth-context"; |
| 20 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 21 | import { formatDecimal } from "@/lib/format"; |
| 22 | |
| 23 | type MarkRow = MarksReport["courses"][number]["results"][number]; |
| 24 | |
| 25 | function scoreWidth(mark: number | null): string { |
| 26 | return `${Math.max(0, Math.min(100, Math.round(mark ?? 0)))}%`; |
| 27 | } |
| 28 | |
| 29 | /** How many sections we bother reading before giving up on naming one. */ |
| 30 | const MAX_SECTIONS = 3; |
| 31 | |
| 32 | export function MarksReportView(props: { |
| 33 | report: MarksReport; |
| 34 | compact?: boolean; |
| 35 | identity?: { name: string; studentNumber?: string | null; classes: string[] }; |
| 36 | }) { |
| 37 | const t = useT(); |
| 38 | const { locale } = usePreferences(); |
| 39 | // "68,2 · Bant 3": the grade is the school's band label, and "68,2 / 3" |
| 40 | // read as a score out of 3. |
| 41 | const markWithGrade = (mark: number | null, grade?: string | null) => { |
| 42 | const band = grade ? t("marks.bandLabel", { grade }) : null; |
| 43 | if (mark == null) return band ?? "—"; |
| 44 | const value = formatDecimal(mark, locale()); |
| 45 | return band ? `${value} · ${band}` : value; |
| 46 | }; |
| 47 | const auth = useAuth(); |
| 48 | // The report names each block only by its instance id, so the section's own |
| 49 | // name has to be read separately: `/classes/me` for the student reading their |
| 50 | // own karne (the one class route a student may call), `/classes/user/{id}` |
| 51 | // for a parent or teacher reading someone else's. A refusal is not an error |
| 52 | // to show — the button simply keeps its generic label. |
| 53 | const [sections] = createResource( |
| 54 | () => props.report.user, |
| 55 | async (userId) => { |
| 56 | try { |
| 57 | const page = userId === auth.user()?.id |
| 58 | ? await getMyClasses({ limit: MAX_SECTIONS }) |
| 59 | : await getClassesByUserId(userId, { limit: MAX_SECTIONS }); |
| 60 | return page.items; |
| 61 | } catch { |
| 62 | return []; |
| 63 | } |
| 64 | }, |
| 65 | { initialValue: [] }, |
| 66 | ); |
| 67 | // Naming the section is only honest when the student sits in exactly one: |
| 68 | // with two, nothing in the payload says which one teaches this course. |
| 69 | const sectionName = () => (sections().length === 1 ? sections()[0]!.name : null); |
| 70 | const openLabel = () => sectionName() ?? t("instances.open"); |
| 71 | const openAria = () => (sectionName() ? `${sectionName()} — ${t("instances.open")}` : t("instances.open")); |
| 72 | type CourseBlock = MarksReport["courses"][number]; |
| 73 | const courseColumns = createMemo<ColumnDef<CourseBlock>[]>(() => [ |
| 74 | { |
| 75 | id: "course", |
| 76 | accessorFn: (row) => row.course.title, |
| 77 | header: t("nav.courses"), |
| 78 | size: 260, |
| 79 | cell: (cell) => ( |
| 80 | <Link to="/courses/$id" params={{ id: cell.row.original.course.id }} title={cell.row.original.course.title} class="block truncate font-medium hover:text-primary-text hover:underline"> |
| 81 | {cell.row.original.course.title} |
| 82 | </Link> |
| 83 | ), |
| 84 | }, |
| 85 | { |
| 86 | id: "instance", |
| 87 | header: t("instances.title"), |
| 88 | size: 170, |
| 89 | meta: { cellClass: "text-muted-foreground" }, |
| 90 | cell: (cell) => ( |
| 91 | <Link |
| 92 | to="/instances/$id" |
| 93 | params={{ id: cell.row.original.instance }} |
| 94 | class={cn(buttonVariants({ variant: "outline", size: "sm" }), "h-8 max-w-full rounded-lg")} |
| 95 | aria-label={openAria()} |
| 96 | > |
| 97 | <IconExternalLink class="h-3.5 w-3.5 shrink-0" /> |
| 98 | <span class="truncate">{openLabel()}</span> |
| 99 | </Link> |
| 100 | ), |
| 101 | }, |
| 102 | // The average and its band are two values, so two columns: "68,2 · Bant 3" |
| 103 | // in one cell was the one row taller and wider than its neighbours. |
| 104 | { |
| 105 | id: "average", |
| 106 | accessorFn: (row) => row.average ?? undefined, |
| 107 | header: t("marks.courseAvg"), |
| 108 | size: 160, |
| 109 | cell: (cell) => ( |
| 110 | <div class="flex min-w-0 items-center gap-2"> |
| 111 | <div class="h-1.5 w-12 min-w-8 shrink overflow-hidden rounded-full bg-muted"> |
| 112 | <div class="h-full rounded-full bg-primary" style={{ width: scoreWidth(cell.row.original.average) }} /> |
| 113 | </div> |
| 114 | <span class="shrink-0 font-semibold tabular-nums">{formatDecimal(cell.row.original.average!, locale())}</span> |
| 115 | </div> |
| 116 | ), |
| 117 | }, |
| 118 | { |
| 119 | id: "band", |
| 120 | accessorFn: (row) => row.average_grade || undefined, |
| 121 | header: t("marks.band"), |
| 122 | size: 110, |
| 123 | }, |
| 124 | ]); |
| 125 | const [settings] = createResource(() => getSettings()); |
| 126 | const compact = () => props.compact === true; |
| 127 | const resultCount = createMemo(() => props.report.courses.reduce((total, course) => total + course.results.length, 0)); |
| 128 | const [tab, setTab] = createSignal<"general" | "byCourse">("general"); |
| 129 | const [activeInstanceId, setActiveInstanceId] = createSignal<string | null>(null); |
| 130 | const activeCourse = createMemo(() => { |
| 131 | const id = activeInstanceId(); |
| 132 | const courses = props.report.courses; |
| 133 | return (id ? courses.find((c) => c.instance === id) : undefined) ?? courses[0]; |
| 134 | }); |
| 135 | const courseOptions = createMemo(() => props.report.courses.map((block) => ({ |
| 136 | value: block.instance, |
| 137 | label: block.course.title, |
| 138 | }))); |
| 139 | const columns = createMemo<ColumnDef<MarkRow>[]>(() => [ |
| 140 | { |
| 141 | accessorKey: "title", |
| 142 | header: t("marks.exam"), |
| 143 | // The title is the one long value; four equal columns cut it to "2. Dönem B…". |
| 144 | size: 240, |
| 145 | meta: { stickyLeft: true }, |
| 146 | cell: (cell) => ( |
| 147 | <ExamLink examId={cell.row.original.exam} title={cell.row.original.title} class="block truncate font-medium hover:underline"> |
| 148 | {cell.row.original.title} |
| 149 | </ExamLink> |
| 150 | ), |
| 151 | }, |
| 152 | // The kind is its own column at every width; the compact view used to hide |
| 153 | // it and stack it under the title as a second line instead. |
| 154 | { |
| 155 | id: "kind", |
| 156 | size: 110, |
| 157 | accessorFn: (row) => examKindLabel(row.kind, t), |
| 158 | header: t("exams.kind"), |
| 159 | cell: (cell) => <Badge variant="outline" class="rounded-sm capitalize">{examKindLabel(cell.row.original.kind, t)}</Badge>, |
| 160 | }, |
| 161 | { |
| 162 | id: "weight", |
| 163 | size: 90, |
| 164 | header: t("marks.weight"), |
| 165 | meta: { align: "right", divider: "left", cellClass: "" }, |
| 166 | cell: (cell) => examWeight(cell.row.original, settings()?.exam_kinds) ?? "—", |
| 167 | }, |
| 168 | { |
| 169 | id: "mark", |
| 170 | size: 100, |
| 171 | accessorFn: (row) => row.mark ?? undefined, |
| 172 | header: t("marks.mark"), |
| 173 | meta: { align: "right", cellClass: "font-semibold" }, |
| 174 | cell: (cell) => formatDecimal(cell.row.original.mark!, locale()), |
| 175 | }, |
| 176 | { |
| 177 | id: "grade", |
| 178 | size: 120, |
| 179 | accessorFn: (row) => row.grade || undefined, |
| 180 | header: t("marks.band"), |
| 181 | meta: { align: "right" }, |
| 182 | }, |
| 183 | ]); |
| 184 | |
| 185 | return ( |
| 186 | <div class="@container min-w-0 space-y-4"> |
| 187 | <section class="overflow-hidden rounded-xl border border-border bg-card"> |
| 188 | <Show when={props.identity}> |
| 189 | {(identity) => ( |
| 190 | <div class="border-b border-border bg-muted/25 px-4 py-4"> |
| 191 | <p class="truncate text-base font-semibold text-foreground">{identity().name}</p> |
| 192 | <div class="mt-2 flex flex-wrap items-center gap-1.5"> |
| 193 | <For each={identity().classes}> |
| 194 | {(className) => <Badge variant="secondary">{className}</Badge>} |
| 195 | </For> |
| 196 | <Show when={identity().studentNumber}> |
| 197 | <span class="text-xs tabular-nums text-muted-foreground"> |
| 198 | {t("roster.studentNumber")}: {identity().studentNumber} |
| 199 | </span> |
| 200 | </Show> |
| 201 | </div> |
| 202 | </div> |
| 203 | )} |
| 204 | </Show> |
| 205 | <div class="grid gap-4 p-4 @lg:grid-cols-[minmax(0,1fr)_auto] @lg:items-center"> |
| 206 | <div class="min-w-0"> |
| 207 | <p class="text-xs font-medium text-muted-foreground">{t("marks.overall")}</p> |
| 208 | <div class="mt-1 flex flex-wrap items-center gap-2"> |
| 209 | <p class={cn("font-semibold tabular-nums leading-none", compact() ? "text-3xl" : "text-4xl")}> |
| 210 | {props.report.overall_average == null ? "—" : formatDecimal(props.report.overall_average, locale())} |
| 211 | </p> |
| 212 | <Show when={props.report.overall_grade}> |
| 213 | <Badge variant="outline">{t("marks.bandLabel", { grade: props.report.overall_grade! })}</Badge> |
| 214 | </Show> |
| 215 | </div> |
| 216 | <Show when={props.report.overall_average != null}> |
| 217 | <div class="mt-3 h-2 max-w-xl overflow-hidden rounded-full bg-muted"> |
| 218 | <div class="h-full rounded-full bg-primary" style={{ width: scoreWidth(props.report.overall_average) }} /> |
| 219 | </div> |
| 220 | </Show> |
| 221 | </div> |
| 222 | <dl class="grid grid-cols-2 gap-2 @lg:min-w-56"> |
| 223 | <div class="rounded-lg bg-muted/45 p-3"> |
| 224 | <dt class="text-xs text-muted-foreground">{t("nav.courses")}</dt> |
| 225 | <dd class="mt-1 text-2xl font-semibold tabular-nums">{props.report.courses.length}</dd> |
| 226 | </div> |
| 227 | <div class="rounded-lg bg-muted/45 p-3"> |
| 228 | <dt class="text-xs text-muted-foreground">{t("marks.examCount")}</dt> |
| 229 | <dd class="mt-1 text-2xl font-semibold tabular-nums">{resultCount()}</dd> |
| 230 | </div> |
| 231 | </dl> |
| 232 | </div> |
| 233 | </section> |
| 234 | |
| 235 | <Show when={props.report.courses.length > 0} fallback={<div class="rounded-xl border border-border bg-card p-4"><EmptyInline illustration="exams" title={t("marks.empty")} /></div>}> |
| 236 | <Tabs value={tab()} onChange={(value) => setTab(value === "byCourse" ? "byCourse" : "general")}> |
| 237 | <TabsList class="w-full @lg:w-fit"> |
| 238 | <TabsTrigger value="general" class="min-w-0">{t("marks.tabGeneral")}</TabsTrigger> |
| 239 | <TabsTrigger value="byCourse" class="min-w-0">{t("marks.tabByCourse")}</TabsTrigger> |
| 240 | </TabsList> |
| 241 | </Tabs> |
| 242 | |
| 243 | <Show when={tab() === "general"}> |
| 244 | <DataTable |
| 245 | columns={courseColumns()} |
| 246 | data={props.report.courses} |
| 247 | enablePagination={false} |
| 248 | enableColumnVisibility={false} |
| 249 | enableSorting={false} |
| 250 | tableClass="min-w-[28rem]" |
| 251 | /> |
| 252 | </Show> |
| 253 | |
| 254 | <Show when={tab() === "byCourse"}> |
| 255 | <div class="space-y-3"> |
| 256 | <div class="max-w-md space-y-1.5"> |
| 257 | <label for="marks-course-select" class="text-xs font-medium text-muted-foreground">{t("marks.selectCourse")}</label> |
| 258 | <SearchableSelect |
| 259 | id="marks-course-select" |
| 260 | value={activeCourse()?.instance ?? ""} |
| 261 | onChange={setActiveInstanceId} |
| 262 | options={courseOptions()} |
| 263 | placeholder={t("marks.selectCourse")} |
| 264 | /> |
| 265 | </div> |
| 266 | |
| 267 | <Show when={activeCourse()} keyed> |
| 268 | {(block) => ( |
| 269 | <div class="rounded-xl border border-border bg-card p-4 shadow-xs"> |
| 270 | <div class="grid gap-3 @lg:grid-cols-[minmax(0,1fr)_auto] @lg:items-start"> |
| 271 | <div class="min-w-0"> |
| 272 | <Link to="/courses/$id" params={{ id: block.course.id }} class="block truncate text-sm font-semibold text-primary-text hover:underline"> |
| 273 | {block.course.title} |
| 274 | </Link> |
| 275 | <Link |
| 276 | to="/instances/$id" |
| 277 | params={{ id: block.instance }} |
| 278 | class="mt-1.5 inline-flex max-w-full items-center gap-1.5 text-xs text-muted-foreground hover:text-primary-text hover:underline" |
| 279 | aria-label={openAria()} |
| 280 | > |
| 281 | <IconExternalLink class="h-3.5 w-3.5 shrink-0" /> |
| 282 | <span class="truncate">{openLabel()}</span> |
| 283 | </Link> |
| 284 | </div> |
| 285 | <div class="@lg:text-right"> |
| 286 | <p class="text-xs font-medium text-muted-foreground">{t("marks.courseAvg")}</p> |
| 287 | <p class="mt-0.5 text-xl font-semibold tabular-nums">{markWithGrade(block.average, block.average_grade)}</p> |
| 288 | </div> |
| 289 | </div> |
| 290 | <div class="mt-3"> |
| 291 | <Show when={block.results.length > 0} fallback={<DataTableEmpty class="py-6">{t("exams.noResults")}</DataTableEmpty>}> |
| 292 | <DataTable |
| 293 | class="min-w-0" |
| 294 | columns={columns()} |
| 295 | data={block.results} |
| 296 | tableClass={cn("w-full min-w-xl", compact() ? "text-xs" : "table-fixed")} |
| 297 | enableColumnVisibility={false} |
| 298 | /> |
| 299 | </Show> |
| 300 | </div> |
| 301 | </div> |
| 302 | )} |
| 303 | </Show> |
| 304 | </div> |
| 305 | </Show> |
| 306 | </Show> |
| 307 | </div> |
| 308 | ); |
| 309 | } |