diff --git a/lib/hooks/search-sidebars.js b/lib/hooks/search-threads.js rename from lib/hooks/search-sidebars.js rename to lib/hooks/search-threads.js --- a/lib/hooks/search-sidebars.js +++ b/lib/hooks/search-threads.js @@ -2,27 +2,38 @@ import * as React from 'react'; +import { + type ChatThreadItem, + useFilteredChatListData, +} from '../selectors/chat-selectors'; import { sidebarInfoSelector } from '../selectors/thread-selectors'; import SearchIndex from '../shared/search-index'; -import { threadSearchText } from '../shared/thread-utils'; +import { threadIsChannel, threadSearchText } from '../shared/thread-utils'; import type { SetState } from '../types/hook-types'; -import type { SidebarInfo, ThreadInfo } from '../types/thread-types'; +import type { + SidebarInfo, + ThreadInfo, + RawThreadInfo, +} from '../types/thread-types'; import { useSelector } from '../utils/redux-utils'; -type SidebarSearchState = { +export type ThreadSearchState = { +text: string, +results: $ReadOnlySet, }; -function useSearchSidebars( - threadInfo: ThreadInfo, -): { - +listData: $ReadOnlyArray, - +searchState: SidebarSearchState, - +setSearchState: SetState, +type SearchThreadsResult = { + +listData: $ReadOnlyArray, + +searchState: ThreadSearchState, + +setSearchState: SetState, +onChangeSearchInputText: (text: string) => mixed, +clearQuery: (event: SyntheticEvent) => void, -} { +}; + +function useSearchThreads( + threadInfo: ThreadInfo, + childThreadInfos: $ReadOnlyArray, +): SearchThreadsResult { const [searchState, setSearchState] = React.useState({ text: '', results: new Set(), @@ -30,33 +41,29 @@ const userInfos = useSelector(state => state.userStore.userInfos); - const sidebarInfos = useSelector( - state => sidebarInfoSelector(state)[threadInfo.id] ?? [], - ); - const listData = React.useMemo(() => { if (!searchState.text) { - return sidebarInfos; + return childThreadInfos; } - return sidebarInfos.filter(sidebarInfo => - searchState.results.has(sidebarInfo.threadInfo.id), + return childThreadInfos.filter(thread => + searchState.results.has(thread.threadInfo.id), ); - }, [sidebarInfos, searchState]); + }, [childThreadInfos, searchState]); const viewerID = useSelector( state => state.currentUserInfo && state.currentUserInfo.id, ); const searchIndex = React.useMemo(() => { const index = new SearchIndex(); - for (const sidebarInfo of sidebarInfos) { - const threadInfoFromSidebarInfo = sidebarInfo.threadInfo; + for (const thread of childThreadInfos) { + const threadInfoFromItem = thread.threadInfo; index.addEntry( - threadInfoFromSidebarInfo.id, - threadSearchText(threadInfoFromSidebarInfo, userInfos, viewerID), + threadInfoFromItem.id, + threadSearchText(threadInfoFromItem, userInfos, viewerID), ); } return index; - }, [sidebarInfos, userInfos, viewerID]); + }, [childThreadInfos, userInfos, viewerID]); const onChangeSearchInputText = React.useCallback( (text: string) => { @@ -94,4 +101,23 @@ ); } -export { useSearchSidebars }; +function useSearchSidebars( + threadInfo: ThreadInfo, +): SearchThreadsResult { + const childThreadInfos = useSelector( + state => sidebarInfoSelector(state)[threadInfo.id] ?? [], + ); + return useSearchThreads(threadInfo, childThreadInfos); +} + +function useSearchSubchannels( + threadInfo: ThreadInfo, +): SearchThreadsResult { + const childThreadInfos = useFilteredChatListData( + (thread: ?(ThreadInfo | RawThreadInfo)) => + threadIsChannel(thread) && thread?.parentThreadID === threadInfo.id, + ); + return useSearchThreads(threadInfo, childThreadInfos); +} + +export { useSearchSubchannels, useSearchSidebars }; diff --git a/native/chat/sidebar-list-modal.react.js b/native/chat/sidebar-list-modal.react.js --- a/native/chat/sidebar-list-modal.react.js +++ b/native/chat/sidebar-list-modal.react.js @@ -3,7 +3,7 @@ import * as React from 'react'; import { TextInput, FlatList, View } from 'react-native'; -import { useSearchSidebars } from 'lib/hooks/search-sidebars'; +import { useSearchSidebars } from 'lib/hooks/search-threads'; import type { ThreadInfo, SidebarInfo } from 'lib/types/thread-types'; import ExtendedArrow from '../components/arrow-extended.react';