Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/search-utils.js
// @flow | // @flow | ||||
import * as React from 'react'; | |||||
import SearchIndex from './search-index.js'; | import SearchIndex from './search-index.js'; | ||||
import { | import { | ||||
userIsMember, | userIsMember, | ||||
threadMemberHasPermission, | threadMemberHasPermission, | ||||
getContainingThreadID, | getContainingThreadID, | ||||
} from './thread-utils.js'; | } from './thread-utils.js'; | ||||
import { | |||||
searchMessages, | |||||
searchMessagesActionTypes, | |||||
} from '../actions/message-actions.js'; | |||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import type { SearchMessagesResponse } from '../types/message-types.js'; | |||||
import { userRelationshipStatus } from '../types/relationship-types.js'; | import { userRelationshipStatus } from '../types/relationship-types.js'; | ||||
import { | import { | ||||
type ThreadInfo, | type ThreadInfo, | ||||
type ThreadType, | type ThreadType, | ||||
threadTypes, | threadTypes, | ||||
threadPermissions, | threadPermissions, | ||||
} from '../types/thread-types.js'; | } from '../types/thread-types.js'; | ||||
import type { AccountUserInfo, UserListItem } from '../types/user-types.js'; | import type { AccountUserInfo, UserListItem } from '../types/user-types.js'; | ||||
import { | |||||
useServerCall, | |||||
useDispatchActionPromise, | |||||
} from '../utils/action-utils.js'; | |||||
const notFriendNotice = 'not friend'; | const notFriendNotice = 'not friend'; | ||||
function getPotentialMemberItems( | function getPotentialMemberItems( | ||||
text: string, | text: string, | ||||
userInfos: { +[id: string]: AccountUserInfo }, | userInfos: { +[id: string]: AccountUserInfo }, | ||||
searchIndex: SearchIndex, | searchIndex: SearchIndex, | ||||
excludeUserIDs: $ReadOnlyArray<string>, | excludeUserIDs: $ReadOnlyArray<string>, | ||||
▲ Show 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | ({ | ||||
if (alertTitle) { | if (alertTitle) { | ||||
result = { ...result, alertTitle, alertText }; | result = { ...result, alertTitle, alertText }; | ||||
} | } | ||||
return result; | return result; | ||||
}, | }, | ||||
); | ); | ||||
} | } | ||||
export { getPotentialMemberItems, notFriendNotice }; | function useSearchMessages( | ||||
query: string, | |||||
threadID: string, | |||||
cursor?: string, | |||||
): SearchMessagesResponse { | |||||
const [searchResults, setSearchResults] = React.useState([]); | |||||
const [endReached, setEndReached] = React.useState(false); | |||||
const callSearchMessages = useServerCall(searchMessages); | |||||
const dispatchActionPromise = useDispatchActionPromise(); | |||||
React.useEffect(() => { | |||||
const searchMessagesPromise = (async () => { | |||||
const { messages, endReached: end } = await callSearchMessages({ | |||||
query, | |||||
threadID, | |||||
cursor, | |||||
}); | |||||
setSearchResults(messages); | |||||
setEndReached(end); | |||||
})(); | |||||
dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); | |||||
}, [callSearchMessages, query, threadID, cursor, dispatchActionPromise]); | |||||
return { messages: searchResults, endReached }; | |||||
} | |||||
export { getPotentialMemberItems, notFriendNotice, useSearchMessages }; |