11import { type TurnId } from "@t3tools/contracts" ;
22import { memo , useCallback , useEffect , useMemo , useState } from "react" ;
33import { type TurnDiffFileChange } from "../../types" ;
4- import {
5- buildTurnDiffTree ,
6- type TurnDiffTreeNode ,
7- } from "../../lib/turnDiffTree" ;
4+ import { buildTurnDiffTree , type TurnDiffTreeNode } from "../../lib/turnDiffTree" ;
85import { ChevronRightIcon , FolderIcon , FolderClosedIcon } from "lucide-react" ;
96import { cn } from "~/lib/utils" ;
107import { DiffStatLabel , hasNonZeroStat } from "./DiffStatLabel" ;
@@ -17,13 +14,7 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
1714 resolvedTheme : "light" | "dark" ;
1815 onOpenTurnDiff : ( turnId : TurnId , filePath ?: string ) => void ;
1916} ) {
20- const {
21- files,
22- allDirectoriesExpanded,
23- onOpenTurnDiff,
24- resolvedTheme,
25- turnId,
26- } = props ;
17+ const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, turnId } = props ;
2718 const treeNodes = useMemo ( ( ) => buildTurnDiffTree ( files ) , [ files ] ) ;
2819 const directoryPathsKey = useMemo (
2920 ( ) => collectDirectoryPaths ( treeNodes ) . join ( "\u0000" ) ,
@@ -37,27 +28,19 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
3728 ) ,
3829 [ allDirectoriesExpanded , directoryPathsKey ] ,
3930 ) ;
40- const [ expandedDirectories , setExpandedDirectories ] = useState <
41- Record < string , boolean >
42- > ( ( ) =>
43- buildDirectoryExpansionState (
44- directoryPathsKey ? directoryPathsKey . split ( "\u0000" ) : [ ] ,
45- true ,
46- ) ,
31+ const [ expandedDirectories , setExpandedDirectories ] = useState < Record < string , boolean > > ( ( ) =>
32+ buildDirectoryExpansionState ( directoryPathsKey ? directoryPathsKey . split ( "\u0000" ) : [ ] , true ) ,
4733 ) ;
4834 useEffect ( ( ) => {
4935 setExpandedDirectories ( allDirectoryExpansionState ) ;
5036 } , [ allDirectoryExpansionState ] ) ;
5137
52- const toggleDirectory = useCallback (
53- ( pathValue : string , fallbackExpanded : boolean ) => {
54- setExpandedDirectories ( ( current ) => ( {
55- ...current ,
56- [ pathValue ] : ! ( current [ pathValue ] ?? fallbackExpanded ) ,
57- } ) ) ;
58- } ,
59- [ ] ,
60- ) ;
38+ const toggleDirectory = useCallback ( ( pathValue : string , fallbackExpanded : boolean ) => {
39+ setExpandedDirectories ( ( current ) => ( {
40+ ...current ,
41+ [ pathValue ] : ! ( current [ pathValue ] ?? fallbackExpanded ) ,
42+ } ) ) ;
43+ } , [ ] ) ;
6144
6245 const renderTreeNode = ( node : TurnDiffTreeNode , depth : number ) => {
6346 const leftPadding = 8 + depth * 14 ;
@@ -88,18 +71,13 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
8871 </ span >
8972 { hasNonZeroStat ( node . stat ) && (
9073 < span className = "ml-auto shrink-0 font-mono text-[10px] tabular-nums" >
91- < DiffStatLabel
92- additions = { node . stat . additions }
93- deletions = { node . stat . deletions }
94- />
74+ < DiffStatLabel additions = { node . stat . additions } deletions = { node . stat . deletions } />
9575 </ span >
9676 ) }
9777 </ button >
9878 { isExpanded && (
9979 < div className = "space-y-0.5" >
100- { node . children . map ( ( childNode ) =>
101- renderTreeNode ( childNode , depth + 1 ) ,
102- ) }
80+ { node . children . map ( ( childNode ) => renderTreeNode ( childNode , depth + 1 ) ) }
10381 </ div >
10482 ) }
10583 </ div >
@@ -126,26 +104,17 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
126104 </ span >
127105 { node . stat && (
128106 < span className = "ml-auto shrink-0 font-mono text-[10px] tabular-nums" >
129- < DiffStatLabel
130- additions = { node . stat . additions }
131- deletions = { node . stat . deletions }
132- />
107+ < DiffStatLabel additions = { node . stat . additions } deletions = { node . stat . deletions } />
133108 </ span >
134109 ) }
135110 </ button >
136111 ) ;
137112 } ;
138113
139- return (
140- < div className = "space-y-0.5" >
141- { treeNodes . map ( ( node ) => renderTreeNode ( node , 0 ) ) }
142- </ div >
143- ) ;
114+ return < div className = "space-y-0.5" > { treeNodes . map ( ( node ) => renderTreeNode ( node , 0 ) ) } </ div > ;
144115} ) ;
145116
146- function collectDirectoryPaths (
147- nodes : ReadonlyArray < TurnDiffTreeNode > ,
148- ) : string [ ] {
117+ function collectDirectoryPaths ( nodes : ReadonlyArray < TurnDiffTreeNode > ) : string [ ] {
149118 const paths : string [ ] = [ ] ;
150119 for ( const node of nodes ) {
151120 if ( node . kind !== "directory" ) continue ;
0 commit comments