signal-desktop/ts/components/ProfileEditor.tsx

810 lines
23 KiB
TypeScript
Raw Normal View History

2021-07-19 19:26:06 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useCallback, useEffect, useRef, useState } from 'react';
import * as log from '../logging/log';
import type { AvatarColorType } from '../types/Colors';
import { AvatarColors } from '../types/Colors';
import type {
2021-08-06 00:17:05 +00:00
AvatarDataType,
DeleteAvatarFromDiskActionType,
ReplaceAvatarActionType,
SaveAvatarToDiskActionType,
} from '../types/Avatar';
import { AvatarEditor } from './AvatarEditor';
import { AvatarPreview } from './AvatarPreview';
2021-07-19 19:26:06 +00:00
import { Button, ButtonVariant } from './Button';
2021-08-06 00:17:05 +00:00
import { ConfirmDiscardDialog } from './ConfirmDiscardDialog';
2021-07-19 19:26:06 +00:00
import { Emoji } from './emoji/Emoji';
import type { Props as EmojiButtonProps } from './emoji/EmojiButton';
import { EmojiButton } from './emoji/EmojiButton';
import type { EmojiPickDataType } from './emoji/EmojiPicker';
2021-07-19 19:26:06 +00:00
import { Input } from './Input';
import { Intl } from './Intl';
import type { LocalizerType, ReplacementValuesType } from '../types/Util';
2021-08-06 00:17:05 +00:00
import { Modal } from './Modal';
2021-07-19 19:26:06 +00:00
import { PanelRow } from './conversation/conversation-details/PanelRow';
import type { ProfileDataType } from '../state/ducks/conversations';
2021-07-19 19:26:06 +00:00
import { getEmojiData, unifiedToEmoji } from './emoji/lib';
import { missingCaseError } from '../util/missingCaseError';
import { ConfirmationDialog } from './ConfirmationDialog';
import {
ConversationDetailsIcon,
IconType,
} from './conversation/conversation-details/ConversationDetailsIcon';
import { Spinner } from './Spinner';
import { UsernameSaveState } from '../state/ducks/conversationsEnums';
2021-07-19 19:26:06 +00:00
export enum EditState {
None = 'None',
2021-08-06 00:17:05 +00:00
BetterAvatar = 'BetterAvatar',
2021-07-19 19:26:06 +00:00
ProfileName = 'ProfileName',
Bio = 'Bio',
Username = 'Username',
}
enum UsernameEditState {
Editing = 'Editing',
ConfirmingDelete = 'ConfirmingDelete',
ShowingErrorPopup = 'ShowingErrorPopup',
Saving = 'Saving',
2021-07-19 19:26:06 +00:00
}
type PropsExternalType = {
onEditStateChanged: (editState: EditState) => unknown;
onProfileChanged: (
profileData: ProfileDataType,
2021-09-24 00:49:05 +00:00
avatarBuffer?: Uint8Array
2021-07-19 19:26:06 +00:00
) => unknown;
};
export type PropsDataType = {
aboutEmoji?: string;
aboutText?: string;
avatarPath?: string;
2021-08-06 00:17:05 +00:00
color?: AvatarColorType;
conversationId: string;
2021-07-19 19:26:06 +00:00
familyName?: string;
firstName: string;
i18n: LocalizerType;
isUsernameFlagEnabled: boolean;
usernameSaveState: UsernameSaveState;
2021-08-06 00:17:05 +00:00
userAvatarData: Array<AvatarDataType>;
username?: string;
2021-07-19 19:26:06 +00:00
} & Pick<EmojiButtonProps, 'recentEmojis' | 'skinTone'>;
type PropsActionType = {
clearUsernameSave: () => unknown;
2021-08-06 00:17:05 +00:00
deleteAvatarFromDisk: DeleteAvatarFromDiskActionType;
2021-07-19 19:26:06 +00:00
onSetSkinTone: (tone: number) => unknown;
2021-08-06 00:17:05 +00:00
replaceAvatar: ReplaceAvatarActionType;
saveAvatarToDisk: SaveAvatarToDiskActionType;
saveUsername: (options: {
username: string | undefined;
previousUsername: string | undefined;
}) => unknown;
2021-07-19 19:26:06 +00:00
};
export type PropsType = PropsDataType & PropsActionType & PropsExternalType;
type DefaultBio = {
i18nLabel: string;
shortName: string;
};
const DEFAULT_BIOS: Array<DefaultBio> = [
{
i18nLabel: 'Bio--speak-freely',
shortName: 'wave',
},
{
i18nLabel: 'Bio--encrypted',
shortName: 'zipper_mouth_face',
},
{
i18nLabel: 'Bio--free-to-chat',
shortName: '+1',
},
{
i18nLabel: 'Bio--coffee-lover',
shortName: 'coffee',
},
{
i18nLabel: 'Bio--taking-break',
shortName: 'mobile_phone_off',
},
];
function getUsernameInvalidKey(
username: string | undefined
): { key: string; replacements?: ReplacementValuesType } | undefined {
if (!username) {
return undefined;
}
const min = 3;
if (username.length < min) {
return {
key: 'ProfileEditor--username--check-character-min',
replacements: { min },
};
}
if (!/^[0-9a-z_]+$/.test(username)) {
return { key: 'ProfileEditor--username--check-characters' };
}
if (/^[0-9]/.test(username)) {
return { key: 'ProfileEditor--username--check-starting-character' };
}
const max = 25;
if (username.length > max) {
return {
key: 'ProfileEditor--username--check-character-max',
replacements: { max },
};
}
return undefined;
}
function mapSaveStateToEditState({
clearUsernameSave,
i18n,
setEditState,
setUsernameEditState,
setUsernameError,
usernameSaveState,
}: {
clearUsernameSave: () => unknown;
i18n: LocalizerType;
setEditState: (state: EditState) => unknown;
setUsernameEditState: (state: UsernameEditState) => unknown;
setUsernameError: (errorText: string) => unknown;
usernameSaveState: UsernameSaveState;
}): void {
if (usernameSaveState === UsernameSaveState.None) {
return;
}
if (usernameSaveState === UsernameSaveState.Saving) {
setUsernameEditState(UsernameEditState.Saving);
return;
}
clearUsernameSave();
if (usernameSaveState === UsernameSaveState.Success) {
setEditState(EditState.None);
setUsernameEditState(UsernameEditState.Editing);
return;
}
if (usernameSaveState === UsernameSaveState.UsernameMalformedError) {
setUsernameEditState(UsernameEditState.Editing);
setUsernameError(i18n('ProfileEditor--username--check-characters'));
return;
}
if (usernameSaveState === UsernameSaveState.UsernameTakenError) {
setUsernameEditState(UsernameEditState.Editing);
setUsernameError(i18n('ProfileEditor--username--check-username-taken'));
return;
}
if (usernameSaveState === UsernameSaveState.GeneralError) {
setUsernameEditState(UsernameEditState.ShowingErrorPopup);
return;
}
if (usernameSaveState === UsernameSaveState.DeleteFailed) {
setUsernameEditState(UsernameEditState.Editing);
return;
}
const state: never = usernameSaveState;
log.error(
`ProfileEditor: useEffect username didn't handle usernameSaveState '${state})'`
);
setEditState(EditState.None);
}
2021-07-19 19:26:06 +00:00
export const ProfileEditor = ({
aboutEmoji,
aboutText,
avatarPath,
clearUsernameSave,
2021-08-06 00:17:05 +00:00
color,
conversationId,
deleteAvatarFromDisk,
2021-07-19 19:26:06 +00:00
familyName,
firstName,
i18n,
isUsernameFlagEnabled,
2021-07-19 19:26:06 +00:00
onEditStateChanged,
onProfileChanged,
onSetSkinTone,
recentEmojis,
2021-08-06 00:17:05 +00:00
replaceAvatar,
saveAvatarToDisk,
saveUsername,
2021-07-19 19:26:06 +00:00
skinTone,
2021-08-06 00:17:05 +00:00
userAvatarData,
username,
usernameSaveState,
2021-07-19 19:26:06 +00:00
}: PropsType): JSX.Element => {
const focusInputRef = useRef<HTMLInputElement | null>(null);
const [editState, setEditState] = useState<EditState>(EditState.None);
const [confirmDiscardAction, setConfirmDiscardAction] = useState<
(() => unknown) | undefined
>(undefined);
// This is here to avoid component re-render jitters in the time it takes
// redux to come back with the correct state
const [fullName, setFullName] = useState({
familyName,
firstName,
});
const [fullBio, setFullBio] = useState({
aboutEmoji,
aboutText,
});
const [newUsername, setNewUsername] = useState<string | undefined>(username);
const [usernameError, setUsernameError] = useState<string | undefined>();
const [usernameEditState, setUsernameEditState] = useState<UsernameEditState>(
UsernameEditState.Editing
);
2021-07-19 19:26:06 +00:00
2021-09-24 00:49:05 +00:00
const [avatarBuffer, setAvatarBuffer] = useState<Uint8Array | undefined>(
2021-07-19 19:26:06 +00:00
undefined
);
const [stagedProfile, setStagedProfile] = useState<ProfileDataType>({
aboutEmoji,
aboutText,
familyName,
firstName,
});
// To make AvatarEditor re-render less often
2021-07-19 19:26:06 +00:00
const handleBack = useCallback(() => {
setEditState(EditState.None);
onEditStateChanged(EditState.None);
}, [setEditState, onEditStateChanged]);
// To make EmojiButton re-render less often
2021-07-19 19:26:06 +00:00
const setAboutEmoji = useCallback(
(ev: EmojiPickDataType) => {
const emojiData = getEmojiData(ev.shortName, skinTone);
setStagedProfile(profileData => ({
...profileData,
aboutEmoji: unifiedToEmoji(emojiData.unified),
}));
},
[setStagedProfile, skinTone]
);
// To make AvatarEditor re-render less often
2021-07-19 19:26:06 +00:00
const handleAvatarChanged = useCallback(
2021-09-24 00:49:05 +00:00
(avatar: Uint8Array | undefined) => {
2021-08-06 00:17:05 +00:00
setAvatarBuffer(avatar);
setEditState(EditState.None);
onProfileChanged(stagedProfile, avatar);
2021-07-19 19:26:06 +00:00
},
2021-08-06 00:17:05 +00:00
[onProfileChanged, stagedProfile]
2021-07-19 19:26:06 +00:00
);
2021-08-06 00:17:05 +00:00
const getFullNameText = () => {
return [fullName.firstName, fullName.familyName].filter(Boolean).join(' ');
};
2021-07-19 19:26:06 +00:00
useEffect(() => {
const focusNode = focusInputRef.current;
if (!focusNode) {
return;
}
focusNode.focus();
2021-10-01 19:27:34 +00:00
focusNode.setSelectionRange(focusNode.value.length, focusNode.value.length);
2021-07-19 19:26:06 +00:00
}, [editState]);
2021-08-06 00:17:05 +00:00
useEffect(() => {
onEditStateChanged(editState);
}, [editState, onEditStateChanged]);
// If there's some in-process username save, or just an unacknowledged save
// completion/error, we clear it out on mount, and then again on unmount.
useEffect(() => {
clearUsernameSave();
return () => {
clearUsernameSave();
};
});
useEffect(() => {
mapSaveStateToEditState({
clearUsernameSave,
i18n,
setEditState,
setUsernameEditState,
setUsernameError,
usernameSaveState,
});
}, [
clearUsernameSave,
i18n,
setEditState,
setUsernameEditState,
setUsernameError,
usernameSaveState,
]);
useEffect(() => {
// Whenever the user makes a change, we'll get rid of the red error text
setUsernameError(undefined);
// And then we'll check the validity of that new username
const timeout = setTimeout(() => {
const key = getUsernameInvalidKey(newUsername);
if (key) {
setUsernameError(i18n(key.key, key.replacements));
}
}, 1000);
return () => {
clearTimeout(timeout);
};
}, [newUsername, i18n, setUsernameError]);
const isCurrentlySaving = usernameEditState === UsernameEditState.Saving;
const shouldDisableUsernameSave = Boolean(
newUsername === username ||
!newUsername ||
usernameError ||
isCurrentlySaving
);
const checkThenSaveUsername = () => {
if (isCurrentlySaving) {
log.error('checkThenSaveUsername: Already saving! Returning early');
return;
}
if (shouldDisableUsernameSave) {
return;
}
const invalidKey = getUsernameInvalidKey(newUsername);
if (invalidKey) {
setUsernameError(i18n(invalidKey.key, invalidKey.replacements));
return;
}
setUsernameError(undefined);
setUsernameEditState(UsernameEditState.Saving);
saveUsername({ username: newUsername, previousUsername: username });
};
const deleteUsername = () => {
if (isCurrentlySaving) {
log.error('deleteUsername: Already saving! Returning early');
return;
}
setNewUsername(undefined);
setUsernameError(undefined);
setUsernameEditState(UsernameEditState.Saving);
saveUsername({ username: undefined, previousUsername: username });
};
// To make AvatarEditor re-render less often
2021-08-06 00:17:05 +00:00
const handleAvatarLoaded = useCallback(avatar => {
setAvatarBuffer(avatar);
}, []);
let content: JSX.Element;
if (editState === EditState.BetterAvatar) {
content = (
<AvatarEditor
avatarColor={color || AvatarColors[0]}
avatarPath={avatarPath}
avatarValue={avatarBuffer}
conversationId={conversationId}
conversationTitle={getFullNameText()}
deleteAvatarFromDisk={deleteAvatarFromDisk}
i18n={i18n}
onCancel={handleBack}
onSave={handleAvatarChanged}
userAvatarData={userAvatarData}
replaceAvatar={replaceAvatar}
saveAvatarToDisk={saveAvatarToDisk}
/>
);
} else if (editState === EditState.ProfileName) {
const shouldDisableSave =
!stagedProfile.firstName ||
(stagedProfile.firstName === fullName.firstName &&
stagedProfile.familyName === fullName.familyName);
2021-07-19 19:26:06 +00:00
content = (
<>
<Input
i18n={i18n}
maxLengthCount={26}
maxByteCount={128}
whenToShowRemainingCount={0}
2021-07-19 19:26:06 +00:00
onChange={newFirstName => {
setStagedProfile(profileData => ({
...profileData,
firstName: String(newFirstName),
}));
}}
placeholder={i18n('ProfileEditor--first-name')}
ref={focusInputRef}
value={stagedProfile.firstName}
/>
<Input
i18n={i18n}
maxLengthCount={26}
maxByteCount={128}
whenToShowRemainingCount={0}
2021-07-19 19:26:06 +00:00
onChange={newFamilyName => {
setStagedProfile(profileData => ({
...profileData,
familyName: newFamilyName,
}));
}}
placeholder={i18n('ProfileEditor--last-name')}
value={stagedProfile.familyName}
/>
2021-08-06 00:17:05 +00:00
<Modal.ButtonFooter>
2021-07-19 19:26:06 +00:00
<Button
onClick={() => {
const handleCancel = () => {
handleBack();
setStagedProfile(profileData => ({
...profileData,
familyName,
firstName,
}));
};
const hasChanges =
stagedProfile.familyName !== fullName.familyName ||
stagedProfile.firstName !== fullName.firstName;
if (hasChanges) {
setConfirmDiscardAction(() => handleCancel);
} else {
handleCancel();
}
}}
variant={ButtonVariant.Secondary}
>
{i18n('cancel')}
</Button>
<Button
disabled={shouldDisableSave}
2021-07-19 19:26:06 +00:00
onClick={() => {
if (!stagedProfile.firstName) {
return;
}
setFullName({
firstName: stagedProfile.firstName,
familyName: stagedProfile.familyName,
2021-07-19 19:26:06 +00:00
});
2021-08-06 00:17:05 +00:00
onProfileChanged(stagedProfile, avatarBuffer);
2021-07-19 19:26:06 +00:00
handleBack();
}}
>
{i18n('save')}
</Button>
2021-08-06 00:17:05 +00:00
</Modal.ButtonFooter>
2021-07-19 19:26:06 +00:00
</>
);
} else if (editState === EditState.Bio) {
const shouldDisableSave =
stagedProfile.aboutText === fullBio.aboutText &&
stagedProfile.aboutEmoji === fullBio.aboutEmoji;
2021-07-19 19:26:06 +00:00
content = (
<>
<Input
expandable
hasClearButton
i18n={i18n}
icon={
<div className="module-composition-area__button-cell">
<EmojiButton
closeOnPick
emoji={stagedProfile.aboutEmoji}
i18n={i18n}
onPickEmoji={setAboutEmoji}
onSetSkinTone={onSetSkinTone}
recentEmojis={recentEmojis}
skinTone={skinTone}
/>
</div>
}
maxLengthCount={140}
maxByteCount={512}
2021-07-19 19:26:06 +00:00
moduleClassName="ProfileEditor__about-input"
onChange={value => {
if (value) {
setStagedProfile(profileData => ({
...profileData,
aboutEmoji: stagedProfile.aboutEmoji,
2021-10-01 19:27:34 +00:00
aboutText: value.replace(/(\r\n|\n|\r)/gm, ''),
2021-07-19 19:26:06 +00:00
}));
} else {
setStagedProfile(profileData => ({
...profileData,
aboutEmoji: undefined,
aboutText: '',
}));
}
}}
ref={focusInputRef}
placeholder={i18n('ProfileEditor--about-placeholder')}
value={stagedProfile.aboutText}
whenToShowRemainingCount={40}
/>
{DEFAULT_BIOS.map(defaultBio => (
<PanelRow
className="ProfileEditor__row"
key={defaultBio.shortName}
icon={
<div className="ProfileEditor__icon--container">
<Emoji shortName={defaultBio.shortName} size={24} />
</div>
}
label={i18n(defaultBio.i18nLabel)}
onClick={() => {
const emojiData = getEmojiData(defaultBio.shortName, skinTone);
setStagedProfile(profileData => ({
...profileData,
aboutEmoji: unifiedToEmoji(emojiData.unified),
aboutText: i18n(defaultBio.i18nLabel),
}));
}}
/>
))}
2021-08-06 00:17:05 +00:00
<Modal.ButtonFooter>
2021-07-19 19:26:06 +00:00
<Button
onClick={() => {
const handleCancel = () => {
handleBack();
setStagedProfile(profileData => ({
...profileData,
...fullBio,
}));
};
const hasChanges =
stagedProfile.aboutText !== fullBio.aboutText ||
stagedProfile.aboutEmoji !== fullBio.aboutEmoji;
if (hasChanges) {
setConfirmDiscardAction(() => handleCancel);
} else {
handleCancel();
}
}}
variant={ButtonVariant.Secondary}
>
{i18n('cancel')}
</Button>
<Button
disabled={shouldDisableSave}
2021-07-19 19:26:06 +00:00
onClick={() => {
setFullBio({
aboutEmoji: stagedProfile.aboutEmoji,
aboutText: stagedProfile.aboutText,
});
2021-08-06 00:17:05 +00:00
onProfileChanged(stagedProfile, avatarBuffer);
2021-07-19 19:26:06 +00:00
handleBack();
}}
>
{i18n('save')}
</Button>
2021-08-06 00:17:05 +00:00
</Modal.ButtonFooter>
2021-07-19 19:26:06 +00:00
</>
);
} else if (editState === EditState.Username) {
content = (
<>
<Input
i18n={i18n}
disabled={isCurrentlySaving}
onChange={changedUsername => {
setUsernameError(undefined);
setNewUsername(changedUsername);
}}
onEnter={checkThenSaveUsername}
placeholder={i18n('ProfileEditor--username--placeholder')}
ref={focusInputRef}
value={newUsername}
/>
<div className="ProfileEditor__error">{usernameError}</div>
<div className="ProfileEditor__info">
<Intl i18n={i18n} id="ProfileEditor--username--helper" />
</div>
<Modal.ButtonFooter>
<Button
disabled={isCurrentlySaving}
onClick={() => {
const handleCancel = () => {
handleBack();
setNewUsername(username);
};
const hasChanges = newUsername !== username;
if (hasChanges) {
setConfirmDiscardAction(() => handleCancel);
} else {
handleCancel();
}
}}
variant={ButtonVariant.Secondary}
>
{i18n('cancel')}
</Button>
<Button
disabled={shouldDisableUsernameSave}
onClick={checkThenSaveUsername}
>
{isCurrentlySaving ? (
<Spinner size="20px" svgSize="small" direction="on-avatar" />
) : (
i18n('save')
)}
</Button>
</Modal.ButtonFooter>
</>
);
2021-07-19 19:26:06 +00:00
} else if (editState === EditState.None) {
content = (
<>
2021-08-06 00:17:05 +00:00
<AvatarPreview
avatarColor={color}
2021-07-19 19:26:06 +00:00
avatarPath={avatarPath}
2021-08-06 00:17:05 +00:00
avatarValue={avatarBuffer}
conversationTitle={getFullNameText()}
2021-07-19 19:26:06 +00:00
i18n={i18n}
2021-08-06 21:35:25 +00:00
isEditable
2021-08-06 00:17:05 +00:00
onAvatarLoaded={handleAvatarLoaded}
onClick={() => {
setEditState(EditState.BetterAvatar);
}}
style={{
2021-08-06 21:35:25 +00:00
height: 80,
width: 80,
2021-07-19 19:26:06 +00:00
}}
/>
<hr className="ProfileEditor__divider" />
<PanelRow
className="ProfileEditor__row"
icon={
<i className="ProfileEditor__icon--container ProfileEditor__icon ProfileEditor__icon--name" />
}
2021-08-06 00:17:05 +00:00
label={getFullNameText()}
2021-07-19 19:26:06 +00:00
onClick={() => {
setEditState(EditState.ProfileName);
}}
/>
{isUsernameFlagEnabled ? (
<PanelRow
className="ProfileEditor__row"
icon={
<i className="ProfileEditor__icon--container ProfileEditor__icon ProfileEditor__icon--username" />
}
label={username || i18n('ProfileEditor--username')}
onClick={
usernameEditState !== UsernameEditState.Saving
? () => {
setNewUsername(username);
setEditState(EditState.Username);
}
: undefined
}
actions={
username ? (
<ConversationDetailsIcon
ariaLabel={i18n('ProfileEditor--username--delete-username')}
icon={
usernameEditState === UsernameEditState.Saving
? IconType.spinner
: IconType.trash
}
disabled={usernameEditState === UsernameEditState.Saving}
fakeButton
onClick={() => {
setUsernameEditState(UsernameEditState.ConfirmingDelete);
}}
/>
) : null
}
/>
) : null}
2021-07-19 19:26:06 +00:00
<PanelRow
className="ProfileEditor__row"
icon={
fullBio.aboutEmoji ? (
<div className="ProfileEditor__icon--container">
<Emoji emoji={fullBio.aboutEmoji} size={24} />
</div>
) : (
<i className="ProfileEditor__icon--container ProfileEditor__icon ProfileEditor__icon--bio" />
)
}
label={fullBio.aboutText || i18n('ProfileEditor--about')}
onClick={() => {
setEditState(EditState.Bio);
}}
/>
<hr className="ProfileEditor__divider" />
<div className="ProfileEditor__info">
<Intl
i18n={i18n}
id="ProfileEditor--info"
components={{
learnMore: (
<a
href="https://support.signal.org/hc/en-us/articles/360007459591"
target="_blank"
rel="noreferrer"
>
{i18n('ProfileEditor--learnMore')}
</a>
),
}}
/>
</div>
</>
);
} else {
throw missingCaseError(editState);
}
return (
<>
{usernameEditState === UsernameEditState.ConfirmingDelete && (
<ConfirmationDialog
i18n={i18n}
onClose={() => setUsernameEditState(UsernameEditState.Editing)}
actions={[
{
text: i18n('ProfileEditor--username--confirm-delete-button'),
style: 'negative',
action: () => deleteUsername(),
},
]}
>
{i18n('ProfileEditor--username--confirm-delete-body')}
</ConfirmationDialog>
)}
{usernameEditState === UsernameEditState.ShowingErrorPopup && (
<ConfirmationDialog
cancelText={i18n('ok')}
cancelButtonVariant={ButtonVariant.Secondary}
i18n={i18n}
onClose={() => setUsernameEditState(UsernameEditState.Editing)}
>
{i18n('ProfileEditor--username--general-error')}
</ConfirmationDialog>
)}
2021-07-19 19:26:06 +00:00
{confirmDiscardAction && (
2021-08-06 00:17:05 +00:00
<ConfirmDiscardDialog
2021-07-19 19:26:06 +00:00
i18n={i18n}
2021-08-06 00:17:05 +00:00
onDiscard={confirmDiscardAction}
2021-07-19 19:26:06 +00:00
onClose={() => setConfirmDiscardAction(undefined)}
2021-08-06 00:17:05 +00:00
/>
2021-07-19 19:26:06 +00:00
)}
<div className="ProfileEditor">{content}</div>
</>
);
};