diff --git a/lib/hooks/search-sidebars.js b/lib/hooks/search-sidebars.js new file mode 100644 index 000000000..172b334ef --- /dev/null +++ b/lib/hooks/search-sidebars.js @@ -0,0 +1,47 @@ +// @flow + +import * as React from 'react'; + +import { sidebarInfoSelector } from '../selectors/thread-selectors'; +import type { SetState } from '../types/hook-types'; +import type { SidebarInfo, ThreadInfo } from '../types/thread-types'; +import { useSelector } from '../utils/redux-utils'; + +type SidebarSearchState = { + +text: string, + +results: $ReadOnlySet, +}; + +function useSearchSidebars( + threadInfo: ThreadInfo, +): { + +listData: $ReadOnlyArray, + +searchState: SidebarSearchState, + +setSearchState: SetState, +} { + const [searchState, setSearchState] = React.useState({ + text: '', + results: new Set(), + }); + + const sidebarInfos = useSelector( + state => sidebarInfoSelector(state)[threadInfo.id] ?? [], + ); + + const listData = React.useMemo(() => { + if (!searchState.text) { + return sidebarInfos; + } + return sidebarInfos.filter(sidebarInfo => + searchState.results.has(sidebarInfo.threadInfo.id), + ); + }, [sidebarInfos, searchState]); + + return React.useMemo(() => ({ listData, searchState, setSearchState }), [ + listData, + setSearchState, + searchState, + ]); +} + +export { useSearchSidebars }; diff --git a/native/chat/sidebar-list-modal.react.js b/native/chat/sidebar-list-modal.react.js index 149ed5c21..4dbe39b20 100644 --- a/native/chat/sidebar-list-modal.react.js +++ b/native/chat/sidebar-list-modal.react.js @@ -1,164 +1,154 @@ // @flow import * as React from 'react'; 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'; import { SidebarItem } from './sidebar-item.react'; export type SidebarListModalParams = { +threadInfo: ThreadInfo, }; function keyExtractor(sidebarInfo: SidebarInfo) { return sidebarInfo.threadInfo.id; } function getItemLayout(data: ?$ReadOnlyArray, index: number) { return { length: 24, offset: 24 * index, index }; } type Props = { +navigation: RootNavigationProp<'SidebarListModal'>, +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 [searchState, setSearchState] = React.useState({ - text: '', - results: new Set(), - }); - - const listData = React.useMemo(() => { - if (!searchState.text) { - return sidebarInfos; - } - return sidebarInfos.filter(({ threadInfo }) => - searchState.results.has(threadInfo.id), - ); - }, [sidebarInfos, searchState]); - 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]); + }, [searchIndex, setSearchState]); const onChangeSearchText = React.useCallback( (searchText: string) => setSearchState({ text: searchText, results: new Set(searchIndex.getSearchResults(searchText)), }), - [searchIndex], + [searchIndex, setSearchState], ); const searchTextInputRef = React.useRef(); const setSearchTextInputRef = React.useCallback( async (textInput: ?React.ElementRef) => { searchTextInputRef.current = textInput; if (!textInput) { return; } await waitForModalInputFocus(); if (searchTextInputRef.current) { searchTextInputRef.current.focus(); } }, [], ); const navigateToThread = useNavigateToThread(); const onPressItem = React.useCallback( (threadInfo: ThreadInfo) => { setSearchState({ text: '', results: new Set(), }); if (searchTextInputRef.current) { searchTextInputRef.current.blur(); } navigateToThread({ threadInfo }); }, - [navigateToThread], + [navigateToThread, setSearchState], ); const renderItem = React.useCallback( (row: { item: SidebarInfo, ... }) => { return ( ); }, [onPressItem], ); const indicatorStyle = useIndicatorStyle(); return ( ); } const styles = StyleSheet.create({ search: { marginBottom: 8, }, sidebar: { backgroundColor: 'transparent', paddingLeft: 0, paddingRight: 5, }, }); export default SidebarListModal; diff --git a/web/modals/chat/sidebar-list-modal.react.js b/web/modals/chat/sidebar-list-modal.react.js index 34f0c45b4..db0dd8770 100644 --- a/web/modals/chat/sidebar-list-modal.react.js +++ b/web/modals/chat/sidebar-list-modal.react.js @@ -1,144 +1,135 @@ // @flow import { faTimesCircle } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import classNames from 'classnames'; 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'; import { useModalContext } from '../modal-provider.react'; import Modal from '../modal.react'; type Props = { +threadInfo: ThreadInfo, }; function SidebarListModal(props: Props): React.Node { const { threadInfo } = props; - const [searchState, setSearchState] = React.useState({ - text: '', - results: new Set(), - }); + const { listData, searchState, setSearchState } = useSearchSidebars( + threadInfo, + ); const { popModal } = useModalContext(); const sidebarInfos = useSelector( state => sidebarInfoSelector(state)[threadInfo.id] ?? [], ); const userInfos = useSelector(state => state.userStore.userInfos); - const listData = React.useMemo(() => { - if (!searchState.text) { - return sidebarInfos; - } - return sidebarInfos.filter(sidebarInfo => - searchState.results.has(sidebarInfo.threadInfo.id), - ); - }, [sidebarInfos, searchState]); - const sidebars = React.useMemo( () => listData.map(item => (
)), [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]); + }, [searchIndex, setSearchState]); const onChangeSearchText = React.useCallback( (event: SyntheticEvent) => { const searchText = event.currentTarget.value; setSearchState({ text: searchText, results: new Set(searchIndex.getSearchResults(searchText)), }); }, - [searchIndex], + [searchIndex, setSearchState], ); const clearQuery = React.useCallback( (event: SyntheticEvent) => { event.preventDefault(); - setSearchState({ text: '', results: [] }); + setSearchState({ text: '', results: new Set() }); }, - [], + [setSearchState], ); let clearQueryButton = null; if (searchState.text) { clearQueryButton = ( ); } return (
{clearQueryButton}
    {sidebars}
); } export default SidebarListModal;