diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -30,7 +30,10 @@ getAllThreadPermissions, makePermissionsBlob, } from '../permissions/thread-permissions.js'; -import type { ChatThreadItem } from '../selectors/chat-selectors.js'; +import type { + ChatThreadItem, + SidebarItem, +} from '../selectors/chat-selectors.js'; import { threadInfoSelector, pendingToRealizedThreadIDsSelector, @@ -1416,6 +1419,49 @@ return null; } +function getSearchResultsForEmptySearchText( + chatListData: $ReadOnlyArray, + threadFilter: ThreadInfo => boolean, +): Array { + const threadHomeSubscriptions = Object.fromEntries( + chatListData.map(chatThreadItem => [ + chatThreadItem.threadInfo.id, + threadIsInHome(chatThreadItem.threadInfo), + ]), + ); + + return chatListData + .filter((item: ChatThreadItem) => { + const { threadInfo } = item; + const { parentThreadID } = threadInfo; + const isInFilteredChatList = + threadInChatList(threadInfo) && threadFilter(threadInfo); + + if (!threadIsSidebar(threadInfo) || !parentThreadID) { + return isInFilteredChatList; + } + const isParentInHome = threadHomeSubscriptions[parentThreadID]; + const isThreadInHome = threadIsInHome(threadInfo); + + return isParentInHome !== isThreadInHome && isInFilteredChatList; + }) + .map((item: ChatThreadItem) => { + if (threadIsSidebar(item.threadInfo)) { + return item; + } + const sidebarsOnlyInSameTab = item.sidebars.filter( + (sidebar: SidebarItem) => + sidebar.type !== 'sidebar' || + threadIsInHome(sidebar.threadInfo) === + threadIsInHome(item.threadInfo), + ); + return { + ...item, + sidebars: sidebarsOnlyInSameTab, + }; + }); +} + function getThreadListSearchResults( chatListData: $ReadOnlyArray, searchText: string, @@ -1425,10 +1471,7 @@ loggedInUserInfo: ?LoggedInUserInfo, ): $ReadOnlyArray { if (!searchText) { - return chatListData.filter( - item => - threadIsTopLevel(item.threadInfo) && threadFilter(item.threadInfo), - ); + return getSearchResultsForEmptySearchText(chatListData, threadFilter); } const privateThreads = [];