diff --git a/lib/hooks/search-sidebars.js b/lib/hooks/search-sidebars.js --- a/lib/hooks/search-sidebars.js +++ b/lib/hooks/search-sidebars.js @@ -3,6 +3,8 @@ import * as React from 'react'; import { sidebarInfoSelector } from '../selectors/thread-selectors'; +import SearchIndex from '../shared/search-index'; +import { threadSearchText } from '../shared/thread-utils'; import type { SetState } from '../types/hook-types'; import type { SidebarInfo, ThreadInfo } from '../types/thread-types'; import { useSelector } from '../utils/redux-utils'; @@ -18,12 +20,15 @@ +listData: $ReadOnlyArray, +searchState: SidebarSearchState, +setSearchState: SetState, + +searchIndex: SearchIndex, } { const [searchState, setSearchState] = React.useState({ text: '', results: new Set(), }); + const userInfos = useSelector(state => state.userStore.userInfos); + const sidebarInfos = useSelector( state => sidebarInfoSelector(state)[threadInfo.id] ?? [], ); @@ -37,11 +42,32 @@ ); }, [sidebarInfos, searchState]); - return React.useMemo(() => ({ listData, searchState, setSearchState }), [ - listData, - setSearchState, - 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; + index.addEntry( + threadInfoFromSidebarInfo.id, + threadSearchText(threadInfoFromSidebarInfo, userInfos, viewerID), + ); + } + return index; + }, [sidebarInfos, userInfos, viewerID]); + + React.useEffect(() => { + setSearchState(curState => ({ + ...curState, + results: new Set(searchIndex.getSearchResults(curState.text)), + })); + }, [searchIndex, setSearchState]); + + return React.useMemo( + () => ({ listData, searchState, setSearchState, searchIndex }), + [listData, setSearchState, searchState, searchIndex], + ); } export { 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 @@ -4,16 +4,12 @@ import { TextInput, FlatList, StyleSheet } from 'react-native'; import { useSearchSidebars } from 'lib/hooks/search-sidebars'; -import { sidebarInfoSelector } from 'lib/selectors/thread-selectors'; -import SearchIndex from 'lib/shared/search-index'; -import { threadSearchText } from 'lib/shared/thread-utils'; import type { ThreadInfo, SidebarInfo } from 'lib/types/thread-types'; import Modal from '../components/modal.react'; import Search from '../components/search.react'; import type { RootNavigationProp } from '../navigation/root-navigator.react'; import type { NavigationRoute } from '../navigation/route-names'; -import { useSelector } from '../redux/redux-utils'; import { useIndicatorStyle } from '../themes/colors'; import { waitForModalInputFocus } from '../utils/timers'; import { useNavigateToThread } from './message-list-types'; @@ -35,35 +31,12 @@ +route: NavigationRoute<'SidebarListModal'>, }; function SidebarListModal(props: Props): React.Node { - const threadID = props.route.params.threadInfo.id; - const { listData, searchState, setSearchState } = useSearchSidebars( - props.route.params.threadInfo, - ); - const sidebarInfos = useSelector( - state => sidebarInfoSelector(state)[threadID] ?? [], - ); - - const userInfos = useSelector(state => state.userStore.userInfos); - const viewerID = useSelector( - state => state.currentUserInfo && state.currentUserInfo.id, - ); - const searchIndex = React.useMemo(() => { - const index = new SearchIndex(); - for (const sidebarInfo of sidebarInfos) { - const { threadInfo } = sidebarInfo; - index.addEntry( - threadInfo.id, - threadSearchText(threadInfo, userInfos, viewerID), - ); - } - return index; - }, [sidebarInfos, userInfos, viewerID]); - React.useEffect(() => { - setSearchState(curState => ({ - ...curState, - results: new Set(searchIndex.getSearchResults(curState.text)), - })); - }, [searchIndex, setSearchState]); + const { + listData, + searchState, + setSearchState, + searchIndex, + } = useSearchSidebars(props.route.params.threadInfo); const onChangeSearchText = React.useCallback( (searchText: string) => diff --git a/web/modals/chat/sidebar-list-modal.react.js b/web/modals/chat/sidebar-list-modal.react.js --- a/web/modals/chat/sidebar-list-modal.react.js +++ b/web/modals/chat/sidebar-list-modal.react.js @@ -6,14 +6,10 @@ import * as React from 'react'; import { useSearchSidebars } from 'lib/hooks/search-sidebars'; -import { sidebarInfoSelector } from 'lib/selectors/thread-selectors'; -import SearchIndex from 'lib/shared/search-index'; -import { threadSearchText } from 'lib/shared/thread-utils'; import type { ThreadInfo } from 'lib/types/thread-types'; import chatThreadListCSS from '../../chat/chat-thread-list.css'; import SidebarItem from '../../chat/sidebar-item.react'; -import { useSelector } from '../../redux/redux-utils'; import globalCSS from '../../style.css'; import { MagnifyingGlass } from '../../vectors.react'; import Input from '../input.react'; @@ -26,16 +22,14 @@ function SidebarListModal(props: Props): React.Node { const { threadInfo } = props; - const { listData, searchState, setSearchState } = useSearchSidebars( - threadInfo, - ); + const { + listData, + searchState, + setSearchState, + searchIndex, + } = useSearchSidebars(threadInfo); const { popModal } = useModalContext(); - const sidebarInfos = useSelector( - state => sidebarInfoSelector(state)[threadInfo.id] ?? [], - ); - const userInfos = useSelector(state => state.userStore.userInfos); - const sidebars = React.useMemo( () => listData.map(item => ( @@ -53,28 +47,6 @@ [popModal, listData], ); - 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; - index.addEntry( - threadInfoFromSidebarInfo.id, - threadSearchText(threadInfoFromSidebarInfo, userInfos, viewerID), - ); - } - return index; - }, [sidebarInfos, userInfos, viewerID]); - - React.useEffect(() => { - setSearchState(curState => ({ - ...curState, - results: new Set(searchIndex.getSearchResults(curState.text)), - })); - }, [searchIndex, setSearchState]); - const onChangeSearchText = React.useCallback( (event: SyntheticEvent) => { const searchText = event.currentTarget.value;