| 1 |
/** |
| 2 |
* Renders a stored icon in admin lists: Yatra SVG library (icons.json) or Font Awesome Free (webfont). |
| 3 |
*/ |
| 4 |
|
| 5 |
import React from "react"; |
| 6 |
import { Package } from "lucide-react"; |
| 7 |
import { getYatraIconSvg } from "../../lib/icons"; |
| 8 |
import { FA_REGULAR_NAME_SET, FA_SOLID_NAME_SET } from "../../lib/fa-free-picker-icons"; |
| 9 |
import type { IconPickerValue } from "../../lib/icon-picker-types"; |
| 10 |
|
| 11 |
export type IconSelectorProvider = "yatra" | "fa-solid" | "fa-regular"; |
| 12 |
|
| 13 |
interface IconSelectorProps { |
| 14 |
/** Legacy: slug only (Yatra library) */ |
| 15 |
iconName: string; |
| 16 |
provider?: IconSelectorProvider; |
| 17 |
className?: string; |
| 18 |
size?: number; |
| 19 |
} |
| 20 |
|
| 21 |
export const IconSelector: React.FC<IconSelectorProps> = ({ |
| 22 |
iconName, |
| 23 |
provider = "yatra", |
| 24 |
className = "w-5 h-5", |
| 25 |
size, |
| 26 |
}: IconSelectorProps) => { |
| 27 |
const sizeStyle = |
| 28 |
size !== undefined |
| 29 |
? { |
| 30 |
fontSize: size, |
| 31 |
width: size, |
| 32 |
height: size, |
| 33 |
lineHeight: 1, |
| 34 |
display: "inline-flex" as const, |
| 35 |
alignItems: "center" as const, |
| 36 |
justifyContent: "center" as const, |
| 37 |
} |
| 38 |
: undefined; |
| 39 |
|
| 40 |
if (provider === "fa-solid" || provider === "fa-regular") { |
| 41 |
const ok = |
| 42 |
provider === "fa-regular" |
| 43 |
? FA_REGULAR_NAME_SET.has(iconName) |
| 44 |
: FA_SOLID_NAME_SET.has(iconName); |
| 45 |
if (ok) { |
| 46 |
const prefix = provider === "fa-regular" ? "fa-regular" : "fa-solid"; |
| 47 |
return ( |
| 48 |
<i |
| 49 |
className={`${prefix} fa-${iconName} ${className}`} |
| 50 |
style={sizeStyle} |
| 51 |
aria-hidden="true" |
| 52 |
/> |
| 53 |
); |
| 54 |
} |
| 55 |
return <Package className={className} style={sizeStyle} />; |
| 56 |
} |
| 57 |
|
| 58 |
const svg = getYatraIconSvg(iconName); |
| 59 |
if (svg) { |
| 60 |
return ( |
| 61 |
<span |
| 62 |
className={`inline-flex items-center justify-center ${className}`} |
| 63 |
// eslint-disable-next-line react/no-danger -- trusted Yatra-bundled SVG from icons.json |
| 64 |
dangerouslySetInnerHTML={{ __html: svg }} |
| 65 |
style={sizeStyle} |
| 66 |
/> |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
return <Package className={className} style={sizeStyle} />; |
| 71 |
}; |
| 72 |
|
| 73 |
/** @deprecated Use getIconOptions() from lib/icons for the full Yatra set */ |
| 74 |
export const availableIcons: Array<{ |
| 75 |
name: string; |
| 76 |
label: string; |
| 77 |
}> = []; |
| 78 |
|
| 79 |
export function iconPickerValueToSelectorProps( |
| 80 |
val: IconPickerValue | null | undefined, |
| 81 |
): { iconName: string; provider: IconSelectorProvider } { |
| 82 |
if (!val || val.type !== "icon" || !val.value) { |
| 83 |
return { iconName: "package", provider: "yatra" }; |
| 84 |
} |
| 85 |
const p = val.provider ?? "yatra"; |
| 86 |
if (p === "fa-solid" || p === "fa-regular") { |
| 87 |
return { iconName: val.value, provider: p }; |
| 88 |
} |
| 89 |
|
| 90 |
return { iconName: val.value, provider: "yatra" }; |
| 91 |
} |
| 92 |
|
| 93 |
export default IconSelector; |
| 94 |
|