fix(webui): separate file cards from media previews

This commit is contained in:
Xubin Ren committed 2026-10-24 23:55:10 +08:00
1 parent d6cc08f335
commit 0b22c1875a
21 files changed
+432 -44

No files matched your search

+31 -27
View File
@@ -1,5 +1,6 @@
import { useState, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { Download } from "lucide-react";
import { cn } from "@/lib/utils";
import type { UIMediaAttachment } from "@/lib/types";
@@ -61,58 +62,61 @@ export function AttachmentTile({ attachment, className, inline = false, variant
);
}
const fileKind = attachment.kind === "file"
? fileKindForPath(attachment.name || attachment.url || "")
: attachment.kind;
const filePath = attachment.name || attachment.url?.split(/[?#]/, 1)[0] || "";
const fileKind = attachment.kind === "file" ? fileKindForPath(filePath) : attachment.kind;
const name = attachment.name || label;
const available = hasUrl && !failed;
const extension = attachment.name?.match(/\.(tar\.(?:gz|bz2|xz)|[a-z0-9]{1,12})$/i)?.[1];
const details = available ? extension?.toUpperCase() || label
: pending ? t("message.delivery.sending") : t("message.attachmentUnavailable", { defaultValue: "Attachment unavailable" });
const cardClassName = cn(
"not-prose flex w-72 min-w-0 max-w-full items-center gap-3 rounded-control",
"border border-border/60 bg-muted/40 px-3 py-2.5 text-sm text-muted-foreground",
variant === "compact" && "w-56 gap-2 px-2.5 py-2 text-xs",
className,
);
const body = (
<>
<FileReferenceIcon kind={fileKind} className="size-4" />
<span className="min-w-0 truncate">{attachment.name ?? label}</span>
<span className={cn("flex size-9 shrink-0 items-center justify-center rounded-compact bg-background/70", variant === "compact" && "size-8")}>
<FileReferenceIcon kind={fileKind} className={variant === "compact" ? "size-4" : "size-5"} />
</span>
<span className="min-w-0 flex-1">
<span className="block truncate font-medium text-foreground">{name}</span>
<span className="block break-words text-xs leading-4">{details}</span>
</span>
</>
);
if (hasUrl && !failed) {
if (available) {
return (
<a
href={attachment.url}
download={attachment.name ?? label}
title={attachment.name ?? undefined}
aria-label={label}
download={name}
title={name}
aria-label={t("message.downloadAttachment", { name })}
className={cn(
"flex max-w-[18rem] items-center gap-2 rounded-control",
"border border-border/60 bg-muted/40 px-3 py-2 text-xs text-muted-foreground",
cardClassName,
"transition-colors hover:bg-muted/55 hover:text-foreground",
variant === "compact" && "max-w-[14rem] rounded-xl px-2.5 py-1.5 text-[11.5px]",
className,
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
)}
>
{body}
<Download aria-hidden strokeWidth={1.75} className="size-4 shrink-0" />
</a>
);
}
const Frame = inline ? "span" : "div";
return (
<div
className={cn(
"flex max-w-[18rem] items-center gap-2 rounded-control",
"border border-border/60 bg-muted/35 px-3 py-2 text-xs text-muted-foreground",
variant === "compact" && "max-w-[14rem] rounded-xl px-2.5 py-1.5 text-[11.5px]",
className,
)}
title={attachment.name ?? undefined}
aria-label={label}
>
<Frame className={cardClassName} title={name} aria-label={attachment.name ? `${label}: ${name}` : label}>
{body}
<span className="text-xs text-muted-foreground">
{pending ? t("message.delivery.sending") : t("message.attachmentUnavailable", { defaultValue: "Attachment unavailable" })}
</span>
{attachment.kind === "video" && failed && !pending ? (
<button type="button" onClick={() => setFailedUrl(undefined)}
className="shrink-0 rounded-sm px-1 text-foreground underline-offset-2 hover:underline focus-visible:outline focus-visible:outline-2">
{t("common.retry")}
</button>
) : null}
</div>
</Frame>
);
}
+43 -1
View File
@@ -1,5 +1,5 @@
import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type MouseEvent, type HTMLAttributes } from "react";
import { File, FileCode2, FileImage, FileJson2, FileText, FileVideo2, type LucideIcon } from "lucide-react";
import { File, FileArchive, FileAudio, FileChartColumn, FileCode2, FileImage, FileJson2, FileSpreadsheet, FileText, FileVideo2, type LucideIcon } from "lucide-react";
import { useTranslation } from "react-i18next";
import { FileActions, useFilePreviewLoader } from "@/components/FileActions";
import { inferMediaKind } from "@/lib/media";
@@ -14,15 +14,20 @@ import { cn } from "@/lib/utils";
export type FileReferenceKind =
| "default"
| "archive"
| "audio"
| "css"
| "document"
| "html"
| "image"
| "javascript"
| "json"
| "markdown"
| "notebook"
| "presentation"
| "python"
| "react"
| "spreadsheet"
| "text"
| "typescript"
| "video";
@@ -232,6 +237,38 @@ export function fileKindForPath(path: string): FileReferenceKind {
return "default";
}
switch (ext) {
case "zip":
case "7z":
case "rar":
case "tar":
case "gz":
case "bz2":
case "xz":
case "tgz":
return "archive";
case "mp3":
case "wav":
case "ogg":
case "m4a":
case "flac":
case "aac":
return "audio";
case "pdf":
case "doc":
case "docx":
case "odt":
case "rtf":
return "document";
case "xls":
case "xlsx":
case "csv":
case "tsv":
case "ods":
return "spreadsheet";
case "ppt":
case "pptx":
case "odp":
return "presentation";
case "avif":
return "image";
case "py":
@@ -273,15 +310,20 @@ export function fileKindForPath(path: string): FileReferenceKind {
const FILE_ICONS = {
default: File,
archive: FileArchive,
audio: FileAudio,
css: FileCode2,
document: FileText,
html: FileCode2,
image: FileImage,
javascript: FileCode2,
json: FileJson2,
markdown: FileText,
notebook: FileCode2,
presentation: FileChartColumn,
python: FileCode2,
react: FileCode2,
spreadsheet: FileSpreadsheet,
text: FileText,
typescript: FileCode2,
video: FileVideo2,
+17 -7
View File
@@ -651,32 +651,42 @@ function MessageMedia({
if (media.length === 0) return null;
const images: UIImage[] = [];
const seen = new Set<string>();
const nonImages: UIMediaAttachment[] = [];
const videos: UIMediaAttachment[] = [];
const files: UIMediaAttachment[] = [];
for (const item of media) {
const normalized = toMediaAttachment(item);
if (normalized.kind === "image") {
if (normalized.url && (inlineImages.has(normalized.url) || seen.has(normalized.url))) continue;
if (normalized.url) seen.add(normalized.url);
images.push({ url: normalized.url, name: normalized.name });
} else if (normalized.kind === "video") {
videos.push(normalized);
} else {
nonImages.push(normalized);
files.push(normalized);
}
}
if (images.length === 0 && nonImages.length === 0) return null;
if (images.length === 0 && videos.length === 0 && files.length === 0) return null;
return (
<div
className={cn(
"mt-2 flex flex-wrap gap-2",
align === "right" ? "justify-end" : "justify-start",
"mt-2 flex min-w-0 max-w-full flex-col gap-2",
align === "right" ? "items-end" : "items-start",
)}
>
{images.length > 0 ? (
<ImageGallery images={images} align={align} size={align === "left" ? "large" : "compact"} />
) : null}
{nonImages.map((item, i) => (
<AttachmentTile key={`${item.kind}-${i}`} attachment={item} pending={pending} />
{videos.map((item, i) => (
<AttachmentTile key={`video-${i}`} attachment={item} pending={pending} className="my-0" />
))}
{files.length > 0 ? (
<div className={cn("flex w-full min-w-0 flex-wrap items-start gap-2", align === "right" && "justify-end")}>
{files.map((item, i) => (
<AttachmentTile key={`file-${i}`} attachment={item} pending={pending} />
))}
</div>
) : null}
</div>
);
}