Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add openThread prop to VirtualizedMessageList #2523

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -61,20 +61,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 @@ -199,6 +203,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 @@ -464,6 +469,7 @@ const VirtualizedMessageListWithContext = <
messageGroupStyles,
MessageSystem,
numItemsPrepended,
openThread,
ownMessagesReadByOthers,
processedMessages,
reactionDetailsSort,
Expand Down Expand Up @@ -511,15 +517,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 @@ -137,6 +137,7 @@ export const messageRenderer = <
messageGroupStyles,
MessageSystem,
numItemsPrepended,
openThread,
ownMessagesReadByOthers,
processedMessages: messageList,
reactionDetailsSort,
Expand Down Expand Up @@ -226,6 +227,7 @@ export const messageRenderer = <
message={message}
Message={MessageUIComponent}
messageActions={messageActions}
openThread={openThread}
reactionDetailsSort={reactionDetailsSort}
readBy={ownMessagesReadByOthers[message.id] || []}
sortReactionDetails={sortReactionDetails}
Expand Down
Loading