import { ICON_ALIASES } from "./icon-names"; /* * ตรวจ/แปลงชื่อไอคอนจากตารางชื่ออย่างเดียว — ไม่ import ไอคอน (ไม่มี "use client") * zod schema / server action ที่ใช้ร่วมกับฟอร์มฝั่ง client จึงไม่ดึงไอคอน 372 ตัว (~51 kB gz) เข้า bundle * ต้องการตัว component ของไอคอน → `getCatalogIcon` (icon-catalog-lookup.ts) */ const NAMES = new Set(Object.values(ICON_ALIASES)); /** "alert-triangle" → "AlertTriangle" (ชื่อ component ของ lucide) */ const toPascal = (name: string) => name .split(/[-_\s]+/) .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) .join(""); /** * ชื่อในชุดมาตรฐานจากชื่อที่เก็บไว้ — รับชื่อในชุด (`"house"`), ชื่อ component ของ lucide (`"House"`, `"CreditCard"`) * และชื่อเดิมก่อน lucide เปลี่ยน (`"Home"`, `"AlertTriangle"`, `"alert-triangle"`) · ไม่มีในชุด = undefined * แอปที่เคยเก็บชื่อแบบ PascalCase ไว้ใน DB จึงย้ายมาใช้ `NamedIcon`/`IconSelect` ได้โดยไม่ต้องแปลงข้อมูล */ export function resolveCatalogIconName(name: string | null | undefined): string | undefined { if (!name) return undefined; if (NAMES.has(name)) return name; const bare = name.replace(/^Lucide/, "").replace(/Icon$/, ""); return ICON_ALIASES[bare] ?? ICON_ALIASES[toPascal(bare)]; } /** * ชื่อนี้แสดงเป็นไอคอนในชุดได้ไหม (รวมชื่อเดิมแบบ `"Home"`) — ใช้ตรวจค่าก่อนบันทึก * * icon: z.string().refine(isCatalogIconName, "ไม่รู้จักไอคอนนี้") */ export function isCatalogIconName(name: unknown): name is string { return typeof name === "string" && resolveCatalogIconName(name) !== undefined; }