From 81ad89b5afece3a39e2d44752e3ba5eb2c9be5f4 Mon Sep 17 00:00:00 2001 From: Devansh Kansagra <125076549+devanshkansagra@users.noreply.github.com> Date: Sun, 22 Dec 2024 14:50:20 +0530 Subject: [PATCH] Added a feature to copy messages and copy link in message tool box (#710) * Added a feature to copy messages * Added copy link feature * fixed inconsistency in layout_editor * Removed changes in layout_editor --- packages/react/src/views/Message/Message.js | 41 +++++++++++++++++++ .../react/src/views/Message/MessageToolbox.js | 20 +++++++++ 2 files changed, 61 insertions(+) diff --git a/packages/react/src/views/Message/Message.js b/packages/react/src/views/Message/Message.js index aafb5642b..a17a3c3a3 100644 --- a/packages/react/src/views/Message/Message.js +++ b/packages/react/src/views/Message/Message.js @@ -118,6 +118,45 @@ const Message = ({ } }; + const handleCopyMessage = async (msg) => { + navigator.clipboard + .writeText(msg.msg) + .then(() => { + dispatchToastMessage({ + type: 'success', + message: 'Message copied successfully', + }); + }) + .catch(() => { + dispatchToastMessage({ + type: 'error', + message: 'Error in copying message', + }); + }); + }; + + const getMessageLink = async (id) => { + const host = await RCInstance.getHost(); + const res = await RCInstance.channelInfo(); + return `${host}/channel/${res.room.name}/?msg=${id}`; + }; + + const handleCopyMessageLink = async (msg) => { + try { + const messageLink = await getMessageLink(msg._id); + await navigator.clipboard.writeText(messageLink); + dispatchToastMessage({ + type: 'success', + message: 'Message link copied successfully', + }); + } catch (err) { + dispatchToastMessage({ + type: 'error', + message: 'Error in copying message link', + }); + } + }; + const handleDeleteMessage = async (msg) => { const res = await RCInstance.deleteMessage(msg._id); @@ -215,6 +254,8 @@ const Message = ({ userRoles={userRoles} pinRoles={pinRoles} editMessageRoles={editMessageRoles} + handleCopyMessage={handleCopyMessage} + handleCopyMessageLink={handleCopyMessageLink} handleOpenThread={handleOpenThread} handleDeleteMessage={handleDeleteMessage} handleStarMessage={handleStarMessage} diff --git a/packages/react/src/views/Message/MessageToolbox.js b/packages/react/src/views/Message/MessageToolbox.js index 0ed468bd7..a3e2c1ec7 100644 --- a/packages/react/src/views/Message/MessageToolbox.js +++ b/packages/react/src/views/Message/MessageToolbox.js @@ -30,6 +30,8 @@ export const MessageToolbox = ({ handleStarMessage, handleDeleteMessage, handlerReportMessage, + handleCopyMessage, + handleCopyMessageLink, handleEditMessage, handleQuoteMessage, isEditing = false, @@ -39,6 +41,8 @@ export const MessageToolbox = ({ 'reply', 'quote', 'star', + 'copy', + 'link', 'pin', 'edit', 'delete', @@ -130,6 +134,20 @@ export const MessageToolbox = ({ color: isEditing ? 'secondary' : 'default', ghost: !isEditing, }, + copy: { + label: 'Copy message', + id: 'copy', + onClick: () => handleCopyMessage(message), + iconName: 'copy', + visible: true, + }, + link: { + label: 'Copy link', + id: 'link', + onClick: () => handleCopyMessageLink(message), + iconName: 'link', + visible: true, + }, delete: { label: 'Delete', id: 'delete', @@ -158,6 +176,8 @@ export const MessageToolbox = ({ handlePinMessage, handleEditMessage, handlerReportMessage, + handleCopyMessage, + isAllowedToPin, ] );