Skip to content

Commit 7cfdc28

Browse files
Render MD within QuotedMessage component
1 parent 881e4b8 commit 7cfdc28

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

src/components/Message/QuotedMessage.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,66 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import clsx from 'clsx';
3+
import type { TranslationLanguages } from 'stream-chat';
34

45
import { Attachment as DefaultAttachment } from '../Attachment';
56
import { Avatar as DefaultAvatar } from '../Avatar';
67
import { Poll } from '../Poll';
7-
88
import { useChatContext } from '../../context/ChatContext';
99
import { useComponentContext } from '../../context/ComponentContext';
1010
import { useMessageContext } from '../../context/MessageContext';
1111
import { useTranslationContext } from '../../context/TranslationContext';
1212
import { useChannelActionContext } from '../../context/ChannelActionContext';
13+
import { renderText as defaultRenderText } from './renderText';
14+
import type { MessageContextValue } from '../../context/MessageContext';
1315

14-
import type { TranslationLanguages } from 'stream-chat';
16+
export type QuotedMessageProps<
17+
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
18+
> = Pick<MessageContextValue<StreamChatGenerics>, 'renderText'>;
1519

1620
import type { DefaultStreamChatGenerics } from '../../types/types';
1721

1822
export const QuotedMessage = <
1923
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
20-
>() => {
24+
>({
25+
renderText: propsRenderText,
26+
}: QuotedMessageProps) => {
2127
const { Attachment = DefaultAttachment, Avatar: ContextAvatar } =
2228
useComponentContext<StreamChatGenerics>('QuotedMessage');
2329
const { client } = useChatContext();
24-
const { isMyMessage, message } = useMessageContext<StreamChatGenerics>('QuotedMessage');
30+
const {
31+
isMyMessage,
32+
message,
33+
renderText: contextRenderText,
34+
} = useMessageContext<StreamChatGenerics>('QuotedMessage');
2535
const { t, userLanguage } = useTranslationContext('QuotedMessage');
2636
const { jumpToMessage } = useChannelActionContext('QuotedMessage');
2737

38+
const renderText = propsRenderText ?? contextRenderText ?? defaultRenderText;
39+
2840
const Avatar = ContextAvatar || DefaultAvatar;
2941

3042
const { quoted_message } = message;
31-
if (!quoted_message) return null;
3243

33-
const poll = quoted_message.poll_id && client.polls.fromState(quoted_message.poll_id);
44+
const poll = quoted_message?.poll_id && client.polls.fromState(quoted_message.poll_id);
3445
const quotedMessageDeleted =
35-
quoted_message.deleted_at || quoted_message.type === 'deleted';
46+
quoted_message?.deleted_at || quoted_message?.type === 'deleted';
3647

3748
const quotedMessageText = quotedMessageDeleted
3849
? t('This message was deleted...')
39-
: quoted_message.i18n?.[`${userLanguage}_text` as `${TranslationLanguages}_text`] ||
40-
quoted_message.text;
50+
: quoted_message?.i18n?.[`${userLanguage}_text` as `${TranslationLanguages}_text`] ||
51+
quoted_message?.text;
4152

4253
const quotedMessageAttachment =
43-
quoted_message.attachments?.length && !quotedMessageDeleted
54+
quoted_message?.attachments?.length && !quotedMessageDeleted
4455
? quoted_message.attachments[0]
4556
: null;
4657

58+
const renderedText = useMemo(
59+
() => renderText(quotedMessageText),
60+
[quotedMessageText, renderText],
61+
);
62+
63+
if (!quoted_message) return null;
4764
if (!quoted_message.poll && !quotedMessageText && !quotedMessageAttachment) return null;
4865

4966
return (
@@ -80,7 +97,7 @@ export const QuotedMessage = <
8097
className='str-chat__quoted-message-bubble__text'
8198
data-testid='quoted-message-text'
8299
>
83-
{quotedMessageText}
100+
{renderedText}
84101
</div>
85102
</>
86103
)}

src/components/MessageInput/MessageInputFlat.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ export const MessageInputFlat = <
141141
const recordingEnabled = !!(recordingController.recorder && navigator.mediaDevices); // account for requirement on iOS as per this bug report: https://bugs.webkit.org/show_bug.cgi?id=252303
142142
const isRecording = !!recordingController.recordingState;
143143

144-
/* This bit here is needed to make sure that we can get rid of the default behaviour
144+
/**
145+
* This bit here is needed to make sure that we can get rid of the default behaviour
145146
* if need be. Essentially this allows us to pass StopAIGenerationButton={null} and
146147
* completely circumvent the default logic if it's not what we want. We need it as a
147148
* prop because there is no other trivial way to override the SendMessage button otherwise.

0 commit comments

Comments
 (0)