Skip to content

Commit 206c898

Browse files
feat: added support for prop 'getMessagesGroupStyles' on Channel (#1749)
* feat: added support for prop 'getMessagesGroupStyles' on Channel * style: align markdown table * docs: added prop doc for getMessagesGroupStyles * refactor: fix lint issue * docs: lint fixes Co-authored-by: Mads Røskar <[email protected]>
1 parent 9d4e1eb commit 206c898

File tree

7 files changed

+29
-4
lines changed

7 files changed

+29
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Messages with message list are grouped with some smart logic e.g., avatar of sender only appears for last message of the group.
2+
Messages are grouped together on UI if they are sent back to back by a user within certain timeframe (this timeframe can be controlled by
3+
a prop `MaxTimeBetweenGroupedMessages` on Channel component). `getMessagesGroupStyles` prop can be used to override the logic
4+
which determines the position of particular message within a group of messages e.g., top, bottom, middle, single etc. And depending
5+
on value, certain UI elements are hidden or shown on UI e.g, message timestamp, user avatar etc.
6+
You can find the default logic in [`getGroupStyles`](https://github.com/GetStream/stream-chat-react-native/blob/develop/package/src/components/MessageList/utils/getGroupStyles.ts) function.
7+
8+
Group styles returned by this function can be accessed by `groupStyles` value from [`MessageContext`](../../../../contexts/message_context.mdx)
9+
within custom components.
10+
11+
| Type |
12+
| -------- |
13+
| function |

docusaurus/docs/reactnative/core-components/channel.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import FlatList from '../common-content/core-components/channel/props/flat_list.
4848
import ForceAlignMessages from '../common-content/core-components/channel/props/force_align_messages.mdx';
4949
import FormatDate from '../common-content/core-components/channel/props/format_date.mdx';
5050
import Gallery from '../common-content/core-components/channel/props/gallery.mdx';
51+
import GetMessagesGroupStyles from '../common-content/core-components/channel/props/get_messages_group_styles.mdx';
5152
import Giphy from '../common-content/core-components/channel/props/giphy.mdx';
5253
import GiphyEnabled from '../common-content/core-components/channel/props/giphy_enabled.mdx';
5354
import GiphyVersion from '../common-content/core-components/channel/props/giphy_version.mdx';
@@ -431,6 +432,10 @@ const doUpdateMessageRequest = (channelId, messageObject) => {
431432

432433
<FormatDate />
433434

435+
### getMessagesGroupStyles
436+
437+
<GetMessagesGroupStyles />
438+
434439
### giphyEnabled
435440

436441
<GiphyEnabled />

examples/SampleApp/src/components/UnreadCountBadge.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { useEffect, useState } from 'react';
22
import { StyleSheet, Text, View } from 'react-native';
3-
import { isOwnUser } from 'stream-chat';
43
import { useTheme } from 'stream-chat-react-native';
54

65
import { useAppContext } from '../context/AppContext';

package/src/components/Channel/Channel.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ export type ChannelPropsWithContext<
239239
| 'forceAlignMessages'
240240
| 'formatDate'
241241
| 'Gallery'
242+
| 'getMessagesGroupStyles'
242243
| 'Giphy'
243244
| 'giphyVersion'
244245
| 'handleBlock'
@@ -430,6 +431,7 @@ const ChannelWithContext = <
430431
forceAlignMessages,
431432
formatDate,
432433
Gallery = GalleryDefault,
434+
getMessagesGroupStyles,
433435
Giphy = GiphyDefault,
434436
giphyEnabled,
435437
giphyVersion = 'fixed_height',
@@ -1662,6 +1664,7 @@ const ChannelWithContext = <
16621664
forceAlignMessages,
16631665
formatDate,
16641666
Gallery,
1667+
getMessagesGroupStyles,
16651668
Giphy,
16661669
giphyVersion,
16671670
handleBlock,

package/src/components/Channel/hooks/useCreateMessagesContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const useCreateMessagesContext = <
2727
forceAlignMessages,
2828
formatDate,
2929
Gallery,
30+
getMessagesGroupStyles,
3031
Giphy,
3132
giphyVersion,
3233
handleBlock,
@@ -117,6 +118,7 @@ export const useCreateMessagesContext = <
117118
forceAlignMessages,
118119
formatDate,
119120
Gallery,
121+
getMessagesGroupStyles,
120122
Giphy,
121123
giphyVersion,
122124
handleBlock,

package/src/components/MessageList/hooks/useMessageList.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type UseMessageListParams = {
2323
threadList?: boolean;
2424
};
2525

26-
export type GroupType = 'bottom' | 'middle' | 'single' | 'top';
26+
export type GroupType = string;
2727

2828
export type MessagesWithStylesReadByAndDateSeparator<
2929
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -56,7 +56,8 @@ export const useMessageList = <
5656
const { client } = useChatContext<StreamChatGenerics>();
5757
const { hideDateSeparators, maxTimeBetweenGroupedMessages, read } =
5858
useChannelContext<StreamChatGenerics>();
59-
const { deletedMessagesVisibilityType } = useMessagesContext<StreamChatGenerics>();
59+
const { deletedMessagesVisibilityType, getMessagesGroupStyles = getGroupStyles } =
60+
useMessagesContext<StreamChatGenerics>();
6061
const { messages } = usePaginatedMessageListContext<StreamChatGenerics>();
6162
const { threadMessages } = useThreadContext<StreamChatGenerics>();
6263

@@ -72,7 +73,7 @@ export const useMessageList = <
7273
userId: client.userID,
7374
});
7475

75-
const messageGroupStyles = getGroupStyles<StreamChatGenerics>({
76+
const messageGroupStyles = getMessagesGroupStyles<StreamChatGenerics>({
7677
dateSeparators,
7778
hideDateSeparators,
7879
maxTimeBetweenGroupedMessages,

package/src/contexts/messagesContext/MessagesContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type { InlineDateSeparatorProps } from '../../components/MessageList/Inli
4242
import type { MessageListProps } from '../../components/MessageList/MessageList';
4343
import type { MessageSystemProps } from '../../components/MessageList/MessageSystem';
4444
import type { ScrollToBottomButtonProps } from '../../components/MessageList/ScrollToBottomButton';
45+
import type { getGroupStyles } from '../../components/MessageList/utils/getGroupStyles';
4546
import type { MessageActionType } from '../../components/MessageOverlay/MessageActionListItem';
4647
import type { OverlayReactionListProps } from '../../components/MessageOverlay/OverlayReactionList';
4748
import type { ReplyProps } from '../../components/Reply/Reply';
@@ -296,6 +297,7 @@ export type MessagesContextValue<
296297
* Optional function to custom format the message date
297298
*/
298299
formatDate?: (date: TDateTimeParserInput) => string;
300+
getMessagesGroupStyles?: typeof getGroupStyles;
299301
handleBlock?: (message: MessageType<StreamChatGenerics>) => Promise<void>;
300302
/** Handler to access when a copy message action is invoked */
301303
handleCopy?: (message: MessageType<StreamChatGenerics>) => Promise<void>;

0 commit comments

Comments
 (0)