import { useEffect, useState, useRef } from "react"; interface CompanyLogoProps extends React.ImgHTMLAttributes { location?: string | null; } const imageModules = import.meta.glob('/src/assets/logos/companies/*.png'); const imageModules2 = import.meta.glob('/src/assets/logos/*.png'); export const CompanyLogo: React.FC = ({ location, ...imgProps }) => { const [logoSrc, setLogoSrc] = useState(); const imageCacheRef = useRef>>({}); useEffect(() => { const loadImage = async () => { const imagePath = (location == undefined || location == null) ? '/src/assets/logos/ferinesia-logo.png' : `/src/assets/logos/companies/${location}`; // Check if the image is already loaded if (imageCacheRef.current[imagePath]) { if (typeof imageCacheRef.current[imagePath] === "string") { // Image is already loaded setLogoSrc(imageCacheRef.current[imagePath]); } else { // Image is still loading const cachedImageUrl = await imageCacheRef.current[imagePath]; setLogoSrc(cachedImageUrl); } return; } // Load the image module and cache the promise const module = (location == undefined || location == null) ? imageModules2[imagePath] : imageModules[imagePath]; if (module) { const imagePromise = module().then((image: any) => { const imageUrl = image.default; // Store the loaded image URL in the cache imageCacheRef.current[imagePath] = imageUrl; return imageUrl; }); // Store the promise in the cache imageCacheRef.current[imagePath] = imagePromise; // Set the logo source once the promise resolves const loadedImageUrl = await imagePromise; setLogoSrc(loadedImageUrl); } else { // Handle case where the module doesn't exist (optional) setLogoSrc('/src/assets/logos/ferinesia-logo.png'); } }; loadImage(); }, [location]); return Company Logo; };