diff --git a/ts/state/selectors/search.ts b/ts/state/selectors/search.ts index 5ed07a074949..e727b0f802f3 100644 --- a/ts/state/selectors/search.ts +++ b/ts/state/selectors/search.ts @@ -26,6 +26,7 @@ import type { GetConversationByIdType } from './conversations'; import { getConversationLookup, getConversationSelector, + getSelectedConversationId, } from './conversations'; import { hydrateRanges } from '../../types/BodyRange'; @@ -113,11 +114,17 @@ export const getMessageSearchResultLookup = createSelector( ); export const getSearchResults = createSelector( - [getSearch, getSearchConversationName, getConversationLookup], + [ + getSearch, + getSearchConversationName, + getConversationLookup, + getSelectedConversationId, + ], ( state: SearchStateType, searchConversationName, - conversationLookup: ConversationLookupType + conversationLookup: ConversationLookupType, + selectedConversationId: string | undefined ): Pick< LeftPaneSearchPropsType, | 'conversationResults' @@ -136,7 +143,7 @@ export const getSearchResults = createSelector( messagesLoading, } = state; - return { + const searchResults: ReturnType = { conversationResults: discussionsLoading ? { isLoading: true } : { @@ -159,6 +166,21 @@ export const getSearchResults = createSelector( searchTerm: state.query, filterByUnread: state.filterByUnread, }; + + if ( + state.filterByUnread && + searchResults.conversationResults.isLoading === false + ) { + searchResults.conversationResults.results = + searchResults.conversationResults.results.map(conversation => { + return { + ...conversation, + isSelected: selectedConversationId === conversation.id, + }; + }); + } + + return searchResults; } ); diff --git a/ts/test-both/state/selectors/search_test.ts b/ts/test-both/state/selectors/search_test.ts index e13646b517c6..1f3504276d7a 100644 --- a/ts/test-both/state/selectors/search_test.ts +++ b/ts/test-both/state/selectors/search_test.ts @@ -454,5 +454,74 @@ describe('both/state/selectors/search', () => { filterByUnread: false, }); }); + + it('adds isSelected flag to conversations when filterByUnread is true', () => { + const conversations: Array = [ + getDefaultConversation({ id: '1' }), + getDefaultConversation({ id: 'selected-id' }), + ]; + + const state: StateType = { + ...getEmptyRootState(), + conversations: { + ...getEmptyConversationState(), + conversationLookup: makeLookup(conversations, 'id'), + selectedConversationId: 'selected-id', + }, + search: { + ...getEmptySearchState(), + query: 'foo bar', + conversationIds: conversations.map(({ id }) => id), + discussionsLoading: false, + filterByUnread: true, + }, + }; + + const searchResults = getSearchResults(state); + + assert.deepEqual(searchResults.conversationResults, { + isLoading: false, + results: [ + { + ...conversations[0], + isSelected: false, + }, + { + ...conversations[1], + isSelected: true, + }, + ], + }); + }); + + it('does not add isSelected flag to conversations when filterByUnread is false', () => { + const conversations: Array = [ + getDefaultConversation({ id: '1' }), + getDefaultConversation({ id: '2' }), + ]; + + const state: StateType = { + ...getEmptyRootState(), + conversations: { + ...getEmptyConversationState(), + conversationLookup: makeLookup(conversations, 'id'), + selectedConversationId: '2', + }, + search: { + ...getEmptySearchState(), + query: 'foo bar', + conversationIds: conversations.map(({ id }) => id), + discussionsLoading: false, + filterByUnread: false, + }, + }; + + const searchResults = getSearchResults(state); + + assert.deepEqual(searchResults.conversationResults, { + isLoading: false, + results: conversations, + }); + }); }); });