2021-02-23 20:34:28 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ReactChild, ChangeEvent } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
import type { PhoneNumber } from 'google-libphonenumber';
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
import { LeftPaneHelper } from './LeftPaneHelper';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Row } from '../ConversationList';
|
|
|
|
import { RowType } from '../ConversationList';
|
|
|
|
import type { PropsDataType as ContactListItemPropsType } from '../conversationList/ContactListItem';
|
|
|
|
import type { PropsData as ConversationListItemPropsType } from '../conversationList/ConversationListItem';
|
2021-05-11 00:50:43 +00:00
|
|
|
import { SearchInput } from '../SearchInput';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../../types/Util';
|
2021-02-23 20:34:28 +00:00
|
|
|
import {
|
|
|
|
instance as phoneNumberInstance,
|
|
|
|
PhoneNumberFormat,
|
|
|
|
} from '../../util/libphonenumberInstance';
|
2021-03-03 20:09:58 +00:00
|
|
|
import { assert } from '../../util/assert';
|
|
|
|
import { missingCaseError } from '../../util/missingCaseError';
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
export type LeftPaneComposePropsType = {
|
|
|
|
composeContacts: ReadonlyArray<ContactListItemPropsType>;
|
2021-10-22 18:12:10 +00:00
|
|
|
composeGroups: ReadonlyArray<ConversationListItemPropsType>;
|
2021-02-23 20:34:28 +00:00
|
|
|
regionCode: string;
|
|
|
|
searchTerm: string;
|
|
|
|
};
|
|
|
|
|
2021-03-03 20:09:58 +00:00
|
|
|
enum TopButton {
|
|
|
|
None,
|
|
|
|
CreateNewGroup,
|
|
|
|
StartNewConversation,
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:38:50 +00:00
|
|
|
export class LeftPaneComposeHelper extends LeftPaneHelper<LeftPaneComposePropsType> {
|
2021-02-23 20:34:28 +00:00
|
|
|
private readonly composeContacts: ReadonlyArray<ContactListItemPropsType>;
|
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
private readonly composeGroups: ReadonlyArray<ConversationListItemPropsType>;
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
private readonly searchTerm: string;
|
|
|
|
|
|
|
|
private readonly phoneNumber: undefined | PhoneNumber;
|
|
|
|
|
|
|
|
constructor({
|
|
|
|
composeContacts,
|
2021-04-20 23:16:49 +00:00
|
|
|
composeGroups,
|
2021-02-23 20:34:28 +00:00
|
|
|
regionCode,
|
|
|
|
searchTerm,
|
|
|
|
}: Readonly<LeftPaneComposePropsType>) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.searchTerm = searchTerm;
|
|
|
|
this.phoneNumber = parsePhoneNumber(searchTerm, regionCode);
|
2021-04-20 23:16:49 +00:00
|
|
|
this.composeGroups = composeGroups;
|
|
|
|
this.composeContacts = composeContacts;
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getHeaderContents({
|
|
|
|
i18n,
|
|
|
|
showInbox,
|
|
|
|
}: Readonly<{
|
|
|
|
i18n: LocalizerType;
|
|
|
|
showInbox: () => void;
|
|
|
|
}>): ReactChild {
|
|
|
|
return (
|
|
|
|
<div className="module-left-pane__header__contents">
|
|
|
|
<button
|
2021-04-02 21:43:39 +00:00
|
|
|
onClick={this.getBackAction({ showInbox })}
|
2021-02-23 20:34:28 +00:00
|
|
|
className="module-left-pane__header__contents__back-button"
|
|
|
|
title={i18n('backToInbox')}
|
|
|
|
aria-label={i18n('backToInbox')}
|
|
|
|
type="button"
|
|
|
|
/>
|
|
|
|
<div className="module-left-pane__header__contents__text">
|
|
|
|
{i18n('newConversation')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:43:39 +00:00
|
|
|
getBackAction({ showInbox }: { showInbox: () => void }): () => void {
|
|
|
|
return showInbox;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
getPreRowsNode({
|
|
|
|
i18n,
|
|
|
|
onChangeComposeSearchTerm,
|
|
|
|
}: Readonly<{
|
|
|
|
i18n: LocalizerType;
|
|
|
|
onChangeComposeSearchTerm: (
|
|
|
|
event: ChangeEvent<HTMLInputElement>
|
|
|
|
) => unknown;
|
|
|
|
}>): ReactChild {
|
|
|
|
return (
|
|
|
|
<>
|
2021-05-11 00:50:43 +00:00
|
|
|
<SearchInput
|
|
|
|
moduleClassName="module-left-pane__compose-search-form"
|
|
|
|
onChange={onChangeComposeSearchTerm}
|
|
|
|
placeholder={i18n('contactSearchPlaceholder')}
|
|
|
|
ref={focusRef}
|
|
|
|
value={this.searchTerm}
|
|
|
|
/>
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
{this.getRowCount() ? null : (
|
|
|
|
<div className="module-left-pane__compose-no-contacts">
|
2021-04-20 23:16:49 +00:00
|
|
|
{i18n('noConversationsFound')}
|
2021-02-23 20:34:28 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowCount(): number {
|
2021-04-20 23:16:49 +00:00
|
|
|
let result = this.composeContacts.length + this.composeGroups.length;
|
2021-03-03 20:09:58 +00:00
|
|
|
if (this.hasTopButton()) {
|
|
|
|
result += 1;
|
|
|
|
}
|
|
|
|
if (this.hasContactsHeader()) {
|
|
|
|
result += 1;
|
|
|
|
}
|
2021-04-20 23:16:49 +00:00
|
|
|
if (this.hasGroupsHeader()) {
|
|
|
|
result += 1;
|
|
|
|
}
|
|
|
|
|
2021-03-03 20:09:58 +00:00
|
|
|
return result;
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
getRow(actualRowIndex: number): undefined | Row {
|
|
|
|
let virtualRowIndex = actualRowIndex;
|
|
|
|
if (this.hasTopButton()) {
|
|
|
|
if (virtualRowIndex === 0) {
|
|
|
|
const topButton = this.getTopButton();
|
|
|
|
switch (topButton) {
|
|
|
|
case TopButton.None:
|
|
|
|
break;
|
|
|
|
case TopButton.StartNewConversation:
|
|
|
|
assert(
|
2021-03-03 20:09:58 +00:00
|
|
|
this.phoneNumber,
|
2021-04-20 23:16:49 +00:00
|
|
|
'LeftPaneComposeHelper: we should have a phone number if the top button is "Start new conversation"'
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
type: RowType.StartNewConversation,
|
|
|
|
phoneNumber: phoneNumberInstance.format(
|
|
|
|
this.phoneNumber,
|
|
|
|
PhoneNumberFormat.E164
|
|
|
|
),
|
|
|
|
};
|
|
|
|
case TopButton.CreateNewGroup:
|
|
|
|
return { type: RowType.CreateNewGroup };
|
|
|
|
default:
|
|
|
|
throw missingCaseError(topButton);
|
|
|
|
}
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
virtualRowIndex -= 1;
|
2021-03-03 20:09:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
if (this.hasContactsHeader()) {
|
|
|
|
if (virtualRowIndex === 0) {
|
|
|
|
return {
|
|
|
|
type: RowType.Header,
|
|
|
|
i18nKey: 'contactsHeader',
|
|
|
|
};
|
|
|
|
}
|
2021-02-23 20:34:28 +00:00
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
virtualRowIndex -= 1;
|
|
|
|
|
|
|
|
const contact = this.composeContacts[virtualRowIndex];
|
|
|
|
if (contact) {
|
|
|
|
return {
|
2021-02-23 20:34:28 +00:00
|
|
|
type: RowType.Contact,
|
|
|
|
contact,
|
2021-04-20 23:16:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtualRowIndex -= this.composeContacts.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasGroupsHeader()) {
|
|
|
|
if (virtualRowIndex === 0) {
|
|
|
|
return {
|
|
|
|
type: RowType.Header,
|
|
|
|
i18nKey: 'groupsHeader',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
virtualRowIndex -= 1;
|
|
|
|
|
|
|
|
const group = this.composeGroups[virtualRowIndex];
|
|
|
|
return {
|
|
|
|
type: RowType.Conversation,
|
|
|
|
conversation: group,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
2021-02-23 20:34:28 +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;
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
shouldRecomputeRowHeights(
|
|
|
|
exProps: Readonly<LeftPaneComposePropsType>
|
|
|
|
): boolean {
|
|
|
|
const prev = new LeftPaneComposeHelper(exProps);
|
|
|
|
const currHeaderIndices = this.getHeaderIndices();
|
|
|
|
const prevHeaderIndices = prev.getHeaderIndices();
|
|
|
|
|
2021-03-03 20:09:58 +00:00
|
|
|
return (
|
2021-04-20 23:16:49 +00:00
|
|
|
currHeaderIndices.top !== prevHeaderIndices.top ||
|
|
|
|
currHeaderIndices.contact !== prevHeaderIndices.contact ||
|
|
|
|
currHeaderIndices.group !== prevHeaderIndices.group
|
2021-03-03 20:09:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getTopButton(): TopButton {
|
|
|
|
if (this.phoneNumber) {
|
|
|
|
return TopButton.StartNewConversation;
|
|
|
|
}
|
2021-10-05 17:04:28 +00:00
|
|
|
if (this.searchTerm) {
|
2021-03-03 20:09:58 +00:00
|
|
|
return TopButton.None;
|
|
|
|
}
|
|
|
|
return TopButton.CreateNewGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasTopButton(): boolean {
|
|
|
|
return this.getTopButton() !== TopButton.None;
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasContactsHeader(): boolean {
|
2021-04-20 23:16:49 +00:00
|
|
|
return Boolean(this.composeContacts.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
private hasGroupsHeader(): boolean {
|
|
|
|
return Boolean(this.composeGroups.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
private getHeaderIndices(): {
|
|
|
|
top?: number;
|
|
|
|
contact?: number;
|
|
|
|
group?: number;
|
|
|
|
} {
|
|
|
|
let top: number | undefined;
|
|
|
|
let contact: number | undefined;
|
|
|
|
let group: number | undefined;
|
|
|
|
let rowCount = 0;
|
|
|
|
if (this.hasTopButton()) {
|
|
|
|
top = 0;
|
|
|
|
rowCount += 1;
|
|
|
|
}
|
|
|
|
if (this.composeContacts.length) {
|
|
|
|
contact = rowCount;
|
|
|
|
rowCount += this.composeContacts.length;
|
|
|
|
}
|
|
|
|
if (this.composeGroups.length) {
|
|
|
|
group = rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
top,
|
|
|
|
contact,
|
|
|
|
group,
|
|
|
|
};
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusRef(el: HTMLElement | null) {
|
|
|
|
if (el) {
|
|
|
|
el.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parsePhoneNumber(
|
|
|
|
str: string,
|
|
|
|
regionCode: string
|
|
|
|
): undefined | PhoneNumber {
|
|
|
|
let result: PhoneNumber;
|
|
|
|
try {
|
|
|
|
result = phoneNumberInstance.parse(str, regionCode);
|
|
|
|
} catch (err) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!phoneNumberInstance.isValidNumber(result)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|