Skip to content

Commit

Permalink
fix: compound names and repetitive cases fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AtilaU19 committed Sep 16, 2024
1 parent 7a437ba commit ec0a0d1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/chat-mention-plugin/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@ function ChatMention({ pluginUuid: uuid }: ChatMentionProps): React.ReactElement

useEffect(() => {
if (response.data && userListBasicInf.data) {
const userNames = userListBasicInf.data.user.map((user) => user.name);
const userNames = userListBasicInf.data.user
.map((user) => user.name)
.sort((a, b) => b.length - a.length);

const idsToApply = response.data.filter((message) => {
const mentions = message.message.match(REGEX);

if (mentions) {
const mentionMatchesUser = mentions.some((mention) => {
const userMatched = userNames.some((name) => mention.includes(name));
return userMatched;
return userNames.some((name) => {
const userNameRegex = new RegExp(`@${name}\\b`, 'i');
return userNameRegex.test(mention.trim());
});
});
return mentionMatchesUser;
}
Expand Down Expand Up @@ -51,14 +55,15 @@ function ChatMention({ pluginUuid: uuid }: ChatMentionProps): React.ReactElement
const style = 'color: #4185cf; background-color: #f2f6f8;';

mentions.forEach((mention) => {
userListBasicInf.data.user.forEach((user) => {
const userNameRegex = new RegExp(`@${user.name}\\b`);
if (userNameRegex.test(mention)) {
userListBasicInf.data.user
.sort((a, b) => b.name.length - a.name.length)
.forEach((user) => {
const userNameRegex = new RegExp(`@${user.name}\\b`, 'gi');
const mentionText = `@${user.name}`;
updatedHtml = updatedHtml.replace(mentionText, `<span style="${style}">${mentionText}</span>`);
}
});
updatedHtml = updatedHtml.replace(userNameRegex, `<span style="${style}">${mentionText}</span>`);
});
});

chatMessageDomElement.innerHTML = updatedHtml;
}
});
Expand Down

0 comments on commit ec0a0d1

Please sign in to comment.