Skip to content

Commit

Permalink
simplify SnapAccountCustomName
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Nov 15, 2024
1 parent 7b0cb95 commit 4722411
Showing 1 changed file with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,38 @@ const SnapAccountCustomNameApproval = () => {

const { styles } = useStyles(styleSheet, {});

const onAddAccountPressed = useCallback(() => {
const onAddAccountPressed = () => {
if (!isNameTaken) {
onConfirm(undefined, { success: true, name: accountName });
}
}, [accountName, onConfirm, isNameTaken]);
};

const checkIfNameTaken = useCallback(
(name: string) =>
internalAccounts.some((account) => account.metadata.name === name),
[internalAccounts],
);
const checkIfNameTaken = (name: string) =>
internalAccounts.some((account) => account.metadata.name === name);

useEffect(() => {
const suggestedName = approvalRequest?.requestData.snapSuggestedAccountName;
const initialName = suggestedName
? generateUniqueNameWithSuffix(suggestedName)
: Engine.context.AccountsController.getNextAvailableAccountName(
KeyringTypes.snap,
);
setAccountName(initialName);

const generateUniqueNameWithSuffix = useCallback(
(baseName: string): string => {
function generateUniqueNameWithSuffix(baseName: string): string {
let suffix = 1;
let candidateName = baseName;

while (checkIfNameTaken(candidateName)) {
while (
internalAccounts.some(
(account) => account.metadata.name === candidateName,

Check failure on line 65 in app/components/Approvals/SnapAccountCustomNameApproval/SnapAccountCustomNameApproval.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint)

Function declared in a loop contains unsafe references to variable(s) 'candidateName'
)
) {
suffix += 1;
candidateName = `${baseName} ${suffix}`;
}
return candidateName;
},
[checkIfNameTaken],
);

const getInitialAccountName = useCallback(
(suggestedName?: string): string => {
if (suggestedName) {
return generateUniqueNameWithSuffix(suggestedName);
}
return Engine.context.AccountsController.getNextAvailableAccountName(
KeyringTypes.snap,
);
},
[generateUniqueNameWithSuffix],
);

useEffect(() => {
const suggestedName = approvalRequest?.requestData.snapSuggestedAccountName;
const initialName = getInitialAccountName(suggestedName);
setAccountName(initialName);
}, [approvalRequest, getInitialAccountName]);
}
}, [approvalRequest, internalAccounts]);

const cancelButtonProps: ButtonProps = {
variant: ButtonVariants.Secondary,
Expand Down

0 comments on commit 4722411

Please sign in to comment.