2023-07-14 19:59:30 +02:00
|
|
|
import { InvisibleCharacter } from '../../components/component-library';
|
2023-08-24 12:27:42 +02:00
|
|
|
import {
|
|
|
|
GOERLI_DISPLAY_NAME,
|
|
|
|
LINEA_GOERLI_DISPLAY_NAME,
|
|
|
|
SEPOLIA_DISPLAY_NAME,
|
|
|
|
} from '../../../shared/constants/network';
|
|
|
|
import { BackgroundColor } from '../constants/design-system';
|
2023-03-06 18:48:28 +01:00
|
|
|
|
2022-12-22 18:27:31 +01:00
|
|
|
export function getAccountNameErrorMessage(
|
|
|
|
accounts,
|
|
|
|
context,
|
|
|
|
newAccountName,
|
|
|
|
defaultAccountName,
|
|
|
|
) {
|
|
|
|
const isDuplicateAccountName = accounts.some(
|
2023-06-22 16:07:56 +02:00
|
|
|
(item) => item.name.toLowerCase() === newAccountName.toLowerCase(),
|
2022-12-22 18:27:31 +01:00
|
|
|
);
|
|
|
|
|
2023-05-08 16:03:34 +02:00
|
|
|
const isEmptyAccountName = newAccountName === '';
|
|
|
|
|
2022-12-22 18:27:31 +01:00
|
|
|
const localizedWordForAccount = context
|
|
|
|
.t('newAccountNumberName')
|
|
|
|
.replace(' $1', '');
|
|
|
|
|
|
|
|
// Match strings starting with ${localizedWordForAccount} and then any numeral, case insensitive
|
|
|
|
// Trim spaces before and after
|
|
|
|
const reservedRegEx = new RegExp(
|
|
|
|
`^\\s*${localizedWordForAccount} \\d+\\s*$`,
|
|
|
|
'iu',
|
|
|
|
);
|
|
|
|
const isReservedAccountName = reservedRegEx.test(newAccountName);
|
|
|
|
|
|
|
|
const isValidAccountName =
|
2023-06-22 16:07:56 +02:00
|
|
|
newAccountName.toLowerCase() === defaultAccountName.toLowerCase() || // What is written in the text field is the same as the placeholder
|
2023-05-08 16:03:34 +02:00
|
|
|
(!isDuplicateAccountName && !isReservedAccountName && !isEmptyAccountName);
|
2022-12-22 18:27:31 +01:00
|
|
|
|
|
|
|
let errorMessage;
|
|
|
|
if (isValidAccountName) {
|
2023-07-14 19:59:30 +02:00
|
|
|
errorMessage = InvisibleCharacter; // Using an invisible character, so the spacing stays constant
|
2022-12-22 18:27:31 +01:00
|
|
|
} else if (isDuplicateAccountName) {
|
|
|
|
errorMessage = context.t('accountNameDuplicate');
|
|
|
|
} else if (isReservedAccountName) {
|
|
|
|
errorMessage = context.t('accountNameReserved');
|
2023-05-08 16:03:34 +02:00
|
|
|
} else if (isEmptyAccountName) {
|
|
|
|
errorMessage = context.t('required');
|
2022-12-22 18:27:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return { isValidAccountName, errorMessage };
|
|
|
|
}
|
2023-08-24 12:27:42 +02:00
|
|
|
|
|
|
|
export function getAvatarNetworkColor(name) {
|
|
|
|
switch (name) {
|
|
|
|
case GOERLI_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.goerli;
|
|
|
|
case LINEA_GOERLI_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.lineaGoerli;
|
|
|
|
case SEPOLIA_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.sepolia;
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|