| 
									
										
										
										
											2025-09-17 11:31:17 -03:00
										 |  |  | // src/components/common/ImageWithFallback.tsx
 | 
					
						
							| 
									
										
										
										
											2025-09-01 14:04:40 -03:00
										 |  |  | import { useState, useEffect } from 'react'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface Props extends React.ImgHTMLAttributes<HTMLImageElement> { | 
					
						
							|  |  |  |   fallbackSrc: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const ImageWithFallback = ({ src, fallbackSrc, ...props }: Props) => { | 
					
						
							|  |  |  |   const [imgSrc, setImgSrc] = useState(src); | 
					
						
							|  |  |  |   const [error, setError] = useState(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     setError(false); | 
					
						
							|  |  |  |     setImgSrc(src); | 
					
						
							|  |  |  |   }, [src]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleError = () => { | 
					
						
							|  |  |  |     setError(true); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <img | 
					
						
							|  |  |  |       src={error || !imgSrc ? fallbackSrc : imgSrc} | 
					
						
							|  |  |  |       onError={handleError} | 
					
						
							|  |  |  |       {...props} | 
					
						
							|  |  |  |     /> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; |