Skip to content

Commit 0e05faa

Browse files
committed
fix: usage of state store in list
1 parent 023d1b2 commit 0e05faa

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

package/src/components/MessageList/MessageFlashList.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ import {
4949
import { mergeThemes, useTheme } from '../../contexts/themeContext/ThemeContext';
5050
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
5151

52-
import { useStableCallback, useStateStore } from '../../hooks';
53-
import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state';
52+
import { useStableCallback } from '../../hooks';
5453
import { FileTypes } from '../../types/types';
5554
import { MessageWrapper } from '../Message/MessageSimple/MessageWrapper';
5655

@@ -259,13 +258,6 @@ const renderItem = ({ item: message }: { item: LocalMessage }) => {
259258
return <MessageWrapper message={message} />;
260259
};
261260

262-
const channelUnreadStateSelector = (state: ChannelUnreadStateStoreType) => ({
263-
first_unread_message_id: state.channelUnreadState?.first_unread_message_id,
264-
last_read: state.channelUnreadState?.last_read,
265-
last_read_message_id: state.channelUnreadState?.last_read_message_id,
266-
unread_messages: state.channelUnreadState?.unread_messages,
267-
});
268-
269261
const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) => {
270262
const LoadingMoreRecentIndicator = props.threadList
271263
? InlineLoadingMoreRecentThreadIndicator
@@ -320,10 +312,6 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
320312
UnreadMessagesNotification,
321313
} = props;
322314
const flashListRef = useRef<FlashListRef<LocalMessage> | null>(null);
323-
const channelUnreadState = useStateStore(
324-
channelUnreadStateStore.state,
325-
channelUnreadStateSelector,
326-
);
327315

328316
const [hasMoved, setHasMoved] = useState(false);
329317
const [scrollToBottomButtonVisible, setScrollToBottomButtonVisible] = useState(false);
@@ -566,6 +554,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
566554
*/
567555
useEffect(() => {
568556
const shouldMarkRead = () => {
557+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
569558
return (
570559
!channelUnreadState?.first_unread_message_id &&
571560
!scrollToBottomButtonVisible &&
@@ -577,6 +566,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
577566
const handleEvent = async (event: Event) => {
578567
const mainChannelUpdated = !event.message?.parent_id || event.message?.show_in_channel;
579568
const isMyOwnMessage = event.message?.user?.id === client.user?.id;
569+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
580570
// When the scrollToBottomButtonVisible is true, we need to manually update the channelUnreadState when its a received message.
581571
if (
582572
(scrollToBottomButtonVisible || channelUnreadState?.first_unread_message_id) &&
@@ -587,7 +577,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
587577
setChannelUnreadState({
588578
...channelUnreadState,
589579
last_read:
590-
channelUnreadState.last_read ??
580+
channelUnreadState?.last_read ??
591581
(previousUnreadCount === 0 && previousLastMessage?.created_at
592582
? new Date(previousLastMessage.created_at)
593583
: new Date(0)), // not having information about the last read message means the whole channel is unread,
@@ -605,7 +595,7 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
605595
};
606596
}, [
607597
channel,
608-
channelUnreadState,
598+
channelUnreadStateStore,
609599
client.user?.id,
610600
markRead,
611601
scrollToBottomButtonVisible,
@@ -646,12 +636,19 @@ const MessageFlashListWithContext = (props: MessageFlashListPropsWithContext) =>
646636
* This function should show or hide the unread indicator depending on the
647637
*/
648638
const updateStickyUnreadIndicator = useStableCallback((viewableItems: ViewToken[]) => {
649-
if (!viewableItems.length || !readEvents) {
650-
setIsUnreadNotificationOpen(false);
651-
return;
652-
}
639+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
640+
// we need this check to make sure that regular list change do not trigger
641+
// the unread notification to appear (for example if the old last read messages
642+
// go out of the viewport).
643+
const lastReadMessageId = channelUnreadState?.last_read_message_id;
644+
const lastReadMessageVisible = viewableItems.some((item) => item.item.id === lastReadMessageId);
653645

654-
if (selectedPicker === 'images') {
646+
if (
647+
!viewableItems.length ||
648+
!readEvents ||
649+
lastReadMessageVisible ||
650+
selectedPicker === 'images'
651+
) {
655652
setIsUnreadNotificationOpen(false);
656653
return;
657654
}

package/src/components/MessageList/MessageList.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ import {
5151
import { mergeThemes, useTheme } from '../../contexts/themeContext/ThemeContext';
5252
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
5353

54-
import { useStableCallback, useStateStore } from '../../hooks';
55-
import { ChannelUnreadStateStoreType } from '../../state-store/channel-unread-state';
54+
import { useStableCallback } from '../../hooks';
5655
import { FileTypes } from '../../types/types';
5756
import { MessageWrapper } from '../Message/MessageSimple/MessageWrapper';
5857

@@ -120,13 +119,6 @@ const getPreviousLastMessage = (messages: LocalMessage[], newMessage?: MessageRe
120119
return previousLastMessage;
121120
};
122121

123-
const channelUnreadStateSelector = (state: ChannelUnreadStateStoreType) => ({
124-
first_unread_message_id: state.channelUnreadState?.first_unread_message_id,
125-
last_read: state.channelUnreadState?.last_read,
126-
last_read_message_id: state.channelUnreadState?.last_read_message_id,
127-
unread_messages: state.channelUnreadState?.unread_messages,
128-
});
129-
130122
type MessageListPropsWithContext = Pick<
131123
AttachmentPickerContextValue,
132124
'closePicker' | 'selectedPicker' | 'setSelectedPicker'
@@ -310,10 +302,6 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
310302
} = props;
311303
const [isUnreadNotificationOpen, setIsUnreadNotificationOpen] = useState<boolean>(false);
312304
const { theme } = useTheme();
313-
const channelUnreadState = useStateStore(
314-
channelUnreadStateStore.state,
315-
channelUnreadStateSelector,
316-
);
317305

318306
const {
319307
colors: { white_snow },
@@ -445,6 +433,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
445433
* This function should show or hide the unread indicator depending on the
446434
*/
447435
const updateStickyUnreadIndicator = useStableCallback((viewableItems: ViewToken[]) => {
436+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
448437
// we need this check to make sure that regular list change do not trigger
449438
// the unread notification to appear (for example if the old last read messages
450439
// go out of the viewport).
@@ -547,6 +536,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
547536
*/
548537
useEffect(() => {
549538
const shouldMarkRead = () => {
539+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
550540
return (
551541
!channelUnreadState?.first_unread_message_id &&
552542
!scrollToBottomButtonVisible &&
@@ -558,17 +548,18 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
558548
const handleEvent = async (event: Event) => {
559549
const mainChannelUpdated = !event.message?.parent_id || event.message?.show_in_channel;
560550
const isMyOwnMessage = event.message?.user?.id === client.user?.id;
551+
const channelUnreadState = channelUnreadStateStore.channelUnreadState;
561552
// When the scrollToBottomButtonVisible is true, we need to manually update the channelUnreadState when its a received message.
562553
if (
563554
(scrollToBottomButtonVisible || channelUnreadState?.first_unread_message_id) &&
564555
!isMyOwnMessage
565556
) {
566-
const previousUnreadCount = channelUnreadState.unread_messages ?? 0;
557+
const previousUnreadCount = channelUnreadState?.unread_messages ?? 0;
567558
const previousLastMessage = getPreviousLastMessage(channel.state.messages, event.message);
568559
setChannelUnreadState({
569560
...channelUnreadState,
570561
last_read:
571-
channelUnreadState.last_read ??
562+
channelUnreadState?.last_read ??
572563
(previousUnreadCount === 0 && previousLastMessage?.created_at
573564
? new Date(previousLastMessage.created_at)
574565
: new Date(0)), // not having information about the last read message means the whole channel is unread,
@@ -586,7 +577,7 @@ const MessageListWithContext = (props: MessageListPropsWithContext) => {
586577
};
587578
}, [
588579
channel,
589-
channelUnreadState,
580+
channelUnreadStateStore,
590581
client.user?.id,
591582
markRead,
592583
scrollToBottomButtonVisible,

0 commit comments

Comments
 (0)