diff --git a/src/components/Componentes/network-image.tsx b/src/components/Componentes/network-image.tsx index 73eca42..a7fcaaf 100644 --- a/src/components/Componentes/network-image.tsx +++ b/src/components/Componentes/network-image.tsx @@ -1,7 +1,7 @@ "use client"; import Image, { type ImageProps } from "next/image"; -import { useState, useEffect } from "react"; +import { useState, useEffect, useRef } from "react"; import { cn } from "@/lib/utils"; export interface NetworkImageProps extends Omit { @@ -22,20 +22,42 @@ export function NetworkImage({ width, height, priority, + unoptimized, ...props }: NetworkImageProps) { - const initialSrc = src || fallbackSrc; - const [imgSrc, setImgSrc] = useState(initialSrc); - const [isLoaded, setIsLoaded] = useState(false); - const [hasError, setHasError] = useState(false); + const targetSrc = + src && typeof src === "string" && src.trim() ? src.trim() : fallbackSrc; + const [imgSrc, setImgSrc] = useState(targetSrc); + const [isLoaded, setIsLoaded] = useState(false); + const [hasError, setHasError] = useState(false); + const imgRef = useRef(null); useEffect(() => { - const target = src || fallbackSrc; - setImgSrc(target); - setHasError(false); - setIsLoaded(false); + const nextTarget = + src && typeof src === "string" && src.trim() ? src.trim() : fallbackSrc; + setImgSrc((prev) => { + if (prev !== nextTarget) { + setIsLoaded(false); + setHasError(false); + return nextTarget; + } + return prev; + }); }, [src, fallbackSrc]); + const isExternal = + typeof imgSrc === "string" && + (imgSrc.startsWith("http://") || imgSrc.startsWith("https://")); + const effectiveUnoptimized = + unoptimized !== undefined ? unoptimized : isExternal; + + const handleRef = (node: HTMLImageElement | null) => { + imgRef.current = node; + if (node?.complete && node.naturalWidth > 0 && !isLoaded) { + setIsLoaded(true); + } + }; + return (
setIsLoaded(true)} + unoptimized={effectiveUnoptimized} + onLoad={() => { + setIsLoaded(true); + setHasError(false); + }} onError={() => { if (imgSrc !== fallbackSrc) { setImgSrc(fallbackSrc); + setIsLoaded(false); + setHasError(false); } else { setHasError(true); + setIsLoaded(true); } }} className={cn( "transition-opacity duration-300", - !isLoaded ? "opacity-0" : "opacity-100", + !isLoaded && !hasError ? "opacity-0" : "opacity-100", className, )} />