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