diff --git a/webui/src/components/AttachmentTile.tsx b/webui/src/components/AttachmentTile.tsx index 0f1b39ab6..9e285d8bd 100644 --- a/webui/src/components/AttachmentTile.tsx +++ b/webui/src/components/AttachmentTile.tsx @@ -1,5 +1,4 @@ import { useState, type ReactNode } from "react"; -import { FileIcon, ImageIcon, PlaySquare } from "lucide-react"; import { useTranslation } from "react-i18next"; import { cn } from "@/lib/utils"; @@ -7,6 +6,7 @@ import type { UIMediaAttachment } from "@/lib/types"; import { ImageThumbnail } from "@/components/ImageGallery"; import { ImageLightbox } from "@/components/ImageLightbox"; import { useRegisterInlineImage } from "@/components/InlineImageContext"; +import { FileReferenceIcon, fileKindForPath } from "@/components/FileReferenceChip"; interface AttachmentTileProps { attachment: UIMediaAttachment; @@ -58,14 +58,12 @@ export function AttachmentTile({ attachment, className, inline = false, variant ); } - const Icon = attachment.kind === "video" - ? PlaySquare - : attachment.kind === "image" - ? ImageIcon - : FileIcon; + const fileKind = attachment.kind === "file" + ? fileKindForPath(attachment.name || attachment.url || "") + : attachment.kind; const body = ( <> - + {attachment.name ?? label} ); diff --git a/webui/src/components/FileReferenceChip.tsx b/webui/src/components/FileReferenceChip.tsx index 5a1c9c6bb..87d64d61f 100644 --- a/webui/src/components/FileReferenceChip.tsx +++ b/webui/src/components/FileReferenceChip.tsx @@ -1,5 +1,5 @@ import { useEffect, useState, type KeyboardEvent, type MouseEvent } from "react"; -import { ImageIcon } from "lucide-react"; +import { File, FileCode2, FileImage, FileJson2, FileText, FileVideo2, type LucideIcon } from "lucide-react"; import { useTranslation } from "react-i18next"; import { FileActions, useFilePreviewLoader } from "@/components/FileActions"; import { inferMediaKind } from "@/lib/media"; @@ -23,7 +23,9 @@ export type FileReferenceKind = | "notebook" | "python" | "react" - | "typescript"; + | "text" + | "typescript" + | "video"; interface FileReferenceChipProps { path: string; @@ -95,7 +97,7 @@ export function FileReferenceChip({ ], )} > - + ; - if (kind === "python") { - return ( - - - - - - - ); - } - if (kind === "react") { - return ( - - - - - - - ); - } - if (kind === "default") { - return ( - - - - - ); - } - const label = fileKindLabel(kind); - return ( - - - - 1 ? "5.8" : "7.2"} - fontWeight="800" - letterSpacing="-0.2" - > - {label} - - - ); -} +const FILE_ICONS = { + default: File, + css: FileCode2, + html: FileCode2, + image: FileImage, + javascript: FileCode2, + json: FileJson2, + markdown: FileText, + notebook: FileCode2, + python: FileCode2, + react: FileCode2, + text: FileText, + typescript: FileCode2, + video: FileVideo2, +} satisfies Record; -function fileKindLabel(kind: FileReferenceKind): string { - switch (kind) { - case "css": - return "#"; - case "html": - return "H"; - case "javascript": - return "JS"; - case "json": - return "{}"; - case "markdown": - return "M"; - case "notebook": - return "N"; - case "python": - return "PY"; - case "typescript": - return "TS"; - default: - return ""; - } +/** The same file silhouette and stroke in replies, tabs, and attachment tiles. */ +export function FileReferenceIcon({ kind, className }: { kind: FileReferenceKind; className?: string }) { + const Icon = FILE_ICONS[kind]; + return ; } diff --git a/webui/src/components/PreviewPane.tsx b/webui/src/components/PreviewPane.tsx index 08f3bd230..bc9602ad4 100644 --- a/webui/src/components/PreviewPane.tsx +++ b/webui/src/components/PreviewPane.tsx @@ -1,5 +1,5 @@ import { useEffect, useId, useRef, useState, type CSSProperties, type PointerEvent, type ReactNode } from "react"; -import { Globe2, Image, PanelRightClose, X } from "lucide-react"; +import { Globe2, PanelRightClose, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { FileReferenceIcon, fileKindForPath, splitFilePath } from "@/components/FileReferenceChip"; import { FileActions } from "@/components/FileActions"; @@ -93,8 +93,8 @@ export function PreviewPane({ tabs, activeId, width, isClosing, onSelect, onClos }} className="flex h-8 min-w-0 items-center gap-1.5 rounded-compact pl-2.5 pr-1.5 text-[12.5px] font-medium leading-5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"> - {tab.kind === "web" ? : /\.(png|jpe?g|gif|webp|avif|bmp)$/i.test(tab.value) - ? : } + {tab.kind === "web" ? + : } {name} diff --git a/webui/src/tests/file-reference-icon.test.tsx b/webui/src/tests/file-reference-icon.test.tsx new file mode 100644 index 000000000..ae2f4e0ff --- /dev/null +++ b/webui/src/tests/file-reference-icon.test.tsx @@ -0,0 +1,72 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; + +import { AttachmentTile } from "@/components/AttachmentTile"; +import { FileReferenceChip, FileReferenceIcon, fileKindForPath } from "@/components/FileReferenceChip"; +import { PreviewPane } from "@/components/PreviewPane"; + +describe("shared file icons", () => { + it.each([ + ["chart.png", "file-image"], + ["chart.svg", "file-image"], + ["chart.avif", "file-image"], + ["index.html", "file-code2"], + ["styles.css", "file-code2"], + ["app.js", "file-code2"], + ["app.ts", "file-code2"], + ["app.tsx", "file-code2"], + ["script.py", "file-code2"], + ["analysis.ipynb", "file-code2"], + ["DATA.JSON", "file-json2"], + ["README.md", "file-text"], + ["notes.txt", "file-text"], + ["build.log", "file-text"], + ["demo.mp4", "file-video2"], + ["archive.zip", "file"], + ])("uses a consistent monochrome file-family icon for %s", (path, icon) => { + const { container } = render(); + const svg = container.querySelector("svg")!; + expect(svg).toHaveClass(`lucide-${icon}`); + expect(svg).toHaveAttribute("viewBox", "0 0 24 24"); + expect(svg).toHaveAttribute("stroke", "currentColor"); + expect(svg).toHaveAttribute("stroke-width", "1.75"); + expect(svg).toHaveAttribute("fill", "none"); + expect(svg).toHaveAttribute("aria-hidden", "true"); + expect(svg).toHaveClass("h-[1em]", "w-[1em]", "shrink-0"); + expect(svg.querySelector("text, [fill], [stroke]")).toBeNull(); + }); + + it.each(["chart.png", "index.html", "data.json", "script.py", "notes.md"])( + "reuses the same glyph for %s in replies, tabs and attachments without changing actions", (path) => { + const open = vi.fn(); + render(<> + + {}} onCloseTab={() => {}} + onClose={() => {}} onResizeStart={() => {}}>

Preview

+ + ); + const reference = screen.getByRole("button", { name: path }); + const tab = screen.getByRole("tab", { name: path }); + const attachment = screen.getByRole("link", { name: "File attachment" }); + const glyph = reference.querySelector("svg")!.innerHTML; + expect(tab.querySelector("svg")!.innerHTML).toBe(glyph); + expect(attachment.querySelector("svg")!.innerHTML).toBe(glyph); + expect(tab.querySelector("svg")).toHaveClass("size-3.5"); + expect(attachment.querySelector("svg")).toHaveClass("size-4"); + expect(tab.querySelector("svg")).not.toHaveClass("translate-y-[0.12em]"); + expect(attachment.querySelector("svg")).not.toHaveClass("translate-y-[0.12em]"); + expect(attachment).toHaveAttribute("href", `/download/${path}`); + expect(attachment).toHaveAttribute("download", path); + fireEvent.click(reference); + expect(open).toHaveBeenCalledWith(path); + }, + ); + + it.each(["image", "video"] as const)("retains %s identity when its attachment is unavailable", (kind) => { + const { container } = render(); + expect(container.querySelector("svg")).toHaveClass(kind === "image" ? "lucide-file-image" : "lucide-file-video2"); + expect(screen.queryByRole("link")).not.toBeInTheDocument(); + expect(screen.getByText("Attachment unavailable")).toBeInTheDocument(); + }); +});