|
| 1 | +import { cn } from "@plane/utils"; |
| 2 | +// helpers |
| 3 | +import { getCoverImageDisplayURL, DEFAULT_COVER_IMAGE_URL } from "@/helpers/cover-image.helper"; |
| 4 | + |
| 5 | +type TCoverImageProps = { |
| 6 | + /** The cover image URL - can be static, uploaded, or external */ |
| 7 | + src: string | null | undefined; |
| 8 | + /** Alt text for the image */ |
| 9 | + alt?: string; |
| 10 | + /** Additional className for the image or skeleton */ |
| 11 | + className?: string; |
| 12 | + /** Whether to show default image when src is null/undefined. If false, shows loading skeleton */ |
| 13 | + showDefaultWhenEmpty?: boolean; |
| 14 | + /** Custom fallback URL to use instead of DEFAULT_COVER_IMAGE_URL */ |
| 15 | + fallbackUrl?: string; |
| 16 | +} & React.ComponentProps<"img">; |
| 17 | + |
| 18 | +/** |
| 19 | + * A reusable cover image component that handles: |
| 20 | + * - Loading states with skeleton |
| 21 | + * - Static images (local assets) |
| 22 | + * - Uploaded images (processed through getFileURL) |
| 23 | + * - External URLs |
| 24 | + * - Fallback to default cover image |
| 25 | + */ |
| 26 | +export function CoverImage(props: TCoverImageProps) { |
| 27 | + const { |
| 28 | + src, |
| 29 | + alt = "Cover image", |
| 30 | + className, |
| 31 | + showDefaultWhenEmpty = false, |
| 32 | + fallbackUrl = DEFAULT_COVER_IMAGE_URL, |
| 33 | + ...restProps |
| 34 | + } = props; |
| 35 | + |
| 36 | + // Show loading skeleton when src is undefined/null and we don't want to show default |
| 37 | + if (!src && !showDefaultWhenEmpty) { |
| 38 | + return <div className={cn("bg-layer-2 animate-pulse", className)} />; |
| 39 | + } |
| 40 | + |
| 41 | + const displayUrl = getCoverImageDisplayURL(src, fallbackUrl); |
| 42 | + |
| 43 | + return <img src={displayUrl} alt={alt} className={cn("object-cover", className)} {...restProps} />; |
| 44 | +} |
0 commit comments