-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathuseFileTreeState.ts
More file actions
33 lines (29 loc) · 817 Bytes
/
useFileTreeState.ts
File metadata and controls
33 lines (29 loc) · 817 Bytes
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
export function useFileTreeState(baseUrl: string) {
const expanded = useState<Set<string>>(`npmx-file-tree${baseUrl}`, () => new Set<string>())
function toggleDir(path: string) {
if (expanded.value.has(path)) {
expanded.value.delete(path)
} else {
expanded.value.add(path)
}
expanded.value = new Set(expanded.value)
}
function isExpanded(path: string) {
return expanded.value.has(path)
}
function autoExpandAncestors(path: string) {
if (!path) return
const parts = path.split('/').filter(Boolean)
let prefix = ''
for (const part of parts) {
prefix = prefix ? `${prefix}/${part}` : part
expanded.value.add(prefix)
}
expanded.value = new Set(expanded.value)
}
return {
toggleDir,
isExpanded,
autoExpandAncestors,
}
}