You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/components/registration/utils/index.tsx

35 lines
981 B
TypeScript

import { sanitizeSessionUsername } from '../../../session/utils/String';
export function sanitizeDisplayNameOrToast(
displayName: string,
setDisplayNameError: (error: string | undefined) => void
) {
try {
const sanitizedName = sanitizeSessionUsername(displayName);
const trimName = sanitizedName.trim();
setDisplayNameError(!trimName ? window.i18n('displayNameEmpty') : undefined);
return sanitizedName;
} catch (e) {
setDisplayNameError(window.i18n('displayNameErrorDescriptionShorter'));
return displayName;
}
}
/**
* Returns undefined if an error happened, or the trim userName.
*
* Be sure to use the trimmed userName for creating the account.
*/
export const displayNameIsValid = (displayName?: string): string => {
if (!displayName) {
throw new Error(window.i18n('displayNameEmpty'));
}
const trimName = displayName.trim();
if (!trimName) {
throw new Error(window.i18n('displayNameEmpty'));
}
return trimName;
};