-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathuseDeleteFilesFromDirectory.ts
More file actions
117 lines (103 loc) · 3.29 KB
/
useDeleteFilesFromDirectory.ts
File metadata and controls
117 lines (103 loc) · 3.29 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
import { useState, useEffect } from "react"
import type { PackageFile } from "@/types/package"
import { isHiddenFile } from "@/components/ViewPackagePage/utils/is-hidden-file"
export interface IDeleteDirectoryProps {
directoryPath: string
onError: (error: Error) => void
}
export interface IDeleteDirectoryResult {
deleted: boolean
}
function getAncestorDirectories(dirPath: string): string[] {
const hasLeadingSlash = dirPath.startsWith("/")
const normalized = hasLeadingSlash ? dirPath.slice(1) : dirPath
const parts = normalized.split("/").filter(Boolean)
const ancestors: string[] = []
for (let i = 1; i < parts.length; i++) {
const ancestor = parts.slice(0, i).join("/")
ancestors.push(hasLeadingSlash ? `/${ancestor}` : ancestor)
}
return ancestors
}
export function useDeleteFilesFromDirectory({
localFiles,
setLocalFiles,
currentFile,
onFileSelect,
}: {
localFiles: PackageFile[]
setLocalFiles: (files: PackageFile[]) => void
currentFile: string | null
onFileSelect: (path: string) => void
}) {
const [preservedDirectories, setPreservedDirectories] = useState<Set<string>>(
new Set(),
)
useEffect(() => {
if (!localFiles) return
setPreservedDirectories((prev) => {
if (prev.size === 0) return prev
const next = new Set(prev)
let changed = false
for (const dir of prev) {
const prefix = dir.endsWith("/") ? dir : dir + "/"
const hasRealContent = localFiles.some((f) => f.path.startsWith(prefix))
if (hasRealContent) {
next.delete(dir)
changed = true
}
}
return changed ? next : prev
})
}, [localFiles])
const deleteDirectory = ({
directoryPath,
onError,
}: IDeleteDirectoryProps): IDeleteDirectoryResult => {
const dirPrefix = directoryPath.endsWith("/")
? directoryPath
: directoryPath + "/"
const hasFiles = localFiles.some((file) => file.path.startsWith(dirPrefix))
if (!hasFiles) {
if (preservedDirectories.has(directoryPath)) {
setPreservedDirectories(
(prev) => new Set([...prev].filter((d) => d !== directoryPath)),
)
return { deleted: true }
}
onError(new Error("Directory does not exist"))
return { deleted: false }
}
const updatedFiles = localFiles.filter(
(file) => !file.path.startsWith(dirPrefix),
)
const ancestors = getAncestorDirectories(directoryPath)
const emptyAncestors = ancestors.filter((ancestor) => {
const prefix = ancestor.endsWith("/") ? ancestor : ancestor + "/"
return !updatedFiles.some((file) => file.path.startsWith(prefix))
})
if (emptyAncestors.length > 0) {
setPreservedDirectories((prev) => {
const next = new Set(prev)
emptyAncestors.forEach((a) => next.add(a))
next.delete(directoryPath)
return next
})
} else if (preservedDirectories.has(directoryPath)) {
setPreservedDirectories(
(prev) => new Set([...prev].filter((d) => d !== directoryPath)),
)
}
setLocalFiles(updatedFiles)
if (currentFile?.startsWith(dirPrefix)) {
onFileSelect(
updatedFiles.filter((file) => !isHiddenFile(file.path))[0]?.path || "",
)
}
return { deleted: true }
}
return {
deleteDirectory,
preservedDirectories,
}
}