2025-08-25 16:07:55 +01:00
|
|
|
|
import React from 'react';
|
|
|
|
|
|
import { addCollection, Icon } from '@iconify/react';
|
2025-10-29 11:58:04 +00:00
|
|
|
|
import iconSet from '../../../assets/material-symbols-icons.json'; // eslint-disable-line no-restricted-imports -- Outside app paths
|
2025-08-25 16:07:55 +01:00
|
|
|
|
|
|
|
|
|
|
// Load icons synchronously at import time - guaranteed to be ready on first render
|
|
|
|
|
|
let iconsLoaded = false;
|
|
|
|
|
|
let localIconCount = 0;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (iconSet) {
|
|
|
|
|
|
addCollection(iconSet);
|
|
|
|
|
|
iconsLoaded = true;
|
|
|
|
|
|
localIconCount = Object.keys(iconSet.icons || {}).length;
|
|
|
|
|
|
console.info(`✅ Local icons loaded: ${localIconCount} icons (${Math.round(JSON.stringify(iconSet).length / 1024)}KB)`);
|
|
|
|
|
|
}
|
2025-09-05 12:16:17 +01:00
|
|
|
|
} catch {
|
2025-08-25 16:07:55 +01:00
|
|
|
|
console.info('ℹ️ Local icons not available - using CDN fallback');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface LocalIconProps {
|
|
|
|
|
|
icon: string;
|
|
|
|
|
|
width?: string | number;
|
|
|
|
|
|
height?: string | number;
|
|
|
|
|
|
style?: React.CSSProperties;
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* LocalIcon component that uses our locally bundled Material Symbols icons
|
|
|
|
|
|
* instead of loading from CDN
|
|
|
|
|
|
*/
|
2025-10-29 09:56:16 +00:00
|
|
|
|
export const LocalIcon: React.FC<LocalIconProps> = ({ icon, width, height, style, ...props }) => {
|
2025-08-25 16:07:55 +01:00
|
|
|
|
// Convert our icon naming convention to the local collection format
|
2025-09-05 12:16:17 +01:00
|
|
|
|
const iconName = icon.startsWith('material-symbols:')
|
|
|
|
|
|
? icon
|
2025-08-25 16:07:55 +01:00
|
|
|
|
: `material-symbols:${icon}`;
|
2025-09-05 12:16:17 +01:00
|
|
|
|
|
2025-08-25 16:07:55 +01:00
|
|
|
|
// Development logging (only in dev mode)
|
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
|
const logKey = `icon-${iconName}`;
|
|
|
|
|
|
if (!sessionStorage.getItem(logKey)) {
|
|
|
|
|
|
const source = iconsLoaded ? 'local' : 'CDN';
|
|
|
|
|
|
console.debug(`🎯 Icon: ${iconName} (${source})`);
|
|
|
|
|
|
sessionStorage.setItem(logKey, 'logged');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-05 12:16:17 +01:00
|
|
|
|
|
2025-10-29 09:56:16 +00:00
|
|
|
|
const iconStyle: React.CSSProperties = { ...style };
|
|
|
|
|
|
|
|
|
|
|
|
// Use width if provided, otherwise fall back to height
|
|
|
|
|
|
const size = width || height;
|
|
|
|
|
|
if (size && typeof size === 'string') {
|
|
|
|
|
|
// If it's a CSS unit string (like '1.5rem'), use it as fontSize
|
|
|
|
|
|
iconStyle.fontSize = size;
|
|
|
|
|
|
} else if (typeof size === 'number') {
|
|
|
|
|
|
// If it's a number, treat it as pixels
|
|
|
|
|
|
iconStyle.fontSize = `${size}px`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-25 16:07:55 +01:00
|
|
|
|
// Always render the icon - Iconify will use local if available, CDN if not
|
2025-10-29 09:56:16 +00:00
|
|
|
|
return <Icon icon={iconName} style={iconStyle} {...props} />;
|
2025-08-25 16:07:55 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-05 12:16:17 +01:00
|
|
|
|
export default LocalIcon;
|