@@ -5,15 +5,21 @@ import type { TreeDataItem } from '@/components/ui/tree-view'
5
5
6
6
import { TreeView } from '@/components/ui/tree-view'
7
7
8
+ type FileTreeItem = TreeDataItem & {
9
+ contents : string
10
+ }
11
+
8
12
export default function FileTree ( {
9
13
prefix,
10
14
tree,
11
15
originalTree,
16
+ localTree,
12
17
onFileSelected,
13
18
} : {
14
19
prefix : string
15
20
tree : Record < string , string >
16
21
originalTree : Record < string , string >
22
+ localTree : Record < string , string >
17
23
onFileSelected : ( file : string ) => void
18
24
} ) {
19
25
const computedTree = useMemo ( ( ) => {
@@ -33,43 +39,69 @@ export default function FileTree({
33
39
return tree [ file ] !== originalTree [ file ]
34
40
}
35
41
36
- Object . keys ( tree )
37
- . sort ( )
38
- . forEach ( ( file ) => {
39
- const parts = file . split ( '/' )
42
+ const allFileSet = Array . from (
43
+ new Set ( [
44
+ ...Object . keys ( tree ) ,
45
+ ...Object . keys ( localTree ) ,
46
+ ...Object . keys ( originalTree ) ,
47
+ ] ) ,
48
+ )
49
+
50
+ allFileSet . sort ( ) . forEach ( ( file ) => {
51
+ const parts = file . split ( '/' )
40
52
41
- let currentLevel = treeData
42
- parts . forEach ( ( part , index ) => {
43
- const existingNode = currentLevel . find ( ( node ) => node . name === part )
44
- if ( existingNode ) {
45
- currentLevel = existingNode . children || [ ]
46
- } else {
47
- const newNode : TreeDataItem = {
48
- id : index === parts . length - 1 ? file : `${ file } -${ index } ` ,
49
- name : part ,
50
- children : index < parts . length - 1 ? [ ] : undefined ,
51
- icon :
52
- index < parts . length - 1
53
- ? ( ) => < Folder className = "w-4 h-4 mr-2" />
54
- : ( ) => < FileText className = "w-4 h-4 mr-2" /> ,
55
- onClick :
56
- index === parts . length - 1
57
- ? ( ) => {
58
- onFileSelected ( file )
59
- }
60
- : undefined ,
61
- className :
62
- index === parts . length - 1 && changed ( file )
63
- ? 'text-green-300'
64
- : '' ,
53
+ let currentLevel = treeData
54
+ parts . forEach ( ( part , index ) => {
55
+ const existingNode = currentLevel . find ( ( node ) => node . name === part )
56
+ if ( existingNode ) {
57
+ currentLevel = existingNode . children || [ ]
58
+ } else {
59
+ let color = ''
60
+ if ( parts . length - 1 ) {
61
+ if ( localTree [ file ] ) {
62
+ if ( tree [ file ] ) {
63
+ if ( localTree [ file ] !== tree [ file ] ) {
64
+ if (
65
+ originalTree [ file ] &&
66
+ localTree [ file ] !== originalTree [ file ]
67
+ ) {
68
+ color = 'text-red-500 font-bold'
69
+ } else {
70
+ color = 'text-green-500 font-bold'
71
+ }
72
+ }
73
+ } else {
74
+ color = 'text-blue-500 font-bold'
75
+ }
76
+ } else {
77
+ color = changed ( file ) ? 'text-green-500 font-bold' : ''
65
78
}
66
- currentLevel . push ( newNode )
67
- currentLevel = newNode . children !
68
79
}
69
- } )
80
+
81
+ const newNode : FileTreeItem = {
82
+ id : index === parts . length - 1 ? file : `${ file } -${ index } ` ,
83
+ name : part ,
84
+ children : index < parts . length - 1 ? [ ] : undefined ,
85
+ icon :
86
+ index < parts . length - 1
87
+ ? ( ) => < Folder className = "w-4 h-4 mr-2" />
88
+ : ( ) => < FileText className = "w-4 h-4 mr-2" /> ,
89
+ onClick :
90
+ index === parts . length - 1
91
+ ? ( ) => {
92
+ onFileSelected ( file )
93
+ }
94
+ : undefined ,
95
+ className : color ,
96
+ contents : tree [ file ] || localTree [ file ] || originalTree [ file ] ,
97
+ }
98
+ currentLevel . push ( newNode )
99
+ currentLevel = newNode . children !
100
+ }
70
101
} )
102
+ } )
71
103
return treeData
72
- } , [ prefix , tree , originalTree ] )
104
+ } , [ prefix , tree , originalTree , localTree ] )
73
105
74
106
return (
75
107
< TreeView
0 commit comments