1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/ui/helpers/utils/accounts.js
Garrett Bear 01057f9824
convert text component to TS (#18329)
* convert text component to TS

* invisible character fix

* storybook fix

* fix types export

* update ButtonBase

* add typeof to objects in TextProps

* fix linting issues

* fix implicit conversion

* lint fix

* add deprecated Text back

* change box ref to any

* fix classnames issue

* account details to use deprecated text reference

* ref update

* make RefObject

* remove RefObject and go back to Ref

* react.ref change to box
2023-04-03 10:42:37 -07:00

40 lines
1.3 KiB
JavaScript

import { INVISIBLE_CHARACTER } from '../../components/component-library/text/deprecated';
export function getAccountNameErrorMessage(
accounts,
context,
newAccountName,
defaultAccountName,
) {
const isDuplicateAccountName = accounts.some(
(item) => item.name === newAccountName,
);
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 =
newAccountName === defaultAccountName || // What is written in the text field is the same as the placeholder
(!isDuplicateAccountName && !isReservedAccountName);
let errorMessage;
if (isValidAccountName) {
errorMessage = INVISIBLE_CHARACTER; // Using an invisible character, so the spacing stays constant
} else if (isDuplicateAccountName) {
errorMessage = context.t('accountNameDuplicate');
} else if (isReservedAccountName) {
errorMessage = context.t('accountNameReserved');
}
return { isValidAccountName, errorMessage };
}