From a943f045c56237f4ec8a4b2e504a6ac3f7fb0db9 Mon Sep 17 00:00:00 2001 From: Timothy Ng <5664347+timorthi@users.noreply.github.com> Date: Wed, 7 Oct 2020 20:23:58 -0700 Subject: [PATCH] Connect: Highlight @ mentions to logged in user (#10216) * Wrap with tags if mentioning current user * Add test for @mention highlight * Match @{username} mention * Remove default param --- .../chat/__tests__/message.test.jsx | 21 +++++++++++++++- app/javascript/chat/message.jsx | 24 ++++++++++++------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/javascript/chat/__tests__/message.test.jsx b/app/javascript/chat/__tests__/message.test.jsx index 3417e03bd..8c9e483e8 100644 --- a/app/javascript/chat/__tests__/message.test.jsx +++ b/app/javascript/chat/__tests__/message.test.jsx @@ -11,12 +11,13 @@ const msg = { color: '#00FFFF', }; -const getMessage = (message) => ( +const getMessage = (message, props) => ( ); @@ -38,4 +39,22 @@ describe('', () => { expect(profileLink.parentElement).toHaveStyle({ color: msg.color }); }); + + it('should highlight @mentions to the logged in user', () => { + const testMessage = { + username: 'testUser', + user_id: 456, + message: "

hello @testUser

", + }; + + const { getByText } = render( + getMessage(testMessage, { currentUserId: testMessage.user_id }), + ); + + const profileLink = getByText(`@${testMessage.username}`); + + expect(profileLink.parentElement.innerHTML).toMatch( + new RegExp(`@${testMessage.username}`, 'i'), + ); + }); }); diff --git a/app/javascript/chat/message.jsx b/app/javascript/chat/message.jsx index e58d5aab2..268a56319 100644 --- a/app/javascript/chat/message.jsx +++ b/app/javascript/chat/message.jsx @@ -27,13 +27,19 @@ const Message = ({ return ; } - const messageArea = ( - - ); + const MessageArea = () => { + if (userID === currentUserId) { + message = message.replace(`@${user}`, `@${user}`); + } + + return ( + + ); + }; const dropdown = (
@@ -115,7 +121,9 @@ const Message = ({
{userID === currentUserId ? dropdown : ' '} -
{messageArea}
+
+ +
);