2026-09-24 01:44:23 +08:00
|
|
|
import { useEffect, useState, type KeyboardEvent, type MouseEvent } from "react";
|
2026-09-24 03:44:25 +08:00
|
|
|
import { File, FileCode2, FileImage, FileJson2, FileText, FileVideo2, type LucideIcon } from "lucide-react";
|
2026-09-24 01:44:23 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { FileActions, useFilePreviewLoader } from "@/components/FileActions";
|
|
|
|
|
import { inferMediaKind } from "@/lib/media";
|
2026-06-06 19:49:33 +08:00
|
|
|
|
2026-05-17 23:52:14 +08:00
|
|
|
import {
|
|
|
|
|
Tooltip,
|
|
|
|
|
TooltipContent,
|
|
|
|
|
TooltipProvider,
|
|
|
|
|
TooltipTrigger,
|
|
|
|
|
} from "@/components/ui/tooltip";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
export type FileReferenceKind =
|
2026-05-17 23:52:14 +08:00
|
|
|
| "default"
|
|
|
|
|
| "css"
|
|
|
|
|
| "html"
|
2026-09-24 01:44:23 +08:00
|
|
|
| "image"
|
2026-06-06 19:49:33 +08:00
|
|
|
| "javascript"
|
2026-05-17 23:52:14 +08:00
|
|
|
| "json"
|
|
|
|
|
| "markdown"
|
|
|
|
|
| "notebook"
|
|
|
|
|
| "python"
|
|
|
|
|
| "react"
|
2026-09-24 03:44:25 +08:00
|
|
|
| "text"
|
|
|
|
|
| "typescript"
|
|
|
|
|
| "video";
|
2026-05-17 23:52:14 +08:00
|
|
|
|
|
|
|
|
interface FileReferenceChipProps {
|
|
|
|
|
path: string;
|
2026-05-18 19:10:50 +08:00
|
|
|
tooltipPath?: string;
|
2026-05-17 23:52:14 +08:00
|
|
|
display?: "name" | "path";
|
|
|
|
|
active?: boolean;
|
|
|
|
|
className?: string;
|
|
|
|
|
textClassName?: string;
|
2026-06-06 19:49:33 +08:00
|
|
|
previewPath?: string;
|
|
|
|
|
onOpen?: (path: string) => void;
|
2026-05-17 23:52:14 +08:00
|
|
|
testId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FileReferenceChip({
|
|
|
|
|
path,
|
2026-05-18 19:10:50 +08:00
|
|
|
tooltipPath,
|
2026-05-17 23:52:14 +08:00
|
|
|
display = "name",
|
|
|
|
|
active = false,
|
|
|
|
|
className,
|
|
|
|
|
textClassName,
|
2026-06-06 19:49:33 +08:00
|
|
|
previewPath,
|
|
|
|
|
onOpen,
|
2026-05-17 23:52:14 +08:00
|
|
|
testId = "inline-file-path",
|
|
|
|
|
}: FileReferenceChipProps) {
|
2026-09-24 01:44:23 +08:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [tooltipOpen, setTooltipOpen] = useState(false);
|
2026-05-18 19:10:50 +08:00
|
|
|
const { directory, name } = splitFilePath(path);
|
2026-05-17 23:52:14 +08:00
|
|
|
const displayText = display === "path" ? path.replace(/\\/g, "/") : name;
|
2026-05-18 19:10:50 +08:00
|
|
|
const fullPath = tooltipPath || path;
|
2026-06-06 19:49:33 +08:00
|
|
|
const targetPath = previewPath || tooltipPath || path;
|
2026-09-24 01:44:23 +08:00
|
|
|
const kind = fileKindForPath(targetPath);
|
2026-06-06 19:49:33 +08:00
|
|
|
const interactive = Boolean(onOpen);
|
2026-09-24 01:44:23 +08:00
|
|
|
const imageQuickLook = kind === "image" && interactive;
|
2026-06-06 19:49:33 +08:00
|
|
|
const openPreview = (event: MouseEvent | KeyboardEvent) => {
|
|
|
|
|
if (!onOpen) return;
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2026-09-24 01:44:23 +08:00
|
|
|
setTooltipOpen(false);
|
2026-06-06 19:49:33 +08:00
|
|
|
onOpen(targetPath);
|
|
|
|
|
};
|
|
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (event.key !== "Enter" && event.key !== " ") return;
|
|
|
|
|
openPreview(event);
|
|
|
|
|
};
|
2026-05-17 23:52:14 +08:00
|
|
|
return (
|
2026-09-24 01:44:23 +08:00
|
|
|
<FileActions path={targetPath}>
|
2026-09-10 11:10:51 +08:00
|
|
|
<TooltipProvider>
|
2026-09-24 01:44:23 +08:00
|
|
|
<Tooltip open={tooltipOpen} onOpenChange={setTooltipOpen}>
|
2026-05-17 23:52:14 +08:00
|
|
|
<TooltipTrigger asChild>
|
|
|
|
|
<span
|
2026-09-24 01:44:23 +08:00
|
|
|
onContextMenuCapture={() => setTooltipOpen(false)}
|
2026-05-18 19:10:50 +08:00
|
|
|
className={cn("not-prose inline-flex max-w-full align-baseline leading-[inherit]", className)}
|
2026-05-17 23:52:14 +08:00
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
data-testid={testId}
|
2026-05-18 19:10:50 +08:00
|
|
|
aria-label={fullPath}
|
2026-06-06 19:49:33 +08:00
|
|
|
role={interactive ? "button" : undefined}
|
|
|
|
|
tabIndex={interactive ? 0 : undefined}
|
|
|
|
|
onClick={interactive ? openPreview : undefined}
|
|
|
|
|
onKeyDown={interactive ? onKeyDown : undefined}
|
2026-05-17 23:52:14 +08:00
|
|
|
className={cn(
|
2026-05-24 17:53:56 +08:00
|
|
|
"inline-flex max-w-full items-baseline gap-[0.28em] font-medium leading-[inherit]",
|
2026-09-21 17:48:49 +08:00
|
|
|
interactive
|
|
|
|
|
? "text-sky-600 transition-colors hover:text-sky-700 dark:text-sky-300 dark:hover:text-sky-200"
|
|
|
|
|
: "text-muted-foreground",
|
2026-06-06 19:49:33 +08:00
|
|
|
interactive && [
|
2026-08-14 07:07:09 +09:00
|
|
|
"cursor-pointer rounded-compact",
|
2026-06-06 19:49:33 +08:00
|
|
|
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-400/45",
|
|
|
|
|
],
|
2026-05-17 23:52:14 +08:00
|
|
|
)}
|
|
|
|
|
>
|
2026-09-24 03:44:25 +08:00
|
|
|
<FileReferenceIcon kind={kind} className="translate-y-[0.12em]" />
|
2026-05-17 23:52:14 +08:00
|
|
|
<span
|
|
|
|
|
data-sheen-text={active ? displayText : undefined}
|
|
|
|
|
className={cn(
|
2026-07-05 06:48:24 +08:00
|
|
|
"min-w-0 max-w-full [overflow-wrap:anywhere] sm:truncate",
|
2026-05-18 19:10:50 +08:00
|
|
|
active && "streaming-text-sheen file-reference-sheen",
|
2026-05-17 23:52:14 +08:00
|
|
|
textClassName,
|
|
|
|
|
)}
|
|
|
|
|
>
|
2026-05-18 19:10:50 +08:00
|
|
|
{display === "path" && directory ? (
|
|
|
|
|
<>
|
2026-10-15 15:21:28 +08:00
|
|
|
<span className="text-muted-foreground">{directory}</span>
|
2026-09-21 17:48:49 +08:00
|
|
|
<span className={cn("font-semibold", interactive && "text-sky-700 dark:text-sky-200")}>{name}</span>
|
2026-05-18 19:10:50 +08:00
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
displayText
|
|
|
|
|
)}
|
2026-05-17 23:52:14 +08:00
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
</TooltipTrigger>
|
|
|
|
|
<TooltipContent
|
2026-09-24 01:44:23 +08:00
|
|
|
aria-label={imageQuickLook ? t("filePreview.aria") : undefined}
|
2026-05-17 23:52:14 +08:00
|
|
|
side="top"
|
|
|
|
|
align="center"
|
|
|
|
|
sideOffset={8}
|
|
|
|
|
collisionPadding={12}
|
|
|
|
|
className={cn(
|
2026-09-24 01:44:23 +08:00
|
|
|
"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",
|
2026-05-17 23:52:14 +08:00
|
|
|
)}
|
|
|
|
|
>
|
2026-09-24 01:44:23 +08:00
|
|
|
{imageQuickLook
|
|
|
|
|
? tooltipOpen ? <FileImageQuickLook key={targetPath} path={targetPath} /> : null
|
|
|
|
|
: fullPath}
|
2026-05-17 23:52:14 +08:00
|
|
|
</TooltipContent>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</TooltipProvider>
|
2026-09-23 14:03:37 +08:00
|
|
|
</FileActions>
|
2026-05-17 23:52:14 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 01:44:23 +08:00
|
|
|
/** 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>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 23:52:14 +08:00
|
|
|
export function isLikelyFilePath(value: string): boolean {
|
|
|
|
|
const raw = value.trim();
|
|
|
|
|
if (!raw || raw.includes("\n")) return false;
|
|
|
|
|
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) return false;
|
2026-06-06 19:49:33 +08:00
|
|
|
if (isFilePatternReference(raw)) return false;
|
2026-05-17 23:52:14 +08:00
|
|
|
if (!/[\\/]/.test(raw) && !/^(dockerfile|makefile|readme|package-lock\.json)$/i.test(raw)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const normalized = raw.replace(/\\/g, "/");
|
|
|
|
|
const name = normalized.split("/").filter(Boolean).pop() ?? normalized;
|
|
|
|
|
if (!name || name === "." || name === "..") return false;
|
|
|
|
|
if (/^(dockerfile|makefile|readme|package-lock\.json)$/i.test(name)) return true;
|
|
|
|
|
return /\.[a-z0-9][a-z0-9_-]{0,12}$/i.test(name);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-06 19:49:33 +08:00
|
|
|
export function isFilePatternReference(value: string): boolean {
|
|
|
|
|
return /[*?[\]{}]/.test(value.trim());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function splitFilePath(path: string): { directory: string; name: string } {
|
2026-05-17 23:52:14 +08:00
|
|
|
const normalized = path.replace(/\\/g, "/");
|
|
|
|
|
const slash = normalized.lastIndexOf("/");
|
|
|
|
|
if (slash < 0) return { directory: "", name: path };
|
|
|
|
|
return {
|
|
|
|
|
directory: normalized.slice(0, slash + 1),
|
|
|
|
|
name: normalized.slice(slash + 1) || normalized,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 01:44:23 +08:00
|
|
|
export function fileKindForPath(path: string): FileReferenceKind {
|
2026-09-24 03:44:25 +08:00
|
|
|
const mediaKind = inferMediaKind({ url: path });
|
|
|
|
|
if (mediaKind === "image" || mediaKind === "video") return mediaKind;
|
2026-05-17 23:52:14 +08:00
|
|
|
const normalized = path.toLowerCase();
|
|
|
|
|
const name = normalized.split(/[\\/]/).pop() ?? normalized;
|
|
|
|
|
const ext = name.includes(".") ? name.split(".").pop() ?? "" : "";
|
|
|
|
|
if (name === "dockerfile") {
|
|
|
|
|
return "default";
|
|
|
|
|
}
|
|
|
|
|
switch (ext) {
|
2026-09-24 03:44:25 +08:00
|
|
|
case "avif":
|
|
|
|
|
return "image";
|
2026-05-17 23:52:14 +08:00
|
|
|
case "py":
|
|
|
|
|
case "pyi":
|
|
|
|
|
return "python";
|
|
|
|
|
case "jsx":
|
|
|
|
|
case "tsx":
|
|
|
|
|
return "react";
|
2026-06-06 19:49:33 +08:00
|
|
|
case "js":
|
|
|
|
|
case "mjs":
|
|
|
|
|
case "cjs":
|
|
|
|
|
return "javascript";
|
2026-05-17 23:52:14 +08:00
|
|
|
case "ts":
|
2026-06-06 19:49:33 +08:00
|
|
|
case "mts":
|
|
|
|
|
case "cts":
|
2026-05-17 23:52:14 +08:00
|
|
|
return "typescript";
|
|
|
|
|
case "html":
|
|
|
|
|
case "htm":
|
|
|
|
|
return "html";
|
|
|
|
|
case "css":
|
|
|
|
|
case "scss":
|
|
|
|
|
case "sass":
|
|
|
|
|
return "css";
|
|
|
|
|
case "json":
|
|
|
|
|
case "jsonl":
|
|
|
|
|
return "json";
|
|
|
|
|
case "md":
|
|
|
|
|
case "mdx":
|
|
|
|
|
return "markdown";
|
|
|
|
|
case "ipynb":
|
|
|
|
|
return "notebook";
|
2026-09-24 03:44:25 +08:00
|
|
|
case "txt":
|
|
|
|
|
case "log":
|
|
|
|
|
return "text";
|
2026-05-17 23:52:14 +08:00
|
|
|
default:
|
|
|
|
|
return "default";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 03:44:25 +08:00
|
|
|
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<FileReferenceKind, LucideIcon>;
|
2026-05-17 23:52:14 +08:00
|
|
|
|
2026-09-24 03:44:25 +08:00
|
|
|
/** 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 <Icon aria-hidden strokeWidth={1.75} className={cn("h-[1em] w-[1em] shrink-0", className)} />;
|
2026-05-17 23:52:14 +08:00
|
|
|
}
|