signal-desktop/ts/components/conversationList/ContactCheckbox.tsx

131 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
2021-03-03 20:09:58 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import type { FunctionComponent } from 'react';
2021-03-03 20:09:58 +00:00
import { HEADER_CONTACT_NAME_CLASS_NAME } from './BaseConversationListItem';
import type { ConversationType } from '../../state/ducks/conversations';
2021-11-17 21:11:21 +00:00
import type { BadgeType } from '../../badges/types';
import type { LocalizerType, ThemeType } from '../../types/Util';
2021-03-03 20:09:58 +00:00
import { ContactName } from '../conversation/ContactName';
import { About } from '../conversation/About';
import { ListTile } from '../ListTile';
import { Avatar, AvatarSize } from '../Avatar';
2021-03-03 20:09:58 +00:00
export enum ContactCheckboxDisabledReason {
// We start the enum at 1 because the default starting value of 0 is falsy.
2021-03-11 21:29:31 +00:00
AlreadyAdded = 1,
MaximumContactsSelected,
2021-03-03 20:09:58 +00:00
}
export type PropsDataType = {
2021-11-17 21:11:21 +00:00
badge: undefined | BadgeType;
2021-03-03 20:09:58 +00:00
disabledReason?: ContactCheckboxDisabledReason;
isChecked: boolean;
} & Pick<
ConversationType,
| 'about'
| 'acceptedMessageRequest'
| 'avatarPath'
| 'color'
2023-01-13 00:24:59 +00:00
| 'groupId'
| 'id'
| 'isMe'
| 'phoneNumber'
| 'profileName'
| 'sharedGroupNames'
| 'title'
| 'type'
| 'unblurredAvatarPath'
2023-08-16 20:54:39 +00:00
| 'serviceId'
>;
2021-03-03 20:09:58 +00:00
type PropsHousekeepingType = {
i18n: LocalizerType;
onClick: (
id: string,
disabledReason: undefined | ContactCheckboxDisabledReason
) => void;
2021-11-17 21:11:21 +00:00
theme: ThemeType;
2021-03-03 20:09:58 +00:00
};
type PropsType = PropsDataType & PropsHousekeepingType;
export const ContactCheckbox: FunctionComponent<PropsType> = React.memo(
2021-08-11 19:29:07 +00:00
function ContactCheckbox({
2021-03-03 20:09:58 +00:00
about,
acceptedMessageRequest,
2021-03-03 20:09:58 +00:00
avatarPath,
2021-11-17 21:11:21 +00:00
badge,
2021-03-03 20:09:58 +00:00
color,
disabledReason,
i18n,
id,
isChecked,
2021-04-27 22:35:35 +00:00
isMe,
2021-03-03 20:09:58 +00:00
onClick,
phoneNumber,
profileName,
sharedGroupNames,
2021-11-17 21:11:21 +00:00
theme,
2021-03-03 20:09:58 +00:00
title,
type,
unblurredAvatarPath,
2021-08-11 19:29:07 +00:00
}) {
2021-03-03 20:09:58 +00:00
const disabled = Boolean(disabledReason);
2021-04-27 22:35:35 +00:00
const headerName = isMe ? (
2023-03-10 18:21:21 +00:00
<ContactName
module={HEADER_CONTACT_NAME_CLASS_NAME}
2023-03-30 00:03:25 +00:00
title={i18n('icu:noteToSelf')}
2023-03-10 18:21:21 +00:00
isMe={isMe}
2023-03-02 06:57:35 +00:00
/>
2021-04-27 22:35:35 +00:00
) : (
2021-10-14 15:48:48 +00:00
<ContactName module={HEADER_CONTACT_NAME_CLASS_NAME} title={title} />
2021-03-03 20:09:58 +00:00
);
let messageText: undefined | string | JSX.Element;
2021-03-11 21:29:31 +00:00
if (disabledReason === ContactCheckboxDisabledReason.AlreadyAdded) {
2023-03-30 00:03:25 +00:00
messageText = i18n('icu:alreadyAMember');
2021-03-11 21:29:31 +00:00
} else if (about) {
messageText = <About className="" text={about} />;
}
2021-03-03 20:09:58 +00:00
const onClickItem = () => {
onClick(id, disabledReason);
};
return (
<ListTile.checkbox
clickable
2021-03-03 20:09:58 +00:00
disabled={disabled}
isChecked={isChecked}
leading={
<Avatar
acceptedMessageRequest={acceptedMessageRequest}
avatarPath={avatarPath}
color={color}
conversationType={type}
noteToSelf={Boolean(isMe)}
i18n={i18n}
isMe={isMe}
phoneNumber={phoneNumber}
profileName={profileName}
title={title}
sharedGroupNames={sharedGroupNames}
size={AvatarSize.THIRTY_TWO}
unblurredAvatarPath={unblurredAvatarPath}
// appease the type checker.
{...(badge ? { badge, theme } : { badge: undefined })}
/>
}
title={headerName}
subtitle={isMe ? undefined : messageText}
subtitleMaxLines={1}
2021-03-03 20:09:58 +00:00
onClick={onClickItem}
/>
);
}
);