hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Appointment, PersonRef } from "@/api/client"; |
| 2 | |
| 3 | export type PersonLike = PersonRef | string | null | undefined; |
| 4 | |
| 5 | export function personId(person: PersonLike): string { |
| 6 | if (!person) return ""; |
| 7 | return typeof person === "string" ? person : person.id; |
| 8 | } |
| 9 | |
| 10 | export function personLabel(person: PersonLike): string { |
| 11 | if (!person) return "—"; |
| 12 | if (typeof person === "string") return person; |
| 13 | return person.display_name || person.username || person.id; |
| 14 | } |
| 15 | |
| 16 | export function personInitials(value: string | null | undefined, fallback = "?"): string { |
| 17 | const parts = value?.trim().split(/\s+/).filter(Boolean) ?? []; |
| 18 | if (parts.length === 0) return fallback; |
| 19 | if (parts.length === 1) return parts[0]!.slice(0, 2).toLocaleUpperCase("tr-TR"); |
| 20 | return `${parts[0]![0] ?? ""}${parts[1]![0] ?? ""}`.toLocaleUpperCase("tr-TR"); |
| 21 | } |
| 22 | |
| 23 | // Every appointment listing is scoped to the viewer (`list_for_requester` / |
| 24 | // `list_for_teacher`), so one of the two sides is always the viewer themselves. |
| 25 | // Show the OTHER person, or the row reads "Ali" to Ali. |
| 26 | export function appointmentCounterpart(a: Appointment, viewerId: string | undefined): string { |
| 27 | return personLabel(a.requester.id === viewerId ? a.teacher ?? a.requester : a.requester); |
| 28 | } |
| 29 | |
| 30 | // Two students can share a name, so a picker adds the student number, else |
| 31 | // the username — never the raw id, which means nothing to a person reading it. |
| 32 | export function personPickerLabel(person: PersonLike): string { |
| 33 | if (!person) return "—"; |
| 34 | if (typeof person === "string") return person; |
| 35 | const label = personLabel(person); |
| 36 | const handle = person.student_number?.trim() || person.username; |
| 37 | return handle && handle !== label ? `${label} (${handle})` : label; |
| 38 | } |