Skip to content

Commit fded783

Browse files
committed
fix: ignore message list rerenders due to state change
1 parent 34d8fde commit fded783

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import { mergeThemes, useTheme } from '../../contexts/themeContext/ThemeContext'
5353
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
5454

5555
import { useStableCallback, useStateStore } from '../../hooks';
56-
import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state';
5756
import { MessageInputHeightState } from '../../state-store/message-input-height-store';
5857
import { primitives } from '../../theme';
5958
import { MessageWrapper } from '../Message/MessageSimple/MessageWrapper';
@@ -105,10 +104,6 @@ const messageInputHeightStoreSelector = (state: MessageInputHeightState) => ({
105104
height: state.height,
106105
});
107106

108-
const channelUnreadStateStoreSelector = (state: ChannelUnreadStateStoreType) => ({
109-
unread_messages: state.channelUnreadState?.unread_messages,
110-
});
111-
112107
type MessageFlashListPropsWithContext = Pick<
113108
AttachmentPickerContextValue,
114109
'closePicker' | 'attachmentPickerStore'
@@ -322,11 +317,6 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
322317
messageInputHeightStoreSelector,
323318
);
324319

325-
const { unread_messages } = useStateStore(
326-
channelUnreadStateStore.state,
327-
channelUnreadStateStoreSelector,
328-
);
329-
330320
const [hasMoved, setHasMoved] = useState(false);
331321
const [scrollToBottomButtonVisible, setScrollToBottomButtonVisible] = useState(false);
332322
const [isUnreadNotificationOpen, setIsUnreadNotificationOpen] = useState<boolean>(false);
@@ -1104,15 +1094,15 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
11041094
<ScrollToBottomButton
11051095
onPress={goToNewMessages}
11061096
showNotification={scrollToBottomButtonVisible}
1107-
unreadCount={threadList ? 0 : unread_messages}
1097+
unreadCount={threadList ? 0 : channel?.countUnread()}
11081098
/>
11091099
</Animated.View>
11101100
<NetworkDownIndicator />
11111101
{isUnreadNotificationOpen && !threadList ? (
11121102
<View style={styles.unreadMessagesNotificationContainer}>
11131103
<UnreadMessagesNotification
11141104
onCloseHandler={onUnreadNotificationClose}
1115-
unreadCount={unread_messages}
1105+
channelUnreadStateStore={channelUnreadStateStore}
11161106
/>
11171107
</View>
11181108
) : null}

package/src/components/MessageList/MessageList.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import { ThreadContextValue, useThreadContext } from '../../contexts/threadConte
6666
import { useStableCallback } from '../../hooks';
6767
import { useStateStore } from '../../hooks/useStateStore';
6868
import { bumpOverlayLayoutRevision } from '../../state-store';
69-
import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state';
7069
import { MessageInputHeightState } from '../../state-store/message-input-height-store';
7170
import { primitives } from '../../theme';
7271
import { MessageWrapper } from '../Message/MessageSimple/MessageWrapper';
@@ -305,10 +304,6 @@ const messageInputHeightStoreSelector = (state: MessageInputHeightState) => ({
305304
height: state.height,
306305
});
307306

308-
const channelUnreadStateStoreSelector = (state: ChannelUnreadStateStoreType) => ({
309-
unread_messages: state.channelUnreadState?.unread_messages,
310-
});
311-
312307
/**
313308
* The message list component renders a list of messages. It consumes the following contexts:
314309
*
@@ -380,10 +375,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
380375
messageInputHeightStore.store,
381376
messageInputHeightStoreSelector,
382377
);
383-
const { unread_messages } = useStateStore(
384-
channelUnreadStateStore.state,
385-
channelUnreadStateStoreSelector,
386-
);
387378

388379
const myMessageThemeString = useMemo(() => JSON.stringify(myMessageTheme), [myMessageTheme]);
389380

@@ -1328,7 +1319,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
13281319
<ScrollToBottomButton
13291320
onPress={goToNewMessages}
13301321
showNotification={scrollToBottomButtonVisible}
1331-
unreadCount={threadList ? 0 : unread_messages}
1322+
unreadCount={threadList ? 0 : channel?.countUnread()}
13321323
/>
13331324
</Animated.View>
13341325
) : null}
@@ -1338,7 +1329,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
13381329
<View style={styles.unreadMessagesNotificationContainer}>
13391330
<UnreadMessagesNotification
13401331
onCloseHandler={onUnreadNotificationClose}
1341-
unreadCount={unread_messages}
1332+
channelUnreadStateStore={channelUnreadStateStore}
13421333
/>
13431334
</View>
13441335
) : null}

package/src/components/MessageList/UnreadMessagesNotification.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import React, { useMemo } from 'react';
22
import { StyleSheet, View } from 'react-native';
33

4-
import { useChannelContext } from '../../contexts/channelContext/ChannelContext';
4+
import {
5+
ChannelContextValue,
6+
useChannelContext,
7+
} from '../../contexts/channelContext/ChannelContext';
58
import { useTheme } from '../../contexts/themeContext/ThemeContext';
69
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
10+
import { useStateStore } from '../../hooks/useStateStore';
711
import { ArrowUp } from '../../icons/ArrowUp';
812
import { NewClose } from '../../icons/NewClose';
13+
import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state';
914
import { primitives } from '../../theme';
1015
import { Button } from '../ui';
1116

12-
export type UnreadMessagesNotificationProps = {
17+
export type UnreadMessagesNotificationProps = Pick<
18+
ChannelContextValue,
19+
'channelUnreadStateStore'
20+
> & {
1321
/**
1422
* Callback to handle the close event
1523
*/
@@ -24,16 +32,21 @@ export type UnreadMessagesNotificationProps = {
2432
unreadCount?: number;
2533
};
2634

35+
const channelUnreadStateSelector = (state: ChannelUnreadStateStoreType) => ({
36+
unread_messages: state.channelUnreadState?.unread_messages,
37+
});
38+
2739
export const UnreadMessagesNotification = (props: UnreadMessagesNotificationProps) => {
28-
const { onCloseHandler, onPressHandler, unreadCount } = props;
40+
const { onCloseHandler, onPressHandler, unreadCount, channelUnreadStateStore } = props;
2941
const { t } = useTranslationContext();
30-
const {
31-
channelUnreadStateStore,
32-
loadChannelAtFirstUnreadMessage,
33-
markRead,
34-
setChannelUnreadState,
35-
setTargetedMessage,
36-
} = useChannelContext();
42+
const { loadChannelAtFirstUnreadMessage, markRead, setChannelUnreadState, setTargetedMessage } =
43+
useChannelContext();
44+
const { unread_messages } = useStateStore(
45+
channelUnreadStateStore.state,
46+
channelUnreadStateSelector,
47+
);
48+
49+
const count = unread_messages ?? unreadCount;
3750

3851
const handleOnPress = async () => {
3952
if (onPressHandler) {
@@ -64,7 +77,7 @@ export const UnreadMessagesNotification = (props: UnreadMessagesNotificationProp
6477
variant='secondary'
6578
type='ghost'
6679
LeadingIcon={ArrowUp}
67-
label={unreadCount ? t('{{count}} unread', { count: unreadCount }) : t('Unread Messages')}
80+
label={count ? t('{{count}} unread', { count }) : t('Unread Messages')}
6881
onPress={handleOnPress}
6982
size='md'
7083
/>

0 commit comments

Comments
 (0)