forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

instance-options.ts77 lines, 3.0 KiBHistoryRawDownload
1import { getMyClasses } from "@/api/classes";
2import { getMyInstances } from "@/api/instances";
3import type { ClassGroup, Role } from "@/api/client";
4import { hasMinRole } from "@/lib/roles";
5import { classList, loadSchoolSections } from "@/lib/instance-labels";
6import { compareClasses } from "@/lib/student-directory";
7
8/** One pickable section: "<ders> — <şube>", plus the ids behind the label. */
9export type InstanceOption = {
10 id: string;
11 /** The catalog course it teaches — what subjects and question banks key on. */
12 course: string;
13 class: string;
14 label: string;
15};
16
17/**
18 * The sections a user may act in, labelled for a picker.
19 *
20 * There is no course-scoped instance route, so the office walks the class list
21 * (one read per class) while everyone else reads the single `/instances/me`
22 * that already answers "which sections are mine".
23 */
24export async function loadInstanceOptions(role: Role | undefined): Promise<InstanceOption[]> {
25 if (!role) return [];
26 const office = hasMinRole(role, "manager");
27
28 // Each section carries its resolved display title, so no per-course read.
29 const rows: { id: string; course: string; class: string; title: string }[] = [];
30 const classesById = new Map<string, ClassGroup>();
31
32 if (office) {
33 // Shared with the label cache: the school is walked once per tab. One
34 // unreadable şube comes back empty instead of emptying the picker.
35 for (const { klass, sections } of await loadSchoolSections()) {
36 classesById.set(klass.id, klass);
37 for (const instance of sections) {
38 rows.push({ id: instance.id, course: instance.course, class: instance.class, title: instance.title });
39 }
40 }
41 } else {
42 const [mine, classes] = await Promise.all([
43 getMyInstances({ limit: 200 }),
44 (hasMinRole(role, "teacher") ? classList().then((items) => ({ items })) : getMyClasses({ limit: 200 })).catch(
45 () => ({ items: [] as ClassGroup[] }),
46 ),
47 ]);
48 for (const klass of classes.items) classesById.set(klass.id, klass);
49 for (const instance of mine.items) {
50 rows.push({ id: instance.id, course: instance.course, class: instance.class, title: instance.title });
51 }
52 }
53
54
55 // Class by class (9-A, 9-B, 10-A…), then by ders inside a class, the order a
56 // school reads its timetable in; a şube this caller cannot name goes last.
57 const collator = new Intl.Collator("tr", { numeric: true, sensitivity: "base" });
58 const courseTitle = (row: { title: string }) => row.title?.trim() || "—";
59 return rows
60 .sort((a, b) => {
61 const classA = classesById.get(a.class);
62 const classB = classesById.get(b.class);
63 if (classA && !classB) return -1;
64 if (!classA && classB) return 1;
65 if (classA && classB) {
66 const byClass = compareClasses(classA, classB);
67 if (byClass !== 0) return byClass;
68 }
69 return collator.compare(courseTitle(a), courseTitle(b));
70 })
71 .map((row) => ({
72 id: row.id,
73 course: row.course,
74 class: row.class,
75 label: `${courseTitle(row)} — ${classesById.get(row.class)?.name ?? "—"}`,
76 }));
77}