2021-02-23 20:34:28 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, { ReactChild } from 'react';
|
|
|
|
|
|
|
|
import { LeftPaneHelper, ToFindType } from './LeftPaneHelper';
|
|
|
|
import { LocalizerType } from '../../types/Util';
|
|
|
|
import { Row, RowType } from '../ConversationList';
|
|
|
|
import { PropsData as ConversationListItemPropsType } from '../conversationList/ConversationListItem';
|
|
|
|
|
|
|
|
import { Intl } from '../Intl';
|
|
|
|
import { Emojify } from '../conversation/Emojify';
|
2021-04-02 22:32:55 +00:00
|
|
|
import { assert } from '../../util/assert';
|
|
|
|
|
|
|
|
// The "correct" thing to do is to measure the size of the left pane and render enough
|
|
|
|
// search results for the container height. But (1) that's slow (2) the list is
|
|
|
|
// virtualized (3) 99 rows is over 6000px tall, taller than most monitors (4) it's fine
|
|
|
|
// if, in some extremely tall window, we have some empty space. So we just hard-code a
|
|
|
|
// fairly big number.
|
|
|
|
const SEARCH_RESULTS_FAKE_ROW_COUNT = 99;
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
type MaybeLoadedSearchResultsType<T> =
|
|
|
|
| { isLoading: true }
|
|
|
|
| { isLoading: false; results: Array<T> };
|
|
|
|
|
|
|
|
export type LeftPaneSearchPropsType = {
|
|
|
|
conversationResults: MaybeLoadedSearchResultsType<
|
|
|
|
ConversationListItemPropsType
|
|
|
|
>;
|
|
|
|
contactResults: MaybeLoadedSearchResultsType<ConversationListItemPropsType>;
|
|
|
|
messageResults: MaybeLoadedSearchResultsType<{
|
|
|
|
id: string;
|
|
|
|
conversationId: string;
|
|
|
|
}>;
|
|
|
|
searchConversationName?: string;
|
|
|
|
searchTerm: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const searchResultKeys: Array<
|
|
|
|
'conversationResults' | 'contactResults' | 'messageResults'
|
|
|
|
> = ['conversationResults', 'contactResults', 'messageResults'];
|
|
|
|
|
|
|
|
export class LeftPaneSearchHelper extends LeftPaneHelper<
|
|
|
|
LeftPaneSearchPropsType
|
|
|
|
> {
|
|
|
|
private readonly conversationResults: MaybeLoadedSearchResultsType<
|
|
|
|
ConversationListItemPropsType
|
|
|
|
>;
|
|
|
|
|
|
|
|
private readonly contactResults: MaybeLoadedSearchResultsType<
|
|
|
|
ConversationListItemPropsType
|
|
|
|
>;
|
|
|
|
|
|
|
|
private readonly messageResults: MaybeLoadedSearchResultsType<{
|
|
|
|
id: string;
|
|
|
|
conversationId: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
private readonly searchConversationName?: string;
|
|
|
|
|
|
|
|
private readonly searchTerm: string;
|
|
|
|
|
|
|
|
constructor({
|
|
|
|
conversationResults,
|
|
|
|
contactResults,
|
|
|
|
messageResults,
|
|
|
|
searchConversationName,
|
|
|
|
searchTerm,
|
|
|
|
}: Readonly<LeftPaneSearchPropsType>) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.conversationResults = conversationResults;
|
|
|
|
this.contactResults = contactResults;
|
|
|
|
this.messageResults = messageResults;
|
|
|
|
this.searchConversationName = searchConversationName;
|
|
|
|
this.searchTerm = searchTerm;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreRowsNode({
|
|
|
|
i18n,
|
|
|
|
}: Readonly<{ i18n: LocalizerType }>): null | ReactChild {
|
|
|
|
const mightHaveSearchResults = this.allResults().some(
|
|
|
|
searchResult => searchResult.isLoading || searchResult.results.length
|
|
|
|
);
|
|
|
|
if (mightHaveSearchResults) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { searchConversationName, searchTerm } = this;
|
|
|
|
|
|
|
|
return !searchConversationName || searchTerm ? (
|
|
|
|
<div
|
|
|
|
// We need this for Ctrl-T shortcut cycling through parts of app
|
|
|
|
tabIndex={-1}
|
|
|
|
className="module-left-pane__no-search-results"
|
|
|
|
key={searchTerm}
|
|
|
|
>
|
|
|
|
{searchConversationName ? (
|
|
|
|
<Intl
|
|
|
|
id="noSearchResultsInConversation"
|
|
|
|
i18n={i18n}
|
|
|
|
components={{
|
|
|
|
searchTerm,
|
|
|
|
conversationName: (
|
|
|
|
<Emojify key="item-1" text={searchConversationName} />
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
i18n('noSearchResults', [searchTerm])
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRowCount(): number {
|
2021-04-02 22:32:55 +00:00
|
|
|
if (this.isLoading()) {
|
|
|
|
// 1 for the header.
|
|
|
|
return 1 + SEARCH_RESULTS_FAKE_ROW_COUNT;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
return this.allResults().reduce(
|
|
|
|
(result: number, searchResults) =>
|
2021-04-02 22:32:55 +00:00
|
|
|
result + getRowCountForLoadedSearchResults(searchResults),
|
2021-02-23 20:34:28 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is currently unimplemented. See DESKTOP-1170.
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
getRowIndexToScrollTo(
|
|
|
|
_selectedConversationId: undefined | string
|
|
|
|
): undefined | number {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRow(rowIndex: number): undefined | Row {
|
|
|
|
const { conversationResults, contactResults, messageResults } = this;
|
|
|
|
|
2021-04-02 22:32:55 +00:00
|
|
|
if (this.isLoading()) {
|
|
|
|
if (rowIndex === 0) {
|
|
|
|
return { type: RowType.SearchResultsLoadingFakeHeader };
|
|
|
|
}
|
|
|
|
if (rowIndex + 1 <= SEARCH_RESULTS_FAKE_ROW_COUNT) {
|
|
|
|
return { type: RowType.SearchResultsLoadingFakeRow };
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversationRowCount = getRowCountForLoadedSearchResults(
|
2021-02-23 20:34:28 +00:00
|
|
|
conversationResults
|
|
|
|
);
|
2021-04-02 22:32:55 +00:00
|
|
|
const contactRowCount = getRowCountForLoadedSearchResults(contactResults);
|
|
|
|
const messageRowCount = getRowCountForLoadedSearchResults(messageResults);
|
2021-02-23 20:34:28 +00:00
|
|
|
|
|
|
|
if (rowIndex < conversationRowCount) {
|
|
|
|
if (rowIndex === 0) {
|
|
|
|
return {
|
|
|
|
type: RowType.Header,
|
|
|
|
i18nKey: 'conversationsHeader',
|
|
|
|
};
|
|
|
|
}
|
2021-04-02 22:32:55 +00:00
|
|
|
assert(
|
|
|
|
!conversationResults.isLoading,
|
|
|
|
"We shouldn't get here with conversation results still loading"
|
|
|
|
);
|
2021-02-23 20:34:28 +00:00
|
|
|
const conversation = conversationResults.results[rowIndex - 1];
|
|
|
|
return conversation
|
|
|
|
? {
|
|
|
|
type: RowType.Conversation,
|
|
|
|
conversation,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowIndex < conversationRowCount + contactRowCount) {
|
|
|
|
const localIndex = rowIndex - conversationRowCount;
|
|
|
|
if (localIndex === 0) {
|
|
|
|
return {
|
|
|
|
type: RowType.Header,
|
|
|
|
i18nKey: 'contactsHeader',
|
|
|
|
};
|
|
|
|
}
|
2021-04-02 22:32:55 +00:00
|
|
|
assert(
|
|
|
|
!contactResults.isLoading,
|
|
|
|
"We shouldn't get here with contact results still loading"
|
|
|
|
);
|
2021-02-23 20:34:28 +00:00
|
|
|
const conversation = contactResults.results[localIndex - 1];
|
|
|
|
return conversation
|
|
|
|
? {
|
|
|
|
type: RowType.Conversation,
|
|
|
|
conversation,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowIndex >= conversationRowCount + contactRowCount + messageRowCount) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const localIndex = rowIndex - conversationRowCount - contactRowCount;
|
|
|
|
if (localIndex === 0) {
|
|
|
|
return {
|
|
|
|
type: RowType.Header,
|
|
|
|
i18nKey: 'messagesHeader',
|
|
|
|
};
|
|
|
|
}
|
2021-04-02 22:32:55 +00:00
|
|
|
assert(
|
|
|
|
!messageResults.isLoading,
|
|
|
|
"We shouldn't get here with message results still loading"
|
|
|
|
);
|
2021-02-23 20:34:28 +00:00
|
|
|
const message = messageResults.results[localIndex - 1];
|
|
|
|
return message
|
|
|
|
? {
|
|
|
|
type: RowType.MessageSearchResult,
|
|
|
|
messageId: message.id,
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2021-04-02 22:32:55 +00:00
|
|
|
isScrollable(): boolean {
|
|
|
|
return !this.isLoading();
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
shouldRecomputeRowHeights(old: Readonly<LeftPaneSearchPropsType>): boolean {
|
2021-04-02 22:32:55 +00:00
|
|
|
const oldIsLoading = new LeftPaneSearchHelper(old).isLoading();
|
|
|
|
const newIsLoading = this.isLoading();
|
|
|
|
if (oldIsLoading && newIsLoading) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (oldIsLoading !== newIsLoading) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-23 20:34:28 +00:00
|
|
|
return searchResultKeys.some(
|
|
|
|
key =>
|
2021-04-02 22:32:55 +00:00
|
|
|
getRowCountForLoadedSearchResults(old[key]) !==
|
|
|
|
getRowCountForLoadedSearchResults(this[key])
|
2021-02-23 20:34:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is currently unimplemented. See DESKTOP-1170.
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
getConversationAndMessageAtIndex(
|
|
|
|
_conversationIndex: number
|
|
|
|
): undefined | { conversationId: string; messageId?: string } {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is currently unimplemented. See DESKTOP-1170.
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
getConversationAndMessageInDirection(
|
|
|
|
_toFind: Readonly<ToFindType>,
|
|
|
|
_selectedConversationId: undefined | string,
|
|
|
|
_selectedMessageId: unknown
|
|
|
|
): undefined | { conversationId: string } {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
private allResults() {
|
|
|
|
return [this.conversationResults, this.contactResults, this.messageResults];
|
|
|
|
}
|
2021-04-02 22:32:55 +00:00
|
|
|
|
|
|
|
private isLoading(): boolean {
|
|
|
|
return this.allResults().some(results => results.isLoading);
|
|
|
|
}
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 22:32:55 +00:00
|
|
|
function getRowCountForLoadedSearchResults(
|
2021-02-23 20:34:28 +00:00
|
|
|
searchResults: Readonly<MaybeLoadedSearchResultsType<unknown>>
|
|
|
|
): number {
|
2021-04-02 22:32:55 +00:00
|
|
|
// It's possible to call this helper with invalid results (e.g., ones that are loading).
|
|
|
|
// We could change the parameter of this function, but that adds a bunch of redundant
|
|
|
|
// checks that are, in the author's opinion, less clear.
|
2021-02-23 20:34:28 +00:00
|
|
|
if (searchResults.isLoading) {
|
2021-04-02 22:32:55 +00:00
|
|
|
assert(
|
|
|
|
false,
|
|
|
|
'getRowCountForLoadedSearchResults: Expected this to be called with loaded search results. Returning 0'
|
|
|
|
);
|
|
|
|
return 0;
|
2021-02-23 20:34:28 +00:00
|
|
|
}
|
2021-04-02 22:32:55 +00:00
|
|
|
|
|
|
|
const resultRows = searchResults.results.length;
|
|
|
|
const hasHeader = Boolean(resultRows);
|
2021-02-23 20:34:28 +00:00
|
|
|
return (hasHeader ? 1 : 0) + resultRows;
|
|
|
|
}
|