2026-01-17 15:09:44 +08:00
|
|
|
import { CopyIcon, DownloadIcon, XIcon } from "lucide-react";
|
|
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { toast } from "sonner";
|
2026-01-17 00:05:19 +08:00
|
|
|
|
2026-01-17 15:09:44 +08:00
|
|
|
import {
|
|
|
|
|
Artifact,
|
|
|
|
|
ArtifactAction,
|
|
|
|
|
ArtifactActions,
|
|
|
|
|
ArtifactContent,
|
|
|
|
|
ArtifactDescription,
|
|
|
|
|
ArtifactHeader,
|
|
|
|
|
ArtifactTitle,
|
|
|
|
|
} from "@/components/ai-elements/artifact";
|
|
|
|
|
import { useArtifactContent } from "@/core/artifacts/hooks";
|
|
|
|
|
import { urlOfArtifact } from "@/core/artifacts/utils";
|
|
|
|
|
import {
|
|
|
|
|
checkCodeFile,
|
|
|
|
|
getFileExtensionDisplayName,
|
|
|
|
|
getFileName,
|
|
|
|
|
} from "@/core/utils/files";
|
2026-01-17 11:02:33 +08:00
|
|
|
import { cn } from "@/lib/utils";
|
2026-01-17 00:05:19 +08:00
|
|
|
|
2026-01-17 15:09:44 +08:00
|
|
|
import { useArtifacts } from "./context";
|
|
|
|
|
import { FileViewer } from "./file-viewer";
|
|
|
|
|
|
2026-01-17 11:02:33 +08:00
|
|
|
export function ArtifactFileDetail({
|
|
|
|
|
className,
|
|
|
|
|
filepath,
|
2026-01-17 15:09:44 +08:00
|
|
|
threadId,
|
2026-01-17 11:02:33 +08:00
|
|
|
}: {
|
|
|
|
|
className?: string;
|
|
|
|
|
filepath: string;
|
2026-01-17 15:09:44 +08:00
|
|
|
threadId: string;
|
2026-01-17 11:02:33 +08:00
|
|
|
}) {
|
2026-01-17 15:09:44 +08:00
|
|
|
const { setOpen } = useArtifacts();
|
|
|
|
|
const { isCodeFile } = useMemo(() => checkCodeFile(filepath), [filepath]);
|
|
|
|
|
const { content } = useArtifactContent({
|
|
|
|
|
threadId,
|
|
|
|
|
filepath,
|
|
|
|
|
enabled: isCodeFile,
|
|
|
|
|
});
|
2026-01-17 00:02:03 +08:00
|
|
|
return (
|
2026-01-17 15:09:44 +08:00
|
|
|
<Artifact className={cn("rounded-none", className)}>
|
|
|
|
|
<ArtifactHeader>
|
2026-01-17 00:02:03 +08:00
|
|
|
<div>
|
2026-01-17 15:09:44 +08:00
|
|
|
<ArtifactTitle>{getFileName(filepath)}</ArtifactTitle>
|
|
|
|
|
<ArtifactDescription className="mt-1 text-xs">
|
|
|
|
|
{getFileExtensionDisplayName(filepath)} file
|
|
|
|
|
</ArtifactDescription>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<ArtifactActions>
|
|
|
|
|
{isCodeFile && (
|
|
|
|
|
<ArtifactAction
|
|
|
|
|
icon={CopyIcon}
|
|
|
|
|
label="Copy"
|
|
|
|
|
disabled={!content}
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await navigator.clipboard.writeText(content ?? "");
|
|
|
|
|
toast.success("Copied to clipboard");
|
|
|
|
|
} catch (error) {
|
|
|
|
|
toast.error("Failed to copy to clipboard");
|
|
|
|
|
console.error(error);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
tooltip="Copy content to clipboard"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<a
|
|
|
|
|
href={urlOfArtifact({ filepath, threadId, download: true })}
|
|
|
|
|
target="_blank"
|
|
|
|
|
>
|
|
|
|
|
<ArtifactAction
|
|
|
|
|
icon={DownloadIcon}
|
|
|
|
|
label="Download"
|
|
|
|
|
onClick={() => console.log("Download")}
|
|
|
|
|
tooltip="Download file"
|
|
|
|
|
/>
|
|
|
|
|
</a>
|
|
|
|
|
<ArtifactAction
|
|
|
|
|
icon={XIcon}
|
|
|
|
|
label="Close"
|
|
|
|
|
onClick={() => setOpen(false)}
|
|
|
|
|
tooltip="Close"
|
|
|
|
|
/>
|
|
|
|
|
</ArtifactActions>
|
2026-01-17 00:02:03 +08:00
|
|
|
</div>
|
2026-01-17 15:09:44 +08:00
|
|
|
</ArtifactHeader>
|
|
|
|
|
<ArtifactContent className="p-0">
|
|
|
|
|
<FileViewer
|
|
|
|
|
className="size-full"
|
|
|
|
|
threadId={threadId}
|
|
|
|
|
filepath={filepath}
|
|
|
|
|
/>
|
|
|
|
|
</ArtifactContent>
|
|
|
|
|
</Artifact>
|
2026-01-17 00:02:03 +08:00
|
|
|
);
|
|
|
|
|
}
|