import { Avatar, AvatarFallback, AvatarImage } from "@suphark/ui/components/ui/avatar"; import { cn } from "@suphark/ui/lib/utils"; import type { ComponentProps } from "react"; import { type Presence, PresenceDot } from "./presence-dot"; // คำนำหน้าชื่อที่ไม่ใช่ชื่อจริง (ไม่นับเป็นตัวย่อ) const TITLES = new Set([ "นาย", "นาง", "นางสาว", "น.ส.", "ด.ช.", "ด.ญ.", "ดร.", "คุณ", "mr.", "mrs.", "ms.", "dr.", ]); /** ตัวแรกที่เป็นพยัญชนะ/ตัวอักษร — ข้ามสระหน้า เ แ โ ใ ไ ("ใจดี" → "จ"); ตัวอักษรแบบรวมใช้ grapheme ทั้งตัว */ function firstLetter(word: string): string { const chars = [...word.normalize("NFC")]; const skip = chars.findIndex((c) => !/[เแโใไ]/.test(c)); const rest = chars.slice(skip < 0 ? 0 : skip).join(""); if (typeof Intl !== "undefined" && "Segmenter" in Intl) { const seg = new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(rest); return seg[Symbol.iterator]().next().value?.segment ?? ""; } return [...rest][0] ?? ""; } /** ตัวอักษรย่อจากชื่อ (ไทย/อังกฤษ): "สมชาย ใจดี" → "สจ", "นาย เอกชัย ไทยดี" → "อท", "Suphark A." → "SA" */ export function initialsOf(name: string | null | undefined, fallback = "?"): string { const all = (name ?? "").trim().split(/\s+/).filter(Boolean); const parts = all.length > 1 ? all.filter((p) => !TITLES.has(p.toLowerCase())) : all; if (parts.length === 0) return fallback; const first = firstLetter(parts[0] ?? ""); const last = parts.length > 1 ? firstLetter(parts[parts.length - 1] ?? "") : ""; return (first + last).toUpperCase(); } /** Avatar ของ shadcn + ตัวย่อชื่อ + สีพื้นคงที่ต่อชื่อ (ไม่ต้องมีรูป) + จุดสถานะ `status` */ export function Initials({ name, src, status, className, children, ...props }: ComponentProps & { name: string | null | undefined; src?: string | null; /** จุดสถานะมุมล่างขวา: online / away / busy / offline */ status?: Presence; }) { const hue = [...(name ?? "")].reduce((h, c) => (h * 31 + c.codePointAt(0)!) % 360, 7); return ( {src && } {initialsOf(name)} {status && } {children} ); }