Skip to content

Commit

Permalink
feat: add VirtualizedMessageListContext
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Jan 24, 2025
1 parent 3801375 commit 5a7defb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/components/MessageList/VirtualizedMessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
} from '../../context/ChannelStateContext';
import { ChatContextValue, useChatContext } from '../../context/ChatContext';
import { ComponentContextValue, useComponentContext } from '../../context/ComponentContext';
import { VirtualizedMessageListContextProvider } from '../../context/VirtualizedMessageListContext';

import type { Channel, ChannelState as StreamChannelState, UserResponse } from 'stream-chat';
import type { DefaultStreamChatGenerics, UnknownType } from '../../types/types';
Expand Down Expand Up @@ -439,7 +440,7 @@ const VirtualizedMessageListWithContext = <
: 'virtualized-message-list-dialog-manager';

return (
<>
<VirtualizedMessageListContextProvider value={{ scrollToBottom }}>
<MessageListMainPanel>
<DialogManagerProvider id={dialogManagerId}>
{!threadList && showUnreadMessagesNotification && (
Expand Down Expand Up @@ -524,7 +525,7 @@ const VirtualizedMessageListWithContext = <
unreadCount={threadList ? undefined : channelUnreadUiState?.unread_messages}
/>
{giphyPreviewMessage && <GiphyPreviewMessage message={giphyPreviewMessage} />}
</>
</VirtualizedMessageListContextProvider>
);
};

Expand Down
27 changes: 27 additions & 0 deletions src/context/VirtualizedMessageListContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { createContext, PropsWithChildren, useContext } from 'react';

export type VirtualizedMessageListContextValue = {
/** Function that scrolls the list to the bottom. */
scrollToBottom: () => void;
};

export const VirtualizedMessageListContext = createContext<
VirtualizedMessageListContextValue | undefined
>(undefined);

/**
* Context provider for components rendered within the `VirtualizedMessageList`
*/
export const VirtualizedMessageListContextProvider = ({
children,
value,
}: PropsWithChildren<{
value: VirtualizedMessageListContextValue;
}>) => (
<VirtualizedMessageListContext.Provider value={value as VirtualizedMessageListContextValue}>
{children}
</VirtualizedMessageListContext.Provider>
);

export const useVirtualizedMessageListContext = () =>
useContext(VirtualizedMessageListContext) as VirtualizedMessageListContextValue;

Check warning on line 27 in src/context/VirtualizedMessageListContext.tsx

View check run for this annotation

Codecov / codecov/patch

src/context/VirtualizedMessageListContext.tsx#L27

Added line #L27 was not covered by tests

0 comments on commit 5a7defb

Please sign in to comment.