2026-05-29 13:09:09 +08:00
|
|
|
import { ImageOff } from "lucide-react";
|
2026-05-29 17:59:33 +08:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2026-05-29 13:09:09 +08:00
|
|
|
|
|
|
|
|
type BubbleImageProps = {
|
|
|
|
|
src: string | undefined;
|
2026-05-29 17:59:33 +08:00
|
|
|
/**
|
|
|
|
|
* Optional absolute URL(s) to try if `src` fails to load. Useful when `src`
|
|
|
|
|
* is an optimized-but-fragile thumbnail (e.g. a root-relative thumbnailUrl)
|
|
|
|
|
* and we want to fall back to the reliable full-size asset before giving up.
|
|
|
|
|
*/
|
|
|
|
|
fallbackSrc?: string | (string | undefined)[];
|
2026-05-29 13:09:09 +08:00
|
|
|
className?: string;
|
|
|
|
|
loading?: "lazy" | "eager";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Thumbnail <img> for message bubbles. Renders with an empty alt (decorative)
|
2026-05-29 17:59:33 +08:00
|
|
|
* and, if every candidate source fails to load, falls back to a neutral
|
|
|
|
|
* placeholder instead of the browser's broken-image box — which would otherwise
|
|
|
|
|
* expose the raw file name via alt text.
|
2026-05-29 13:09:09 +08:00
|
|
|
*/
|
2026-05-29 17:59:33 +08:00
|
|
|
export function BubbleImage({
|
|
|
|
|
src,
|
|
|
|
|
fallbackSrc,
|
|
|
|
|
className,
|
|
|
|
|
loading,
|
|
|
|
|
}: BubbleImageProps) {
|
|
|
|
|
// Ordered, de-duplicated list of sources to attempt in turn.
|
|
|
|
|
const candidates = useMemo(() => {
|
|
|
|
|
const extra = Array.isArray(fallbackSrc) ? fallbackSrc : [fallbackSrc];
|
|
|
|
|
return [src, ...extra].filter(
|
|
|
|
|
(value, index, all): value is string =>
|
|
|
|
|
!!value && all.indexOf(value) === index,
|
|
|
|
|
);
|
|
|
|
|
}, [src, fallbackSrc]);
|
|
|
|
|
|
|
|
|
|
const [attempt, setAttempt] = useState(0);
|
2026-05-29 13:09:09 +08:00
|
|
|
|
2026-05-29 17:59:33 +08:00
|
|
|
// Reset when the candidate set changes so a reused element re-attempts.
|
2026-05-29 13:09:09 +08:00
|
|
|
useEffect(() => {
|
2026-05-29 17:59:33 +08:00
|
|
|
setAttempt(0);
|
|
|
|
|
}, [candidates]);
|
|
|
|
|
|
|
|
|
|
const current = candidates[attempt];
|
2026-05-29 13:09:09 +08:00
|
|
|
|
2026-05-29 17:59:33 +08:00
|
|
|
if (!current) {
|
2026-05-29 13:09:09 +08:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
className={`flex items-center justify-center bg-gradient-to-br from-neutral-800 to-neutral-900 ${
|
|
|
|
|
className ?? ""
|
|
|
|
|
}`}
|
|
|
|
|
aria-hidden
|
|
|
|
|
>
|
|
|
|
|
<ImageOff className="h-8 w-8 text-neutral-600" />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<img
|
2026-05-29 17:59:33 +08:00
|
|
|
src={current}
|
2026-05-29 13:09:09 +08:00
|
|
|
alt=""
|
|
|
|
|
loading={loading}
|
|
|
|
|
className={className}
|
2026-05-29 17:59:33 +08:00
|
|
|
onError={() => setAttempt((i) => i + 1)}
|
2026-05-29 13:09:09 +08:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|