@@ -3,9 +3,9 @@ import type {
33 ContextMenuOpenContext as TreeContextMenuOpenContext ,
44} from "@pierre/trees" ;
55import type { EnvironmentId , ProjectEntry } from "@t3tools/contracts" ;
6- import { FileTree , useFileTree , useFileTreeSearch } from "@pierre/trees/react" ;
6+ import { FileTree , useFileTree , useFileTreeSearch , useFileTreeSelector } from "@pierre/trees/react" ;
77import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger" ;
8- import { RotateCw } from "lucide-react" ;
8+ import { ChevronsDownUpIcon , ChevronsUpDownIcon , RotateCw } from "lucide-react" ;
99import { useEffect , useMemo , useRef } from "react" ;
1010
1111import { Button } from "~/components/ui/button" ;
@@ -21,6 +21,7 @@ import { readLocalApi } from "~/localApi";
2121import { T3_PIERRE_ICONS } from "~/pierre-icons" ;
2222
2323import { createFileTreeDragMentionController } from "./fileTreeDragMention" ;
24+ import { areAllDirectoriesExpanded , setAllDirectoriesExpanded } from "./fileTreeExpansion" ;
2425import { useProjectEntriesQuery } from "./projectFilesQueryState" ;
2526
2627interface FileBrowserPanelProps {
@@ -121,6 +122,10 @@ export default function FileBrowserPanel({
121122 ) ;
122123 const entryKindsRef = useRef < ReadonlyMap < string , ProjectEntry [ "kind" ] > > ( entryKinds ) ;
123124 const treePaths = useMemo ( ( ) => entries . map ( treePath ) , [ entries ] ) ;
125+ const directoryPaths = useMemo (
126+ ( ) => entries . filter ( ( entry ) => entry . kind === "directory" ) . map ( treePath ) ,
127+ [ entries ] ,
128+ ) ;
124129 const previousTreePathsRef = useRef < readonly string [ ] > ( [ ] ) ;
125130 const syncingSelectionRef = useRef ( false ) ;
126131 const treeSelectionPathRef = useRef < string | null > ( null ) ;
@@ -252,6 +257,12 @@ export default function FileBrowserPanel({
252257 unsafeCSS : TREE_UNSAFE_CSS ,
253258 } ) ;
254259 const search = useFileTreeSearch ( model ) ;
260+ const allDirectoriesExpanded = useFileTreeSelector ( model , ( currentModel ) =>
261+ areAllDirectoriesExpanded ( currentModel , directoryPaths ) ,
262+ ) ;
263+ const toggleAllDirectories = ( ) => {
264+ setAllDirectoriesExpanded ( model , directoryPaths , ! allDirectoriesExpanded ) ;
265+ } ;
255266 const handleSearchValueChange = ( value : string ) => {
256267 if ( value . trim ( ) . length === 0 ) {
257268 search . close ( ) ;
@@ -376,6 +387,32 @@ export default function FileBrowserPanel({
376387 onValueChange = { handleSearchValueChange }
377388 onClose = { search . close }
378389 />
390+ { directoryPaths . length > 0 ? (
391+ < Tooltip >
392+ < TooltipTrigger
393+ render = {
394+ < Button
395+ type = "button"
396+ size = "icon-xs"
397+ variant = "ghost"
398+ aria-label = {
399+ allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"
400+ }
401+ onClick = { toggleAllDirectories }
402+ />
403+ }
404+ >
405+ { allDirectoriesExpanded ? (
406+ < ChevronsDownUpIcon className = "size-3.5" />
407+ ) : (
408+ < ChevronsUpDownIcon className = "size-3.5" />
409+ ) }
410+ </ TooltipTrigger >
411+ < TooltipPopup >
412+ { allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders" }
413+ </ TooltipPopup >
414+ </ Tooltip >
415+ ) : null }
379416 </ div >
380417 { entriesQuery . error && entriesQuery . data === null ? (
381418 < div className = "p-4 text-xs leading-relaxed text-destructive" > { entriesQuery . error } </ div >
0 commit comments