Skip to content

Commit fdf5d03

Browse files
committed
feat: add openThread prop to VirtualizedMessageList
1 parent 8a76b2a commit fdf5d03

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
@@ -61,20 +61,24 @@ import type { Channel, ChannelState as StreamChannelState, UserResponse } from '
6161
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
6262
import { DEFAULT_NEXT_CHANNEL_PAGE_SIZE } from '../../constants/limits';
6363

64-
type VirtualizedMessageListPropsForContext =
64+
type PropsDrilledToMessage =
6565
| 'additionalMessageInputProps'
66-
| 'closeReactionSelectorOnClick'
6766
| 'customMessageActions'
68-
| 'customMessageRenderer'
6967
| 'formatDate'
68+
| 'messageActions'
69+
| 'openThread'
70+
| 'reactionDetailsSort'
71+
| 'sortReactions'
72+
| 'sortReactionDetails';
73+
74+
type VirtualizedMessageListPropsForContext =
75+
| PropsDrilledToMessage
76+
| 'closeReactionSelectorOnClick'
77+
| 'customMessageRenderer'
7078
| 'head'
7179
| 'loadingMore'
7280
| 'Message'
73-
| 'messageActions'
7481
| 'shouldGroupByUser'
75-
| 'reactionDetailsSort'
76-
| 'sortReactions'
77-
| 'sortReactionDetails'
7882
| 'threadList';
7983

8084
/**
@@ -199,6 +203,7 @@ const VirtualizedMessageListWithContext = <
199203
messageLimit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE,
200204
messages,
201205
notifications,
206+
openThread,
202207
// TODO: refactor to scrollSeekPlaceHolderConfiguration and components.ScrollSeekPlaceholder, like the Virtuoso Component
203208
overscan = 0,
204209
read,
@@ -464,6 +469,7 @@ const VirtualizedMessageListWithContext = <
464469
messageGroupStyles,
465470
MessageSystem,
466471
numItemsPrepended,
472+
openThread,
467473
ownMessagesReadByOthers,
468474
processedMessages,
469475
reactionDetailsSort,
@@ -511,15 +517,6 @@ const VirtualizedMessageListWithContext = <
511517
);
512518
};
513519

514-
type PropsDrilledToMessage =
515-
| 'additionalMessageInputProps'
516-
| 'customMessageActions'
517-
| 'formatDate'
518-
| 'messageActions'
519-
| 'reactionDetailsSort'
520-
| 'sortReactions'
521-
| 'sortReactionDetails';
522-
523520
export type VirtualizedMessageListProps<
524521
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
525522
> = Partial<Pick<MessageProps<StreamChatGenerics>, PropsDrilledToMessage>> & {

src/components/MessageList/VirtualizedMessageListComponents.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export const messageRenderer = <
137137
messageGroupStyles,
138138
MessageSystem,
139139
numItemsPrepended,
140+
openThread,
140141
ownMessagesReadByOthers,
141142
processedMessages: messageList,
142143
reactionDetailsSort,
@@ -226,6 +227,7 @@ export const messageRenderer = <
226227
message={message}
227228
Message={MessageUIComponent}
228229
messageActions={messageActions}
230+
openThread={openThread}
229231
reactionDetailsSort={reactionDetailsSort}
230232
readBy={ownMessagesReadByOthers[message.id] || []}
231233
sortReactionDetails={sortReactionDetails}

0 commit comments

Comments
 (0)