feat(webui): refine attachment galleries and file cards (#6457)
* feat(webui): refine attachment galleries and file cards * fix(webui): align attachment tests and Escape handling
This commit is contained in:
1 parent
44a7ef53ce
commit
eed7b47753
19 files changed
+660
-255
No files matched your search
@@ -1,8 +1,9 @@
|
||||
import { useState } from "react";
|
||||
import { useLayoutEffect, useRef, useState, type CSSProperties } from "react";
|
||||
import { ImageIcon } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { ImageLightbox } from "@/components/ImageLightbox";
|
||||
import { IMAGE_GAP, imageFrameRatio, layoutImageRows, type ImageDimensions } from "@/lib/image-gallery-layout";
|
||||
import type { UIImage } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
@@ -14,20 +15,64 @@ export function ImageGallery({ images, align = "left", size = "large" }: {
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [index, setIndex] = useState<number | null>(null);
|
||||
const [dimensions, setDimensions] = useState<Record<string, ImageDimensions | null>>({});
|
||||
const [width, setWidth] = useState(0);
|
||||
const container = useRef<HTMLSpanElement>(null);
|
||||
const viewable = images.filter((image) => image.url);
|
||||
const multiple = images.length > 1;
|
||||
const visible = images.slice(0, 4);
|
||||
const measured = visible.map((image) => image.url ? dimensions[image.url] : null);
|
||||
const ready = measured.every((image) => image !== undefined);
|
||||
const maxWidth = size === "large" ? 448 : 360;
|
||||
const maxHeight = visible.length === 1 ? (size === "large" ? 352 : 320) : (size === "large" ? 256 : 224);
|
||||
const single = visible.length === 1 ? measured[0] : null;
|
||||
const preferredWidth = single ? Math.min(maxWidth, single.width, maxHeight * imageFrameRatio(single)) : maxWidth;
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const element = container.current;
|
||||
if (!element) return;
|
||||
const measure = () => setWidth(element.getBoundingClientRect().width);
|
||||
measure();
|
||||
const observer = new ResizeObserver(measure);
|
||||
observer.observe(element);
|
||||
return () => observer.disconnect();
|
||||
}, [preferredWidth]);
|
||||
|
||||
const layoutWidth = Math.min(width || preferredWidth, preferredWidth);
|
||||
const rows = layoutImageRows(
|
||||
ready ? measured : visible.map(() => null), layoutWidth,
|
||||
size === "large" ? 176 : 144, maxHeight,
|
||||
);
|
||||
let top = 0;
|
||||
const tiles = rows.flatMap((row) => {
|
||||
const rowWidth = row.widths.reduce((sum, item) => sum + item, IMAGE_GAP * (row.widths.length - 1));
|
||||
let left = align === "right" ? layoutWidth - rowWidth : 0;
|
||||
const items = row.widths.map((tileWidth, offset) => {
|
||||
const tile = { position: row.start + offset, frame: { width: tileWidth, height: row.height, left, top } };
|
||||
left += tileWidth + IMAGE_GAP;
|
||||
return tile;
|
||||
});
|
||||
top += row.height + IMAGE_GAP;
|
||||
return items;
|
||||
});
|
||||
const remember = (url: string, value: ImageDimensions | null) => setDimensions((current) => {
|
||||
const previous = current[url];
|
||||
if (previous === value || (previous && value && previous.width === value.width && previous.height === value.height)) return current;
|
||||
return { ...current, [url]: value };
|
||||
});
|
||||
|
||||
return <>
|
||||
<span className={cn(
|
||||
"max-w-full gap-2",
|
||||
multiple && size === "large" ? "grid w-[28rem] grid-cols-2" : "flex flex-wrap items-end",
|
||||
align === "right" ? "ml-auto justify-end" : "mr-auto justify-start",
|
||||
)}>
|
||||
{visible.map((image, position) => {
|
||||
<span ref={container} data-image-gallery className={cn(
|
||||
"relative block max-w-full",
|
||||
align === "right" ? "ml-auto" : "mr-auto",
|
||||
)} style={{ width: preferredWidth, height: Math.max(0, top - IMAGE_GAP) }}>
|
||||
{tiles.map(({ position, frame }) => {
|
||||
const image = visible[position];
|
||||
const remaining = images.length - visible.length;
|
||||
const overflow = position === visible.length - 1 && remaining > 0;
|
||||
return <ImageThumbnail key={`${image.url ?? image.name}-${position}`}
|
||||
image={image} size={multiple && size === "large" ? "grid" : size}
|
||||
image={image} size={size} frame={frame}
|
||||
onLoad={(value) => { if (image.url) remember(image.url, value); }}
|
||||
onError={() => { if (image.url) remember(image.url, null); }}
|
||||
label={overflow ? t("lightbox.viewAll", { count: images.length }) : undefined}
|
||||
onOpen={image.url || (overflow && viewable.length) ? () => setIndex(Math.max(0, viewable.indexOf(image))) : undefined}
|
||||
overlay={overflow ? `+${remaining}` : undefined} />;
|
||||
@@ -38,31 +83,39 @@ export function ImageGallery({ images, align = "left", size = "large" }: {
|
||||
</>;
|
||||
}
|
||||
|
||||
export function ImageThumbnail({ image, size = "large", onOpen, label, overlay, onError }: {
|
||||
export function ImageThumbnail({ image, size = "large", frame, onOpen, label, overlay, onLoad, onError }: {
|
||||
image: UIImage;
|
||||
size?: "compact" | "large" | "grid";
|
||||
size?: "compact" | "large";
|
||||
frame?: ImageDimensions & { left: number; top: number };
|
||||
onOpen?: () => void;
|
||||
label?: string;
|
||||
overlay?: string;
|
||||
onLoad?: (dimensions: ImageDimensions) => void;
|
||||
onError?: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [failed, setFailed] = useState(false);
|
||||
const classes = cn(
|
||||
"relative block max-w-full overflow-hidden rounded-control border border-border/60 bg-muted/25",
|
||||
size === "large" ? "w-[min(100%,28rem)]" : size === "grid" ? "aspect-[8/5] w-full" : "h-24 w-24",
|
||||
"relative block max-w-full shrink-0 overflow-hidden rounded-control bg-muted/25 ring-1 ring-inset ring-border/60",
|
||||
!frame && (size === "large" ? "w-[min(100%,28rem)]" : "w-48"),
|
||||
);
|
||||
const style: CSSProperties | undefined = frame ? { ...frame, position: "absolute" } : undefined;
|
||||
const unavailable = t("message.attachmentUnavailable");
|
||||
const content = image.url && !failed ? <img src={image.url} alt={image.name ?? ""}
|
||||
loading="lazy" decoding="async" draggable={false}
|
||||
onLoad={(event) => {
|
||||
const { naturalWidth, naturalHeight } = event.currentTarget;
|
||||
onLoad?.({ width: naturalWidth, height: naturalHeight });
|
||||
}}
|
||||
onError={() => { setFailed(true); onError?.(); }}
|
||||
className={cn("block w-full object-contain", size === "large" ? "h-auto max-h-[22rem]" : "h-full")} />
|
||||
: <span className={cn("flex min-h-24 items-center justify-center gap-2 px-3 py-5 text-xs text-muted-foreground", size === "grid" && "h-full")}
|
||||
aria-label={t("message.attachmentUnavailable")}>
|
||||
className={cn("block w-full object-contain text-transparent", frame ? "h-full" : "h-auto max-h-[22rem]")} />
|
||||
: <span className={cn("flex items-center justify-center gap-2 px-3 text-xs text-muted-foreground", frame ? "h-full" : "min-h-24 py-5")}
|
||||
aria-label={image.name ? `${unavailable}: ${image.name}` : unavailable} title={image.name}>
|
||||
<ImageIcon className="h-4 w-4 shrink-0" aria-hidden />
|
||||
<span className="min-w-0 truncate">{image.name || t("message.imageAttachment")}</span>
|
||||
<span className="min-w-0 truncate">{unavailable}</span>
|
||||
</span>;
|
||||
if (!onOpen || ((!image.url || failed) && !overlay)) return <span className={classes}>{content}</span>;
|
||||
return <button type="button" onClick={onOpen}
|
||||
if (!onOpen || ((!image.url || failed) && !overlay)) return <span className={classes} style={style}>{content}</span>;
|
||||
return <button type="button" onClick={onOpen} style={style}
|
||||
aria-label={label ?? (image.name ? `${t("lightbox.open")}: ${image.name}` : t("lightbox.open"))}
|
||||
className={cn(classes, "cursor-zoom-in p-0 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background")}>
|
||||
{content}
|
||||
|
||||
Reference in new issue
Block a user