Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/search-utils.js
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { useSelector } from 'react-redux'; | |||||
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 { | import { | ||||
searchMessages, | searchMessages, | ||||
searchMessagesActionTypes, | searchMessagesActionTypes, | ||||
} from '../actions/message-actions.js'; | } from '../actions/message-actions.js'; | ||||
import { | |||||
searchUsers, | |||||
searchUsersActionTypes, | |||||
} from '../actions/user-actions.js'; | |||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import type { RawMessageInfo } from '../types/message-types.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 { | import type { | ||||
AccountUserInfo, | AccountUserInfo, | ||||
▲ Show 20 Lines • Show All 215 Lines • ▼ Show 20 Lines | (query, threadID, onResultsReceived, cursor) => { | ||||
})(); | })(); | ||||
dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); | dispatchActionPromise(searchMessagesActionTypes, searchMessagesPromise); | ||||
}, | }, | ||||
[callSearchMessages, dispatchActionPromise], | [callSearchMessages, dispatchActionPromise], | ||||
); | ); | ||||
} | } | ||||
export { getPotentialMemberItems, notFriendNotice, useSearchMessages }; | function useSearchUsers( | ||||
usernameInputText: string, | |||||
): $ReadOnlyArray<GlobalAccountUserInfo> { | |||||
const currentUserID = useSelector( | |||||
state => state.currentUserInfo && state.currentUserInfo.id, | |||||
); | |||||
const [serverSearchResults, setServerSearchResults] = React.useState< | |||||
$ReadOnlyArray<GlobalAccountUserInfo>, | |||||
>([]); | |||||
const callSearchUsers = useServerCall(searchUsers); | |||||
const dispatchActionPromise = useDispatchActionPromise(); | |||||
React.useEffect(() => { | |||||
const searchUsersPromise = (async () => { | |||||
if (usernameInputText.length === 0) { | |||||
setServerSearchResults([]); | |||||
} else { | |||||
try { | |||||
const { userInfos } = await callSearchUsers(usernameInputText); | |||||
setServerSearchResults( | |||||
userInfos.filter(({ id }) => id !== currentUserID), | |||||
); | |||||
} catch (err) { | |||||
setServerSearchResults([]); | |||||
} | |||||
} | |||||
})(); | |||||
dispatchActionPromise(searchUsersActionTypes, searchUsersPromise); | |||||
}, [ | |||||
callSearchUsers, | |||||
currentUserID, | |||||
dispatchActionPromise, | |||||
usernameInputText, | |||||
]); | |||||
return serverSearchResults; | |||||
} | |||||
export { | |||||
getPotentialMemberItems, | |||||
notFriendNotice, | |||||
useSearchMessages, | |||||
useSearchUsers, | |||||
}; |