Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3489145
D6487.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D6487.diff
View Options
diff --git a/native/chat/reaction-message-utils.js b/native/chat/reaction-message-utils.js
--- a/native/chat/reaction-message-utils.js
+++ b/native/chat/reaction-message-utils.js
@@ -8,9 +8,14 @@
sendReactionMessage,
sendReactionMessageActionTypes,
} from 'lib/actions/message-actions';
+import type { MessageReactionInfo } from 'lib/selectors/chat-selectors';
import { messageTypes } from 'lib/types/message-types';
import type { RawReactionMessageInfo } from 'lib/types/messages/reaction';
import type { BindServerCall, DispatchFunctions } from 'lib/utils/action-utils';
+import {
+ useDispatchActionPromise,
+ useServerCall,
+} from 'lib/utils/action-utils';
import { cloneError } from 'lib/utils/errors';
import type { InputState } from '../input/input-state';
@@ -123,6 +128,93 @@
);
}
+function useSendReaction(
+ messageID: ?string,
+ localID: string,
+ threadID: string,
+ reactions: $ReadOnlyMap<string, MessageReactionInfo>,
+): (reaction: string) => mixed {
+ const viewerID = useSelector(
+ state => state.currentUserInfo && state.currentUserInfo.id,
+ );
+
+ const callSendReactionMessage = useServerCall(sendReactionMessage);
+ const dispatchActionPromise = useDispatchActionPromise();
+
+ return React.useCallback(
+ reaction => {
+ if (!messageID) {
+ return;
+ }
+
+ invariant(viewerID, 'viewerID should be set');
+
+ const viewerReacted = reactions.get(reaction)?.viewerReacted;
+ const action = viewerReacted ? 'remove_reaction' : 'add_reaction';
+
+ const reactionMessagePromise = (async () => {
+ try {
+ const result = await callSendReactionMessage({
+ threadID,
+ localID,
+ targetMessageID: messageID,
+ reaction,
+ action,
+ });
+ return {
+ localID,
+ serverID: result.id,
+ threadID,
+ time: result.time,
+ interface: result.interface,
+ };
+ } catch (e) {
+ Alert.alert(
+ 'Couldn’t send the reaction',
+ 'Please try again later',
+ [{ text: 'OK' }],
+ {
+ cancelable: true,
+ },
+ );
+
+ const copy = cloneError(e);
+ copy.localID = localID;
+ copy.threadID = threadID;
+ throw copy;
+ }
+ })();
+
+ const startingPayload: RawReactionMessageInfo = {
+ type: messageTypes.REACTION,
+ threadID,
+ localID,
+ creatorID: viewerID,
+ time: Date.now(),
+ targetMessageID: messageID,
+ reaction,
+ action,
+ };
+
+ dispatchActionPromise(
+ sendReactionMessageActionTypes,
+ reactionMessagePromise,
+ undefined,
+ startingPayload,
+ );
+ },
+ [
+ messageID,
+ viewerID,
+ reactions,
+ threadID,
+ localID,
+ dispatchActionPromise,
+ callSendReactionMessage,
+ ],
+ );
+}
+
type ReactionSelectionPopoverPositionArgs = {
+initialCoordinates: LayoutCoordinates,
+verticalBounds: VerticalBounds,
@@ -189,4 +281,4 @@
]);
}
-export { onPressReact, useReactionSelectionPopoverPosition };
+export { onPressReact, useSendReaction, useReactionSelectionPopoverPosition };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Dec 19, 11:43 AM (20 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2677201
Default Alt Text
D6487.diff (3 KB)
Attached To
Mode
D6487: [native] introduce useSendReaction hook
Attached
Detach File
Event Timeline
Log In to Comment