hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB0069847Burak Boduroğlu2 months ago552 commits
| 1 | import { ApiError, formatApiError } from "@/api/client"; |
| 2 | import type { MessageKey } from "@/i18n/messages"; |
| 3 | |
| 4 | type Translate = (key: MessageKey, params?: Record<string, string>) => string; |
| 5 | |
| 6 | const MODULE_KEYS: Record<string, MessageKey> = { |
| 7 | appointments: "module.appointments", |
| 8 | attendance: "module.attendance", |
| 9 | bank_questions: "module.bank_questions", |
| 10 | boards: "module.boards", |
| 11 | chatbot: "module.chatbot", |
| 12 | classes: "module.classes", |
| 13 | course_notes: "module.course_notes", |
| 14 | courses: "module.courses", |
| 15 | events: "module.events", |
| 16 | exams: "module.exams", |
| 17 | homework: "module.homework", |
| 18 | marks: "module.marks", |
| 19 | meals: "module.meals", |
| 20 | messages: "module.messages", |
| 21 | notes: "module.notes", |
| 22 | payments: "module.payments", |
| 23 | pomodoro: "module.pomodoro", |
| 24 | questions: "module.questions", |
| 25 | sessions: "module.sessions", |
| 26 | subjects: "module.subjects", |
| 27 | work: "module.work", |
| 28 | }; |
| 29 | |
| 30 | const PACKAGE_KEYS: Record<string, MessageKey> = { |
| 31 | academics: "package.academics", |
| 32 | ai: "package.ai", |
| 33 | communication: "package.communication", |
| 34 | operations: "package.operations", |
| 35 | }; |
| 36 | |
| 37 | /** Localized module name; a module newer than this table falls back to its wire name. */ |
| 38 | export function moduleLabel(module: string, t: Translate): string { |
| 39 | const key = MODULE_KEYS[module]; |
| 40 | return key ? t(key) : module; |
| 41 | } |
| 42 | |
| 43 | export function packageLabel(pkg: string, t: Translate): string { |
| 44 | const key = PACKAGE_KEYS[pkg]; |
| 45 | return key ? t(key) : pkg; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * A module write refused with 409 names its broken dependencies in fixed |
| 50 | * English (module.rs `validate`, builder.rs `refuse_if_needed`); render them |
| 51 | * with localized module names instead of the generic error line. |
| 52 | */ |
| 53 | export function formatModuleError(err: unknown, t: Translate): string { |
| 54 | if (err instanceof ApiError && err.status === 409) { |
| 55 | const text = err.message.replace(/^conflict:\s*/i, ""); |
| 56 | const missing = [...text.matchAll(/([a-z_]+) requires ([a-z_]+), which is not enabled/g)]; |
| 57 | if (missing.length > 0) { |
| 58 | return missing |
| 59 | .map((match) => t("modules.conflictRequires", { module: moduleLabel(match[1], t), needed: moduleLabel(match[2], t) })) |
| 60 | .join(" "); |
| 61 | } |
| 62 | const requiredBy = text.match(/^([a-z_]+) is required by ([a-z_, ]+)$/); |
| 63 | if (requiredBy) { |
| 64 | const dependents = requiredBy[2].split(",").map((name) => moduleLabel(name.trim(), t)).join(", "); |
| 65 | return t("modules.conflictRequiredBy", { module: moduleLabel(requiredBy[1], t), dependents }); |
| 66 | } |
| 67 | } |
| 68 | return formatApiError(err); |
| 69 | } |