import React from "react"; import { useSelector } from "react-redux"; import { useFiles } from "#/context/files"; import { cn } from "#/utils/utils"; import { useListFiles } from "#/hooks/query/use-list-files"; import { useListFile } from "#/hooks/query/use-list-file"; import { Filename } from "./filename"; import { RootState } from "#/store"; interface TreeNodeProps { path: string; defaultOpen?: boolean; } function TreeNode({ path, defaultOpen = false }: TreeNodeProps) { const { setFileContent, setSelectedPath, files, selectedPath } = useFiles(); const [isOpen, setIsOpen] = React.useState(defaultOpen); const { curAgentState } = useSelector((state: RootState) => state.agent); const isDirectory = path.endsWith("/"); const { data: paths } = useListFiles({ path, enabled: isDirectory && isOpen, }); const { data: fileContent, refetch } = useListFile({ path }); React.useEffect(() => { if (fileContent) { if (fileContent !== files[path]) { setFileContent(path, fileContent); } } }, [fileContent, path]); React.useEffect(() => { if (selectedPath === path && !isDirectory) { refetch(); } }, [curAgentState, selectedPath, path, isDirectory]); const fileParts = path.split("/"); const filename = fileParts[fileParts.length - 1] || fileParts[fileParts.length - 2]; const handleClick = async () => { if (isDirectory) setIsOpen((prev) => !prev); else { setSelectedPath(path); await refetch(); } }; return (
{isOpen && paths && (
{paths.map((child, index) => ( ))}
)}
); } export default React.memo(TreeNode);