signal-desktop/ts/components/conversation/conversation-details/ConversationDetailsHeader.tsx

192 lines
4.8 KiB
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React, { useState } from 'react';
import { Avatar } from '../../Avatar';
2021-08-06 00:17:05 +00:00
import { AvatarLightbox } from '../../AvatarLightbox';
import type { ConversationType } from '../../../state/ducks/conversations';
2021-08-06 00:17:05 +00:00
import { Emojify } from '../Emojify';
2021-06-02 00:24:28 +00:00
import { GroupDescription } from '../GroupDescription';
import { About } from '../About';
import type { GroupV2Membership } from './ConversationDetailsMembershipList';
2021-11-02 23:01:13 +00:00
import type { LocalizerType, ThemeType } from '../../../types/Util';
import { bemGenerator } from './util';
2021-11-02 23:01:13 +00:00
import { BadgeDialog } from '../../BadgeDialog';
import type { BadgeType } from '../../../badges/types';
2021-11-09 15:51:56 +00:00
import { shouldShowBadges } from '../../../badges/shouldShowBadges';
export type Props = {
2021-11-02 23:01:13 +00:00
badges?: ReadonlyArray<BadgeType>;
canEdit: boolean;
conversation: ConversationType;
i18n: LocalizerType;
isGroup: boolean;
isMe: boolean;
memberships: Array<GroupV2Membership>;
startEditing: (isGroupTitle: boolean) => void;
2021-11-02 23:01:13 +00:00
theme: ThemeType;
};
2021-11-02 23:01:13 +00:00
enum ConversationDetailsHeaderActiveModal {
ShowingAvatar,
ShowingBadges,
}
const bem = bemGenerator('ConversationDetails-header');
export const ConversationDetailsHeader: React.ComponentType<Props> = ({
2021-11-02 23:01:13 +00:00
badges,
canEdit,
conversation,
i18n,
isGroup,
isMe,
memberships,
startEditing,
2021-11-02 23:01:13 +00:00
theme,
}) => {
2021-11-02 23:01:13 +00:00
const [activeModal, setActiveModal] = useState<
undefined | ConversationDetailsHeaderActiveModal
>();
2021-08-06 00:17:05 +00:00
2021-11-02 23:01:13 +00:00
let preferredBadge: undefined | BadgeType;
2021-06-02 00:24:28 +00:00
let subtitle: ReactNode;
if (isGroup) {
if (conversation.groupDescription) {
subtitle = (
<GroupDescription
i18n={i18n}
text={conversation.groupDescription}
title={conversation.title}
/>
);
} else if (canEdit) {
subtitle = i18n('ConversationDetailsHeader--add-group-description');
} else {
subtitle = i18n('ConversationDetailsHeader--members', [
memberships.length.toString(),
]);
}
} else if (!isMe) {
subtitle = (
<>
<div className={bem('subtitle__about')}>
<About text={conversation.about} />
</div>
<div className={bem('subtitle__phone-number')}>
{conversation.phoneNumber}
</div>
</>
);
2021-11-02 23:01:13 +00:00
preferredBadge = badges?.[0];
2021-06-02 00:24:28 +00:00
}
2021-08-06 00:17:05 +00:00
const avatar = (
<Avatar
2021-11-02 23:01:13 +00:00
badge={preferredBadge}
conversationType={conversation.type}
2021-08-06 00:17:05 +00:00
i18n={i18n}
size={80}
{...conversation}
noteToSelf={isMe}
2021-11-02 23:01:13 +00:00
onClick={() => {
setActiveModal(
2021-11-09 15:51:56 +00:00
preferredBadge && shouldShowBadges()
2021-11-02 23:01:13 +00:00
? ConversationDetailsHeaderActiveModal.ShowingBadges
: ConversationDetailsHeaderActiveModal.ShowingAvatar
);
}}
2021-08-06 00:17:05 +00:00
sharedGroupNames={[]}
2021-11-02 23:01:13 +00:00
theme={theme}
2021-08-06 00:17:05 +00:00
/>
);
const contents = (
2021-08-06 00:17:05 +00:00
<div>
<div className={bem('title')}>
<Emojify text={isMe ? i18n('noteToSelf') : conversation.title} />
</div>
2021-08-06 00:17:05 +00:00
</div>
);
2021-11-02 23:01:13 +00:00
let modal: ReactNode;
switch (activeModal) {
case ConversationDetailsHeaderActiveModal.ShowingAvatar:
modal = (
<AvatarLightbox
avatarColor={conversation.color}
avatarPath={conversation.avatarPath}
conversationTitle={conversation.title}
i18n={i18n}
isGroup={isGroup}
onClose={() => {
setActiveModal(undefined);
}}
/>
);
break;
case ConversationDetailsHeaderActiveModal.ShowingBadges:
modal = (
<BadgeDialog
badges={badges || []}
firstName={conversation.firstName}
i18n={i18n}
onClose={() => {
setActiveModal(undefined);
}}
title={conversation.title}
/>
);
break;
default:
modal = null;
break;
}
2021-08-06 00:17:05 +00:00
if (canEdit) {
return (
<div className={bem('root')}>
2021-11-02 23:01:13 +00:00
{modal}
2021-08-06 00:17:05 +00:00
{avatar}
<button
type="button"
onClick={ev => {
ev.preventDefault();
ev.stopPropagation();
startEditing(true);
}}
className={bem('root', 'editable')}
>
{contents}
</button>
<button
type="button"
onClick={ev => {
2021-06-17 17:15:51 +00:00
if (ev.target instanceof HTMLAnchorElement) {
return;
}
ev.preventDefault();
ev.stopPropagation();
startEditing(false);
}}
className={bem('root', 'editable')}
>
<div className={bem('subtitle')}>{subtitle}</div>
</button>
</div>
);
}
2021-08-06 00:17:05 +00:00
return (
<div className={bem('root')}>
2021-11-02 23:01:13 +00:00
{modal}
2021-08-06 00:17:05 +00:00
{avatar}
{contents}
<div className={bem('subtitle')}>{subtitle}</div>
2021-08-06 00:17:05 +00:00
</div>
);
};