Skip to content

Commit 3014fbd

Browse files
authored
Merge pull request #837 from GetStream/hide-new-message-separator
Hide new message separator option
2 parents 111e3cf + 070085c commit 3014fbd

File tree

6 files changed

+75
-23
lines changed

6 files changed

+75
-23
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Changelog
22

33

4+
## [5.1.2](https://github.com/GetStream/stream-chat-react/releases/tag/v5.1.2) 2021-04-05
5+
6+
### Feature
7+
8+
- Provide option to hide `DateSeparator` component for new messages with the `hideNewMessageSeparator` prop on `MessageList` and `VirtualizedMessageList` components [#837](https://github.com/GetStream/stream-chat-react/pull/837)
9+
10+
### Bug
11+
12+
- Fix bad conditional in `useMentionsHandlers` custom hook [#836](https://github.com/GetStream/stream-chat-react/pull/836)
13+
414
## [5.1.1](https://github.com/GetStream/stream-chat-react/releases/tag/v5.1.1) 2021-04-02
515

616
### Feature

src/components/MessageList/MessageList.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class MessageListWithContext<
291291
HeaderComponent={this.props.HeaderComponent}
292292
headerPosition={this.props.headerPosition}
293293
hideDeletedMessages={this.props.hideDeletedMessages}
294+
hideNewMessageSeparator={this.props.hideNewMessageSeparator}
294295
internalInfiniteScrollProps={{
295296
hasMore: this.props.hasMore,
296297
isLoading: this.props.loadingMore,
@@ -386,6 +387,7 @@ type PropsDrilledToMessageListInner =
386387
| 'HeaderComponent'
387388
| 'headerPosition'
388389
| 'hideDeletedMessages'
390+
| 'hideNewMessageSeparator'
389391
| 'internalInfiniteScrollProps'
390392
| 'messages'
391393
| 'MessageSystem'

src/components/MessageList/MessageListInner.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { insertDates } from './utils';
77
import { InfiniteScroll, InfiniteScrollProps } from '../InfiniteScrollPaginator';
88
import { Message } from '../Message';
99

10+
import { isDate } from '../../context/TranslationContext';
11+
1012
import type { Channel, StreamChat, UserResponse } from 'stream-chat';
1113

1214
import type { DateSeparatorProps } from '../DateSeparator/DateSeparator';
@@ -70,6 +72,8 @@ export type MessageListInnerProps<
7072
headerPosition?: number;
7173
/** Hides the MessageDeleted components from the list, defaults to `false` */
7274
hideDeletedMessages?: boolean;
75+
/** Hides the DateSeparator component when new messages are received in a channel that's watched but not active, defaults to `false` */
76+
hideNewMessageSeparator?: boolean;
7377
/** Overrides the default props passed to [InfiniteScroll](https://github.com/GetStream/stream-chat-react/blob/master/src/components/InfiniteScrollPaginator/InfiniteScroll.tsx) */
7478
internalInfiniteScrollProps?: InfiniteScrollProps;
7579
/** Overrides the default props passed to [Message](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Message/Message.tsx) */
@@ -277,6 +281,7 @@ const UnMemoizedMessageListInner = <
277281
HeaderComponent,
278282
headerPosition,
279283
hideDeletedMessages = false,
284+
hideNewMessageSeparator = false,
280285
internalInfiniteScrollProps,
281286
internalMessageProps,
282287
messages,
@@ -293,7 +298,14 @@ const UnMemoizedMessageListInner = <
293298
const enrichMessages = () => {
294299
const messageWithDates = threadList
295300
? messages
296-
: insertDates(messages, lastRead, client.userID, hideDeletedMessages, disableDateSeparator);
301+
: insertDates(
302+
messages,
303+
lastRead,
304+
client.userID,
305+
hideDeletedMessages,
306+
disableDateSeparator,
307+
hideNewMessageSeparator,
308+
);
297309

298310
if (HeaderComponent) {
299311
return insertIntro(messageWithDates, headerPosition);
@@ -335,10 +347,10 @@ const UnMemoizedMessageListInner = <
335347
const elements = useMemo(
336348
() =>
337349
enrichedMessages.map((message) => {
338-
if (message.type === 'message.date') {
350+
if (message.type === 'message.date' && message.date && isDate(message.date)) {
339351
return (
340-
<li key={`${(message.date as Date).toISOString()}-i`}>
341-
<DateSeparator date={message.date as Date} unread={!!message.unread} />
352+
<li key={`${message.date.toISOString()}-i`}>
353+
<DateSeparator date={message.date} unread={message.unread} />
342354
</li>
343355
);
344356
}

src/components/MessageList/VirtualizedMessageList.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from '../Message';
3535

3636
import { StreamMessage, useChannelContext } from '../../context/ChannelContext';
37-
import { useTranslationContext } from '../../context/TranslationContext';
37+
import { isDate, useTranslationContext } from '../../context/TranslationContext';
3838

3939
import type { Channel, StreamChat } from 'stream-chat';
4040

@@ -84,6 +84,10 @@ export type VirtualizedMessageListProps<
8484
disableDateSeparator?: boolean;
8585
/** The UI Indicator to use when MessageList or ChannelList is empty */
8686
EmptyStateIndicator?: React.ComponentType<EmptyStateIndicatorProps> | null;
87+
/** Hides the MessageDeleted components from the list, defaults to `false` */
88+
hideDeletedMessages?: boolean;
89+
/** Hides the DateSeparator component when new messages are received in a channel that's watched but not active, defaults to false */
90+
hideNewMessageSeparator?: boolean;
8791
/** Component to render at the top of the MessageList while loading new messages */
8892
LoadingIndicator?: React.ComponentType<LoadingIndicatorProps>;
8993
/** Available from [ChannelContext](https://getstream.github.io/stream-chat-react/#section-channelcontext) */
@@ -151,6 +155,8 @@ const VirtualizedMessageListWithContext = <
151155
DateSeparator = DefaultDateSeparator,
152156
EmptyStateIndicator = DefaultEmptyStateIndicator,
153157
hasMore,
158+
hideDeletedMessages = false,
159+
hideNewMessageSeparator = false,
154160
LoadingIndicator = DefaultLoadingIndicator,
155161
loadMore,
156162
loadingMore,
@@ -174,8 +180,25 @@ const VirtualizedMessageListWithContext = <
174180
if (typeof messages === 'undefined') {
175181
return undefined;
176182
}
177-
return disableDateSeparator ? messages : insertDates(messages, lastRead, userID);
178-
}, [messages, lastRead, disableDateSeparator, userID, messages?.length]);
183+
return disableDateSeparator
184+
? messages
185+
: insertDates(
186+
messages,
187+
lastRead,
188+
userID,
189+
hideDeletedMessages,
190+
disableDateSeparator,
191+
hideNewMessageSeparator,
192+
);
193+
}, [
194+
disableDateSeparator,
195+
hideDeletedMessages,
196+
hideNewMessageSeparator,
197+
lastRead,
198+
messages,
199+
messages?.length,
200+
userID,
201+
]);
179202

180203
const { t } = useTranslationContext();
181204

@@ -201,8 +224,7 @@ const VirtualizedMessageListWithContext = <
201224

202225
const message = messageList[streamMessageIndex];
203226

204-
if (message.type === 'message.date') {
205-
//@ts-expect-error
227+
if (message.type === 'message.date' && message.date && isDate(message.date)) {
206228
return <DateSeparator date={message.date} unread={message.unread} />;
207229
}
208230

src/components/MessageList/utils.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/* eslint-disable no-continue */
2+
import { isDate } from '../../context/TranslationContext';
3+
24
import type {
35
DefaultAttachmentType,
46
DefaultChannelType,
@@ -25,6 +27,7 @@ export const insertDates = <
2527
userID?: string,
2628
hideDeletedMessages?: boolean,
2729
disableDateSeparator?: boolean,
30+
hideNewMessageSeparator?: boolean,
2831
) => {
2932
let unread = false;
3033
let lastDateSeparator;
@@ -42,29 +45,31 @@ export const insertDates = <
4245
continue;
4346
}
4447

45-
//@ts-expect-error
46-
const messageDate = message.created_at.toDateString();
48+
const messageDate =
49+
(message.created_at && isDate(message.created_at) && message.created_at.toDateString()) || '';
4750
let prevMessageDate = messageDate;
51+
const previousMessage = messages[i - 1];
4852

49-
if (i > 0 && !disableDateSeparator) {
50-
//@ts-expect-error
51-
prevMessageDate = messages[i - 1].created_at.toDateString();
53+
if (
54+
i > 0 &&
55+
!disableDateSeparator &&
56+
previousMessage.created_at &&
57+
isDate(previousMessage.created_at)
58+
) {
59+
prevMessageDate = previousMessage.created_at.toDateString();
5260
}
5361

54-
if (!unread) {
55-
//@ts-expect-error
56-
unread = lastRead && new Date(lastRead) < message.created_at;
62+
if (!unread && !hideNewMessageSeparator) {
63+
unread = (lastRead && message.created_at && new Date(lastRead) < message.created_at) || false;
5764

5865
// do not show date separator for current user's messages
59-
//@ts-expect-error
60-
if (!disableDateSeparator && unread && message.user.id !== userID) {
61-
//@ts-expect-error
66+
if (!disableDateSeparator && unread && message.user?.id !== userID) {
6267
newMessages.push({
6368
date: message.created_at,
6469
id: message.id,
6570
type: 'message.date',
6671
unread,
67-
});
72+
} as StreamMessage<At, Ch, Co, Ev, Me, Re, Us>);
6873
}
6974
}
7075

@@ -80,12 +85,11 @@ export const insertDates = <
8085
lastDateSeparator = messageDate;
8186

8287
newMessages.push(
83-
//@ts-expect-error
8488
{
8589
date: message.created_at,
8690
id: message.id,
8791
type: 'message.date',
88-
},
92+
} as StreamMessage<At, Ch, Co, Ev, Me, Re, Us>,
8993
message,
9094
);
9195
} else {

types/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type DefaultCommandType = LiteralStringForUnion;
2525
export type DefaultEventType = UnknownType;
2626

2727
export type DefaultMessageType = UnknownType & {
28+
date?: string | Date;
2829
errorStatusCode?: number;
2930
event?: Event<
3031
DefaultAttachmentType,
@@ -35,6 +36,7 @@ export type DefaultMessageType = UnknownType & {
3536
DefaultReactionType,
3637
DefaultUserType
3738
>;
39+
unread?: boolean;
3840
};
3941

4042
export type DefaultReactionType = UnknownType;

0 commit comments

Comments
 (0)