forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

question-generation.ts76 lines, 3.3 KiBHistoryRawDownload
1import { getClasses } from "@/api/classes";
2import type { ClassGroup, Course } from "@/api/client";
3import { getCourseById } from "@/api/courses";
4import { getMyInstances } from "@/api/instances";
5import { gradeLevelWireLabel } from "@/lib/grade-level";
6import { FAN_OUT_LIMIT, mapConcurrent } from "@/lib/map-concurrent";
7
8/** Pages one request may name; a wider range is almost always a typo. */
9export const MAX_PAGES = 200;
10
11/**
12 * "3-7, 10" → [3, 4, 5, 6, 7, 10]. Null for anything that is not a list of
13 * positive page numbers and ascending ranges, or that names more than
14 * MAX_PAGES pages — the form says so instead of sending a guess.
15 */
16export function parsePages(input: string): number[] | null {
17 const parts = input.split(",").map((part) => part.trim()).filter(Boolean);
18 if (parts.length === 0) return null;
19 const pages = new Set<number>();
20 for (const part of parts) {
21 const range = /^(\d+)\s*-\s*(\d+)$/.exec(part);
22 if (range) {
23 const from = Number(range[1]);
24 const to = Number(range[2]);
25 if (from < 1 || to < from || to - from + 1 > MAX_PAGES) return null;
26 for (let page = from; page <= to; page += 1) pages.add(page);
27 } else if (/^\d+$/.test(part) && Number(part) >= 1) {
28 pages.add(Number(part));
29 } else {
30 return null;
31 }
32 if (pages.size > MAX_PAGES) return null;
33 }
34 return [...pages].sort((a, b) => a - b);
35}
36
37/** One `(sinif, ders)` pair a teacher may generate over, with the catalog course behind it. */
38export type StudyScopeOption = {
39 key: string;
40 ders: string;
41 /** The şube grade; null for a school-wide club/etüt corpus. */
42 sinif: string | null;
43 course: Course;
44 label: string;
45};
46
47/**
48 * The backend derives a caller's RAG scope from their own sections' instances
49 * — `ders` is the catalog course, `sinif` the şube's grade — and refuses any
50 * other pair, so the picker offers exactly those. Several şubeler of the same
51 * grade and course collapse into one pair.
52 */
53export async function loadStudyScopes(): Promise<StudyScopeOption[]> {
54 const [instances, classes] = await Promise.all([
55 getMyInstances({ limit: 200 }),
56 getClasses({ limit: 200 }).catch(() => ({ items: [] as ClassGroup[] })),
57 ]);
58 const gradeOf = new Map(classes.items.map((klass) => [klass.id, gradeLevelWireLabel(klass.grade_level)]));
59 const courseIds = [...new Set(instances.items.map((instance) => instance.course))];
60 const courses = await mapConcurrent(courseIds, FAN_OUT_LIMIT, (id) => getCourseById(id).catch(() => null));
61 const courseById = new Map(courses.filter((course): course is Course => course !== null).map((course) => [course.id, course]));
62
63 const options = new Map<string, StudyScopeOption>();
64 for (const instance of instances.items) {
65 const course = courseById.get(instance.course);
66 if (!course) continue;
67 const sinif = course.kind === "course" ? gradeOf.get(instance.class) ?? null : null;
68 // The backend names a section's pair by its resolved title, not the catalog's.
69 const ders = instance.title || course.title;
70 const key = `${ders}\u0000${sinif ?? ""}`;
71 if (!options.has(key)) {
72 options.set(key, { key, ders, sinif, course, label: sinif ? `${ders} · ${sinif}` : ders });
73 }
74 }
75 return [...options.values()].sort((a, b) => a.label.localeCompare(b.label, "tr"));
76}