1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-14 09:20:06 +00:00

fix(web-drive): upon delete remove entry of deleted file from directory tree in sidebar

This commit is contained in:
2022-03-16 18:52:00 +00:00
parent fa627aabf9
commit fb77d99177
2 changed files with 44 additions and 2 deletions

View File

@@ -54,11 +54,47 @@ const Drive = () => {
setSelectedFilePath(node.relativePath)
}
const removeFileFromTree = (path: string) => {
if (directoryData) {
const newTree = JSON.parse(JSON.stringify(directoryData)) as TreeNode
findAndRemoveNode(newTree, newTree, path)
setDirectoryData(newTree)
}
}
const findAndRemoveNode = (
node: TreeNode,
parentNode: TreeNode,
path: string
) => {
if (node.relativePath === path) {
removeNodeFromParent(parentNode, path)
return true
}
if (Array.isArray(node.children)) {
for (let i = 0; i < node.children.length; i++) {
if (findAndRemoveNode(node.children[i], node, path)) return
}
}
}
const removeNodeFromParent = (parent: TreeNode, path: string) => {
const index = parent.children.findIndex(
(node) => node.relativePath === path
)
if (index !== -1) {
parent.children.splice(index, 1)
}
}
return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
<SideBar directoryData={directoryData} handleSelect={handleSelect} />
<Main selectedFilePath={selectedFilePath} />
<Main
selectedFilePath={selectedFilePath}
removeFileFromTree={removeFileFromTree}
/>
</Box>
)
}