- vite: /uploads 代理指向源站根而非 /apnew 前缀,relative thumbnailUrl (/uploads/thumb…) 在 dev 下不再落到 SPA index.html - BubbleImage: 新增 fallbackSrc 候选链,缩略图加载失败时自动回退到绝对 thumbUrl/url,全部失败才显示占位符 - AlbumBubble / ImageLightbox: 缩略图传入绝对地址作为兜底 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { ImageOff } from "lucide-react";
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
type BubbleImageProps = {
|
|
src: string | undefined;
|
|
/**
|
|
* 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)[];
|
|
className?: string;
|
|
loading?: "lazy" | "eager";
|
|
};
|
|
|
|
/**
|
|
* Thumbnail <img> for message bubbles. Renders with an empty alt (decorative)
|
|
* 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.
|
|
*/
|
|
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);
|
|
|
|
// Reset when the candidate set changes so a reused element re-attempts.
|
|
useEffect(() => {
|
|
setAttempt(0);
|
|
}, [candidates]);
|
|
|
|
const current = candidates[attempt];
|
|
|
|
if (!current) {
|
|
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
|
|
src={current}
|
|
alt=""
|
|
loading={loading}
|
|
className={className}
|
|
onError={() => setAttempt((i) => i + 1)}
|
|
/>
|
|
);
|
|
}
|