Skip to content

Commit 409b832

Browse files
author
Tom Hutman
committed
pass mentioned user as prop into Mention component, in case it's needed to implement some additional functionality
1 parent 7157316 commit 409b832

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/utils.tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,29 @@ const emojiMarkdownPlugin = () => {
107107
return transform;
108108
};
109109

110-
const mentionsMarkdownPlugin = (mentionedUsersRegex: RegExp) => () => {
111-
function replace(match: RegExpMatchArray | null) {
110+
type MentionedUser<
111+
Us extends DefaultUserType<Us> = DefaultUserType
112+
> = UserResponse<Us>;
113+
114+
const mentionsMarkdownPlugin = (mentioned_users: MentionedUser[]) => () => {
115+
const mentioned_usernames = mentioned_users
116+
.map((user) => user.name || user.id)
117+
.filter(Boolean)
118+
.map(escapeRegExp);
119+
120+
const mentionedUsersRegex = new RegExp(
121+
mentioned_usernames.map((username) => `@${username}`).join('|'),
122+
'g',
123+
);
124+
125+
function replace(match: string) {
126+
const usernameOrId = match.replace('@', '');
127+
const user = mentioned_users.find(
128+
({ id, name }) => name === usernameOrId || id === usernameOrId,
129+
);
112130
return {
113131
children: [{ type: 'text', value: match }],
132+
mentioned_user: user,
114133
type: 'mention',
115134
};
116135
}
@@ -123,14 +142,16 @@ const mentionsMarkdownPlugin = (mentionedUsersRegex: RegExp) => () => {
123142
return transform;
124143
};
125144

126-
const Mention: React.FC = ({ children }) => (
145+
type MentionProps = { mentioned_user: MentionedUser };
146+
147+
const Mention: React.FC<MentionProps> = ({ children }) => (
127148
<span className='str-chat__message-mention'>{children}</span>
128149
);
129150

130-
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
151+
export const renderText = (
131152
text?: string,
132-
mentioned_users?: UserResponse<Us>[],
133-
MentionComponent: React.ComponentType = Mention,
153+
mentioned_users?: MentionedUser[],
154+
MentionComponent: React.ComponentType<MentionProps> = Mention,
134155
) => {
135156
// take the @ mentions and turn them into markdown?
136157
// translate links
@@ -169,17 +190,8 @@ export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
169190

170191
const plugins = [emojiMarkdownPlugin];
171192

172-
const mentioned_usernames = mentioned_users
173-
?.map((user) => user.name || user.id)
174-
.filter(Boolean)
175-
.map(escapeRegExp);
176-
177-
if (mentioned_usernames?.length) {
178-
const mentionedUsersRegex = new RegExp(
179-
mentioned_usernames.map((username) => `@${username}`).join('|'),
180-
'g',
181-
);
182-
plugins.push(mentionsMarkdownPlugin(mentionedUsersRegex));
193+
if (mentioned_users?.length) {
194+
plugins.push(mentionsMarkdownPlugin(mentioned_users));
183195
}
184196

185197
const renderers = {

0 commit comments

Comments
 (0)