Skip to content

Instantly share code, notes, and snippets.

@sendbird-community
Created December 29, 2021 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sendbird-community/6f32e8c9cd20bfe38234b04e1db0eed9 to your computer and use it in GitHub Desktop.
Save sendbird-community/6f32e8c9cd20bfe38234b04e1db0eed9 to your computer and use it in GitHub Desktop.
import { useCallback } from "react";
import * as messageActionTypes from "./actionTypes";
import * as topics from "./topics";
export default function useUpdateMessageCallback(
{ currentChannel, messagesDispatcher, onBeforeUpdateUserMessage, updateLastMessage },
{ logger, pubSub, sdk }
) {
return useCallback(
(messageId, text, cb) => {
const createParamsDefault = (txt) => {
const params = new sdk.UserMessageParams();
params.message = txt;
return params;
};
const createCustomPrams =
onBeforeUpdateUserMessage &&
typeof onBeforeUpdateUserMessage === "function";
if (createCustomPrams) {
logger.info(
"Channel: creating params using onBeforeUpdateUserMessage",
onBeforeUpdateUserMessage
);
}
const params = onBeforeUpdateUserMessage
? onBeforeUpdateUserMessage(text)
: createParamsDefault(text);
currentChannel.updateUserMessage(messageId, params, (r, e) => {
logger.info("Channel: Updating message!", params);
const swapParams = sdk.getErrorFirstCallback();
let message = r;
let err = e;
if (swapParams) {
message = e;
err = r;
}
if (cb) {
cb(err, message);
}
if (!err) {
logger.info("Channel: Updating message success!", message);
messagesDispatcher({
type: messageActionTypes.ON_MESSAGE_UPDATED,
payload: {
channel: currentChannel,
message,
},
});
pubSub.publish(topics.UPDATE_USER_MESSAGE, {
message,
channel: currentChannel,
});
} else {
logger.warning("Channel: Updating message failed!", err);
}
updateLastMessage(currentChannel.url, message.messageId, params);
});
},
[
messagesDispatcher,
onBeforeUpdateUserMessage,
logger,
pubSub,
sdk,
currentChannel,
]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment