signal-desktop/ts/components/leftPane/LeftPaneChooseGroupMembersHelper.tsx

416 lines
12 KiB
TypeScript
Raw Normal View History

2022-01-27 22:12:26 +00:00
// Copyright 2021-2022 Signal Messenger, LLC
2021-03-03 20:09:58 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactChild, ChangeEvent } from 'react';
import React from 'react';
2021-03-03 20:09:58 +00:00
import { LeftPaneHelper } from './LeftPaneHelper';
import type { Row } from '../ConversationList';
import { RowType } from '../ConversationList';
import type { ConversationType } from '../../state/ducks/conversations';
2021-03-03 20:09:58 +00:00
import { ContactCheckboxDisabledReason } from '../conversationList/ContactCheckbox';
import { ContactPills } from '../ContactPills';
import { ContactPill } from '../ContactPill';
2021-05-11 00:50:43 +00:00
import { SearchInput } from '../SearchInput';
2021-03-11 21:29:31 +00:00
import {
AddGroupMemberErrorDialog,
AddGroupMemberErrorDialogMode,
} from '../AddGroupMemberErrorDialog';
2021-03-03 20:09:58 +00:00
import { Button } from '../Button';
import type { LocalizerType } from '../../types/Util';
import { getUsernameFromSearch } from '../../types/Username';
import type { ParsedE164Type } from '../../util/libphonenumberInstance';
import { parseAndFormatPhoneNumber } from '../../util/libphonenumberInstance';
import type { UUIDFetchStateType } from '../../util/uuidFetchState';
2022-06-17 00:38:28 +00:00
import {
isFetchingByUsername,
isFetchingByE164,
} from '../../util/uuidFetchState';
2021-03-03 20:09:58 +00:00
export type LeftPaneChooseGroupMembersPropsType = {
uuidFetchState: UUIDFetchStateType;
2021-03-03 20:09:58 +00:00
candidateContacts: ReadonlyArray<ConversationType>;
groupSizeRecommendedLimit: number;
groupSizeHardLimit: number;
2021-03-03 20:09:58 +00:00
isShowingRecommendedGroupSizeModal: boolean;
isShowingMaximumGroupSizeModal: boolean;
2022-06-17 00:38:28 +00:00
isUsernamesEnabled: boolean;
2021-03-03 20:09:58 +00:00
searchTerm: string;
regionCode: string | undefined;
2021-03-03 20:09:58 +00:00
selectedContacts: Array<ConversationType>;
};
2021-04-26 16:38:50 +00:00
export class LeftPaneChooseGroupMembersHelper extends LeftPaneHelper<LeftPaneChooseGroupMembersPropsType> {
2021-03-03 20:09:58 +00:00
private readonly candidateContacts: ReadonlyArray<ConversationType>;
private readonly isPhoneNumberChecked: boolean;
2022-06-17 00:38:28 +00:00
private readonly isUsernameChecked: boolean;
2021-03-03 20:09:58 +00:00
private readonly isShowingMaximumGroupSizeModal: boolean;
private readonly isShowingRecommendedGroupSizeModal: boolean;
private readonly groupSizeRecommendedLimit: number;
private readonly groupSizeHardLimit: number;
2021-03-03 20:09:58 +00:00
private readonly searchTerm: string;
private readonly phoneNumber: ParsedE164Type | undefined;
2022-06-17 00:38:28 +00:00
private readonly username: string | undefined;
2021-03-03 20:09:58 +00:00
private readonly selectedContacts: Array<ConversationType>;
private readonly selectedConversationIdsSet: Set<string>;
private readonly uuidFetchState: UUIDFetchStateType;
2021-03-03 20:09:58 +00:00
constructor({
candidateContacts,
isShowingMaximumGroupSizeModal,
isShowingRecommendedGroupSizeModal,
2022-06-17 00:38:28 +00:00
isUsernamesEnabled,
groupSizeRecommendedLimit,
groupSizeHardLimit,
2021-03-03 20:09:58 +00:00
searchTerm,
regionCode,
2021-03-03 20:09:58 +00:00
selectedContacts,
uuidFetchState,
2021-03-03 20:09:58 +00:00
}: Readonly<LeftPaneChooseGroupMembersPropsType>) {
super();
this.uuidFetchState = uuidFetchState;
this.groupSizeRecommendedLimit = groupSizeRecommendedLimit - 1;
this.groupSizeHardLimit = groupSizeHardLimit - 1;
2021-03-03 20:09:58 +00:00
this.candidateContacts = candidateContacts;
this.isShowingMaximumGroupSizeModal = isShowingMaximumGroupSizeModal;
2021-11-11 22:43:05 +00:00
this.isShowingRecommendedGroupSizeModal =
isShowingRecommendedGroupSizeModal;
2021-03-03 20:09:58 +00:00
this.searchTerm = searchTerm;
2022-10-18 17:12:02 +00:00
if (isUsernamesEnabled) {
2022-06-17 00:38:28 +00:00
const username = getUsernameFromSearch(searchTerm);
const isVisible = this.candidateContacts.every(
contact => contact.username !== username
);
if (isVisible) {
this.username = username;
}
this.isUsernameChecked = selectedContacts.some(
contact => contact.username === this.username
);
} else {
this.isUsernameChecked = false;
}
2022-10-18 17:12:02 +00:00
const phoneNumber = parseAndFormatPhoneNumber(searchTerm, regionCode);
if (!this.username && phoneNumber) {
this.isPhoneNumberChecked =
phoneNumber.isValid &&
selectedContacts.some(contact => contact.e164 === phoneNumber.e164);
const isVisible = this.candidateContacts.every(
contact => contact.e164 !== phoneNumber.e164
);
if (isVisible) {
this.phoneNumber = phoneNumber;
}
} else {
this.isPhoneNumberChecked = false;
}
2021-03-03 20:09:58 +00:00
this.selectedContacts = selectedContacts;
this.selectedConversationIdsSet = new Set(
selectedContacts.map(contact => contact.id)
);
}
override getHeaderContents({
2021-03-03 20:09:58 +00:00
i18n,
startComposing,
}: Readonly<{
i18n: LocalizerType;
startComposing: () => void;
}>): ReactChild {
const backButtonLabel = i18n('chooseGroupMembers__back-button');
return (
<div className="module-left-pane__header__contents">
<button
aria-label={backButtonLabel}
className="module-left-pane__header__contents__back-button"
onClick={this.getBackAction({ startComposing })}
2021-03-03 20:09:58 +00:00
title={backButtonLabel}
type="button"
/>
<div className="module-left-pane__header__contents__text">
{i18n('chooseGroupMembers__title')}
</div>
</div>
);
}
override getBackAction({
startComposing,
}: {
startComposing: () => void;
}): () => void {
return startComposing;
}
2022-01-27 22:12:26 +00:00
override getSearchInput({
i18n,
onChangeComposeSearchTerm,
}: Readonly<{
i18n: LocalizerType;
onChangeComposeSearchTerm: (
event: ChangeEvent<HTMLInputElement>
) => unknown;
}>): ReactChild {
return (
<SearchInput
2022-02-14 17:57:11 +00:00
i18n={i18n}
2022-01-27 22:12:26 +00:00
moduleClassName="module-left-pane__compose-search-form"
onChange={onChangeComposeSearchTerm}
placeholder={i18n('contactSearchPlaceholder')}
ref={focusRef}
value={this.searchTerm}
/>
);
}
override getPreRowsNode({
2021-03-03 20:09:58 +00:00
closeMaximumGroupSizeModal,
closeRecommendedGroupSizeModal,
i18n,
removeSelectedContact,
}: Readonly<{
closeMaximumGroupSizeModal: () => unknown;
closeRecommendedGroupSizeModal: () => unknown;
i18n: LocalizerType;
removeSelectedContact: (conversationId: string) => unknown;
}>): ReactChild {
2021-03-11 21:29:31 +00:00
let modalNode: undefined | ReactChild;
2021-03-03 20:09:58 +00:00
if (this.isShowingMaximumGroupSizeModal) {
2021-03-11 21:29:31 +00:00
modalNode = (
<AddGroupMemberErrorDialog
i18n={i18n}
maximumNumberOfContacts={this.groupSizeHardLimit}
2021-03-11 21:29:31 +00:00
mode={AddGroupMemberErrorDialogMode.MaximumGroupSize}
onClose={closeMaximumGroupSizeModal}
/>
);
2021-03-03 20:09:58 +00:00
} else if (this.isShowingRecommendedGroupSizeModal) {
2021-03-11 21:29:31 +00:00
modalNode = (
<AddGroupMemberErrorDialog
i18n={i18n}
recommendedMaximumNumberOfContacts={this.groupSizeRecommendedLimit}
2021-03-11 21:29:31 +00:00
mode={AddGroupMemberErrorDialogMode.RecommendedMaximumGroupSize}
onClose={closeRecommendedGroupSizeModal}
/>
);
2021-03-03 20:09:58 +00:00
}
return (
<>
{Boolean(this.selectedContacts.length) && (
<ContactPills>
{this.selectedContacts.map(contact => (
<ContactPill
key={contact.id}
2021-05-07 22:21:10 +00:00
acceptedMessageRequest={contact.acceptedMessageRequest}
2021-03-03 20:09:58 +00:00
avatarPath={contact.avatarPath}
color={contact.color}
firstName={contact.systemGivenName ?? contact.firstName}
2021-03-03 20:09:58 +00:00
i18n={i18n}
id={contact.id}
2021-05-07 22:21:10 +00:00
isMe={contact.isMe}
2021-03-03 20:09:58 +00:00
phoneNumber={contact.phoneNumber}
profileName={contact.profileName}
2021-05-07 22:21:10 +00:00
sharedGroupNames={contact.sharedGroupNames}
2021-03-03 20:09:58 +00:00
title={contact.title}
onClickRemove={removeSelectedContact}
/>
))}
</ContactPills>
)}
{this.getRowCount() ? null : (
<div className="module-left-pane__compose-no-contacts">
2021-03-11 21:29:31 +00:00
{i18n('noContactsFound')}
2021-03-03 20:09:58 +00:00
</div>
)}
2021-03-11 21:29:31 +00:00
{modalNode}
2021-03-03 20:09:58 +00:00
</>
);
}
override getFooterContents({
2021-03-03 20:09:58 +00:00
i18n,
startSettingGroupMetadata,
}: Readonly<{
i18n: LocalizerType;
startSettingGroupMetadata: () => void;
}>): ReactChild {
return (
<Button
disabled={this.hasExceededMaximumNumberOfContacts()}
onClick={startSettingGroupMetadata}
>
{this.selectedContacts.length
? i18n('chooseGroupMembers__next')
: i18n('chooseGroupMembers__skip')}
</Button>
);
}
getRowCount(): number {
let rowCount = 0;
// Header + Phone Number
if (this.phoneNumber) {
rowCount += 2;
2021-03-03 20:09:58 +00:00
}
2022-06-17 00:38:28 +00:00
// Header + Username
if (this.username) {
rowCount += 2;
}
// Header + Contacts
if (this.candidateContacts.length) {
rowCount += 1 + this.candidateContacts.length;
}
// Footer
if (rowCount > 0) {
rowCount += 1;
}
return rowCount;
2021-03-03 20:09:58 +00:00
}
getRow(actualRowIndex: number): undefined | Row {
2022-06-17 00:38:28 +00:00
if (!this.candidateContacts.length && !this.phoneNumber && !this.username) {
2021-03-03 20:09:58 +00:00
return undefined;
}
const rowCount = this.getRowCount();
2021-03-03 20:09:58 +00:00
// This puts a blank row for the footer.
if (actualRowIndex === rowCount - 1) {
2021-03-03 20:09:58 +00:00
return { type: RowType.Blank };
}
let virtualRowIndex = actualRowIndex;
if (this.candidateContacts.length) {
if (virtualRowIndex === 0) {
return {
type: RowType.Header,
i18nKey: 'contactsHeader',
};
}
if (virtualRowIndex <= this.candidateContacts.length) {
const contact = this.candidateContacts[virtualRowIndex - 1];
const isChecked = this.selectedConversationIdsSet.has(contact.id);
const disabledReason =
!isChecked && this.hasSelectedMaximumNumberOfContacts()
? ContactCheckboxDisabledReason.MaximumContactsSelected
: undefined;
return {
type: RowType.ContactCheckbox,
contact,
isChecked,
disabledReason,
};
}
virtualRowIndex -= 1 + this.candidateContacts.length;
}
if (this.phoneNumber) {
if (virtualRowIndex === 0) {
return {
type: RowType.Header,
i18nKey: 'findByPhoneNumberHeader',
};
}
if (virtualRowIndex === 1) {
return {
type: RowType.PhoneNumberCheckbox,
isChecked: this.isPhoneNumberChecked,
isFetching: isFetchingByE164(
this.uuidFetchState,
this.phoneNumber.e164
),
phoneNumber: this.phoneNumber,
};
}
virtualRowIndex -= 2;
2021-03-03 20:09:58 +00:00
}
2022-06-17 00:38:28 +00:00
if (this.username) {
if (virtualRowIndex === 0) {
return {
type: RowType.Header,
i18nKey: 'findByUsernameHeader',
};
}
if (virtualRowIndex === 1) {
return {
type: RowType.UsernameCheckbox,
isChecked: this.isUsernameChecked,
isFetching: isFetchingByUsername(this.uuidFetchState, this.username),
username: this.username,
};
}
virtualRowIndex -= 2;
}
return undefined;
2021-03-03 20:09:58 +00:00
}
// This is deliberately unimplemented because these keyboard shortcuts shouldn't work in
// the composer. The same is true for the "in direction" function below.
getConversationAndMessageAtIndex(
..._args: ReadonlyArray<unknown>
): undefined {
return undefined;
}
getConversationAndMessageInDirection(
..._args: ReadonlyArray<unknown>
): undefined {
return undefined;
}
shouldRecomputeRowHeights(_old: unknown): boolean {
return false;
}
private hasSelectedMaximumNumberOfContacts(): boolean {
return this.selectedContacts.length >= this.groupSizeHardLimit;
2021-03-03 20:09:58 +00:00
}
private hasExceededMaximumNumberOfContacts(): boolean {
// It should be impossible to reach this state. This is here as a failsafe.
return this.selectedContacts.length > this.groupSizeHardLimit;
2021-03-03 20:09:58 +00:00
}
}
function focusRef(el: HTMLElement | null) {
if (el) {
el.focus();
}
}