| 1 |
/** |
| 2 |
* Yatra icon library (includes/icons.json, expanded via scripts/sync-lucide-icons-json.mjs) |
| 3 |
* + helpers for admin UI. Public PHP rendering uses yatra_svg_icon() / yatra_stored_picker_icon_markup(). |
| 4 |
*/ |
| 5 |
|
| 6 |
import { createElement } from "react"; |
| 7 |
import type { SVGProps } from "react"; |
| 8 |
import yatraIconsManifest from "@yatra/icons"; |
| 9 |
|
| 10 |
export interface YatraManifestEntry { |
| 11 |
label: string; |
| 12 |
category: string; |
| 13 |
svg: string; |
| 14 |
} |
| 15 |
|
| 16 |
export type IconOption = { |
| 17 |
name: string; |
| 18 |
label: string; |
| 19 |
category: string; |
| 20 |
svg: string; |
| 21 |
}; |
| 22 |
|
| 23 |
/** @deprecated All string slugs from manifest are valid */ |
| 24 |
export type IconName = string; |
| 25 |
|
| 26 |
export function getYatraIconManifest(): Record<string, YatraManifestEntry> { |
| 27 |
return yatraIconsManifest as Record<string, YatraManifestEntry>; |
| 28 |
} |
| 29 |
|
| 30 |
export function getIconOptions(): IconOption[] { |
| 31 |
const m = yatraIconsManifest as Record<string, YatraManifestEntry>; |
| 32 |
// Some manifest entries only carry `svg` (sync script left them without |
| 33 |
// label/category). Without defensive defaults, IconPicker's search filter |
| 34 |
// calls `.toLowerCase()` on undefined and crashes the whole panel. |
| 35 |
// Derive a readable label from the slug ("hot-air-balloon" → "Hot Air |
| 36 |
// Balloon") and fall back to a "general" bucket for the category so the |
| 37 |
// search and category-filter UI still works. |
| 38 |
const slugToLabel = (slug: string): string => |
| 39 |
slug |
| 40 |
.split(/[-_\s]+/) |
| 41 |
.filter(Boolean) |
| 42 |
.map((w) => w.charAt(0).toUpperCase() + w.slice(1)) |
| 43 |
.join(" "); |
| 44 |
return Object.entries(m).map(([name, meta]) => { |
| 45 |
const safeMeta = (meta || {}) as Partial<YatraManifestEntry>; |
| 46 |
return { |
| 47 |
name, |
| 48 |
label: |
| 49 |
typeof safeMeta.label === "string" && safeMeta.label.length > 0 |
| 50 |
? safeMeta.label |
| 51 |
: slugToLabel(name), |
| 52 |
category: |
| 53 |
typeof safeMeta.category === "string" && safeMeta.category.length > 0 |
| 54 |
? safeMeta.category |
| 55 |
: "general", |
| 56 |
svg: safeMeta.svg || "", |
| 57 |
}; |
| 58 |
}); |
| 59 |
} |
| 60 |
|
| 61 |
export function getYatraIconSvg(slug: string): string | null { |
| 62 |
const m = yatraIconsManifest as Record<string, YatraManifestEntry>; |
| 63 |
return m[slug]?.svg ?? null; |
| 64 |
} |
| 65 |
|
| 66 |
/** @deprecated Prefer getYatraIconSvg + inline SVG; kept for narrow compatibility */ |
| 67 |
export function getIconComponent(): null { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
export function renderIcon( |
| 72 |
iconName: string, |
| 73 |
props: SVGProps<HTMLSpanElement> = {}, |
| 74 |
): ReturnType<typeof createElement> | null { |
| 75 |
const svg = getYatraIconSvg(iconName); |
| 76 |
if (!svg) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
return createElement("span", { |
| 80 |
...props, |
| 81 |
dangerouslySetInnerHTML: { __html: svg }, |
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
export async function loadIcons(): Promise<Record<string, YatraManifestEntry>> { |
| 86 |
return getYatraIconManifest(); |
| 87 |
} |
| 88 |
|
| 89 |
export default { |
| 90 |
loadIcons, |
| 91 |
getIconOptions, |
| 92 |
getIconComponent, |
| 93 |
renderIcon, |
| 94 |
getYatraIconSvg, |
| 95 |
getYatraIconManifest, |
| 96 |
}; |
| 97 |
|