2019-08-09 00:46:49 +00:00
|
|
|
import memoizee from 'memoizee';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { createSelector } from 'reselect';
|
2019-05-31 22:42:01 +00:00
|
|
|
import { getSearchResultsProps } from '../../shims/Whisper';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
import { StateType } from '../reducer';
|
|
|
|
|
|
|
|
import {
|
2019-08-09 00:46:49 +00:00
|
|
|
MessageSearchResultLookupType,
|
|
|
|
MessageSearchResultType,
|
|
|
|
SearchStateType,
|
|
|
|
} from '../ducks/search';
|
|
|
|
import {
|
|
|
|
ConversationLookupType,
|
|
|
|
ConversationType,
|
|
|
|
} from '../ducks/conversations';
|
|
|
|
|
|
|
|
import {
|
|
|
|
PropsDataType as SearchResultsPropsType,
|
|
|
|
SearchResultRowType,
|
|
|
|
} from '../../components/SearchResults';
|
|
|
|
import { PropsDataType as MessageSearchResultPropsDataType } from '../../components/MessageSearchResult';
|
|
|
|
|
|
|
|
import { getRegionCode, getUserNumber } from './user';
|
|
|
|
import {
|
|
|
|
GetConversationByIdType,
|
2019-01-14 21:49:58 +00:00
|
|
|
getConversationLookup,
|
2019-08-09 00:46:49 +00:00
|
|
|
getConversationSelector,
|
2019-01-14 21:49:58 +00:00
|
|
|
getSelectedConversation,
|
|
|
|
} from './conversations';
|
2019-03-12 00:20:16 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export const getSearch = (state: StateType): SearchStateType => state.search;
|
|
|
|
|
|
|
|
export const getQuery = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType): string => state.query
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getSelectedMessage = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType): string | undefined => state.selectedMessage
|
|
|
|
);
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
export const getSearchConversationId = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType): string | undefined => state.searchConversationId
|
|
|
|
);
|
|
|
|
|
|
|
|
export const getSearchConversationName = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType): string | undefined => state.searchConversationName
|
|
|
|
);
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export const isSearching = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType) => {
|
2019-08-20 19:34:52 +00:00
|
|
|
const { query } = state;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-20 19:34:52 +00:00
|
|
|
return query && query.trim().length > 1;
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
export const getMessageSearchResultLookup = createSelector(
|
|
|
|
getSearch,
|
|
|
|
(state: SearchStateType) => state.messageLookup
|
|
|
|
);
|
2019-01-14 21:49:58 +00:00
|
|
|
export const getSearchResults = createSelector(
|
2019-08-09 00:46:49 +00:00
|
|
|
[getSearch, getRegionCode, getConversationLookup, getSelectedConversation],
|
2019-01-14 21:49:58 +00:00
|
|
|
(
|
|
|
|
state: SearchStateType,
|
2019-03-12 00:20:16 +00:00
|
|
|
regionCode: string,
|
2019-01-14 21:49:58 +00:00
|
|
|
lookup: ConversationLookupType,
|
2019-08-09 00:46:49 +00:00
|
|
|
selectedConversation?: string
|
2019-09-04 14:46:28 +00:00
|
|
|
// tslint:disable-next-line max-func-body-length
|
2019-08-20 19:34:52 +00:00
|
|
|
): SearchResultsPropsType | undefined => {
|
2019-08-09 23:12:29 +00:00
|
|
|
const {
|
|
|
|
contacts,
|
|
|
|
conversations,
|
2019-09-04 14:46:28 +00:00
|
|
|
discussionsLoading,
|
2019-08-09 23:12:29 +00:00
|
|
|
messageIds,
|
2019-09-04 14:46:28 +00:00
|
|
|
messagesLoading,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationName,
|
|
|
|
} = state;
|
2019-08-09 00:46:49 +00:00
|
|
|
|
|
|
|
const showStartNewConversation = Boolean(
|
|
|
|
state.normalizedPhoneNumber && !lookup[state.normalizedPhoneNumber]
|
|
|
|
);
|
|
|
|
const haveConversations = conversations && conversations.length;
|
|
|
|
const haveContacts = contacts && contacts.length;
|
|
|
|
const haveMessages = messageIds && messageIds.length;
|
|
|
|
const noResults =
|
2019-09-04 14:46:28 +00:00
|
|
|
!discussionsLoading &&
|
|
|
|
!messagesLoading &&
|
2019-08-09 00:46:49 +00:00
|
|
|
!showStartNewConversation &&
|
|
|
|
!haveConversations &&
|
|
|
|
!haveContacts &&
|
|
|
|
!haveMessages;
|
|
|
|
|
|
|
|
const items: Array<SearchResultRowType> = [];
|
|
|
|
|
|
|
|
if (showStartNewConversation) {
|
|
|
|
items.push({
|
|
|
|
type: 'start-new-conversation',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveConversations) {
|
|
|
|
items.push({
|
|
|
|
type: 'conversations-header',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
conversations.forEach(id => {
|
|
|
|
const data = lookup[id];
|
|
|
|
items.push({
|
|
|
|
type: 'conversation',
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
isSelected: Boolean(data && id === selectedConversation),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2019-09-04 14:46:28 +00:00
|
|
|
} else if (discussionsLoading) {
|
|
|
|
items.push({
|
|
|
|
type: 'conversations-header',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
items.push({
|
|
|
|
type: 'spinner',
|
|
|
|
data: undefined,
|
|
|
|
});
|
2019-08-09 00:46:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (haveContacts) {
|
|
|
|
items.push({
|
|
|
|
type: 'contacts-header',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
contacts.forEach(id => {
|
|
|
|
const data = lookup[id];
|
|
|
|
items.push({
|
|
|
|
type: 'contact',
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
isSelected: Boolean(data && id === selectedConversation),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveMessages) {
|
|
|
|
items.push({
|
|
|
|
type: 'messages-header',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
messageIds.forEach(messageId => {
|
|
|
|
items.push({
|
|
|
|
type: 'message',
|
|
|
|
data: messageId,
|
|
|
|
});
|
|
|
|
});
|
2019-09-04 14:46:28 +00:00
|
|
|
} else if (messagesLoading) {
|
|
|
|
items.push({
|
|
|
|
type: 'messages-header',
|
|
|
|
data: undefined,
|
|
|
|
});
|
|
|
|
items.push({
|
|
|
|
type: 'spinner',
|
|
|
|
data: undefined,
|
|
|
|
});
|
2019-08-09 00:46:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return {
|
2019-09-04 14:46:28 +00:00
|
|
|
discussionsLoading,
|
2019-08-09 00:46:49 +00:00
|
|
|
items,
|
2019-09-04 14:46:28 +00:00
|
|
|
messagesLoading,
|
2019-08-09 00:46:49 +00:00
|
|
|
noResults,
|
2019-03-12 00:20:16 +00:00
|
|
|
regionCode: regionCode,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationName,
|
2019-01-14 21:49:58 +00:00
|
|
|
searchTerm: state.query,
|
2019-08-09 00:46:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export function _messageSearchResultSelector(
|
|
|
|
message: MessageSearchResultType,
|
|
|
|
// @ts-ignore
|
|
|
|
ourNumber: string,
|
|
|
|
// @ts-ignore
|
|
|
|
regionCode: string,
|
|
|
|
// @ts-ignore
|
|
|
|
sender?: ConversationType,
|
|
|
|
// @ts-ignore
|
|
|
|
recipient?: ConversationType,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId?: string,
|
2019-08-09 00:46:49 +00:00
|
|
|
selectedMessageId?: string
|
|
|
|
): MessageSearchResultPropsDataType {
|
|
|
|
// Note: We don't use all of those parameters here, but the shim we call does.
|
|
|
|
// We want to call this function again if any of those parameters change.
|
|
|
|
return {
|
|
|
|
...getSearchResultsProps(message),
|
|
|
|
isSelected: message.id === selectedMessageId,
|
2019-08-09 23:12:29 +00:00
|
|
|
isSearchingInConversation: Boolean(searchConversationId),
|
2019-08-09 00:46:49 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// A little optimization to reset our selector cache whenever high-level application data
|
|
|
|
// changes: regionCode and userNumber.
|
|
|
|
type CachedMessageSearchResultSelectorType = (
|
|
|
|
message: MessageSearchResultType,
|
|
|
|
ourNumber: string,
|
|
|
|
regionCode: string,
|
|
|
|
sender?: ConversationType,
|
|
|
|
recipient?: ConversationType,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId?: string,
|
2019-08-09 00:46:49 +00:00
|
|
|
selectedMessageId?: string
|
|
|
|
) => MessageSearchResultPropsDataType;
|
|
|
|
export const getCachedSelectorForMessageSearchResult = createSelector(
|
|
|
|
getRegionCode,
|
|
|
|
getUserNumber,
|
|
|
|
(): CachedMessageSearchResultSelectorType => {
|
|
|
|
// Note: memoizee will check all parameters provided, and only run our selector
|
|
|
|
// if any of them have changed.
|
|
|
|
return memoizee(_messageSearchResultSelector, { max: 500 });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
type GetMessageSearchResultByIdType = (
|
|
|
|
id: string
|
|
|
|
) => MessageSearchResultPropsDataType | undefined;
|
|
|
|
export const getMessageSearchResultSelector = createSelector(
|
|
|
|
getCachedSelectorForMessageSearchResult,
|
|
|
|
getMessageSearchResultLookup,
|
|
|
|
getSelectedMessage,
|
|
|
|
getConversationSelector,
|
2019-08-09 23:12:29 +00:00
|
|
|
getSearchConversationId,
|
2019-08-09 00:46:49 +00:00
|
|
|
getRegionCode,
|
|
|
|
getUserNumber,
|
|
|
|
(
|
|
|
|
messageSearchResultSelector: CachedMessageSearchResultSelectorType,
|
|
|
|
messageSearchResultLookup: MessageSearchResultLookupType,
|
|
|
|
selectedMessage: string | undefined,
|
|
|
|
conversationSelector: GetConversationByIdType,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId: string | undefined,
|
2019-08-09 00:46:49 +00:00
|
|
|
regionCode: string,
|
|
|
|
ourNumber: string
|
|
|
|
): GetMessageSearchResultByIdType => {
|
|
|
|
return (id: string) => {
|
|
|
|
const message = messageSearchResultLookup[id];
|
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { conversationId, source, type } = message;
|
|
|
|
let sender: ConversationType | undefined;
|
|
|
|
let recipient: ConversationType | undefined;
|
|
|
|
|
|
|
|
if (type === 'incoming') {
|
|
|
|
sender = conversationSelector(source);
|
|
|
|
recipient = conversationSelector(ourNumber);
|
|
|
|
} else if (type === 'outgoing') {
|
|
|
|
sender = conversationSelector(ourNumber);
|
|
|
|
recipient = conversationSelector(conversationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageSearchResultSelector(
|
|
|
|
message,
|
|
|
|
ourNumber,
|
|
|
|
regionCode,
|
|
|
|
sender,
|
|
|
|
recipient,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId,
|
2019-08-09 00:46:49 +00:00
|
|
|
selectedMessage
|
|
|
|
);
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|