11import type { EnvironmentId , ProjectEntry } from "@t3tools/contracts" ;
22import { FileTree , useFileTree } from "@pierre/trees/react" ;
33import { RefreshCw , Search } from "lucide-react" ;
4- import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
4+ import { useEffect , useMemo , useRef } from "react" ;
55
6- import { readEnvironmentApi } from "~/environmentApi" ;
76import { useTheme } from "~/hooks/useTheme" ;
87import { cn } from "~/lib/utils" ;
98import { T3_PIERRE_ICONS } from "~/pierre-icons" ;
109
10+ import { useProjectEntriesQuery } from "./projectFilesQueryState" ;
11+
1112interface FileBrowserPanelProps {
1213 environmentId : EnvironmentId ;
1314 cwd : string ;
@@ -38,12 +39,15 @@ export default function FileBrowserPanel({
3839 onOpenFile,
3940} : FileBrowserPanelProps ) {
4041 const { resolvedTheme } = useTheme ( ) ;
41- const [ entries , setEntries ] = useState < readonly ProjectEntry [ ] > ( [ ] ) ;
42- const [ entriesError , setEntriesError ] = useState < string | null > ( null ) ;
43- const [ isLoadingEntries , setIsLoadingEntries ] = useState ( true ) ;
44- const [ isTruncated , setIsTruncated ] = useState ( false ) ;
45- const entryKindsRef = useRef ( new Map < string , ProjectEntry [ "kind" ] > ( ) ) ;
46- const requestGenerationRef = useRef ( 0 ) ;
42+ const entriesQuery = useProjectEntriesQuery ( environmentId , cwd ) ;
43+ const entries = entriesQuery . data ?. entries ?? [ ] ;
44+ const entryKinds = useMemo (
45+ ( ) => new Map ( entries . map ( ( entry ) => [ entry . path , entry . kind ] as const ) ) ,
46+ [ entries ] ,
47+ ) ;
48+ const entryKindsRef = useRef < ReadonlyMap < string , ProjectEntry [ "kind" ] > > ( entryKinds ) ;
49+ const treePaths = useMemo ( ( ) => entries . map ( treePath ) , [ entries ] ) ;
50+ const previousTreePathsRef = useRef < readonly string [ ] > ( [ ] ) ;
4751
4852 const { model } = useFileTree ( {
4953 density : "compact" ,
@@ -62,44 +66,12 @@ export default function FileBrowserPanel({
6266 unsafeCSS : TREE_UNSAFE_CSS ,
6367 } ) ;
6468
65- const loadEntries = useCallback ( ( ) => {
66- const api = readEnvironmentApi ( environmentId ) ;
67- requestGenerationRef . current += 1 ;
68- const generation = requestGenerationRef . current ;
69- setIsLoadingEntries ( true ) ;
70- setEntriesError ( null ) ;
71-
72- if ( ! api ) {
73- setEntriesError ( "Environment is not connected." ) ;
74- setIsLoadingEntries ( false ) ;
75- return ;
76- }
77-
78- void api . projects . listEntries ( { cwd } ) . then (
79- ( result ) => {
80- if ( generation !== requestGenerationRef . current ) return ;
81- entryKindsRef . current = new Map (
82- result . entries . map ( ( entry ) => [ entry . path , entry . kind ] as const ) ,
83- ) ;
84- setEntries ( result . entries ) ;
85- setIsTruncated ( result . truncated ) ;
86- model . resetPaths ( result . entries . map ( treePath ) ) ;
87- setIsLoadingEntries ( false ) ;
88- } ,
89- ( error : unknown ) => {
90- if ( generation !== requestGenerationRef . current ) return ;
91- setEntriesError ( error instanceof Error ? error . message : String ( error ) ) ;
92- setIsLoadingEntries ( false ) ;
93- } ,
94- ) ;
95- } , [ cwd , environmentId , model ] ) ;
96-
9769 useEffect ( ( ) => {
98- loadEntries ( ) ;
99- return ( ) => {
100- requestGenerationRef . current += 1 ;
101- } ;
102- } , [ loadEntries ] ) ;
70+ if ( previousTreePathsRef . current === treePaths ) return ;
71+ entryKindsRef . current = entryKinds ;
72+ previousTreePathsRef . current = treePaths ;
73+ model . resetPaths ( treePaths ) ;
74+ } , [ entryKinds , model , treePaths ] ) ;
10375
10476 const fileCount = useMemo (
10577 ( ) => entries . reduce ( ( count , entry ) => count + ( entry . kind === "file" ? 1 : 0 ) , 0 ) ,
@@ -112,8 +84,10 @@ export default function FileBrowserPanel({
11284 < div className = "min-w-0 flex-1" >
11385 < div className = "truncate text-xs font-medium text-foreground" > { projectName } </ div >
11486 < div className = "truncate text-[10px] leading-none text-muted-foreground" >
115- { isLoadingEntries ? "Indexing…" : `${ fileCount . toLocaleString ( ) } files` }
116- { isTruncated ? " · partial" : "" }
87+ { entriesQuery . isPending && entriesQuery . data === null
88+ ? "Indexing…"
89+ : `${ fileCount . toLocaleString ( ) } files` }
90+ { entriesQuery . data ?. truncated ? " · partial" : "" }
11791 </ div >
11892 </ div >
11993 < button
@@ -128,13 +102,13 @@ export default function FileBrowserPanel({
128102 type = "button"
129103 className = "rounded-md p-1.5 text-muted-foreground hover:bg-accent hover:text-foreground"
130104 aria-label = "Refresh workspace files"
131- onClick = { loadEntries }
105+ onClick = { entriesQuery . refresh }
132106 >
133- < RefreshCw className = { cn ( "size-3.5" , isLoadingEntries && "animate-spin" ) } />
107+ < RefreshCw className = { cn ( "size-3.5" , entriesQuery . isPending && "animate-spin" ) } />
134108 </ button >
135109 </ div >
136- { entriesError ? (
137- < div className = "p-4 text-xs leading-relaxed text-destructive" > { entriesError } </ div >
110+ { entriesQuery . error && entriesQuery . data === null ? (
111+ < div className = "p-4 text-xs leading-relaxed text-destructive" > { entriesQuery . error } </ div >
138112 ) : (
139113 < FileTree
140114 model = { model }
0 commit comments