Skip to content

Commit

Permalink
Refactor the deleteItem function
Browse files Browse the repository at this point in the history
Simplify deletion by only taking the selected files and the current
directory the files are in. Not only does it make the code more clear
but it also opens up a way to drop selectedContext.
  • Loading branch information
jelly committed Feb 1, 2024
1 parent 127bd3a commit 658dfd8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
15 changes: 6 additions & 9 deletions src/apis/spawnHelpers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ const renameCommand = ({ selected, path, name }) => {
: ["mv", path.join("/") + "/" + selected.name, path.join("/") + "/" + name];
};

export const spawnDeleteItem = ({ path, selected, itemPath, Dialogs, setSelected, setHistory, setHistoryIndex }) => {
export const spawnDeleteItem = ({ path, selected, Dialogs, setSelected, setHistory, setHistoryIndex }) => {
cockpit.spawn([
"rm",
"-r",
...selected.length > 1
? selected.map(f => path + f.name)
: [itemPath]
...selected.map(f => path + f.name)
], options)
.then(() => {
setSelected([]);
Expand All @@ -52,20 +50,19 @@ export const spawnDeleteItem = ({ path, selected, itemPath, Dialogs, setSelected
.catch(err => {
Dialogs.show(
<ForceDeleteModal
selected={selected} itemPath={itemPath}
path={path}
selected={selected}
initialError={err.message}
/>
);
});
};

export const spawnForceDelete = ({ selected, path, itemPath, Dialogs, setDeleteFailed, setErrorMessage }) => {
export const spawnForceDelete = ({ selected, path, Dialogs, setDeleteFailed, setErrorMessage }) => {
cockpit.spawn([
"rm",
"-r",
...selected.length > 1
? selected.map(f => path + f.name)
: [itemPath]
...selected.map(f => path + f.name)
], options)
.then(Dialogs.close, err => {
setDeleteFailed(true);
Expand Down
15 changes: 5 additions & 10 deletions src/fileActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ export const createLink = (Dialogs, currentPath, files, selected) => {
export const deleteItem = (Dialogs, options) => {
Dialogs.show(
<ConfirmDeletionDialog
selected={options.selected} itemPath={options.itemPath}
selected={options.selected}
path={options.path} setSelected={options.setSelected}
setHistory={options.setHistory} setHistoryIndex={options.setHistoryIndex}
currentDirectory={options.currentDirectory}
/>
);
};
Expand Down Expand Up @@ -107,23 +106,19 @@ export const pasteItem = (clipboard, targetPath, asSymlink, addAlert) => {
};

export const ConfirmDeletionDialog = ({
itemPath,
path,
selected,
setHistory,
setHistoryIndex,
setSelected,
currentDirectory
}) => {
const Dialogs = useDialogs();

let modalTitle;
if (selected.length > 1) {
modalTitle = cockpit.format(_("Delete $0 items?"), selected.length);
} else {
const selectedItem = selected.length === 1
? selected[0]
: currentDirectory;
const selectedItem = selected[0];
if (selectedItem.type === "reg") {
modalTitle = cockpit.format(_("Delete file $0?"), selectedItem.name);
} else if (selectedItem.type === "lnk") {
Expand All @@ -136,7 +131,7 @@ export const ConfirmDeletionDialog = ({
}

const deleteItem = () => {
spawnDeleteItem({ Dialogs, selected, itemPath, path, setHistory, setHistoryIndex, setSelected });
spawnDeleteItem({ Dialogs, selected, path, setHistory, setHistoryIndex, setSelected });
};

return (
Expand All @@ -157,7 +152,7 @@ export const ConfirmDeletionDialog = ({
);
};

export const ForceDeleteModal = ({ selected, itemPath, initialError }) => {
export const ForceDeleteModal = ({ selected, path, initialError }) => {
const Dialogs = useDialogs();
const [errorMessage, setErrorMessage] = useState(initialError);
const [deleteFailed, setDeleteFailed] = useState(false);
Expand All @@ -181,7 +176,7 @@ export const ForceDeleteModal = ({ selected, itemPath, initialError }) => {
}

const forceDeleteItem = () => {
spawnForceDelete({ Dialogs, selected, itemPath, setDeleteFailed, setErrorMessage });
spawnForceDelete({ Dialogs, selected, path, setDeleteFailed, setErrorMessage });
};

return (
Expand Down
1 change: 0 additions & 1 deletion src/navigator-card-body.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const ContextMenuItems = ({ path, currentDir, selected, selectedContext, setSele
Dialogs,
{
selected,
itemPath: currentDir + selectedContext.name,
setHistory,
setHistoryIndex,
path: currentDir,
Expand Down

0 comments on commit 658dfd8

Please sign in to comment.