Skip to content

Commit 97d4fbb

Browse files
authored
[CLNP-7806]Fix a bug where mentionedMessageTemplate is being set even when the message is not a mentioned message (#1373)
[fix]: Fix a bug where `mentionedMessageTemplate` is being set even when the message is not a mentioned message Fixes [CLNP-7806](https://sendbird.atlassian.net/browse/CLNP-7806) ### Changelogs - Fix a bug where `mentionedMessageTemplate` is being set even when the message is not a mentioned message - Fix a bug where URL links inside Markdown-type messages do not open in a new tab ### Checklist Put an `x` in the boxes that apply. You can also fill these out after creating the PR. If unsure, ask the members. This is a reminder of what we look for before merging your code. - [x] **All tests pass locally with my changes** - [ ] **I have added tests that prove my fix is effective or that my feature works** - [ ] **Public components / utils / props are appropriately exported** - [ ] I have added necessary documentation (if appropriate) ## External Contributions This project is not yet set up to accept pull requests from external contributors. If you have a pull request that you believe should be accepted, please contact the Developer Relations team <developer-advocates@sendbird.com> with details and we'll evaluate if we can set up a [CLA](https://en.wikipedia.org/wiki/Contributor_License_Agreement) to allow for the contribution. [CLNP-7806]: https://sendbird.atlassian.net/browse/CLNP-7806?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 6fa3789 commit 97d4fbb

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/modules/Message/components/TextFragment/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export default function TextFragment({
5050
: 'sendbird-label--color-onbackground-1'
5151
}
5252
href={asSafeURL(groups[2])}
53+
target="_blank"
54+
rel="noopener noreferrer"
5355
>
5456
<TextFragment tokens={tokenizeMarkdown({ messageText: groups[1] })}/>
5557
</a>

src/ui/MessageInput/__tests__/MessageInput.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ describe('ui/MessageInput', () => {
172172
expect(input.textContent).toBe(mockText);
173173

174174
fireEvent.keyDown(input, { key: 'Enter' });
175-
expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText });
175+
expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: '', message: mockText });
176176
});
177177

178178
it('should call sendMessage with valid string; new lines included', async () => {
@@ -187,7 +187,7 @@ describe('ui/MessageInput', () => {
187187
expect(input.textContent).toBe(mockText);
188188

189189
fireEvent.keyDown(input, { key: 'Enter' });
190-
expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText });
190+
expect(onSendMessage).toHaveBeenCalledWith({ mentionTemplate: '', message: mockText });
191191
});
192192

193193
it('should not call sendMessage with invalid string; only white spaces', async() => {
@@ -202,7 +202,7 @@ describe('ui/MessageInput', () => {
202202
expect(input.textContent).toBe(mockText);
203203

204204
fireEvent.keyDown(input, { key: 'Enter' });
205-
expect(onSendMessage).not.toHaveBeenCalledWith({ mentionTemplate: mockText, message: mockText });
205+
expect(onSendMessage).not.toHaveBeenCalledWith({ mentionTemplate: '', message: mockText });
206206
});
207207

208208
it('should render send icon if text is present', async() => {

src/ui/MessageInput/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ const MessageInput = React.forwardRef<HTMLInputElement, MessageInputProps>((prop
349349
try {
350350
const textField = internalRef?.current;
351351
if (!isEdit && textField?.textContent) {
352-
const { messageText, mentionTemplate } = extractTextAndMentions(textField.childNodes);
352+
const { messageText, mentionTemplate, isMentionedMessage } = extractTextAndMentions(textField.childNodes);
353353
const params = { message: messageText, mentionTemplate };
354+
if (!isMentionedMessage) params.mentionTemplate = '';
354355
onSendMessage(params);
355356
resetInput(internalRef);
356357
/**

src/ui/MessageInput/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ function isHTMLElement(node: ChildNode): node is HTMLElement {
3434
export function extractTextAndMentions(childNodes: NodeListOf<ChildNode>) {
3535
let messageText = '';
3636
let mentionTemplate = '';
37+
let isMentionedMessage = false;
3738
childNodes.forEach((node) => {
3839
if (isHTMLElement(node) && node.nodeName === NodeNames.Span) {
3940
const { innerText, dataset = {} } = node;
4041
const { userid = '' } = dataset;
42+
if (userid) isMentionedMessage = true;
4143
messageText += innerText;
4244
mentionTemplate += `${USER_MENTION_TEMP_CHAR}{${userid}}`;
4345
} else if (isHTMLElement(node) && node.nodeName === NodeNames.Br) {
@@ -53,5 +55,5 @@ export function extractTextAndMentions(childNodes: NodeListOf<ChildNode>) {
5355
mentionTemplate += textContent;
5456
}
5557
});
56-
return { messageText, mentionTemplate };
58+
return { messageText, mentionTemplate, isMentionedMessage };
5759
}

0 commit comments

Comments
 (0)