// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useCallback, useEffect, useRef, useState } from 'react'; import { AvatarColors, AvatarColorType } from '../types/Colors'; import { AvatarDataType, DeleteAvatarFromDiskActionType, ReplaceAvatarActionType, SaveAvatarToDiskActionType, } from '../types/Avatar'; import { AvatarEditor } from './AvatarEditor'; import { AvatarPreview } from './AvatarPreview'; import { Button, ButtonVariant } from './Button'; import { ConfirmDiscardDialog } from './ConfirmDiscardDialog'; import { Emoji } from './emoji/Emoji'; import { EmojiButton, Props as EmojiButtonProps } from './emoji/EmojiButton'; import { EmojiPickDataType } from './emoji/EmojiPicker'; import { Input } from './Input'; import { Intl } from './Intl'; import { LocalizerType } from '../types/Util'; import { Modal } from './Modal'; import { PanelRow } from './conversation/conversation-details/PanelRow'; import { ProfileDataType } from '../state/ducks/conversations'; import { getEmojiData, unifiedToEmoji } from './emoji/lib'; import { missingCaseError } from '../util/missingCaseError'; export enum EditState { None = 'None', BetterAvatar = 'BetterAvatar', ProfileName = 'ProfileName', Bio = 'Bio', } type PropsExternalType = { onEditStateChanged: (editState: EditState) => unknown; onProfileChanged: ( profileData: ProfileDataType, avatarBuffer?: Uint8Array ) => unknown; }; export type PropsDataType = { aboutEmoji?: string; aboutText?: string; avatarPath?: string; color?: AvatarColorType; conversationId: string; familyName?: string; firstName: string; i18n: LocalizerType; userAvatarData: Array; } & Pick; type PropsActionType = { deleteAvatarFromDisk: DeleteAvatarFromDiskActionType; onSetSkinTone: (tone: number) => unknown; replaceAvatar: ReplaceAvatarActionType; saveAvatarToDisk: SaveAvatarToDiskActionType; }; export type PropsType = PropsDataType & PropsActionType & PropsExternalType; type DefaultBio = { i18nLabel: string; shortName: string; }; const DEFAULT_BIOS: Array = [ { 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', }, ]; export const ProfileEditor = ({ aboutEmoji, aboutText, avatarPath, color, conversationId, deleteAvatarFromDisk, familyName, firstName, i18n, onEditStateChanged, onProfileChanged, onSetSkinTone, recentEmojis, replaceAvatar, saveAvatarToDisk, skinTone, userAvatarData, }: PropsType): JSX.Element => { const focusInputRef = useRef(null); const [editState, setEditState] = useState(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 [avatarBuffer, setAvatarBuffer] = useState( undefined ); const [stagedProfile, setStagedProfile] = useState({ aboutEmoji, aboutText, familyName, firstName, }); const handleBack = useCallback(() => { setEditState(EditState.None); onEditStateChanged(EditState.None); }, [setEditState, onEditStateChanged]); const setAboutEmoji = useCallback( (ev: EmojiPickDataType) => { const emojiData = getEmojiData(ev.shortName, skinTone); setStagedProfile(profileData => ({ ...profileData, aboutEmoji: unifiedToEmoji(emojiData.unified), })); }, [setStagedProfile, skinTone] ); const handleAvatarChanged = useCallback( (avatar: Uint8Array | undefined) => { setAvatarBuffer(avatar); setEditState(EditState.None); onProfileChanged(stagedProfile, avatar); }, [onProfileChanged, stagedProfile] ); const getTextEncoder = useCallback(() => new TextEncoder(), []); const countByteLength = useCallback( (str: string) => getTextEncoder().encode(str).byteLength, [getTextEncoder] ); const calculateLengthCount = useCallback( (name = '') => { return 256 - countByteLength(name); }, [countByteLength] ); const getFullNameText = () => { return [fullName.firstName, fullName.familyName].filter(Boolean).join(' '); }; useEffect(() => { const focusNode = focusInputRef.current; if (!focusNode) { return; } focusNode.focus(); focusNode.setSelectionRange(focusNode.value.length, focusNode.value.length); }, [editState]); useEffect(() => { onEditStateChanged(editState); }, [editState, onEditStateChanged]); const handleAvatarLoaded = useCallback(avatar => { setAvatarBuffer(avatar); }, []); let content: JSX.Element; if (editState === EditState.BetterAvatar) { content = ( ); } else if (editState === EditState.ProfileName) { const shouldDisableSave = !stagedProfile.firstName || (stagedProfile.firstName === fullName.firstName && stagedProfile.familyName === fullName.familyName); content = ( <> { setStagedProfile(profileData => ({ ...profileData, firstName: String(newFirstName), })); }} placeholder={i18n('ProfileEditor--first-name')} ref={focusInputRef} value={stagedProfile.firstName} /> { setStagedProfile(profileData => ({ ...profileData, familyName: newFamilyName, })); }} placeholder={i18n('ProfileEditor--last-name')} value={stagedProfile.familyName} /> ); } else if (editState === EditState.Bio) { const shouldDisableSave = stagedProfile.aboutText === fullBio.aboutText && stagedProfile.aboutEmoji === fullBio.aboutEmoji; content = ( <> } maxLengthCount={140} moduleClassName="ProfileEditor__about-input" onChange={value => { if (value) { setStagedProfile(profileData => ({ ...profileData, aboutEmoji: stagedProfile.aboutEmoji, aboutText: value.replace(/(\r\n|\n|\r)/gm, ''), })); } else { setStagedProfile(profileData => ({ ...profileData, aboutEmoji: undefined, aboutText: '', })); } }} ref={focusInputRef} placeholder={i18n('ProfileEditor--about-placeholder')} value={stagedProfile.aboutText} whenToShowRemainingCount={40} /> {DEFAULT_BIOS.map(defaultBio => ( } label={i18n(defaultBio.i18nLabel)} onClick={() => { const emojiData = getEmojiData(defaultBio.shortName, skinTone); setStagedProfile(profileData => ({ ...profileData, aboutEmoji: unifiedToEmoji(emojiData.unified), aboutText: i18n(defaultBio.i18nLabel), })); }} /> ))} ); } else if (editState === EditState.None) { content = ( <> { setEditState(EditState.BetterAvatar); }} style={{ height: 80, width: 80, }} />
} label={getFullNameText()} onClick={() => { setEditState(EditState.ProfileName); }} /> ) : ( ) } label={fullBio.aboutText || i18n('ProfileEditor--about')} onClick={() => { setEditState(EditState.Bio); }} />
{i18n('ProfileEditor--learnMore')} ), }} />
); } else { throw missingCaseError(editState); } return ( <> {confirmDiscardAction && ( setConfirmDiscardAction(undefined)} /> )}
{content}
); };