File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
web_app/Source_webapp/src/utils/components Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -16,14 +16,36 @@ const ImageWithFallBack: React.FC<ImageWithFallbackProps> = ({
16
16
const [ isLoaded , setIsLoaded ] = useState < boolean > ( false )
17
17
18
18
useEffect ( ( ) => {
19
+ setIsLoaded ( false ) // Reset loaded state on src change
20
+ let timerId : NodeJS . Timeout | null = null
19
21
const img = new Image ( )
20
22
img . src = src
23
+
21
24
img . onload = ( ) => {
25
+ if ( timerId ) clearTimeout ( timerId )
22
26
setIsLoaded ( true )
23
27
setImageSrc ( src )
24
28
}
29
+
25
30
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
27
49
}
28
50
} , [ src ] )
29
51
You can’t perform that action at this time.
0 commit comments