Skip to content

Commit 9c8ddbf

Browse files
committed
fix: move logic to component
1 parent 5d1d3d1 commit 9c8ddbf

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ export type ChannelPropsWithContext<
447447
* Custom loading error indicator to override the Stream default
448448
*/
449449
LoadingErrorIndicator?: React.ComponentType<LoadingErrorProps>;
450+
/**
451+
* Boolean flag to enable/disable marking the channel as read on mount
452+
*/
453+
markReadOnMount?: boolean;
450454
maxMessageLength?: number;
451455
/**
452456
* Load the channel at a specified message instead of the most recent message.
@@ -581,6 +585,7 @@ const ChannelWithContext = <
581585
loadingMore: loadingMoreProp,
582586
loadingMoreRecent: loadingMoreRecentProp,
583587
markdownRules,
588+
markReadOnMount = true,
584589
maxMessageLength: maxMessageLengthProp,
585590
maxNumberOfFiles = 10,
586591
maxTimeBetweenGroupedMessages,
@@ -841,7 +846,7 @@ const ChannelWithContext = <
841846
});
842847
}
843848

844-
if (channel.countUnread() > 0) {
849+
if (channel.countUnread() > 0 && markReadOnMount) {
845850
await markRead({ updateChannelUnreadState: false });
846851
}
847852

package/src/components/MessageList/MessageList.tsx

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ type MessageListPropsWithContext<
115115
| 'hideStickyDateHeader'
116116
| 'highlightedMessageId'
117117
| 'loadChannelAroundMessage'
118-
| 'loadChannelAtFirstUnreadMessage'
119118
| 'loading'
120119
| 'LoadingIndicator'
121120
| 'markRead'
122121
| 'NetworkDownIndicator'
123122
| 'reloadChannel'
124123
| 'scrollToFirstUnreadThreshold'
125124
| 'setTargetedMessage'
126-
| 'setChannelUnreadState'
127125
| 'StickyHeader'
128126
| 'targetedMessage'
129127
| 'threadList'
@@ -250,7 +248,6 @@ const MessageListWithContext = <
250248
isListActive = false,
251249
legacyImageViewerSwipeBehaviour,
252250
loadChannelAroundMessage,
253-
loadChannelAtFirstUnreadMessage,
254251
loading,
255252
LoadingIndicator,
256253
loadMore,
@@ -269,7 +266,6 @@ const MessageListWithContext = <
269266
reloadChannel,
270267
ScrollToBottomButton,
271268
selectedPicker,
272-
setChannelUnreadState,
273269
setFlatListRef,
274270
setMessages,
275271
setSelectedPicker,
@@ -1000,14 +996,6 @@ const MessageListWithContext = <
1000996
}
1001997
};
1002998

1003-
const onUnreadNotificationPress = async () => {
1004-
await loadChannelAtFirstUnreadMessage({
1005-
channelUnreadState,
1006-
setChannelUnreadState,
1007-
setTargetedMessage,
1008-
});
1009-
};
1010-
1011999
const onUnreadNotificationClose = async () => {
10121000
await markRead();
10131001
setIsUnreadNotificationOpen(false);
@@ -1154,10 +1142,7 @@ const MessageListWithContext = <
11541142
/>
11551143
<NetworkDownIndicator />
11561144
{isUnreadNotificationOpen && !threadList ? (
1157-
<UnreadMessagesNotification
1158-
onCloseHandler={onUnreadNotificationClose}
1159-
onPressHandler={onUnreadNotificationPress}
1160-
/>
1145+
<UnreadMessagesNotification onCloseHandler={onUnreadNotificationClose} />
11611146
) : null}
11621147
</View>
11631148
);
@@ -1184,14 +1169,12 @@ export const MessageList = <
11841169
highlightedMessageId,
11851170
isChannelActive,
11861171
loadChannelAroundMessage,
1187-
loadChannelAtFirstUnreadMessage,
11881172
loading,
11891173
LoadingIndicator,
11901174
markRead,
11911175
NetworkDownIndicator,
11921176
reloadChannel,
11931177
scrollToFirstUnreadThreshold,
1194-
setChannelUnreadState,
11951178
setTargetedMessage,
11961179
StickyHeader,
11971180
targetedMessage,
@@ -1241,7 +1224,6 @@ export const MessageList = <
12411224
isListActive: isChannelActive,
12421225
legacyImageViewerSwipeBehaviour,
12431226
loadChannelAroundMessage,
1244-
loadChannelAtFirstUnreadMessage,
12451227
loading,
12461228
LoadingIndicator,
12471229
loadMore,
@@ -1258,7 +1240,6 @@ export const MessageList = <
12581240
ScrollToBottomButton,
12591241
scrollToFirstUnreadThreshold,
12601242
selectedPicker,
1261-
setChannelUnreadState,
12621243
setMessages,
12631244
setSelectedPicker,
12641245
setTargetedMessage,

package/src/components/MessageList/UnreadMessagesNotification.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22
import { Pressable, StyleSheet, Text } from 'react-native';
33

4+
import { useChannelContext } from '../../contexts/channelContext/ChannelContext';
45
import { useTheme } from '../../contexts/themeContext/ThemeContext';
56
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
67
import { Close } from '../../icons';
@@ -9,20 +10,44 @@ export type UnreadMessagesNotificationProps = {
910
/**
1011
* Callback to handle the press event
1112
*/
12-
onPressHandler: () => void;
13+
onPressHandler?: () => Promise<void>;
1314
/**
1415
* Callback to handle the close event
1516
*/
1617
onCloseHandler?: () => void;
17-
/**
18-
* If the notification is visible
19-
*/
20-
visible?: boolean;
2118
};
2219

2320
export const UnreadMessagesNotification = (props: UnreadMessagesNotificationProps) => {
24-
const { onCloseHandler, onPressHandler, visible = true } = props;
21+
const { onCloseHandler, onPressHandler } = props;
2522
const { t } = useTranslationContext();
23+
const {
24+
channelUnreadState,
25+
loadChannelAtFirstUnreadMessage,
26+
markRead,
27+
setChannelUnreadState,
28+
setTargetedMessage,
29+
} = useChannelContext();
30+
31+
const handleOnPress = async () => {
32+
if (onPressHandler) {
33+
await onPressHandler();
34+
} else {
35+
await loadChannelAtFirstUnreadMessage({
36+
channelUnreadState,
37+
setChannelUnreadState,
38+
setTargetedMessage,
39+
});
40+
}
41+
};
42+
43+
const handleClose = async () => {
44+
if (onCloseHandler) {
45+
await onCloseHandler();
46+
} else {
47+
await markRead();
48+
}
49+
};
50+
2651
const {
2752
theme: {
2853
colors: { text_low_emphasis, white_snow },
@@ -32,11 +57,9 @@ export const UnreadMessagesNotification = (props: UnreadMessagesNotificationProp
3257
},
3358
} = useTheme();
3459

35-
if (!visible) return null;
36-
3760
return (
3861
<Pressable
39-
onPress={onPressHandler}
62+
onPress={handleOnPress}
4063
style={({ pressed }) => [
4164
styles.container,
4265
{ backgroundColor: text_low_emphasis, opacity: pressed ? 0.8 : 1 },
@@ -45,7 +68,7 @@ export const UnreadMessagesNotification = (props: UnreadMessagesNotificationProp
4568
>
4669
<Text style={[styles.text, { color: white_snow }, text]}>{t<string>('Unread Messages')}</Text>
4770
<Pressable
48-
onPress={onCloseHandler}
71+
onPress={handleClose}
4972
style={({ pressed }) => [
5073
{
5174
opacity: pressed ? 0.8 : 1,

0 commit comments

Comments
 (0)