Skip to content

Commit

Permalink
Merge pull request #13 from devklick/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
devklick authored Oct 29, 2023
2 parents ffdca8e + 4302f5a commit ad2e4a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useBindKeyToAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function useBindKeyToAction({ keys, action }: UseBindKeyToActionProps) {
window?.addEventListener("keydown", handler);

return () => {
window?.addEventListener("keydown", handler);
window?.removeEventListener("keydown", handler);
};
}, [action, keys]);
}
Expand Down
33 changes: 26 additions & 7 deletions src/stores/localFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,38 @@ export const useLocalFS = create<LocalFSState>()(
) => {
const oldName = fsObject.name;
const oldPath = fsObject.path;

// Update the FSObject to have the new name
fsObject.name = newName;
fsObject.path = [
const newPath = [
...fsObject.path.split(pathSeparator).slice(0, -1),
newName,
].join(pathSeparator);

// Delete the old one from the parent
delete parentDirectory.contents[oldName];

// Update the parent so it knows about the rename
parentDirectory.contents[newName] = fsObject;
delete parentDirectory.contents[oldName];

// Update the FSObject to have the new name
fsObject.name = newName;

// The path is also stored on each of the children,
// so we need to update the renamed dir on them to.
// This isnt ideal, as we'll going to have to loop over
// them all recursively. A better way to do this might be to not
// store the path on each FSObject and instead store a reference to
// the parent FSDirectory, but this would very tricky to persist
function updatePathRecursive(fsObject: FSObject) {
console.log("Updating", fsObject.path);
const regex = new RegExp(`^${oldPath}`);
fsObject.path = fsObject.path.replace(regex, newPath);
console.log("Updated to", fsObject.path);

if (!isFSDirectory(fsObject)) return;

for (const child of Object.values<FSObject>(fsObject.contents)) {
updatePathRecursive(child);
}
}

updatePathRecursive(fsObject);

// If the FSObject is in the favorites, update that too.
const favorites = get().favorites;
Expand Down

0 comments on commit ad2e4a9

Please sign in to comment.