Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new messages indicator after comment linking #55582

Merged
merged 5 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/components/InvertedFlatList/BaseInvertedFlatList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useMemo, useState} from 'react';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import type {FlatListProps, ListRenderItem, ListRenderItemInfo, FlatList as RNFlatList, ScrollViewProps} from 'react-native';
import FlatList from '@components/FlatList';
import usePrevious from '@hooks/usePrevious';
Expand Down Expand Up @@ -98,11 +98,37 @@ function BaseInvertedFlatList<T>(props: BaseInvertedFlatListProps<T>, ref: Forwa
return config;
}, [shouldEnableAutoScrollToTopThreshold, isLoadingData, wasLoadingData]);

const listRef = useRef<RNFlatList | null>(null);
useImperativeHandle(ref, () => {
// If we're trying to scroll at the start of the list we need to make sure to
// render all items.
const scrollToOffsetFn: RNFlatList['scrollToOffset'] = (params) => {
if (params.offset === 0) {
setCurrentDataId(null);
}
requestAnimationFrame(() => {
listRef.current?.scrollToOffset(params);
});
};

return new Proxy(
{},
{
get: (_target, prop) => {
if (prop === 'scrollToOffset') {
return scrollToOffsetFn;
}
return listRef.current?.[prop as keyof RNFlatList];
},
},
) as RNFlatList;
});

return (
<FlatList
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
ref={ref}
ref={listRef}
maintainVisibleContentPosition={maintainVisibleContentPosition}
inverted
data={displayedData}
Expand Down
25 changes: 18 additions & 7 deletions src/pages/home/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,18 @@ function ReportActionsList({
hasNewestReportActionRef.current = hasNewestReportAction;
const previousLastIndex = useRef(lastActionIndex);

const isLastPendingActionIsDelete = sortedReportActions?.at(0)?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
// Display the new message indicator when comment linking and not close to the newest message.
const reportActionID = route?.params?.reportActionID;
const indexOfLinkedAction = reportActionID ? sortedVisibleReportActions.findIndex((action) => action.reportActionID === reportActionID) : -1;
const isLinkedActionCloseToNewest = indexOfLinkedAction < 5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on testing in Prod, it should be 15 or something. Btw, we should move this into a constant

Screen.Recording.2025-01-22.at.18.30.28.mov

const [isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible] = useState(!isLinkedActionCloseToNewest);

const [isFloatingMessageCounterVisible, setIsFloatingMessageCounterVisible] = useState(false);
useEffect(() => {
if (isLinkedActionCloseToNewest) {
return;
}
setIsFloatingMessageCounterVisible(true);
}, [isLinkedActionCloseToNewest, route]);

useEffect(() => {
if (
Expand All @@ -400,6 +409,7 @@ function ReportActionsList({
reportActionSize.current > sortedVisibleReportActions.length &&
hasNewestReportAction
) {
setIsFloatingMessageCounterVisible(false);
reportScrollManager.scrollToBottom();
}
previousLastIndex.current = lastActionIndex;
Expand Down Expand Up @@ -438,6 +448,7 @@ function ReportActionsList({
return;
}
InteractionManager.runAfterInteractions(() => {
setIsFloatingMessageCounterVisible(false);
reportScrollManager.scrollToBottom();
});
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
Expand All @@ -455,6 +466,7 @@ function ReportActionsList({
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.reportID));
return;
}
setIsFloatingMessageCounterVisible(false);
reportScrollManager.scrollToBottom();
});
},
Expand Down Expand Up @@ -515,6 +527,8 @@ function ReportActionsList({
};

const scrollToBottomAndMarkReportAsRead = useCallback(() => {
setIsFloatingMessageCounterVisible(false);

if (!hasNewestReportAction) {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.reportID));
openReport(report.reportID);
Expand Down Expand Up @@ -742,15 +756,12 @@ function ReportActionsList({
loadOlderChats(false);
}, [loadOlderChats]);

// When performing comment linking, initially 25 items are added to the list. Subsequent fetches add 15 items from the cache or 50 items from the server.
// This is to ensure that the user is able to see the 'scroll to newer comments' button when they do comment linking and have not reached the end of the list yet.
const canScrollToNewerComments = !isLoadingInitialReportActions && !hasNewestReportAction && sortedReportActions.length > 25 && !isLastPendingActionIsDelete;
const [reportActionsListTestID, reportActionsListFSClass] = getChatFSAttributes(participantsContext, 'ReportActionsList', report);

return (
<>
<FloatingMessageCounter
isActive={(isFloatingMessageCounterVisible && !!unreadMarkerReportActionID) || canScrollToNewerComments}
isActive={isFloatingMessageCounterVisible}
onClick={scrollToBottomAndMarkReportAsRead}
/>
<View
Expand Down Expand Up @@ -782,7 +793,7 @@ function ReportActionsList({
extraData={extraData}
key={listID}
shouldEnableAutoScrollToTopThreshold={shouldEnableAutoScrollToTopThreshold}
initialScrollKey={route?.params?.reportActionID}
initialScrollKey={reportActionID}
/>
</View>
</>
Expand Down
Loading