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 { RawMessageInfo } from '../types/message-types.js'; | |||||
import { userRelationshipStatus } from '../types/relationship-types.js'; | import { userRelationshipStatus } from '../types/relationship-types.js'; | ||||
import { threadPermissions } from '../types/thread-permission-types.js'; | import { threadPermissions } from '../types/thread-permission-types.js'; | ||||
import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; | import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; | ||||
import { type ThreadInfo } from '../types/thread-types.js'; | import { type ThreadInfo } 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, | |||||
onResultsReceived: ( | |||||
messages: $ReadOnlyArray<RawMessageInfo>, | |||||
endReached: boolean, | |||||
) => mixed, | |||||
cursor?: string, | |||||
) => void { | |||||
const callSearchMessages = useServerCall(searchMessages); | |||||
const dispatchActionPromise = useDispatchActionPromise(); | |||||
return React.useCallback( | |||||
(query, threadID, onResultsReceived, cursor) => { | |||||
const searchMessagesPromise = (async () => { | |||||
if (query === '') { | |||||
onResultsReceived([], true); | |||||
return; | |||||
} | |||||
const { messages, endReached } = await callSearchMessages({ | |||||
query, | |||||
threadID, | |||||
cursor, | |||||
}); | |||||
onResultsReceived(messages, endReached); | |||||
})(); | |||||
dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); | |||||
}, | |||||
[callSearchMessages, dispatchActionPromise], | |||||
); | |||||
} | |||||
export { getPotentialMemberItems, notFriendNotice, useSearchMessages }; |