Skip to content

Commit e95eaa4

Browse files
committed
feat: add openThread prop to VirtualizedMessageList (#2523)
1 parent 9cb8e47 commit e95eaa4

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

docusaurus/docs/React/components/core-components/message-list.mdx

+25-4
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,32 @@ Custom action handler function to run on hover of user avatar.
396396

397397
### openThread
398398

399-
Custom action handler to open a [`Thread`](./thread.mdx) component.
399+
Custom handler invoked when the button in the `Message` component that opens [`Thread`](./thread.mdx) component is clicked. To be able to define custom logic to `openThread`, we need to have a wrapper around `MessageList` component and reach out to `ChannelActionContext` for the default `openThread` function.
400400

401-
| Type | Default |
402-
| -------- | -------------------------------------------------------------------------------------------- |
403-
| function | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |
401+
```tsx
402+
import { useCallback } from 'react';
403+
import { MessageList, useChannelActionContext } from 'stream-chat-react';
404+
import type { StreamMessage } from 'stream-chat-react';
405+
import type { StreamChatGenerics } from './types';
406+
407+
const MessageListWrapper = () => {
408+
const { openThread: contextOpenThread } = useChannelActionContext<StreamChatGenerics>();
409+
410+
const openThread = useCallback(
411+
(message: StreamMessage<StreamChatGenerics>, event?: React.BaseSyntheticEvent) => {
412+
// custom logic
413+
contextOpenThread(message, event);
414+
},
415+
[contextOpenThread],
416+
);
417+
418+
return <MessageList openThread={openThread} />;
419+
};
420+
```
421+
422+
| Type | Default |
423+
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
424+
| `(message: StreamMessage, event?: React.BaseSyntheticEvent) => void` | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |
404425

405426
### pinPermissions
406427

docusaurus/docs/React/components/core-components/virtualized-list.mdx

+29
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,35 @@ The messages to render in the list, provide your own array to override the data
209209
| ----- | -------------------------------------------------------------------------------------- |
210210
| array | [ChannelStateContextValue['messages']](../contexts/channel-state-context.mdx#messages) |
211211

212+
### openThread
213+
214+
Custom handler invoked when the button in the `Message` component that opens [`Thread`](./thread.mdx) component is clicked. To be able to define custom logic to `openThread`, we need to have a wrapper around `VirtualizedMessageList` component and reach out to `ChannelActionContext` for the default `openThread` function.
215+
216+
```tsx
217+
import { useCallback } from 'react';
218+
import { VirtualizedMessageList, useChannelActionContext } from 'stream-chat-react';
219+
import type { StreamMessage } from 'stream-chat-react';
220+
import type { StreamChatGenerics } from './types';
221+
222+
const MessageListWrapper = () => {
223+
const { openThread: contextOpenThread } = useChannelActionContext<StreamChatGenerics>();
224+
225+
const openThread = useCallback(
226+
(message: StreamMessage<StreamChatGenerics>, event?: React.BaseSyntheticEvent) => {
227+
// custom logic
228+
contextOpenThread(message, event);
229+
},
230+
[contextOpenThread],
231+
);
232+
233+
return <VirtualizedMessageList openThread={openThread} />;
234+
};
235+
```
236+
237+
| Type | Default |
238+
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
239+
| `(message: StreamMessage, event?: React.BaseSyntheticEvent) => void` | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |
240+
212241
### overscan
213242

214243
The amount of extra content the list should render in addition to what's necessary to fill in the viewport.

src/components/MessageList/VirtualizedMessageList.tsx

+13-16
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,24 @@ import type { Channel, ChannelState as StreamChannelState, UserResponse } from '
6565
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
6666
import { DEFAULT_NEXT_CHANNEL_PAGE_SIZE } from '../../constants/limits';
6767

68-
type VirtualizedMessageListPropsForContext =
68+
type PropsDrilledToMessage =
6969
| 'additionalMessageInputProps'
70-
| 'closeReactionSelectorOnClick'
7170
| 'customMessageActions'
72-
| 'customMessageRenderer'
7371
| 'formatDate'
72+
| 'messageActions'
73+
| 'openThread'
74+
| 'reactionDetailsSort'
75+
| 'sortReactions'
76+
| 'sortReactionDetails';
77+
78+
type VirtualizedMessageListPropsForContext =
79+
| PropsDrilledToMessage
80+
| 'closeReactionSelectorOnClick'
81+
| 'customMessageRenderer'
7482
| 'head'
7583
| 'loadingMore'
7684
| 'Message'
77-
| 'messageActions'
7885
| 'shouldGroupByUser'
79-
| 'reactionDetailsSort'
80-
| 'sortReactions'
81-
| 'sortReactionDetails'
8286
| 'threadList';
8387

8488
/**
@@ -202,6 +206,7 @@ const VirtualizedMessageListWithContext = <
202206
messageLimit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE,
203207
messages,
204208
notifications,
209+
openThread,
205210
// TODO: refactor to scrollSeekPlaceHolderConfiguration and components.ScrollSeekPlaceholder, like the Virtuoso Component
206211
overscan = 0,
207212
read,
@@ -469,6 +474,7 @@ const VirtualizedMessageListWithContext = <
469474
messageGroupStyles,
470475
MessageSystem,
471476
numItemsPrepended,
477+
openThread,
472478
ownMessagesReadByOthers,
473479
processedMessages,
474480
reactionDetailsSort,
@@ -518,15 +524,6 @@ const VirtualizedMessageListWithContext = <
518524
);
519525
};
520526

521-
type PropsDrilledToMessage =
522-
| 'additionalMessageInputProps'
523-
| 'customMessageActions'
524-
| 'formatDate'
525-
| 'messageActions'
526-
| 'reactionDetailsSort'
527-
| 'sortReactions'
528-
| 'sortReactionDetails';
529-
530527
export type VirtualizedMessageListProps<
531528
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
532529
> = Partial<Pick<MessageProps<StreamChatGenerics>, PropsDrilledToMessage>> & {

src/components/MessageList/VirtualizedMessageListComponents.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const messageRenderer = <
135135
messageGroupStyles,
136136
MessageSystem,
137137
numItemsPrepended,
138+
openThread,
138139
ownMessagesReadByOthers,
139140
processedMessages: messageList,
140141
reactionDetailsSort,
@@ -225,6 +226,7 @@ export const messageRenderer = <
225226
message={message}
226227
Message={MessageUIComponent}
227228
messageActions={messageActions}
229+
openThread={openThread}
228230
reactionDetailsSort={reactionDetailsSort}
229231
readBy={ownMessagesReadByOthers[message.id] || []}
230232
sortReactionDetails={sortReactionDetails}

0 commit comments

Comments
 (0)