hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { getCourses } from "@/api/courses"; |
| 2 | import { getExams } from "@/api/exams"; |
| 3 | import { getEvents } from "@/api/events"; |
| 4 | import { getHomework } from "@/api/homework"; |
| 5 | import { getMyInstances } from "@/api/instances"; |
| 6 | import type { Role } from "@/api/client"; |
| 7 | import { LIST_CAP, loadCappedList } from "@/lib/capped-list"; |
| 8 | import { loadInstanceLabels } from "@/lib/instance-labels"; |
| 9 | import { matchesSearch } from "@/lib/search-text"; |
| 10 | |
| 11 | /** |
| 12 | * Records the command palette can search besides pages and actions: the |
| 13 | * courses, exams, homework and events the user's own list pages show. |
| 14 | * |
| 15 | * Each kind is read the way its list page reads it (same endpoint, same cap, |
| 16 | * same role scoping), once when the palette opens — never per keystroke — and |
| 17 | * the typed query is matched client-side with the app's Turkish folding. |
| 18 | */ |
| 19 | export type CommandRecordKind = "course" | "exam" | "homework" | "event"; |
| 20 | |
| 21 | export const COMMAND_RECORD_KINDS: readonly CommandRecordKind[] = ["course", "exam", "homework", "event"]; |
| 22 | |
| 23 | /** The list route each kind lives under; a kind is searched only when its route is in the user's nav. */ |
| 24 | export const COMMAND_RECORD_ROUTES: Record<CommandRecordKind, string> = { |
| 25 | course: "/courses", |
| 26 | exam: "/exams", |
| 27 | homework: "/homework", |
| 28 | event: "/events", |
| 29 | }; |
| 30 | |
| 31 | export type CommandRecord = { |
| 32 | kind: CommandRecordKind; |
| 33 | id: string; |
| 34 | title: string; |
| 35 | /** "<ders> — <şube>" for exams and homework; null when it could not be read. */ |
| 36 | context: string | null; |
| 37 | description: string | null; |
| 38 | /** Course kind (course / study / club) for catalog rows. */ |
| 39 | courseKind?: string; |
| 40 | /** Start time (exam, event) or due time (homework). */ |
| 41 | at: number | null; |
| 42 | }; |
| 43 | |
| 44 | /** Below this many characters a query would match nearly every record. */ |
| 45 | export const COMMAND_RECORD_MIN_QUERY = 2; |
| 46 | export const COMMAND_RECORDS_PER_KIND = 5; |
| 47 | |
| 48 | /** |
| 49 | * Records matching `query`, in kind order, at most `perKind` of each — the |
| 50 | * palette is a jump list, not a results page, and the list page holds the rest. |
| 51 | */ |
| 52 | export function filterCommandRecords( |
| 53 | records: readonly CommandRecord[], |
| 54 | query: string, |
| 55 | perKind = COMMAND_RECORDS_PER_KIND, |
| 56 | ): CommandRecord[] { |
| 57 | const q = query.trim(); |
| 58 | if (q.length < COMMAND_RECORD_MIN_QUERY) return []; |
| 59 | const counts = new Map<CommandRecordKind, number>(); |
| 60 | const out: CommandRecord[] = []; |
| 61 | for (const kind of COMMAND_RECORD_KINDS) { |
| 62 | for (const record of records) { |
| 63 | if (record.kind !== kind) continue; |
| 64 | if ((counts.get(kind) ?? 0) >= perKind) break; |
| 65 | if (!matchesSearch(q, record.title, record.context, record.description)) continue; |
| 66 | counts.set(kind, (counts.get(kind) ?? 0) + 1); |
| 67 | out.push(record); |
| 68 | } |
| 69 | } |
| 70 | return out; |
| 71 | } |
| 72 | |
| 73 | async function loadCourses(): Promise<CommandRecord[]> { |
| 74 | // GET /courses is already scoped: a student's list is their own enrolments. |
| 75 | const page = await getCourses(); |
| 76 | return page.items.map((course) => ({ |
| 77 | kind: "course", |
| 78 | id: course.id, |
| 79 | title: course.title, |
| 80 | context: null, |
| 81 | description: course.description || null, |
| 82 | courseKind: course.kind, |
| 83 | at: null, |
| 84 | })); |
| 85 | } |
| 86 | |
| 87 | async function loadExams(role: Role): Promise<CommandRecord[]> { |
| 88 | const [page, mine] = await Promise.all([ |
| 89 | loadCappedList(getExams, LIST_CAP, false), |
| 90 | // As on /exams: a student only sees exams set in their own sections. |
| 91 | role === "student" ? getMyInstances({ limit: 200 }).then((p) => new Set(p.items.map((i) => i.id))) : null, |
| 92 | ]); |
| 93 | const items = mine ? page.items.filter((exam) => mine.has(exam.class_course)) : page.items; |
| 94 | const labels = await loadInstanceLabels(items.map((exam) => exam.class_course), role).catch(() => new Map()); |
| 95 | return items.map((exam) => ({ |
| 96 | kind: "exam", |
| 97 | id: exam.id, |
| 98 | title: exam.title, |
| 99 | context: labels.get(exam.class_course)?.label ?? null, |
| 100 | description: exam.description || null, |
| 101 | at: exam.starts_at, |
| 102 | })); |
| 103 | } |
| 104 | |
| 105 | async function loadHomework(role: Role): Promise<CommandRecord[]> { |
| 106 | const items = (await getHomework({ limit: LIST_CAP })).items; |
| 107 | const labels = await loadInstanceLabels(items.map((item) => item.class_course), role).catch(() => new Map()); |
| 108 | return items.map((item) => ({ |
| 109 | kind: "homework", |
| 110 | id: item.id, |
| 111 | title: item.title, |
| 112 | context: labels.get(item.class_course)?.label ?? null, |
| 113 | description: item.description || null, |
| 114 | at: item.due_at, |
| 115 | })); |
| 116 | } |
| 117 | |
| 118 | async function loadEvents(): Promise<CommandRecord[]> { |
| 119 | const page = await loadCappedList(getEvents, LIST_CAP, false); |
| 120 | return page.items.map((event) => ({ |
| 121 | kind: "event", |
| 122 | id: event.id, |
| 123 | title: event.title, |
| 124 | context: null, |
| 125 | description: event.description || null, |
| 126 | at: event.starts_at, |
| 127 | })); |
| 128 | } |
| 129 | |
| 130 | const LOADERS: Record<CommandRecordKind, (role: Role) => Promise<CommandRecord[]>> = { |
| 131 | course: loadCourses, |
| 132 | exam: loadExams, |
| 133 | homework: loadHomework, |
| 134 | event: loadEvents, |
| 135 | }; |
| 136 | |
| 137 | /** Reopening the palette within this window reuses the last read. */ |
| 138 | const CACHE_MS = 60_000; |
| 139 | let cache: { key: string; at: number; records: Promise<CommandRecord[]> } | null = null; |
| 140 | |
| 141 | /** |
| 142 | * Every searchable record for `role`, limited to `kinds`. A kind that fails to |
| 143 | * load is left out rather than emptying the others. |
| 144 | */ |
| 145 | export function loadCommandRecords( |
| 146 | role: Role, |
| 147 | kinds: readonly CommandRecordKind[], |
| 148 | now: number = Date.now(), |
| 149 | ): Promise<CommandRecord[]> { |
| 150 | const key = `${role}|${[...kinds].sort().join(",")}`; |
| 151 | if (cache && cache.key === key && now - cache.at < CACHE_MS) return cache.records; |
| 152 | const records = Promise.allSettled(kinds.map((kind) => LOADERS[kind](role))).then((results) => |
| 153 | results.flatMap((result) => (result.status === "fulfilled" ? result.value : [])), |
| 154 | ); |
| 155 | cache = { key, at: now, records }; |
| 156 | return records; |
| 157 | } |
| 158 | |
| 159 | /** Test hook: forget the last read. */ |
| 160 | export function resetCommandRecordCache(): void { |
| 161 | cache = null; |
| 162 | } |