import React, { useState, useRef, useEffect } from 'react'; import { Box, Skeleton } from '@mui/material'; interface OptimizedImageProps { src: string; alt: string; sx?: any; loading?: 'lazy' | 'eager'; placeholder?: 'blur' | 'empty'; sizes?: string; width?: number | string; height?: number | string; } export const OptimizedImage: React.FC = ({ src, alt, sx = {}, loading = 'lazy', placeholder = 'blur', sizes, width, height, }) => { const [isLoaded, setIsLoaded] = useState(false); const [isInView, setIsInView] = useState(loading === 'eager'); const [hasError, setHasError] = useState(false); const imgRef = useRef(null); useEffect(() => { if (loading === 'eager') { setIsInView(true); return; } const observer = new IntersectionObserver( entries => { entries.forEach(entry => { if (entry.isIntersecting) { setIsInView(true); observer.disconnect(); } }); }, { rootMargin: '50px', threshold: 0.01, } ); if (imgRef.current) { observer.observe(imgRef.current); } return () => { observer.disconnect(); }; }, [loading]); const handleLoad = () => { setIsLoaded(true); }; const handleError = () => { setHasError(true); setIsLoaded(true); }; // Extract clip-path and other advanced CSS from sx to apply to wrapper const { clipPath, gridArea, '--progress': progress, ...imgSx } = sx || {}; return ( {!isLoaded && !hasError && ( )} {isInView && ( )} {hasError && ( Failed to load image )} ); };