'use client'; import { Loader2 } from "lucide-react"; import { Separator } from "@/components/ui/separator"; import { getRepoInfoByName } from "@/actions"; import { PathHeader } from "@/app/[domain]/components/pathHeader"; import { useCallback, useRef } from "react"; import { FileTreeItem, getFolderContents } from "@/features/fileTree/actions"; import { FileTreeItemComponent } from "@/features/fileTree/components/fileTreeItemComponent"; import { useBrowseNavigation } from "../../hooks/useBrowseNavigation"; import { ScrollArea } from "@/components/ui/scroll-area"; import { unwrapServiceError } from "@/lib/utils"; import { useBrowseParams } from "../../hooks/useBrowseParams"; import { useDomain } from "@/hooks/useDomain"; import { useQuery } from "@tanstack/react-query"; import { usePrefetchFileSource } from "@/hooks/usePrefetchFileSource"; import { usePrefetchFolderContents } from "@/hooks/usePrefetchFolderContents"; export const TreePreviewPanel = () => { const { path } = useBrowseParams(); const { repoName, revisionName } = useBrowseParams(); const domain = useDomain(); const { navigateToPath } = useBrowseNavigation(); const { prefetchFileSource } = usePrefetchFileSource(); const { prefetchFolderContents } = usePrefetchFolderContents(); const scrollAreaRef = useRef(null); const { data: repoInfoResponse, isPending: isRepoInfoPending, isError: isRepoInfoError } = useQuery({ queryKey: ['repoInfo', repoName, domain], queryFn: () => unwrapServiceError(getRepoInfoByName(repoName, domain)), }); const { data, isPending: isFolderContentsPending, isError: isFolderContentsError } = useQuery({ queryKey: ['tree', repoName, revisionName, path, domain], queryFn: () => unwrapServiceError( getFolderContents({ repoName, revisionName: revisionName ?? 'HEAD', path, }, domain) ), }); const onNodeClicked = useCallback((node: FileTreeItem) => { navigateToPath({ repoName: repoName, revisionName: revisionName, path: node.path, pathType: node.type === 'tree' ? 'tree' : 'blob', }); }, [navigateToPath, repoName, revisionName]); const onNodeMouseEnter = useCallback((node: FileTreeItem) => { if (node.type === 'blob') { prefetchFileSource(repoName, revisionName ?? 'HEAD', node.path); } else if (node.type === 'tree') { prefetchFolderContents(repoName, revisionName ?? 'HEAD', node.path); } }, [prefetchFileSource, prefetchFolderContents, repoName, revisionName]); if (isFolderContentsPending || isRepoInfoPending) { return (
Loading...
) } if (isFolderContentsError || isRepoInfoError) { return
Error loading tree
} return ( <>
{data.map((item) => ( onNodeClicked(item)} onMouseEnter={() => onNodeMouseEnter(item)} parentRef={scrollAreaRef} /> ))} ) }