hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 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 | export function personLabelWithId(person: PersonLike): string { |
| 31 | if (!person) return "—"; |
| 32 | if (typeof person === "string") return person; |
| 33 | return `${personLabel(person)} - ${person.id}`; |
| 34 | } |