forked from Gerome-Elassaad/CodingIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-tree.tsx
More file actions
196 lines (185 loc) · 5.71 KB
/
Copy pathfile-tree.tsx
File metadata and controls
196 lines (185 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { useState } from 'react'
import {
ChevronDown,
ChevronRight,
File as FileIcon,
Folder,
Plus,
Trash2,
FolderPlus,
} from 'lucide-react'
import { Button } from './ui/button'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from './ui/dialog'
import { Input } from './ui/input'
export interface FileSystemNode {
name: string
isDirectory: boolean
path?: string
children?: FileSystemNode[]
}
interface FileTreeProps {
files: FileSystemNode[]
onSelectFile: (path: string) => void
onCreateFile?: (path: string, isDirectory: boolean) => void
onDeleteFile?: (path: string) => void
}
export function FileTree({ files, onSelectFile, onCreateFile, onDeleteFile }: FileTreeProps) {
const [newFileName, setNewFileName] = useState('')
const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false)
const [createType, setCreateType] = useState<'file' | 'folder'>('file')
const handleCreateFile = () => {
if (newFileName.trim() && onCreateFile) {
onCreateFile(newFileName.trim(), createType === 'folder')
setNewFileName('')
setIsCreateDialogOpen(false)
}
}
return (
<div className="p-2">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-medium text-muted-foreground">Files</span>
<div className="flex gap-1">
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
<DialogTrigger asChild>
<Button
variant="ghost"
size="icon"
className="h-6 w-6"
>
<Plus className="h-3 w-3" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Create New {createType === 'file' ? 'File' : 'Folder'}</DialogTitle>
</DialogHeader>
<div className="flex items-center space-x-2">
<Input
placeholder={createType === 'file' ? 'filename.ts' : 'folder-name'}
value={newFileName}
onChange={(e) => setNewFileName(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleCreateFile()
}
}}
/>
<Button onClick={handleCreateFile}>Create</Button>
</div>
<div className="flex gap-2 mt-2">
<Button
variant={createType === 'file' ? 'default' : 'outline'}
size="sm"
onClick={() => setCreateType('file')}
>
<FileIcon className="h-3 w-3 mr-1" />
File
</Button>
<Button
variant={createType === 'folder' ? 'default' : 'outline'}
size="sm"
onClick={() => setCreateType('folder')}
>
<FolderPlus className="h-3 w-3 mr-1" />
Folder
</Button>
</div>
</DialogContent>
</Dialog>
</div>
</div>
{Array.isArray(files) && files.map(file => (
<FileTreeNode
key={file.name}
node={file}
onSelectFile={onSelectFile}
onDeleteFile={onDeleteFile}
/>
))}
</div>
)
}
interface FileTreeNodeProps {
node: FileSystemNode
onSelectFile: (path: string) => void
onDeleteFile?: (path: string) => void
level?: number
}
function FileTreeNode({
node,
onSelectFile,
onDeleteFile,
level = 0,
path = '',
}: FileTreeNodeProps & { path?: string }) {
const [isOpen, setIsOpen] = useState(false)
const isDirectory = node.isDirectory
const hasChildren = node.children && node.children.length > 0
const newPath = `${path}/${node.name}`
const handleToggle = () => {
if (isDirectory) {
setIsOpen(!isOpen)
} else {
onSelectFile(newPath)
}
}
const handleDelete = () => {
if (onDeleteFile) {
onDeleteFile(newPath)
}
}
return (
<div>
<div
className="flex items-center cursor-pointer hover:bg-primary/5 dark:hover:bg-muted/50 rounded-sm p-1 group"
style={{ paddingLeft: level * 16 + 4 }}
onClick={handleToggle}
>
{isDirectory ? (
<>
{isOpen ? (
<ChevronDown size={16} className="mr-1 text-muted-foreground" />
) : (
<ChevronRight size={16} className="mr-1 text-muted-foreground" />
)}
<Folder size={16} className="mr-2 text-blue-500" />
</>
) : (
<FileIcon size={16} className="mr-2 ml-4 text-muted-foreground" />
)}
<span className="text-sm truncate flex-1">{node.name}</span>
{onDeleteFile && (
<Button
variant="ghost"
size="icon"
className="h-4 w-4 opacity-0 group-hover:opacity-100 ml-1"
onClick={(e) => {
e.stopPropagation()
handleDelete()
}}
>
<Trash2 className="h-3 w-3 text-red-500" />
</Button>
)}
</div>
{isOpen &&
hasChildren &&
node.children?.map(child => (
<FileTreeNode
key={child.name}
node={child}
onSelectFile={onSelectFile}
onDeleteFile={onDeleteFile}
level={level + 1}
path={newPath}
/>
))}
</div>
)
}