forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

exam-labels.ts49 lines, 1.9 KiBHistoryRawDownload
1import type { KnownExamKind } from "@/api/client";
2import type { MessageKey } from "@/i18n/messages";
3
4type T = (key: MessageKey, vars?: Record<string, string | number>) => string;
5
6const EXAM_KIND_LABELS: Record<KnownExamKind, MessageKey> = {
7 homework: "exams.kind.homework",
8 quiz: "exams.kind.quiz",
9 midterm: "exams.kind.midterm",
10 final: "exams.kind.final",
11 project: "exams.kind.project",
12 oral: "exams.kind.oral",
13};
14
15// Kinds a school's settings commonly seed (Turkish ASCII keys). They are
16// ordinary school-defined kinds — renamable in settings — but a key like
17// "yazili" should still read as "Yazılı" wherever it is shown.
18const SEEDED_EXAM_KIND_LABELS: Record<string, MessageKey> = {
19 yazili: "exams.kind.yazili",
20 sozlu: "exams.kind.oral",
21 uygulama: "exams.kind.uygulama",
22 performans: "exams.kind.performans",
23 proje: "exams.kind.project",
24};
25
26export function isKnownExamKind(kind: string): kind is KnownExamKind {
27 return Object.hasOwn(EXAM_KIND_LABELS, kind);
28}
29
30/** Display label for an exam kind key; unknown (custom) kinds show as typed. */
31export function examKindLabel(kind: string, t: T): string {
32 if (isKnownExamKind(kind)) return t(EXAM_KIND_LABELS[kind]);
33 const seeded = Object.hasOwn(SEEDED_EXAM_KIND_LABELS, kind) ? SEEDED_EXAM_KIND_LABELS[kind] : undefined;
34 return seeded ? t(seeded) : kind;
35}
36
37/** Attempts cell for the live monitor. `maxAttempts` comes from the monitored
38 * exam itself (the roster rows don't carry it); `0` means unlimited. */
39export function attemptLabel(
40 entry: { attempt?: number | null; attempts_used?: number | null },
41 maxAttempts: number | null | undefined,
42): string {
43 if (entry.attempts_used != null && maxAttempts != null && maxAttempts > 0) {
44 return `${entry.attempts_used} / ${maxAttempts}`;
45 }
46 if (entry.attempt != null) return String(entry.attempt);
47 if (entry.attempts_used != null) return String(entry.attempts_used);
48 return "—";
49}