Skip to content

Commit

Permalink
Added a feature to copy messages and copy link in message tool box (#710
Browse files Browse the repository at this point in the history
)

* Added a feature to copy messages

* Added copy link feature

* fixed inconsistency in layout_editor

* Removed changes in layout_editor
  • Loading branch information
devanshkansagra authored Dec 22, 2024
1 parent fbb95b6 commit 81ad89b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/react/src/views/Message/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -215,6 +254,8 @@ const Message = ({
userRoles={userRoles}
pinRoles={pinRoles}
editMessageRoles={editMessageRoles}
handleCopyMessage={handleCopyMessage}
handleCopyMessageLink={handleCopyMessageLink}
handleOpenThread={handleOpenThread}
handleDeleteMessage={handleDeleteMessage}
handleStarMessage={handleStarMessage}
Expand Down
20 changes: 20 additions & 0 deletions packages/react/src/views/Message/MessageToolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const MessageToolbox = ({
handleStarMessage,
handleDeleteMessage,
handlerReportMessage,
handleCopyMessage,
handleCopyMessageLink,
handleEditMessage,
handleQuoteMessage,
isEditing = false,
Expand All @@ -39,6 +41,8 @@ export const MessageToolbox = ({
'reply',
'quote',
'star',
'copy',
'link',
'pin',
'edit',
'delete',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -158,6 +176,8 @@ export const MessageToolbox = ({
handlePinMessage,
handleEditMessage,
handlerReportMessage,
handleCopyMessage,
isAllowedToPin,
]
);

Expand Down

0 comments on commit 81ad89b

Please sign in to comment.