signal-desktop/ts/components/ProfileEditorModal.tsx

81 lines
2.2 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, { useState } from 'react';
import { Modal } from './Modal';
import { ConfirmationDialog } from './ConfirmationDialog';
import type { PropsType as ProfileEditorPropsType } from './ProfileEditor';
import { ProfileEditor, EditState } from './ProfileEditor';
import type { ProfileDataType } from '../state/ducks/conversations';
import type { AvatarUpdateType } from '../types/Avatar';
2021-07-19 19:26:06 +00:00
export type PropsDataType = {
hasError: boolean;
};
type PropsType = {
myProfileChanged: (
profileData: ProfileDataType,
avatar: AvatarUpdateType
2021-07-19 19:26:06 +00:00
) => unknown;
toggleProfileEditor: () => unknown;
toggleProfileEditorHasError: () => unknown;
} & PropsDataType &
Omit<ProfileEditorPropsType, 'onEditStateChanged' | 'onProfileChanged'>;
2021-07-19 19:26:06 +00:00
export const ProfileEditorModal = ({
hasError,
i18n,
myProfileChanged,
onSetSkinTone,
toggleProfileEditor,
toggleProfileEditorHasError,
...restProps
}: PropsType): JSX.Element => {
const MODAL_TITLES_BY_EDIT_STATE: Record<EditState, string> = {
[EditState.BetterAvatar]: i18n('ProfileEditorModal--avatar'),
[EditState.Bio]: i18n('ProfileEditorModal--about'),
[EditState.None]: i18n('ProfileEditorModal--profile'),
[EditState.ProfileName]: i18n('ProfileEditorModal--name'),
[EditState.Username]: i18n('ProfileEditorModal--username'),
2021-07-19 19:26:06 +00:00
};
const [modalTitle, setModalTitle] = useState(
MODAL_TITLES_BY_EDIT_STATE[EditState.None]
);
2021-07-19 19:26:06 +00:00
if (hasError) {
return (
<ConfirmationDialog
cancelText={i18n('Confirmation--confirm')}
i18n={i18n}
onClose={toggleProfileEditorHasError}
>
{i18n('ProfileEditorModal--error')}
</ConfirmationDialog>
);
}
return (
<>
<Modal
2021-08-06 00:17:05 +00:00
hasStickyButtons
2021-07-19 19:26:06 +00:00
hasXButton
i18n={i18n}
onClose={toggleProfileEditor}
title={modalTitle}
>
<ProfileEditor
{...restProps}
i18n={i18n}
onEditStateChanged={editState => {
setModalTitle(MODAL_TITLES_BY_EDIT_STATE[editState]);
2021-07-19 19:26:06 +00:00
}}
onProfileChanged={myProfileChanged}
2021-07-19 19:26:06 +00:00
onSetSkinTone={onSetSkinTone}
/>
</Modal>
</>
);
};