diff --git a/lib/shared/search-utils.js b/lib/shared/search-utils.js --- a/lib/shared/search-utils.js +++ b/lib/shared/search-utils.js @@ -1,17 +1,28 @@ // @flow +import * as React from 'react'; + import SearchIndex from './search-index.js'; import { userIsMember, threadMemberHasPermission, getContainingThreadID, } from './thread-utils.js'; +import { + searchMessages, + searchMessagesActionTypes, +} from '../actions/message-actions.js'; import genesis from '../facts/genesis.js'; +import type { RawMessageInfo } from '../types/message-types.js'; import { userRelationshipStatus } from '../types/relationship-types.js'; import { threadPermissions } from '../types/thread-permission-types.js'; import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; import { type ThreadInfo } from '../types/thread-types.js'; import type { AccountUserInfo, UserListItem } from '../types/user-types.js'; +import { + useServerCall, + useDispatchActionPromise, +} from '../utils/action-utils.js'; const notFriendNotice = 'not friend'; @@ -169,4 +180,43 @@ ); } -export { getPotentialMemberItems, notFriendNotice }; +function useSearchMessages( + query: string, + threadID: string, + cursor?: string, + callback: (messages: $ReadOnlyArray) => void, +): boolean { + const [endReached, setEndReached] = React.useState(false); + + const callSearchMessages = useServerCall(searchMessages); + const dispatchActionPromise = useDispatchActionPromise(); + + React.useEffect(() => { + const searchMessagesPromise = (async () => { + if (query === '') { + setEndReached(true); + return; + } + const { messages, endReached: end } = await callSearchMessages({ + query, + threadID, + cursor, + }); + setEndReached(end); + callback(messages); + })(); + + dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); + }, [ + callSearchMessages, + query, + threadID, + cursor, + dispatchActionPromise, + callback, + ]); + + return endReached; +} + +export { getPotentialMemberItems, notFriendNotice, useSearchMessages };