Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connector unique name suffix #2866

Merged
merged 7 commits into from
Dec 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/extension/background-script/actions/accounts/add.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { v4 as uuidv4 } from "uuid";
import { encryptData } from "~/common/lib/crypto";
import state from "~/extension/background-script/state";
import type { MessageAccountAdd } from "~/types";
import type { Account, MessageAccountAdd } from "~/types";

const add = async (message: MessageAccountAdd) => {
const newAccount = message.args;
let name = newAccount.name;
const accounts = state.getState().accounts;
const tmpAccounts = { ...accounts };

// get all accounts names to avoid duplicate names
const accountNames = Object.values(accounts).map((el: Account) => el.name);
reneaaron marked this conversation as resolved.
Show resolved Hide resolved

// increase nameSuffix number recursively
while (accountNames.includes(name)) {
// get number in between (1) and increase by 1
const bracketsValue = +name.substring(name.indexOf("(") + 1, name.lastIndexOf(")"));
const accountNameCount = typeof bracketsValue === 'number' ? bracketsValue + 1 : 0;
const nameContainsSuffix = name.match(/\(\d\)/);
// if name already contains a suffix, remove it to add the increased
if (nameContainsSuffix) {
const suffixIndex = name.lastIndexOf('(');
name = name.substring(0, suffixIndex);
}
const nameSuffix = ` (${accountNameCount})`;
name = `${name}${nameSuffix}`;
}


// TODO: add validations
// TODO: make sure a password is set
const password = await state.getState().password();
Expand All @@ -19,6 +39,7 @@ const add = async (message: MessageAccountAdd) => {
tmpAccounts[accountId] = {
...newAccount,
id: accountId,
name,
};

state.setState({ accounts: tmpAccounts });
Expand Down