-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathtransformFilesToTreeData.tsx
More file actions
231 lines (221 loc) · 8.02 KB
/
transformFilesToTreeData.tsx
File metadata and controls
231 lines (221 loc) · 8.02 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React from "react"
import type { TreeDataItem } from "@/components/ui/tree-view"
import type {
IRenameFileProps,
IDeleteFileProps,
IDeleteDirectoryProps,
} from "@/hooks/useFileManagement"
import { isHiddenFile } from "@/components/ViewPackagePage/utils/is-hidden-file"
import { File, Folder, MoreVertical, Pencil, Trash2 } from "lucide-react"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { useToast } from "@/hooks/use-toast"
type FileName = string
type TreeNode = Omit<TreeDataItem, "children"> & {
children?: Record<string, TreeNode>
}
interface TransformFilesToTreeDataProps {
files: Record<FileName, string>
currentFile: FileName | null
renamingFile: string | null
handleRenameFile: (props: IRenameFileProps) => { fileRenamed: boolean }
handleDeleteFile: (props: IDeleteFileProps) => { fileDeleted: boolean }
handleDeleteDirectory: (props: IDeleteDirectoryProps) => {
directoryDeleted: boolean
}
setRenamingFile: (filename: string | null) => void
onFileSelect: (filename: FileName) => void
onFolderSelect: (folderPath: string) => void
canModifyFiles: boolean
setErrorMessage: (message: string) => void
setSelectedFolderForCreation: (folder: string | null) => void
openDropdownId: string | null
setOpenDropdownId: (id: string | null) => void
}
export const transformFilesToTreeData = ({
files,
currentFile,
renamingFile,
handleRenameFile,
handleDeleteFile,
handleDeleteDirectory,
setRenamingFile,
onFileSelect,
onFolderSelect,
canModifyFiles,
setErrorMessage,
setSelectedFolderForCreation,
openDropdownId,
setOpenDropdownId,
}: TransformFilesToTreeDataProps): TreeDataItem[] => {
const { toast } = useToast()
const root: Record<string, TreeNode> = {}
Object.keys(files).forEach((filePath) => {
const hasLeadingSlash = filePath.startsWith("/")
const pathSegments = (hasLeadingSlash ? filePath.slice(1) : filePath)
.trim()
.split("/")
let currentNode: Record<string, TreeNode> = root
pathSegments.forEach((segment, segmentIndex) => {
const isLeafNode = segmentIndex === pathSegments.length - 1
const ancestorPath = pathSegments.slice(0, segmentIndex).join("/")
const relativePath = ancestorPath ? `${ancestorPath}/${segment}` : segment
const absolutePath = hasLeadingSlash ? `/${relativePath}` : relativePath
const itemId = absolutePath
if (
!currentNode[segment] &&
(!isHiddenFile(relativePath) ||
isHiddenFile(
currentFile?.startsWith("/")
? currentFile.slice(1)
: currentFile || "",
))
) {
currentNode[segment] = {
id: itemId,
name: segment,
isRenaming: renamingFile === itemId,
onRename: (newFilename: string) => {
const oldPath = itemId
const pathParts = oldPath.split("/").filter((part) => part !== "")
let newPath: string
if (pathParts.length > 1) {
const folderPath = pathParts.slice(0, -1).join("/")
newPath = folderPath + "/" + newFilename
} else {
newPath = newFilename
}
if (oldPath.startsWith("/") && !newPath.startsWith("/")) {
newPath = "/" + newPath
}
const { fileRenamed } = handleRenameFile({
oldFilename: itemId,
newFilename: newPath,
onError: (error) => {
toast({
title: `Error renaming file`,
description: error.message,
variant: "destructive",
})
},
})
if (fileRenamed) {
setRenamingFile(null)
}
},
onCancelRename: () => {
setRenamingFile(null)
},
icon: isLeafNode ? File : Folder,
onClick: isLeafNode
? () => {
onFileSelect(absolutePath)
setSelectedFolderForCreation(null)
}
: () => onFolderSelect(absolutePath),
draggable: false,
droppable: !isLeafNode,
children: isLeafNode ? undefined : {},
actions: canModifyFiles ? (
<>
<DropdownMenu
key={itemId}
open={openDropdownId === itemId}
onOpenChange={(open) => {
setOpenDropdownId(open ? itemId : null)
}}
>
<DropdownMenuTrigger asChild>
<MoreVertical className="w-4 h-4 text-gray-500 hover:text-gray-700" />
</DropdownMenuTrigger>
<DropdownMenuContent
className="w-fit bg-white shadow-lg rounded-md border-4 z-[100] border-white"
style={{
position: "absolute",
top: "100%",
left: "0",
marginTop: "0.5rem",
width: "8rem",
padding: "0.01rem",
}}
>
<DropdownMenuGroup>
{isLeafNode && (
<DropdownMenuItem
onClick={() => {
setRenamingFile(itemId)
setOpenDropdownId(null)
}}
className="flex items-center px-3 py-1 text-xs text-black hover:bg-gray-100 cursor-pointer"
>
<Pencil className="mr-2 h-3 w-3" />
Rename
</DropdownMenuItem>
)}
<DropdownMenuItem
onClick={() => {
if (isLeafNode) {
const { fileDeleted } = handleDeleteFile({
filename: itemId,
onError: (error) => {
toast({
title: `Error deleting file ${itemId}`,
description: error.message,
})
},
})
if (fileDeleted) setErrorMessage("")
} else {
const { directoryDeleted } = handleDeleteDirectory({
directoryPath: itemId,
onError: (error) => {
toast({
title: `Error deleting directory ${itemId}`,
description: error.message,
})
},
})
if (directoryDeleted) setErrorMessage("")
}
setOpenDropdownId(null)
}}
className="flex items-center px-3 py-1 text-xs text-red-600 hover:bg-gray-100 cursor-pointer"
>
<Trash2 className="mr-2 h-3 w-3" />
Delete
</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuContent>
</DropdownMenu>
</>
) : undefined,
onContextMenu: canModifyFiles
? (e: React.MouseEvent) => {
e.preventDefault()
e.stopPropagation()
setOpenDropdownId(itemId)
}
: undefined,
}
}
if (!isLeafNode && currentNode[segment]?.children) {
currentNode = currentNode[segment].children
}
})
})
const convertToArray = (items: Record<string, TreeNode>): TreeDataItem[] => {
return Object.values(items).map((item) => ({
...item,
children: item.children ? convertToArray(item.children) : undefined,
}))
}
return convertToArray(root).filter((x) => {
if (x.children?.length === 0) return false
return true
})
}