forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

person.ts34 lines, 1.4 KiBHistoryRawDownload
1import type { Appointment, PersonRef } from "@/api/client";
2
3export type PersonLike = PersonRef | string | null | undefined;
4
5export function personId(person: PersonLike): string {
6 if (!person) return "";
7 return typeof person === "string" ? person : person.id;
8}
9
10export 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
16export 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.
26export function appointmentCounterpart(a: Appointment, viewerId: string | undefined): string {
27 return personLabel(a.requester.id === viewerId ? a.teacher ?? a.requester : a.requester);
28}
29
30export function personLabelWithId(person: PersonLike): string {
31 if (!person) return "—";
32 if (typeof person === "string") return person;
33 return `${personLabel(person)} - ${person.id}`;
34}