Skip to content

Commit 37d54e2

Browse files
author
tfomkin
committed
fix: trigger loading indicator only when screen is focused
1 parent 8771fbc commit 37d54e2

1 file changed

Lines changed: 15 additions & 20 deletions

File tree

libs/mobile/chat/features/menu-list/src/lib/component.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTranslation } from '@ronas-it/react-native-common-modules/i18n';
2-
import { FlashList } from '@shopify/flash-list';
2+
import { useFocusEffect } from 'expo-router';
33
import { ReactElement, useCallback, useEffect, useRef, useState } from 'react';
44
import {
55
ChatActionsMenuSheet,
@@ -38,9 +38,7 @@ export function ChatMenuList({
3838
}: ChatMenuListProps): ReactElement {
3939
const translate = useTranslation('CHAT.CHAT_MENU_LIST');
4040
const chatActionsSheetRef = useRef<ChatActionsMenuSheetMethods>(null);
41-
const listRef = useRef<FlashList<ChatListItem>>(null);
42-
const previousIsRefetchingRef = useRef<boolean>(false);
43-
const scrollOffsetRef = useRef(0);
41+
const isScreenFocusedRef = useRef(false);
4442

4543
const [isFirstLoading, setIsFirstLoading] = useState<boolean>(true);
4644

@@ -78,6 +76,16 @@ export function ChatMenuList({
7876
}
7977
}, [isFirstLoading, isRefetching]);
8078

79+
useFocusEffect(
80+
useCallback(() => {
81+
isScreenFocusedRef.current = true;
82+
83+
return () => {
84+
isScreenFocusedRef.current = false;
85+
};
86+
}, []),
87+
);
88+
8189
const renderItem = useCallback(
8290
({ item }: { item: ChatListItem }) => (
8391
<ChatListRow
@@ -90,17 +98,6 @@ export function ChatMenuList({
9098
[onChatPress],
9199
);
92100

93-
// NOTE: Reset scroll position when auto refresh completes to remove extra space left by refresh control
94-
useEffect(() => {
95-
const wasRefetching = previousIsRefetchingRef.current;
96-
97-
if (wasRefetching && !isRefetching && listRef.current && scrollOffsetRef.current > 0) {
98-
listRef.current.scrollToOffset({ offset: 0, animated: false });
99-
}
100-
101-
previousIsRefetchingRef.current = isRefetching;
102-
}, [isRefetching]);
103-
104101
return (
105102
<View className='flex-1'>
106103
<PressableSearchInput onPress={onSearchPress} containerClassName='mx-16 pt-8' />
@@ -110,16 +107,14 @@ export function ChatMenuList({
110107
</View>
111108
) : (
112109
<DateSectionList
113-
ref={listRef}
114110
data={chats || []}
115111
estimatedItemSize={52}
116112
renderItem={renderItem}
117-
onScroll={(e) => {
118-
scrollOffsetRef.current = e.nativeEvent.contentOffset.y;
119-
}}
120113
transformSectionTitle={transformSectionTitle}
121114
onEndReached={fetchNextPage}
122-
refreshControl={<AppRefreshControl onRefresh={refetch} refreshing={isRefetching} />}
115+
refreshControl={
116+
<AppRefreshControl onRefresh={refetch} refreshing={isScreenFocusedRef.current && isRefetching} />
117+
}
123118
ListHeaderComponent={
124119
<View>
125120
{isFeatureEnabled(FeatureID.CHAT_FOLDERS) && (

0 commit comments

Comments
 (0)