Files
deer-flow/frontend/src/core/artifacts/hooks.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-01-17 15:09:44 +08:00
import { useQuery } from "@tanstack/react-query";
2026-01-18 17:13:15 +08:00
import { useMemo } from "react";
import { useThread } from "@/components/workspace/messages/context";
2026-01-17 15:09:44 +08:00
2026-01-19 19:41:46 +08:00
import { loadArtifactContent, loadArtifactContentFromToolCall } from "./loader";
2026-01-17 15:09:44 +08:00
export function useArtifactContent({
filepath,
threadId,
enabled,
}: {
filepath: string;
threadId: string;
enabled?: boolean;
}) {
2026-01-18 17:13:15 +08:00
const isWriteFile = useMemo(() => {
return filepath.startsWith("write-file:");
}, [filepath]);
const { thread } = useThread();
const content = useMemo(() => {
if (isWriteFile) {
2026-01-19 19:41:46 +08:00
return loadArtifactContentFromToolCall({ url: filepath, thread });
2026-01-18 17:13:15 +08:00
}
return null;
2026-01-19 19:41:46 +08:00
}, [filepath, isWriteFile, thread]);
2026-01-17 15:09:44 +08:00
const { data, isLoading, error } = useQuery({
queryKey: ["artifact", filepath, threadId],
2026-01-18 17:13:15 +08:00
queryFn: () => {
return loadArtifactContent({ filepath, threadId });
},
2026-01-17 15:09:44 +08:00
enabled,
// Cache artifact content for 5 minutes to avoid repeated fetches (especially for .skill ZIP extraction)
staleTime: 5 * 60 * 1000,
2026-01-17 15:09:44 +08:00
});
2026-01-18 17:13:15 +08:00
return { content: isWriteFile ? content : data, isLoading, error };
2026-01-17 15:09:44 +08:00
}