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

238 lines
6.2 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';
2022-12-09 20:37:45 +00:00
import { Avatar, AvatarSize } from '../../Avatar';
2021-08-06 00:17:05 +00:00
import { AvatarLightbox } from '../../AvatarLightbox';
import type { ConversationType } from '../../../state/ducks/conversations';
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 { assertDev } from '../../../util/assert';
2021-11-02 23:01:13 +00:00
import { BadgeDialog } from '../../BadgeDialog';
import type { BadgeType } from '../../../badges/types';
2023-04-20 17:03:43 +00:00
import { UserText } from '../../UserText';
export type Props = {
areWeASubscriber: boolean;
2021-11-02 23:01:13 +00:00
badges?: ReadonlyArray<BadgeType>;
canEdit: boolean;
conversation: ConversationType;
i18n: LocalizerType;
isGroup: boolean;
isMe: boolean;
memberships: ReadonlyArray<GroupV2Membership>;
startEditing: (isGroupTitle: boolean) => void;
toggleAboutContactModal: (contactId: string) => void;
2021-11-02 23:01:13 +00:00
theme: ThemeType;
};
2021-11-02 23:01:13 +00:00
enum ConversationDetailsHeaderActiveModal {
ShowingAvatar,
ShowingBadges,
}
2022-11-18 00:45:19 +00:00
export function ConversationDetailsHeader({
areWeASubscriber,
2021-11-02 23:01:13 +00:00
badges,
canEdit,
conversation,
i18n,
isGroup,
isMe,
memberships,
startEditing,
toggleAboutContactModal,
2021-11-02 23:01:13 +00:00
theme,
2022-11-18 00:45:19 +00:00
}: Props): JSX.Element {
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;
let hasNestedButton = false;
if (isGroup) {
if (conversation.groupDescription) {
subtitle = (
<GroupDescription
i18n={i18n}
text={conversation.groupDescription}
title={conversation.title}
/>
);
hasNestedButton = true;
} else if (canEdit) {
2023-03-30 00:03:25 +00:00
subtitle = i18n('icu:ConversationDetailsHeader--add-group-description');
} else {
2023-03-30 00:03:25 +00:00
subtitle = i18n('icu:ConversationDetailsHeader--members', {
2023-04-03 19:03:00 +00:00
number: memberships.length,
2023-03-27 23:37:39 +00:00
});
}
} else if (!isMe) {
subtitle = (
<>
<div className="ConversationDetailsHeader__subtitle__about">
<About text={conversation.about} />
</div>
<div className="ConversationDetailsHeader__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}
2022-12-09 20:37:45 +00:00
size={AvatarSize.EIGHTY}
2021-08-06 00:17:05 +00:00
{...conversation}
noteToSelf={isMe}
2021-11-02 23:01:13 +00:00
onClick={() => {
2021-11-18 20:01:53 +00:00
setActiveModal(ConversationDetailsHeaderActiveModal.ShowingAvatar);
}}
onClickBadge={() => {
setActiveModal(ConversationDetailsHeaderActiveModal.ShowingBadges);
2021-11-02 23:01:13 +00:00
}}
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
/>
);
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
areWeASubscriber={areWeASubscriber}
2021-11-02 23:01:13 +00:00
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) {
assertDev(isGroup, 'Only groups support editable title');
return (
<div
className="ConversationDetailsHeader"
data-testid="ConversationDetailsHeader"
>
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="ConversationDetailsHeader__edit-button"
>
<div className="ConversationDetailsHeader__title">
<UserText text={conversation.title} />
</div>
</button>
{hasNestedButton ? (
<div className="ConversationDetailsHeader__subtitle">{subtitle}</div>
) : (
<button
type="button"
onClick={ev => {
if (ev.target instanceof HTMLAnchorElement) {
return;
}
ev.preventDefault();
ev.stopPropagation();
startEditing(false);
}}
className="ConversationDetailsHeader__edit-button"
>
<div className="ConversationDetailsHeader__subtitle">
{subtitle}
</div>
</button>
)}
</div>
);
}
let title: JSX.Element;
if (isMe) {
title = (
<div className="ConversationDetailsHeader__title">
{i18n('icu:noteToSelf')}
<span className="ContactModal__official-badge__large" />
</div>
);
} else if (isGroup) {
title = (
<div className="ConversationDetailsHeader__title">
<UserText text={conversation.title} />
</div>
);
} else {
title = (
<button
type="button"
onClick={ev => {
ev.preventDefault();
ev.stopPropagation();
toggleAboutContactModal(conversation.id);
}}
className="ConversationDetailsHeader__about-button"
>
<div className="ConversationDetailsHeader__title">
<UserText text={conversation.title} />
<span className="ConversationDetailsHeader__about-icon" />
</div>
</button>
);
}
2021-08-06 00:17:05 +00:00
return (
<div
className="ConversationDetailsHeader"
data-testid="ConversationDetailsHeader"
>
2021-11-02 23:01:13 +00:00
{modal}
2021-08-06 00:17:05 +00:00
{avatar}
{title}
<div className="ConversationDetailsHeader__subtitle">{subtitle}</div>
2021-08-06 00:17:05 +00:00
</div>
);
2022-11-18 00:45:19 +00:00
}