refactor: move caching logic to styleUtils (#7968)

move caching logic to style utils
This commit is contained in:
Mike Fortman 2025-05-09 13:19:09 -05:00 committed by GitHub
commit 2efb7a6cad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 55 deletions

View file

@ -1,6 +1,6 @@
import React, { Suspense, forwardRef, lazy, memo } from "react";
import React, { Suspense, forwardRef, memo } from "react";
import { IconComponentProps } from "../../../types/components";
import { getNodeIcon, iconCache } from "../../../utils/styleUtils";
import { getCachedIcon, getNodeIcon } from "../../../utils/styleUtils";
import { cn } from "../../../utils/utils";
import { Skeleton } from "@/components/ui/skeleton";
@ -23,55 +23,42 @@ export const ForwardedIconComponent = memo(
) => {
const [showFallback, setShowFallback] = useState(false);
const [iconError, setIconError] = useState(false);
const [initialName, setInitialName] = useState(name);
const [TargetIcon, setTargetIcon] = useState<any>(iconCache.get(name));
const [TargetIcon, setTargetIcon] = useState<any>(getCachedIcon(name));
useEffect(() => {
// Reset states when icon name changes
if (!TargetIcon || initialName !== name) {
setInitialName(name);
setIconError(false);
setTargetIcon(null);
setIconError(false);
setTargetIcon(null);
setShowFallback(false);
const cachedIcon = iconCache.get(name);
let isMounted = true;
let timer: NodeJS.Timeout | null = null;
const timer = cachedIcon
? setTimeout(() => {
setShowFallback(true);
}, 30)
: null;
if (name && typeof name === "string") {
getNodeIcon(name)
.then((component) => {
if (isMounted) {
setTargetIcon(component);
setShowFallback(false);
}
})
.catch((error) => {
if (isMounted) {
console.error(`Error loading icon ${name}:`, error);
setIconError(true);
setShowFallback(false);
}
});
// Load the icon if we have a name
if (name && typeof name === "string") {
// Check if the icon is already in cache
if (cachedIcon && cachedIcon !== TargetIcon) {
setTargetIcon(cachedIcon);
setShowFallback(false);
} else {
getNodeIcon(name)
.then((component) => {
// Store the fetched icon in cache
iconCache.set(name, component);
setTargetIcon(component);
setShowFallback(false);
})
.catch((error) => {
console.error(`Error loading icon ${name}:`, error);
setIconError(true);
setShowFallback(false);
});
}
} else {
setShowFallback(false);
}
return () => {
if (timer) {
clearTimeout(timer);
}
};
// Show fallback skeleton if icon takes too long
timer = setTimeout(() => {
if (isMounted) setShowFallback(true);
}, 30);
}
return () => {
isMounted = false;
if (timer) clearTimeout(timer);
};
}, [name]);
const style = {

View file

@ -6,7 +6,7 @@ import dynamicIconImports from "lucide-react/dynamicIconImports";
import { lazy } from "react";
import { FaApple, FaDiscord, FaGithub } from "react-icons/fa";
export const iconCache = new Map<string, any>();
const iconCache = new Map<string, any>();
export const BG_NOISE =
"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAAUVBMVEWFhYWDg4N3d3dtbW17e3t1dXWBgYGHh4d5eXlzc3OLi4ubm5uVlZWPj4+NjY19fX2JiYl/f39ra2uRkZGZmZlpaWmXl5dvb29xcXGTk5NnZ2c8TV1mAAAAG3RSTlNAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAvEOwtAAAFVklEQVR4XpWWB67c2BUFb3g557T/hRo9/WUMZHlgr4Bg8Z4qQgQJlHI4A8SzFVrapvmTF9O7dmYRFZ60YiBhJRCgh1FYhiLAmdvX0CzTOpNE77ME0Zty/nWWzchDtiqrmQDeuv3powQ5ta2eN0FY0InkqDD73lT9c9lEzwUNqgFHs9VQce3TVClFCQrSTfOiYkVJQBmpbq2L6iZavPnAPcoU0dSw0SUTqz/GtrGuXfbyyBniKykOWQWGqwwMA7QiYAxi+IlPdqo+hYHnUt5ZPfnsHJyNiDtnpJyayNBkF6cWoYGAMY92U2hXHF/C1M8uP/ZtYdiuj26UdAdQQSXQErwSOMzt/XWRWAz5GuSBIkwG1H3FabJ2OsUOUhGC6tK4EMtJO0ttC6IBD3kM0ve0tJwMdSfjZo+EEISaeTr9P3wYrGjXqyC1krcKdhMpxEnt5JetoulscpyzhXN5FRpuPHvbeQaKxFAEB6EN+cYN6xD7RYGpXpNndMmZgM5Dcs3YSNFDHUo2LGfZuukSWyUYirJAdYbF3MfqEKmjM+I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/Sfj7ei6UUVk4AIl3MyD4MSSTOFgSwsH/QJWaQ5as7ZcmgBZkzjjU1UrQ74ci1gWBCSGHtuV1H2mhSnO3Wp/3fEV5a+4wz//6qy8JxjZsmxxy5+4w9CDNJY09T072iKG0EnOS0arEYgXqYnXcYHwjTtUNAcMelOd4xpkoqiTYICWFq0JSiPfPDQdnt+4/wuqcXY47QILbgAAAABJRU5ErkJggg==)";
@ -448,37 +448,51 @@ export const eagerLoadedIconsMap = {
TwitterLogoIcon: TwitterLogoIcon,
};
// Function to get a lazy-loaded icon component
export const getCachedIcon = (name: string) => {
return iconCache.get(name);
};
export const getNodeIcon = async (name: string) => {
const cacheAndReturn = (icon: any) => {
iconCache.set(name, icon);
return icon;
};
if (iconCache.has(name)) {
return iconCache.get(name);
}
const iconName = nodeIconToDisplayIconMap[name];
if (eagerLoadedIconsMap[iconName || name]) {
return eagerLoadedIconsMap[iconName || name];
return cacheAndReturn(eagerLoadedIconsMap[iconName || name]);
}
if (isFontAwesomeIcon(iconName || name)) {
return fontAwesomeIcons[iconName || name];
return cacheAndReturn(fontAwesomeIcons[iconName || name]);
}
const iconMappings = await iconMappingsPromise;
if (iconMappings[iconName || name]) {
return lazy(iconMappings[iconName || name]);
return cacheAndReturn(lazy(iconMappings[iconName || name]));
}
const lucideIconName = getLucideIconName(iconName || name);
if (dynamicIconImports[lucideIconName]) {
try {
return lazy(dynamicIconImports[lucideIconName]);
return cacheAndReturn(lazy(dynamicIconImports[lucideIconName]));
} catch (e) {
// Fall through to next option
}
}
// If all else fails, return a simple empty component
return lazy(() =>
Promise.resolve({
default: () => null,
}),
return cacheAndReturn(
lazy(() =>
Promise.resolve({
default: () => null,
}),
),
);
};