forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

blueprint-skip.ts50 lines, 2.2 KiBHistoryRawDownload
1import type { MessageKey } from "@/i18n/messages";
2
3// A blueprint's `skipped[]` rows carry the backend's own English prose. Raw
4// backend text is never shown to a user anywhere else in this app, so it is
5// not shown here either: each sentence maps to a localized key, and anything
6// unmapped falls back to a generic line rather than leaking English.
7//
8// Normalization mirrors client.ts's normalizeApiMessage, so a change in
9// punctuation or casing on the backend does not silently drop a mapping.
10function normalize(reason: string): string {
11 return reason
12 .trim()
13 .replace(/[’‘]/g, "'")
14 .replace(/\s+/g, " ")
15 .replace(/[.!?]+$/, "")
16 .toLowerCase();
17}
18
19const FIXED_REASONS: Record<string, MessageKey> = {
20 "the class was deleted while the blueprint was being applied": "classBlueprints.skipClassGone",
21 "this course no longer exists — it has been dropped from the blueprint": "classBlueprints.skipCoursePruned",
22 "this course no longer exists - it has been dropped from the blueprint": "classBlueprints.skipCoursePruned",
23 "the class is already at its course ceiling": "classBlueprints.skipCeiling",
24 "the class holds more students than a course attach is allowed to enroll at once":
25 "classBlueprints.skipOverloaded",
26};
27
28// The two remaining reasons name the course, so they carry a parameter.
29const NO_SEAT = /^(.+) has no free seat for the whole class$/;
30const COURSE_GONE = /^(.+) no longer exists [—-] detach it from this class first$/;
31
32export type SkipCopy = { key: MessageKey; vars?: Record<string, string> };
33
34/**
35 * `courseTitle` resolves the row's course id to a title; the backend's own
36 * rendering of the course is discarded so the two locales read the same.
37 */
38export function skipReasonCopy(reason: string, courseTitle: string): SkipCopy {
39 const normalized = normalize(reason);
40
41 const fixed = FIXED_REASONS[normalized];
42 if (fixed) return { key: fixed };
43
44 if (NO_SEAT.test(normalized)) return { key: "classBlueprints.skipNoSeat", vars: { course: courseTitle } };
45 if (COURSE_GONE.test(normalized)) {
46 return { key: "classBlueprints.skipCourseGone", vars: { course: courseTitle } };
47 }
48
49 return { key: "classBlueprints.skipOther" };
50}