1- 'use client' ;
21
3- import { Loader2 } from "lucide-react" ;
42import { Separator } from "@/components/ui/separator" ;
53import { getRepoInfoByName } from "@/actions" ;
64import { PathHeader } from "@/app/[domain]/components/pathHeader" ;
7- import { useCallback , useRef } from "react" ;
8- import { FileTreeItem , getFolderContents } from "@/features/fileTree/actions" ;
9- import { FileTreeItemComponent } from "@/features/fileTree/components/fileTreeItemComponent" ;
10- import { useBrowseNavigation } from "../../hooks/useBrowseNavigation" ;
11- import { ScrollArea } from "@/components/ui/scroll-area" ;
12- import { unwrapServiceError } from "@/lib/utils" ;
13- import { useBrowseParams } from "../../hooks/useBrowseParams" ;
14- import { useDomain } from "@/hooks/useDomain" ;
15- import { useQuery } from "@tanstack/react-query" ;
16- import { usePrefetchFileSource } from "@/hooks/usePrefetchFileSource" ;
17- import { usePrefetchFolderContents } from "@/hooks/usePrefetchFolderContents" ;
18-
19- export const TreePreviewPanel = ( ) => {
20- const { path } = useBrowseParams ( ) ;
21- const { repoName, revisionName } = useBrowseParams ( ) ;
22- const domain = useDomain ( ) ;
23- const { navigateToPath } = useBrowseNavigation ( ) ;
24- const { prefetchFileSource } = usePrefetchFileSource ( ) ;
25- const { prefetchFolderContents } = usePrefetchFolderContents ( ) ;
26- const scrollAreaRef = useRef < HTMLDivElement > ( null ) ;
27-
28- const { data : repoInfoResponse , isPending : isRepoInfoPending , isError : isRepoInfoError } = useQuery ( {
29- queryKey : [ 'repoInfo' , repoName , domain ] ,
30- queryFn : ( ) => unwrapServiceError ( getRepoInfoByName ( repoName , domain ) ) ,
31- } ) ;
32-
33- const { data, isPending : isFolderContentsPending , isError : isFolderContentsError } = useQuery ( {
34- queryKey : [ 'tree' , repoName , revisionName , path , domain ] ,
35- queryFn : ( ) => unwrapServiceError (
36- getFolderContents ( {
37- repoName,
38- revisionName : revisionName ?? 'HEAD' ,
39- path,
40- } , domain )
41- ) ,
42- } ) ;
43-
44- const onNodeClicked = useCallback ( ( node : FileTreeItem ) => {
45- navigateToPath ( {
46- repoName : repoName ,
47- revisionName : revisionName ,
48- path : node . path ,
49- pathType : node . type === 'tree' ? 'tree' : 'blob' ,
50- } ) ;
51- } , [ navigateToPath , repoName , revisionName ] ) ;
52-
53- const onNodeMouseEnter = useCallback ( ( node : FileTreeItem ) => {
54- if ( node . type === 'blob' ) {
55- prefetchFileSource ( repoName , revisionName ?? 'HEAD' , node . path ) ;
56- } else if ( node . type === 'tree' ) {
57- prefetchFolderContents ( repoName , revisionName ?? 'HEAD' , node . path ) ;
58- }
59- } , [ prefetchFileSource , prefetchFolderContents , repoName , revisionName ] ) ;
60-
61- if ( isFolderContentsPending || isRepoInfoPending ) {
62- return (
63- < div className = "flex flex-col w-full min-h-full items-center justify-center" >
64- < Loader2 className = "w-4 h-4 animate-spin" />
65- Loading...
66- </ div >
67- )
68- }
69-
70- if ( isFolderContentsError || isRepoInfoError ) {
71- return < div > Error loading tree</ div >
5+ import { getFolderContents } from "@/features/fileTree/actions" ;
6+ import { isServiceError } from "@/lib/utils" ;
7+ import { PureTreePreviewPanel } from "./pureTreePreviewPanel" ;
8+
9+ interface TreePreviewPanelProps {
10+ path : string ;
11+ repoName : string ;
12+ revisionName ?: string ;
13+ domain : string ;
14+ }
15+
16+ export const TreePreviewPanel = async ( { path, repoName, revisionName, domain } : TreePreviewPanelProps ) => {
17+ const [ repoInfoResponse , folderContentsResponse ] = await Promise . all ( [
18+ getRepoInfoByName ( repoName , domain ) ,
19+ getFolderContents ( {
20+ repoName,
21+ revisionName : revisionName ?? 'HEAD' ,
22+ path,
23+ } , domain )
24+ ] ) ;
25+
26+ if ( isServiceError ( folderContentsResponse ) || isServiceError ( repoInfoResponse ) ) {
27+ return < div > Error loading tree preview</ div >
7228 }
7329
7430 return (
@@ -86,23 +42,7 @@ export const TreePreviewPanel = () => {
8642 />
8743 </ div >
8844 < Separator />
89- < ScrollArea
90- className = "flex flex-col p-0.5"
91- ref = { scrollAreaRef }
92- >
93- { data . map ( ( item ) => (
94- < FileTreeItemComponent
95- key = { item . path }
96- node = { item }
97- isActive = { false }
98- depth = { 0 }
99- isCollapseChevronVisible = { false }
100- onClick = { ( ) => onNodeClicked ( item ) }
101- onMouseEnter = { ( ) => onNodeMouseEnter ( item ) }
102- parentRef = { scrollAreaRef }
103- />
104- ) ) }
105- </ ScrollArea >
45+ < PureTreePreviewPanel items = { folderContentsResponse } />
10646 </ >
10747 )
10848}
0 commit comments