Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 14 additions & 4 deletions src/pages/home/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {ListRenderItemInfo} from '@react-native/virtualized-lists/Lists/Vir
import {useIsFocused, useRoute} from '@react-navigation/native';
// eslint-disable-next-line lodash/import-scope
import type {DebouncedFunc} from 'lodash';
import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState} from 'react';
import {DeviceEventEmitter, InteractionManager, View} from 'react-native';
import type {LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent, StyleProp, ViewStyle} from 'react-native';
import {useOnyx} from 'react-native-onyx';
Expand Down Expand Up @@ -276,6 +276,10 @@ function ReportActionsList({
return false;
}

const isPendingAdd = (action: OnyxTypes.ReportAction) => {
return action?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD;
};

// If no unread marker exists, don't set an unread marker for newly added messages from the current user.
const isFromCurrentUser = accountID === (ReportActionsUtils.isReportPreviewAction(message) ? message.childLastActorAccountID : message.actorAccountID);
const isNewMessage = !prevSortedVisibleReportActionsObjects[message.reportActionID];
Expand All @@ -284,10 +288,16 @@ function ReportActionsList({
// The `unreadMarkerTime` has already been updated to match the optimistic action created time,
// but once the new action is saved on the backend, the actual created time will be later than the optimistic one.
// Therefore, we also need to prevent the unread marker from appearing for previously optimistic actions.
const isPreviouslyOptimistic = !!prevSortedVisibleReportActionsObjects[message.reportActionID]?.isOptimisticAction && !message.isOptimisticAction;
const isPreviouslyOptimistic =
(isPendingAdd(prevSortedVisibleReportActionsObjects[message.reportActionID]) && !isPendingAdd(message)) ||
(!!prevSortedVisibleReportActionsObjects[message.reportActionID]?.isOptimisticAction && !message.isOptimisticAction);
const shouldIgnoreUnreadForCurrentUserMessage = !prevUnreadMarkerReportActionID.current && isFromCurrentUser && (isNewMessage || isPreviouslyOptimistic);

return !shouldIgnoreUnreadForCurrentUserMessage;
if (isFromCurrentUser) {
return !shouldIgnoreUnreadForCurrentUserMessage;
}

return !isNewMessage || scrollingVerticalOffset.current >= MSG_VISIBLE_THRESHOLD;
};

// If there are message that were recevied while offline,
Expand Down Expand Up @@ -332,7 +342,7 @@ function ReportActionsList({
* the MSG_VISIBLE_THRESHOLD), the unread marker will display over those new messages rather than the initial
* lastReadTime.
*/
useEffect(() => {
useLayoutEffect(() => {
if (unreadMarkerReportActionID) {
return;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/UnreadIndicatorsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -587,4 +587,25 @@ describe('Unread Indicators', () => {

Onyx.disconnect(connection);
});

it('Do not display the new line indicator when receiving a new message from another user', async () => {
// Given a read report
await signInAndGetAppWithUnreadChat();

Report.readNewestAction(REPORT_ID, true);

await waitForBatchedUpdates();

await navigateToSidebarOption(0);

// When another user adds a new message
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, {
10: TestHelper.buildTestReportComment(DateUtils.getDBTime(), USER_B_ACCOUNT_ID, '10'),
});

// Then the new line indicator shouldn't be displayed
const newMessageLineIndicatorHintText = Localize.translateLocal('accessibilityHints.newMessageLineIndicator');
const unreadIndicator = screen.queryAllByLabelText(newMessageLineIndicatorHintText);
expect(unreadIndicator).toHaveLength(0);
});
});