Skip to content

Commit 4eb47c6

Browse files
committed
Update ImageWithFallBack.tsx
1 parent 2e4ef33 commit 4eb47c6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

web_app/Source_webapp/src/utils/components/ImageWithFallBack.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,36 @@ const ImageWithFallBack: React.FC<ImageWithFallbackProps> = ({
1616
const [isLoaded, setIsLoaded] = useState<boolean>(false)
1717

1818
useEffect(() => {
19+
setIsLoaded(false) // Reset loaded state on src change
20+
let timerId: NodeJS.Timeout | null = null
1921
const img = new Image()
2022
img.src = src
23+
2124
img.onload = () => {
25+
if (timerId) clearTimeout(timerId)
2226
setIsLoaded(true)
2327
setImageSrc(src)
2428
}
29+
2530
img.onerror = () => {
26-
setIsLoaded(true)
31+
if (timerId) clearTimeout(timerId)
32+
setIsLoaded(true) // Keep fallback image
33+
}
34+
35+
// Set timeout
36+
timerId = setTimeout(() => {
37+
if (!isLoaded) {
38+
// Check if still loading after 5s
39+
setIsLoaded(true) // Trigger fallback display
40+
}
41+
}, 5000) // 5 seconds timeout
42+
43+
// Cleanup function
44+
return () => {
45+
if (timerId) clearTimeout(timerId)
46+
// Optional: Abort image loading if possible, though standard Image doesn't have abort
47+
img.onload = null
48+
img.onerror = null
2749
}
2850
}, [src])
2951

0 commit comments

Comments
 (0)