diff --git a/lib/hooks/relationship-prompt.js b/lib/hooks/relationship-prompt.js --- a/lib/hooks/relationship-prompt.js +++ b/lib/hooks/relationship-prompt.js @@ -17,14 +17,16 @@ import type { UserInfo } from '../types/user-types'; import { useDispatchActionPromise, useServerCall } from '../utils/action-utils'; +type RelationshipCallbacks = { + +blockUser: () => void, + +unblockUser: () => void, + +friendUser: () => void, + +unfriendUser: () => void, +}; + type RelationshipPromptData = { +otherUserInfo: ?UserInfo, - +callbacks: { - +blockUser: () => void, - +unblockUser: () => void, - +friendUser: () => void, - +unfriendUser: () => void, - }, + +callbacks: RelationshipCallbacks, }; function useRelationshipPrompt( @@ -45,21 +47,36 @@ : pendingPersonalThreadUserInfo; }); + const callbacks = useRelationshipCallbacks( + otherUserInfo?.id, + onErrorCallback, + ); + + return { + otherUserInfo, + callbacks, + }; +} + +function useRelationshipCallbacks( + userID?: string, + onErrorCallback?: () => void, +): RelationshipCallbacks { const callUpdateRelationships = useServerCall(serverUpdateRelationships); const updateRelationship = React.useCallback( async (action: RelationshipAction) => { try { - invariant(otherUserInfo?.id, 'Other user info id should be present'); + invariant(userID, 'Other user info id should be present'); return await callUpdateRelationships({ action, - userIDs: [otherUserInfo.id], + userIDs: [userID], }); } catch (e) { onErrorCallback?.(); throw e; } }, - [callUpdateRelationships, onErrorCallback, otherUserInfo?.id], + [callUpdateRelationships, onErrorCallback, userID], ); const dispatchActionPromise = useDispatchActionPromise(); @@ -89,15 +106,16 @@ () => onButtonPress(relationshipActions.UNFRIEND), [onButtonPress], ); - return { - otherUserInfo, - callbacks: { + + return React.useMemo( + () => ({ blockUser, unblockUser, friendUser, unfriendUser, - }, - }; + }), + [blockUser, friendUser, unblockUser, unfriendUser], + ); } -export { useRelationshipPrompt }; +export { useRelationshipPrompt, useRelationshipCallbacks };