Skip to content

Commit

Permalink
use Map instead of normal Object
Browse files Browse the repository at this point in the history
Map guarantees that Map.keys() are in the same order as they were
insterted.
  • Loading branch information
tomasmatus committed Jan 14, 2025
1 parent faa8115 commit 4e29ac6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/dialogs/copyPasteOwnership.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function pasteAsOwner(clipboard: ClipboardInfo, dstPath: string, owner: st
}

function makeCandidatesMap(currentUser: cockpit.UserInfo, cwdInfo: FileInfo, clipboard: ClipboardInfo) {
const map: Record<string, string> = {};
const map: Map<string, string> = new Map();
const candidates = get_owner_candidates(currentUser, cwdInfo);

// also add current ownership if it is same for all files (shallow check)
Expand All @@ -91,7 +91,7 @@ function makeCandidatesMap(currentUser: cockpit.UserInfo, cwdInfo: FileInfo, cli
key = `${key} ${_("(original owner)")}`;
}

map[key] = owner;
map.set(key, owner);
});

return map;
Expand All @@ -114,24 +114,27 @@ const CopyPasteAsOwnerModal = ({
cockpit.user().then(user => setCurrentUser(user));
}, []);

let candidatesMap: Record<string, string> = {};
let candidatesMap: Map<string, string> = new Map();
if (superuser.allowed && currentUser && cwdInfo) {
candidatesMap = makeCandidatesMap(currentUser, cwdInfo, clipboard);
if (selectedOwner === undefined) {
setSelectedOwner(Object.keys(candidatesMap)[0]);
setSelectedOwner(candidatesMap.keys().next().value);
}
}

if (selectedOwner === undefined) {
return;
}

const selectedVal = candidatesMap.get(selectedOwner);
cockpit.assert(selectedVal !== undefined, "New file ownership undefined");

const modalFooter = (
<>
<Button
variant="primary"
onClick={() => {
pasteAsOwner(clipboard, path, candidatesMap[selectedOwner], addAlert);
pasteAsOwner(clipboard, path, selectedVal, addAlert);
dialogResult.resolve();
}}
>
Expand Down Expand Up @@ -164,7 +167,7 @@ const CopyPasteAsOwnerModal = ({
value={selectedOwner}
onChange={(_ev, val) => setSelectedOwner(val)}
>
{Object.keys(candidatesMap).map(user =>
{[...candidatesMap.keys()].map((user: string) =>
<FormSelectOption
key={user}
value={user}
Expand Down

0 comments on commit 4e29ac6

Please sign in to comment.