Skip to content

Commit

Permalink
feat: add openThread prop to VirtualizedMessageList (#2523)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Oct 7, 2024
1 parent 9cb8e47 commit e95eaa4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
29 changes: 25 additions & 4 deletions docusaurus/docs/React/components/core-components/message-list.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,32 @@ Custom action handler function to run on hover of user avatar.

### openThread

Custom action handler to open a [`Thread`](./thread.mdx) component.
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.

| Type | Default |
| -------- | -------------------------------------------------------------------------------------------- |
| function | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |
```tsx
import { useCallback } from 'react';
import { MessageList, useChannelActionContext } from 'stream-chat-react';
import type { StreamMessage } from 'stream-chat-react';
import type { StreamChatGenerics } from './types';

const MessageListWrapper = () => {
const { openThread: contextOpenThread } = useChannelActionContext<StreamChatGenerics>();

const openThread = useCallback(
(message: StreamMessage<StreamChatGenerics>, event?: React.BaseSyntheticEvent) => {
// custom logic
contextOpenThread(message, event);
},
[contextOpenThread],
);

return <MessageList openThread={openThread} />;
};
```

| Type | Default |
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| `(message: StreamMessage, event?: React.BaseSyntheticEvent) => void` | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |

### pinPermissions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,35 @@ The messages to render in the list, provide your own array to override the data
| ----- | -------------------------------------------------------------------------------------- |
| array | [ChannelStateContextValue['messages']](../contexts/channel-state-context.mdx#messages) |

### openThread

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.

```tsx
import { useCallback } from 'react';
import { VirtualizedMessageList, useChannelActionContext } from 'stream-chat-react';
import type { StreamMessage } from 'stream-chat-react';
import type { StreamChatGenerics } from './types';

const MessageListWrapper = () => {
const { openThread: contextOpenThread } = useChannelActionContext<StreamChatGenerics>();

const openThread = useCallback(
(message: StreamMessage<StreamChatGenerics>, event?: React.BaseSyntheticEvent) => {
// custom logic
contextOpenThread(message, event);
},
[contextOpenThread],
);

return <VirtualizedMessageList openThread={openThread} />;
};
```

| Type | Default |
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| `(message: StreamMessage, event?: React.BaseSyntheticEvent) => void` | [ChannelActionContextValue['openThread']](../contexts/channel-action-context.mdx#openthread) |

### overscan

The amount of extra content the list should render in addition to what's necessary to fill in the viewport.
Expand Down
29 changes: 13 additions & 16 deletions src/components/MessageList/VirtualizedMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,24 @@ import type { Channel, ChannelState as StreamChannelState, UserResponse } from '
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
import { DEFAULT_NEXT_CHANNEL_PAGE_SIZE } from '../../constants/limits';

type VirtualizedMessageListPropsForContext =
type PropsDrilledToMessage =
| 'additionalMessageInputProps'
| 'closeReactionSelectorOnClick'
| 'customMessageActions'
| 'customMessageRenderer'
| 'formatDate'
| 'messageActions'
| 'openThread'
| 'reactionDetailsSort'
| 'sortReactions'
| 'sortReactionDetails';

type VirtualizedMessageListPropsForContext =
| PropsDrilledToMessage
| 'closeReactionSelectorOnClick'
| 'customMessageRenderer'
| 'head'
| 'loadingMore'
| 'Message'
| 'messageActions'
| 'shouldGroupByUser'
| 'reactionDetailsSort'
| 'sortReactions'
| 'sortReactionDetails'
| 'threadList';

/**
Expand Down Expand Up @@ -202,6 +206,7 @@ const VirtualizedMessageListWithContext = <
messageLimit = DEFAULT_NEXT_CHANNEL_PAGE_SIZE,
messages,
notifications,
openThread,
// TODO: refactor to scrollSeekPlaceHolderConfiguration and components.ScrollSeekPlaceholder, like the Virtuoso Component
overscan = 0,
read,
Expand Down Expand Up @@ -469,6 +474,7 @@ const VirtualizedMessageListWithContext = <
messageGroupStyles,
MessageSystem,
numItemsPrepended,
openThread,
ownMessagesReadByOthers,
processedMessages,
reactionDetailsSort,
Expand Down Expand Up @@ -518,15 +524,6 @@ const VirtualizedMessageListWithContext = <
);
};

type PropsDrilledToMessage =
| 'additionalMessageInputProps'
| 'customMessageActions'
| 'formatDate'
| 'messageActions'
| 'reactionDetailsSort'
| 'sortReactions'
| 'sortReactionDetails';

export type VirtualizedMessageListProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = Partial<Pick<MessageProps<StreamChatGenerics>, PropsDrilledToMessage>> & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const messageRenderer = <
messageGroupStyles,
MessageSystem,
numItemsPrepended,
openThread,
ownMessagesReadByOthers,
processedMessages: messageList,
reactionDetailsSort,
Expand Down Expand Up @@ -225,6 +226,7 @@ export const messageRenderer = <
message={message}
Message={MessageUIComponent}
messageActions={messageActions}
openThread={openThread}
reactionDetailsSort={reactionDetailsSort}
readBy={ownMessagesReadByOthers[message.id] || []}
sortReactionDetails={sortReactionDetails}
Expand Down

0 comments on commit e95eaa4

Please sign in to comment.