Skip to content

Commit 32928f3

Browse files
feat: add MessageListMainPanel to ComponentContext (#2528)
### 🎯 Goal Allows to override `MessageListMainPanel` component especially for integrators migrating to `v12` where `MessageListMainPanel` now always renders a `div` since the theming v2 is now a default. #### Additional changs: - Added new class name to the default component to correct naming (`str-chat__message-list-main-panel`), ideally the styling would be moved too and the existing class name deprecated. - Simplified `ChannelPropsForwardedToComponentContext` type by picking types from the `ComponentContextValue` type directly.
1 parent 08e38e5 commit 32928f3

File tree

8 files changed

+140
-118
lines changed

8 files changed

+140
-118
lines changed

src/components/Channel/Channel.tsx

+54-103
Large diffs are not rendered by default.

src/components/MessageList/MessageList.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { InfiniteScroll, InfiniteScrollProps } from '../InfiniteScrollPaginator/
3030
import { LoadingIndicator as DefaultLoadingIndicator } from '../Loading';
3131
import { defaultPinPermissions, MESSAGE_ACTIONS } from '../Message/utils';
3232
import { TypingIndicator as DefaultTypingIndicator } from '../TypingIndicator';
33-
import { MessageListMainPanel } from './MessageListMainPanel';
33+
import { MessageListMainPanel as DefaultMessageListMainPanel } from './MessageListMainPanel';
3434

3535
import { defaultRenderMessages, MessageRenderer } from './renderMessages';
3636

@@ -104,6 +104,7 @@ const MessageListWithContext = <
104104
MessageNotification = DefaultMessageNotification,
105105
TypingIndicator = DefaultTypingIndicator,
106106
UnreadMessagesNotification = DefaultUnreadMessagesNotification,
107+
MessageListMainPanel = DefaultMessageListMainPanel,
107108
} = useComponentContext<StreamChatGenerics>('MessageList');
108109

109110
const {

src/components/MessageList/MessageListMainPanel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import type { PropsWithChildrenOnly } from '../../types/types';
33

4-
export const MESSAGE_LIST_MAIN_PANEL_CLASS = 'str-chat__main-panel-inner' as const;
4+
export const MESSAGE_LIST_MAIN_PANEL_CLASS = 'str-chat__main-panel-inner str-chat__message-list-main-panel' as const;
55

66
export const MessageListMainPanel = ({ children }: PropsWithChildrenOnly) => (
77
<div className={MESSAGE_LIST_MAIN_PANEL_CLASS}>{children}</div>

src/components/MessageList/VirtualizedMessageList.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { useMarkRead } from './hooks/useMarkRead';
2323

2424
import { MessageNotification as DefaultMessageNotification } from './MessageNotification';
2525
import { MessageListNotifications as DefaultMessageListNotifications } from './MessageListNotifications';
26-
import { MessageListMainPanel } from './MessageListMainPanel';
26+
import { MessageListMainPanel as DefaultMessageListMainPanel } from './MessageListMainPanel';
2727
import {
2828
getGroupStyles,
2929
getLastReceived,
@@ -235,6 +235,7 @@ const VirtualizedMessageListWithContext = <
235235
MessageListNotifications = DefaultMessageListNotifications,
236236
MessageNotification = DefaultMessageNotification,
237237
MessageSystem = DefaultMessageSystem,
238+
MessageListMainPanel = DefaultMessageListMainPanel,
238239
UnreadMessagesNotification = DefaultUnreadMessagesNotification,
239240
UnreadMessagesSeparator = DefaultUnreadMessagesSeparator,
240241
VirtualMessage: MessageUIComponentFromContext = MessageSimple,

src/components/MessageList/__tests__/MessageList.test.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { EmptyStateIndicator as EmptyStateIndicatorMock } from '../../EmptyState
2525
import { ScrollToBottomButton } from '../ScrollToBottomButton';
2626
import { MessageListNotifications } from '../MessageListNotifications';
2727
import { mockedApiResponse } from '../../../mock-builders/api/utils';
28+
import { nanoid } from 'nanoid';
2829

2930
expect.extend(toHaveNoViolations);
3031

@@ -326,7 +327,11 @@ describe('MessageList', () => {
326327
});
327328

328329
describe('unread messages', () => {
329-
const messages = Array.from({ length: 5 }, generateMessage);
330+
const timestamp = new Date().getTime();
331+
const messages = Array.from({ length: 5 }, (_, index) =>
332+
generateMessage({ created_at: new Date(timestamp + index * 1000).toISOString() }),
333+
);
334+
330335
const unread_messages = 2;
331336
const lastReadMessage = messages[unread_messages];
332337
const separatorText = `Unread messages`;
@@ -668,24 +673,34 @@ describe('MessageList', () => {
668673
msgListProps,
669674
});
670675
if (expected === 'should') {
671-
expect(screen.queryByTestId(UNREAD_MESSAGES_NOTIFICATION_TEST_ID)).toBeInTheDocument();
676+
await waitFor(() =>
677+
expect(
678+
screen.queryByTestId(UNREAD_MESSAGES_NOTIFICATION_TEST_ID),
679+
).toBeInTheDocument(),
680+
);
672681
} else {
673-
expect(
674-
screen.queryByTestId(UNREAD_MESSAGES_NOTIFICATION_TEST_ID),
675-
).not.toBeInTheDocument();
682+
await waitFor(() =>
683+
expect(
684+
screen.queryByTestId(UNREAD_MESSAGES_NOTIFICATION_TEST_ID),
685+
).not.toBeInTheDocument(),
686+
);
676687
}
677688
},
678689
);
679690

680691
it('should display custom unread messages notification', async () => {
681-
const customUnreadMessagesNotificationText = 'customUnreadMessagesNotificationText';
682-
const UnreadMessagesNotification = () => <div>{customUnreadMessagesNotificationText}</div>;
692+
const customUnreadMessagesNotificationText = nanoid();
693+
const UnreadMessagesNotification = () => (
694+
<div data-testid={customUnreadMessagesNotificationText}>aaa</div>
695+
);
683696
await setupTest({
684697
channelProps: { UnreadMessagesNotification },
685698
entries: observerEntriesScrolledBelowSeparator,
686699
});
687700

688-
expect(screen.getByText(customUnreadMessagesNotificationText)).toBeInTheDocument();
701+
await waitFor(() =>
702+
expect(screen.getByTestId(customUnreadMessagesNotificationText)).toBeInTheDocument(),
703+
);
689704
});
690705

691706
it('should not display unread messages notification when unread count is 0', async () => {

src/components/MessageList/__tests__/__snapshots__/VirtualizedMessageList.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`VirtualizedMessageList should render the list without any message 1`] =
88
className="str-chat__container"
99
>
1010
<div
11-
className="str-chat__main-panel-inner"
11+
className="str-chat__main-panel-inner str-chat__message-list-main-panel"
1212
>
1313
<div
1414
className="str-chat__virtual-list"

src/components/MessageList/hooks/MessageList/useUnreadMessagesNotification.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export const useUnreadMessagesNotification = ({
3636
return;
3737
}
3838

39-
const msgListPanel = document.querySelector(`.${MESSAGE_LIST_MAIN_PANEL_CLASS}`);
39+
const [msgListPanel] = document.getElementsByClassName(MESSAGE_LIST_MAIN_PANEL_CLASS);
4040
if (!msgListPanel) return;
4141

42-
const observedTarget = document.querySelector(`.${UNREAD_MESSAGE_SEPARATOR_CLASS}`);
42+
const [observedTarget] = document.getElementsByClassName(UNREAD_MESSAGE_SEPARATOR_CLASS);
4343
if (!observedTarget) {
4444
setShow(true);
4545
return;

0 commit comments

Comments
 (0)