fix(webui): unify file reference icons across preview surfaces
This commit is contained in:
1 parent
f8bfcf4250
commit
ebb29759c0
4 files changed
+111
-127
No files matched your search
@@ -1,5 +1,4 @@
|
|||||||
import { useState, type ReactNode } from "react";
|
import { useState, type ReactNode } from "react";
|
||||||
import { FileIcon, ImageIcon, PlaySquare } from "lucide-react";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
@@ -7,6 +6,7 @@ import type { UIMediaAttachment } from "@/lib/types";
|
|||||||
import { ImageThumbnail } from "@/components/ImageGallery";
|
import { ImageThumbnail } from "@/components/ImageGallery";
|
||||||
import { ImageLightbox } from "@/components/ImageLightbox";
|
import { ImageLightbox } from "@/components/ImageLightbox";
|
||||||
import { useRegisterInlineImage } from "@/components/InlineImageContext";
|
import { useRegisterInlineImage } from "@/components/InlineImageContext";
|
||||||
|
import { FileReferenceIcon, fileKindForPath } from "@/components/FileReferenceChip";
|
||||||
|
|
||||||
interface AttachmentTileProps {
|
interface AttachmentTileProps {
|
||||||
attachment: UIMediaAttachment;
|
attachment: UIMediaAttachment;
|
||||||
@@ -58,14 +58,12 @@ export function AttachmentTile({ attachment, className, inline = false, variant
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Icon = attachment.kind === "video"
|
const fileKind = attachment.kind === "file"
|
||||||
? PlaySquare
|
? fileKindForPath(attachment.name || attachment.url || "")
|
||||||
: attachment.kind === "image"
|
: attachment.kind;
|
||||||
? ImageIcon
|
|
||||||
: FileIcon;
|
|
||||||
const body = (
|
const body = (
|
||||||
<>
|
<>
|
||||||
<Icon className="h-4 w-4 flex-none" aria-hidden />
|
<FileReferenceIcon kind={fileKind} className="size-4" />
|
||||||
<span className="min-w-0 truncate">{attachment.name ?? label}</span>
|
<span className="min-w-0 truncate">{attachment.name ?? label}</span>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useState, type KeyboardEvent, type MouseEvent } from "react";
|
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 { useTranslation } from "react-i18next";
|
||||||
import { FileActions, useFilePreviewLoader } from "@/components/FileActions";
|
import { FileActions, useFilePreviewLoader } from "@/components/FileActions";
|
||||||
import { inferMediaKind } from "@/lib/media";
|
import { inferMediaKind } from "@/lib/media";
|
||||||
@@ -23,7 +23,9 @@ export type FileReferenceKind =
|
|||||||
| "notebook"
|
| "notebook"
|
||||||
| "python"
|
| "python"
|
||||||
| "react"
|
| "react"
|
||||||
| "typescript";
|
| "text"
|
||||||
|
| "typescript"
|
||||||
|
| "video";
|
||||||
|
|
||||||
interface FileReferenceChipProps {
|
interface FileReferenceChipProps {
|
||||||
path: string;
|
path: string;
|
||||||
@@ -95,7 +97,7 @@ export function FileReferenceChip({
|
|||||||
],
|
],
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
<FileReferenceIcon kind={kind} interactive={interactive} />
|
<FileReferenceIcon kind={kind} className="translate-y-[0.12em]" />
|
||||||
<span
|
<span
|
||||||
data-sheen-text={active ? displayText : undefined}
|
data-sheen-text={active ? displayText : undefined}
|
||||||
className={cn(
|
className={cn(
|
||||||
@@ -192,7 +194,8 @@ export function splitFilePath(path: string): { directory: string; name: string }
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function fileKindForPath(path: string): FileReferenceKind {
|
export function fileKindForPath(path: string): FileReferenceKind {
|
||||||
if (inferMediaKind({ url: path }) === "image") return "image";
|
const mediaKind = inferMediaKind({ url: path });
|
||||||
|
if (mediaKind === "image" || mediaKind === "video") return mediaKind;
|
||||||
const normalized = path.toLowerCase();
|
const normalized = path.toLowerCase();
|
||||||
const name = normalized.split(/[\\/]/).pop() ?? normalized;
|
const name = normalized.split(/[\\/]/).pop() ?? normalized;
|
||||||
const ext = name.includes(".") ? name.split(".").pop() ?? "" : "";
|
const ext = name.includes(".") ? name.split(".").pop() ?? "" : "";
|
||||||
@@ -200,6 +203,8 @@ export function fileKindForPath(path: string): FileReferenceKind {
|
|||||||
return "default";
|
return "default";
|
||||||
}
|
}
|
||||||
switch (ext) {
|
switch (ext) {
|
||||||
|
case "avif":
|
||||||
|
return "image";
|
||||||
case "py":
|
case "py":
|
||||||
case "pyi":
|
case "pyi":
|
||||||
return "python";
|
return "python";
|
||||||
@@ -229,123 +234,32 @@ export function fileKindForPath(path: string): FileReferenceKind {
|
|||||||
return "markdown";
|
return "markdown";
|
||||||
case "ipynb":
|
case "ipynb":
|
||||||
return "notebook";
|
return "notebook";
|
||||||
|
case "txt":
|
||||||
|
case "log":
|
||||||
|
return "text";
|
||||||
default:
|
default:
|
||||||
return "default";
|
return "default";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function FileReferenceIcon({ kind, interactive }: { kind: FileReferenceKind; interactive: boolean }) {
|
const FILE_ICONS = {
|
||||||
if (kind === "image") return <ImageIcon aria-hidden className="h-[1em] w-[1em] shrink-0 translate-y-[0.12em]" />;
|
default: File,
|
||||||
if (kind === "python") {
|
css: FileCode2,
|
||||||
return (
|
html: FileCode2,
|
||||||
<svg
|
image: FileImage,
|
||||||
aria-hidden
|
javascript: FileCode2,
|
||||||
className="h-[1em] w-[1em] shrink-0 translate-y-[0.12em]"
|
json: FileJson2,
|
||||||
viewBox="0 0 24 24"
|
markdown: FileText,
|
||||||
>
|
notebook: FileCode2,
|
||||||
<path
|
python: FileCode2,
|
||||||
d="M11.9 2.3c-3 0-4.5.8-4.5 2.3v2.1h4.8v.8H5.5C4 7.5 3 8.8 3 10.8v2.1c0 1.8 1.1 3 2.7 3h1.6v-2.3c0-1.7 1.4-3.1 3.1-3.1h4.2c1.3 0 2.3-1 2.3-2.3V4.6c0-1.4-1.5-2.3-4.6-2.3h-.4Z"
|
react: FileCode2,
|
||||||
fill="#3776AB"
|
text: FileText,
|
||||||
/>
|
typescript: FileCode2,
|
||||||
<path
|
video: FileVideo2,
|
||||||
d="M12.1 21.7c3 0 4.5-.8 4.5-2.3v-2.1h-4.8v-.8h6.7c1.5 0 2.5-1.3 2.5-3.3v-2.1c0-1.8-1.1-3-2.7-3h-1.6v2.3c0 1.7-1.4 3.1-3.1 3.1H9.4c-1.3 0-2.3 1-2.3 2.3v3.6c0 1.4 1.5 2.3 4.6 2.3h.4Z"
|
} satisfies Record<FileReferenceKind, LucideIcon>;
|
||||||
fill="#FFD43B"
|
|
||||||
/>
|
|
||||||
<circle cx="9" cy="5.1" r="0.8" fill="#fff" />
|
|
||||||
<circle cx="15" cy="18.9" r="0.8" fill="#5C3B00" opacity="0.85" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (kind === "react") {
|
|
||||||
return (
|
|
||||||
<svg
|
|
||||||
aria-hidden
|
|
||||||
className={cn("h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em]", interactive && "text-sky-500 dark:text-sky-300")}
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="1.6"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
>
|
|
||||||
<circle cx="12" cy="12" r="1.9" fill="currentColor" stroke="none" />
|
|
||||||
<ellipse cx="12" cy="12" rx="9" ry="3.7" />
|
|
||||||
<ellipse cx="12" cy="12" rx="9" ry="3.7" transform="rotate(60 12 12)" />
|
|
||||||
<ellipse cx="12" cy="12" rx="9" ry="3.7" transform="rotate(120 12 12)" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (kind === "default") {
|
|
||||||
return (
|
|
||||||
<svg
|
|
||||||
aria-hidden
|
|
||||||
className={cn("h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em]", interactive && "text-sky-500 dark:text-sky-300")}
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="1.9"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
>
|
|
||||||
<path d="M14 2H7a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7z" />
|
|
||||||
<path d="M14 2v5h5" />
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const label = fileKindLabel(kind);
|
|
||||||
return (
|
|
||||||
<svg
|
|
||||||
aria-hidden
|
|
||||||
className={cn("h-[0.96em] w-[0.96em] shrink-0 translate-y-[0.12em]", interactive && "text-sky-500 dark:text-sky-300")}
|
|
||||||
viewBox="0 0 24 24"
|
|
||||||
fill="none"
|
|
||||||
>
|
|
||||||
<path
|
|
||||||
d="M7 3.5h6.6L18 7.9V19a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 6 19V5a1.5 1.5 0 0 1 1.5-1.5Z"
|
|
||||||
fill="currentColor"
|
|
||||||
opacity="0.12"
|
|
||||||
/>
|
|
||||||
<path
|
|
||||||
d="M13.5 3.75V8h4.25M7 3.5h6.6L18 7.9V19a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 6 19V5a1.5 1.5 0 0 1 1.5-1.5Z"
|
|
||||||
stroke="currentColor"
|
|
||||||
strokeWidth="1.75"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
/>
|
|
||||||
<text
|
|
||||||
x="12"
|
|
||||||
y="15.7"
|
|
||||||
textAnchor="middle"
|
|
||||||
fill="currentColor"
|
|
||||||
fontSize={label.length > 1 ? "5.8" : "7.2"}
|
|
||||||
fontWeight="800"
|
|
||||||
letterSpacing="-0.2"
|
|
||||||
>
|
|
||||||
{label}
|
|
||||||
</text>
|
|
||||||
</svg>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function fileKindLabel(kind: FileReferenceKind): string {
|
/** The same file silhouette and stroke in replies, tabs, and attachment tiles. */
|
||||||
switch (kind) {
|
export function FileReferenceIcon({ kind, className }: { kind: FileReferenceKind; className?: string }) {
|
||||||
case "css":
|
const Icon = FILE_ICONS[kind];
|
||||||
return "#";
|
return <Icon aria-hidden strokeWidth={1.75} className={cn("h-[1em] w-[1em] shrink-0", className)} />;
|
||||||
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 "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useEffect, useId, useRef, useState, type CSSProperties, type PointerEvent, type ReactNode } from "react";
|
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 { useTranslation } from "react-i18next";
|
||||||
import { FileReferenceIcon, fileKindForPath, splitFilePath } from "@/components/FileReferenceChip";
|
import { FileReferenceIcon, fileKindForPath, splitFilePath } from "@/components/FileReferenceChip";
|
||||||
import { FileActions } from "@/components/FileActions";
|
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">
|
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">
|
||||||
<span aria-hidden className="flex size-3.5 shrink-0 items-center justify-center text-muted-foreground">
|
<span aria-hidden className="flex size-3.5 shrink-0 items-center justify-center text-muted-foreground">
|
||||||
{tab.kind === "web" ? <Globe2 className="size-3.5" /> : /\.(png|jpe?g|gif|webp|avif|bmp)$/i.test(tab.value)
|
{tab.kind === "web" ? <Globe2 className="size-3.5" />
|
||||||
? <Image className="size-3.5" /> : <FileReferenceIcon kind={fileKindForPath(tab.value)} interactive={false} />}
|
: <FileReferenceIcon kind={fileKindForPath(tab.value)} className="size-3.5" />}
|
||||||
</span>
|
</span>
|
||||||
<span className="truncate">{name}</span>
|
<span className="truncate">{name}</span>
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -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(<FileReferenceIcon kind={fileKindForPath(path)} />);
|
||||||
|
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(<>
|
||||||
|
<FileReferenceChip path={path} onOpen={open} />
|
||||||
|
<PreviewPane tabs={[{ id: path, kind: "file", value: path }]} activeId={path}
|
||||||
|
width={544} isClosing={false} onSelect={() => {}} onCloseTab={() => {}}
|
||||||
|
onClose={() => {}} onResizeStart={() => {}}><p>Preview</p></PreviewPane>
|
||||||
|
<AttachmentTile attachment={{ kind: "file", name: path, url: `/download/${path}` }} />
|
||||||
|
</>);
|
||||||
|
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(<AttachmentTile attachment={{ kind, name: "Unavailable" }} />);
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user