signal-desktop/ts/components/conversation/ContactModal.tsx

299 lines
9.5 KiB
TypeScript
Raw Normal View History

2022-07-22 00:44:35 +00:00
// Copyright 2020-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2021-09-21 22:37:10 +00:00
import React, { useEffect, useState } from 'react';
import type { ReactNode } from 'react';
2022-06-16 19:12:50 +00:00
import type {
ConversationType,
ShowConversationType,
} from '../../state/ducks/conversations';
2022-07-22 00:44:35 +00:00
import type { BadgeType } from '../../badges/types';
import type { HasStories } from '../../types/Stories';
2021-11-11 16:23:00 +00:00
import type { LocalizerType, ThemeType } from '../../types/Util';
2022-07-22 00:44:35 +00:00
import * as log from '../../logging/log';
import { About } from './About';
import { Avatar } from '../Avatar';
import { AvatarLightbox } from '../AvatarLightbox';
2021-11-02 23:01:13 +00:00
import { BadgeDialog } from '../BadgeDialog';
2021-09-21 22:37:10 +00:00
import { ConfirmationDialog } from '../ConfirmationDialog';
2022-07-22 00:44:35 +00:00
import { Modal } from '../Modal';
import { RemoveGroupMemberConfirmationDialog } from './RemoveGroupMemberConfirmationDialog';
2022-07-22 00:44:35 +00:00
import { SharedGroupNames } from '../SharedGroupNames';
import { missingCaseError } from '../../util/missingCaseError';
2021-09-21 22:37:10 +00:00
export type PropsDataType = {
areWeASubscriber: boolean;
areWeAdmin: boolean;
2021-11-02 23:01:13 +00:00
badges: ReadonlyArray<BadgeType>;
contact?: ConversationType;
conversation?: ConversationType;
2022-07-22 00:44:35 +00:00
hasStories?: HasStories;
readonly i18n: LocalizerType;
isAdmin: boolean;
isMember: boolean;
2021-11-11 16:23:00 +00:00
theme: ThemeType;
};
2021-09-21 22:37:10 +00:00
type PropsActionType = {
hideContactModal: () => void;
removeMemberFromGroup: (conversationId: string, contactId: string) => void;
2022-06-16 19:12:50 +00:00
showConversation: ShowConversationType;
2021-09-21 22:37:10 +00:00
toggleAdmin: (conversationId: string, contactId: string) => void;
toggleSafetyNumberModal: (conversationId: string) => unknown;
2021-09-21 22:37:10 +00:00
updateConversationModelSharedGroups: (conversationId: string) => void;
2022-07-22 00:44:35 +00:00
viewUserStories: (cid: string) => unknown;
2021-09-21 22:37:10 +00:00
};
export type PropsType = PropsDataType & PropsActionType;
2021-11-02 23:01:13 +00:00
enum ContactModalView {
Default,
ShowingAvatar,
ShowingBadges,
}
enum SubModalState {
None = 'None',
ToggleAdmin = 'ToggleAdmin',
MemberRemove = 'MemberRemove',
}
export const ContactModal = ({
areWeASubscriber,
areWeAdmin,
2021-11-02 23:01:13 +00:00
badges,
contact,
conversation,
2022-07-22 00:44:35 +00:00
hasStories,
2021-09-21 22:37:10 +00:00
hideContactModal,
i18n,
isAdmin,
isMember,
2021-09-21 22:37:10 +00:00
removeMemberFromGroup,
2022-06-16 19:12:50 +00:00
showConversation,
2021-11-11 16:23:00 +00:00
theme,
toggleAdmin,
toggleSafetyNumberModal,
2021-09-21 22:37:10 +00:00
updateConversationModelSharedGroups,
2022-07-22 00:44:35 +00:00
viewUserStories,
2021-09-21 22:37:10 +00:00
}: PropsType): JSX.Element => {
if (!contact) {
throw new Error('Contact modal opened without a matching contact');
}
2021-11-02 23:01:13 +00:00
const [view, setView] = useState(ContactModalView.Default);
const [subModalState, setSubModalState] = useState<SubModalState>(
SubModalState.None
);
2021-08-06 00:17:05 +00:00
useEffect(() => {
if (contact?.id) {
2021-09-21 22:37:10 +00:00
// Kick off the expensive hydration of the current sharedGroupNames
updateConversationModelSharedGroups(contact.id);
}
}, [contact?.id, updateConversationModelSharedGroups]);
let modalNode: ReactNode;
switch (subModalState) {
case SubModalState.None:
modalNode = undefined;
break;
case SubModalState.ToggleAdmin:
if (!conversation?.id) {
log.warn('ContactModal: ToggleAdmin state - missing conversationId');
modalNode = undefined;
break;
}
modalNode = (
<ConfirmationDialog
actions={[
{
action: () => toggleAdmin(conversation.id, contact.id),
text: isAdmin
? i18n('ContactModal--rm-admin')
: i18n('ContactModal--make-admin'),
},
]}
i18n={i18n}
onClose={() => setSubModalState(SubModalState.None)}
>
{isAdmin
? i18n('ContactModal--rm-admin-info', [contact.title])
: i18n('ContactModal--make-admin-info', [contact.title])}
</ConfirmationDialog>
);
break;
case SubModalState.MemberRemove:
if (!contact || !conversation?.id) {
log.warn(
'ContactModal: MemberRemove state - missing contact or conversationId'
);
modalNode = undefined;
break;
}
modalNode = (
<RemoveGroupMemberConfirmationDialog
conversation={contact}
group={conversation}
i18n={i18n}
onClose={() => {
setSubModalState(SubModalState.None);
}}
onRemove={() => {
removeMemberFromGroup(conversation?.id, contact.id);
}}
/>
);
break;
default: {
const state: never = subModalState;
log.warn(`ContactModal: unexpected ${state}!`);
modalNode = undefined;
break;
}
}
2021-11-02 23:01:13 +00:00
switch (view) {
case ContactModalView.Default: {
const preferredBadge: undefined | BadgeType = badges[0];
2021-09-21 22:37:10 +00:00
2021-11-02 23:01:13 +00:00
return (
<Modal
moduleClassName="ContactModal__modal"
hasXButton
2021-08-06 00:17:05 +00:00
i18n={i18n}
2021-11-02 23:01:13 +00:00
onClose={hideContactModal}
>
<div className="ContactModal">
<Avatar
acceptedMessageRequest={contact.acceptedMessageRequest}
avatarPath={contact.avatarPath}
badge={preferredBadge}
color={contact.color}
conversationType="direct"
2021-09-21 22:37:10 +00:00
i18n={i18n}
2021-11-02 23:01:13 +00:00
isMe={contact.isMe}
name={contact.name}
2022-07-22 00:44:35 +00:00
onClick={() => {
if (conversation && hasStories) {
viewUserStories(conversation.id);
} else {
setView(ContactModalView.ShowingAvatar);
}
}}
onClickBadge={() => setView(ContactModalView.ShowingBadges)}
2021-11-02 23:01:13 +00:00
profileName={contact.profileName}
sharedGroupNames={contact.sharedGroupNames}
size={96}
2022-07-22 00:44:35 +00:00
storyRing={hasStories}
2021-11-11 16:23:00 +00:00
theme={theme}
2021-11-02 23:01:13 +00:00
title={contact.title}
unblurredAvatarPath={contact.unblurredAvatarPath}
/>
<div className="ContactModal__name">{contact.title}</div>
<div className="module-about__container">
<About text={contact.about} />
</div>
{contact.phoneNumber && (
<div className="ContactModal__info">{contact.phoneNumber}</div>
)}
{!contact.isMe && (
<div className="ContactModal__info">
<SharedGroupNames
i18n={i18n}
sharedGroupNames={contact.sharedGroupNames || []}
/>
</div>
2021-11-02 23:01:13 +00:00
)}
<div className="ContactModal__button-container">
<button
type="button"
2021-11-02 23:01:13 +00:00
className="ContactModal__button ContactModal__send-message"
onClick={() => {
hideContactModal();
2022-06-16 19:12:50 +00:00
showConversation({ conversationId: contact.id });
2021-11-02 23:01:13 +00:00
}}
>
2021-09-21 22:37:10 +00:00
<div className="ContactModal__bubble-icon">
2021-11-02 23:01:13 +00:00
<div className="ContactModal__send-message__bubble-icon" />
</div>
2021-11-02 23:01:13 +00:00
<span>{i18n('ContactModal--message')}</span>
</button>
2021-11-02 23:01:13 +00:00
{!contact.isMe && (
<button
type="button"
className="ContactModal__button ContactModal__safety-number"
onClick={() => {
hideContactModal();
toggleSafetyNumberModal(contact.id);
}}
>
<div className="ContactModal__bubble-icon">
<div className="ContactModal__safety-number__bubble-icon" />
</div>
<span>{i18n('showSafetyNumber')}</span>
</button>
)}
{!contact.isMe && areWeAdmin && isMember && conversation?.id && (
2021-11-02 23:01:13 +00:00
<>
<button
type="button"
className="ContactModal__button ContactModal__make-admin"
onClick={() => setSubModalState(SubModalState.ToggleAdmin)}
2021-11-02 23:01:13 +00:00
>
<div className="ContactModal__bubble-icon">
<div className="ContactModal__make-admin__bubble-icon" />
</div>
{isAdmin ? (
<span>{i18n('ContactModal--rm-admin')}</span>
) : (
<span>{i18n('ContactModal--make-admin')}</span>
)}
</button>
<button
type="button"
className="ContactModal__button ContactModal__remove-from-group"
onClick={() => setSubModalState(SubModalState.MemberRemove)}
2021-11-02 23:01:13 +00:00
>
<div className="ContactModal__bubble-icon">
<div className="ContactModal__remove-from-group__bubble-icon" />
</div>
<span>{i18n('ContactModal--remove-from-group')}</span>
</button>
</>
)}
</div>
{modalNode}
2021-11-02 23:01:13 +00:00
</div>
</Modal>
);
}
case ContactModalView.ShowingAvatar:
return (
<AvatarLightbox
avatarColor={contact.color}
avatarPath={contact.avatarPath}
conversationTitle={contact.title}
i18n={i18n}
onClose={() => setView(ContactModalView.Default)}
/>
);
case ContactModalView.ShowingBadges:
return (
<BadgeDialog
areWeASubscriber={areWeASubscriber}
2021-11-02 23:01:13 +00:00
badges={badges}
firstName={contact.firstName}
i18n={i18n}
onClose={() => setView(ContactModalView.Default)}
title={contact.title}
/>
);
default:
throw missingCaseError(view);
}
};