feat(webui): polish tabbed previews and image presentation
This commit is contained in:
1 parent
fccf415283
commit
b555164a17
41 files changed
+1459
-621
No files matched your search
@@ -1,5 +1,8 @@
|
||||
import type { KeyboardEvent, MouseEvent } from "react";
|
||||
import { FileActions } from "@/components/FileActions";
|
||||
import { useEffect, useState, type KeyboardEvent, type MouseEvent } from "react";
|
||||
import { ImageIcon } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { FileActions, useFilePreviewLoader } from "@/components/FileActions";
|
||||
import { inferMediaKind } from "@/lib/media";
|
||||
|
||||
import {
|
||||
Tooltip,
|
||||
@@ -13,6 +16,7 @@ export type FileReferenceKind =
|
||||
| "default"
|
||||
| "css"
|
||||
| "html"
|
||||
| "image"
|
||||
| "javascript"
|
||||
| "json"
|
||||
| "markdown"
|
||||
@@ -44,16 +48,20 @@ export function FileReferenceChip({
|
||||
onOpen,
|
||||
testId = "inline-file-path",
|
||||
}: FileReferenceChipProps) {
|
||||
const { t } = useTranslation();
|
||||
const [tooltipOpen, setTooltipOpen] = useState(false);
|
||||
const { directory, name } = splitFilePath(path);
|
||||
const kind = fileKindForPath(path);
|
||||
const displayText = display === "path" ? path.replace(/\\/g, "/") : name;
|
||||
const fullPath = tooltipPath || path;
|
||||
const targetPath = previewPath || tooltipPath || path;
|
||||
const kind = fileKindForPath(targetPath);
|
||||
const interactive = Boolean(onOpen);
|
||||
const imageQuickLook = kind === "image" && interactive;
|
||||
const openPreview = (event: MouseEvent | KeyboardEvent) => {
|
||||
if (!onOpen) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
setTooltipOpen(false);
|
||||
onOpen(targetPath);
|
||||
};
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
@@ -61,11 +69,12 @@ export function FileReferenceChip({
|
||||
openPreview(event);
|
||||
};
|
||||
return (
|
||||
<FileActions path={targetPath} onPreview={onOpen}>
|
||||
<FileActions path={targetPath}>
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<Tooltip open={tooltipOpen} onOpenChange={setTooltipOpen}>
|
||||
<TooltipTrigger asChild>
|
||||
<span
|
||||
onContextMenuCapture={() => setTooltipOpen(false)}
|
||||
className={cn("not-prose inline-flex max-w-full align-baseline leading-[inherit]", className)}
|
||||
>
|
||||
<span
|
||||
@@ -108,17 +117,21 @@ export function FileReferenceChip({
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent
|
||||
aria-label={imageQuickLook ? t("filePreview.aria") : undefined}
|
||||
side="top"
|
||||
align="center"
|
||||
sideOffset={8}
|
||||
collisionPadding={12}
|
||||
className={cn(
|
||||
"max-w-[min(38rem,calc(100vw-2rem))] rounded-control",
|
||||
"px-2.5 py-1.5",
|
||||
"break-all font-mono text-[11px] leading-snug text-popover-foreground",
|
||||
"rounded-control text-popover-foreground",
|
||||
imageQuickLook
|
||||
? "w-max max-w-[calc(100vw-2rem)] p-2"
|
||||
: "max-w-[min(38rem,calc(100vw-2rem))] break-all px-2.5 py-1.5 font-mono text-[11px] leading-snug",
|
||||
)}
|
||||
>
|
||||
{fullPath}
|
||||
{imageQuickLook
|
||||
? tooltipOpen ? <FileImageQuickLook key={targetPath} path={targetPath} /> : null
|
||||
: fullPath}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
@@ -126,6 +139,29 @@ export function FileReferenceChip({
|
||||
);
|
||||
}
|
||||
|
||||
/** Only loads after the normal hover/focus delay, through the session's guarded preview API. */
|
||||
function FileImageQuickLook({ path }: { path: string }) {
|
||||
const { t } = useTranslation();
|
||||
const load = useFilePreviewLoader();
|
||||
const [result, setResult] = useState<{ load: typeof load; source: string | null }>();
|
||||
const source = result?.load === load ? result?.source : undefined;
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
void load?.(path).then((payload) => {
|
||||
if (!cancelled) setResult({ load, source: payload.kind === "image" ? payload.data_url : null });
|
||||
}).catch(() => { if (!cancelled) setResult({ load, source: null }); });
|
||||
return () => { cancelled = true; };
|
||||
}, [path, load]);
|
||||
if (!load || source === null) return <span className="block px-1 py-1 text-xs text-muted-foreground">
|
||||
{t("filePreview.failed", { defaultValue: "Could not preview this file." })}
|
||||
</span>;
|
||||
return <span data-testid="file-image-quick-look" className="block w-max max-w-full overflow-hidden rounded-mark" aria-hidden>
|
||||
{source ? <img src={source} alt="" decoding="async" draggable={false}
|
||||
className="block h-auto max-h-36 w-auto max-w-[min(14rem,calc(100vw-3rem))] rounded-mark object-contain" onError={() => setResult({ load, source: null })} />
|
||||
: <span className="block h-36 w-56 max-w-[calc(100vw-3rem)] animate-pulse bg-muted/40 motion-reduce:animate-none" />}
|
||||
</span>;
|
||||
}
|
||||
|
||||
export function isLikelyFilePath(value: string): boolean {
|
||||
const raw = value.trim();
|
||||
if (!raw || raw.includes("\n")) return false;
|
||||
@@ -155,7 +191,8 @@ export function splitFilePath(path: string): { directory: string; name: string }
|
||||
};
|
||||
}
|
||||
|
||||
function fileKindForPath(path: string): FileReferenceKind {
|
||||
export function fileKindForPath(path: string): FileReferenceKind {
|
||||
if (inferMediaKind({ url: path }) === "image") return "image";
|
||||
const normalized = path.toLowerCase();
|
||||
const name = normalized.split(/[\\/]/).pop() ?? normalized;
|
||||
const ext = name.includes(".") ? name.split(".").pop() ?? "" : "";
|
||||
@@ -197,7 +234,8 @@ function fileKindForPath(path: string): FileReferenceKind {
|
||||
}
|
||||
}
|
||||
|
||||
function FileReferenceIcon({ kind, interactive }: { kind: FileReferenceKind; interactive: boolean }) {
|
||||
export function FileReferenceIcon({ kind, interactive }: { kind: FileReferenceKind; interactive: boolean }) {
|
||||
if (kind === "image") return <ImageIcon aria-hidden className="h-[1em] w-[1em] shrink-0 translate-y-[0.12em]" />;
|
||||
if (kind === "python") {
|
||||
return (
|
||||
<svg
|
||||
|
||||
Reference in new issue
Block a user