Skip to content

Commit 5ac18be

Browse files
committed
add generics to functions/types
1 parent d603592 commit 5ac18be

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/utils.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { PropsWithChildren } from 'react';
22
import emojiRegex from 'emoji-regex';
33
import * as linkify from 'linkifyjs';
44
//@ts-expect-error
@@ -8,7 +8,7 @@ import ReactMarkdown from 'react-markdown/with-html';
88

99
import type { UserResponse } from 'stream-chat';
1010

11-
import type { UnknownType } from '../types/types';
11+
import type { DefaultUserType, UnknownType } from '../types/types';
1212

1313
export const isOnlyEmojis = (text?: string) => {
1414
if (!text) return false;
@@ -107,7 +107,11 @@ const emojiMarkdownPlugin = () => {
107107
return transform;
108108
};
109109

110-
const mentionsMarkdownPlugin = (mentioned_users: UserResponse[]) => () => {
110+
const mentionsMarkdownPlugin = <
111+
Us extends DefaultUserType<Us> = DefaultUserType
112+
>(
113+
mentioned_users: UserResponse<Us>[],
114+
) => () => {
111115
const mentioned_usernames = mentioned_users
112116
.map((user) => user.name || user.id)
113117
.filter(Boolean)
@@ -138,16 +142,18 @@ const mentionsMarkdownPlugin = (mentioned_users: UserResponse[]) => () => {
138142
return transform;
139143
};
140144

141-
type MentionProps = { mentioned_user: UserResponse };
145+
type MentionProps<Us extends DefaultUserType<Us> = DefaultUserType> = {
146+
mentioned_user: UserResponse<Us>;
147+
};
142148

143-
const Mention: React.FC<MentionProps> = ({ children }) => (
144-
<span className='str-chat__message-mention'>{children}</span>
145-
);
149+
const Mention = <Us extends DefaultUserType<Us> = DefaultUserType>(
150+
props: PropsWithChildren<Us>,
151+
) => <span className='str-chat__message-mention'>{props.children}</span>;
146152

147-
export const renderText = (
153+
export const renderText = <Us extends DefaultUserType<Us> = DefaultUserType>(
148154
text?: string,
149-
mentioned_users?: UserResponse[],
150-
MentionComponent: React.ComponentType<MentionProps> = Mention,
155+
mentioned_users?: UserResponse<Us>[],
156+
MentionComponent: React.ComponentType<MentionProps<Us>> = Mention,
151157
) => {
152158
// take the @ mentions and turn them into markdown?
153159
// translate links

0 commit comments

Comments
 (0)