admin/components/media/FallbackImage.tsx
alisaza e1eaf5eff5 feat: initial ghabilee-admin backoffice app
Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js
app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
2026-09-05 13:12:59 +03:30

45 lines
1.0 KiB
TypeScript

'use client'
import type { ImageProps } from 'next/image'
import type { SyntheticEvent } from 'react'
import Image from 'next/image'
import { useEffect, useState } from 'react'
type FallbackImageProps = Omit<ImageProps, 'alt' | 'height' | 'src' | 'width'> & {
alt: string
src?: string | null
fallbackSrc: string
height?: number
width?: number
}
const FallbackImage = ({ alt, src, fallbackSrc, height = 600, sizes = '100vw', width = 960, onError, ...props }: FallbackImageProps) => {
const resolvedSrc = src || fallbackSrc
const [imageSrc, setImageSrc] = useState(resolvedSrc)
useEffect(() => {
setImageSrc(resolvedSrc)
}, [resolvedSrc])
const handleError = (event: SyntheticEvent<HTMLImageElement>) => {
if (imageSrc !== fallbackSrc) setImageSrc(fallbackSrc)
onError?.(event)
}
return (
<Image
{...props}
alt={alt}
height={height}
sizes={sizes}
src={imageSrc}
width={width}
onError={handleError}
/>
)
}
export default FallbackImage