From 49e097ed1a560f0f478395dc1227bbce649f62b3 Mon Sep 17 00:00:00 2001 From: burczu Date: Tue, 21 May 2024 07:11:40 +0200 Subject: [PATCH 1/8] filtering out whispers when checking for newest report actions --- src/pages/home/report/ReportActionsList.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index d8aed24e3b1..b83fc1fe01c 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -209,9 +209,17 @@ function ReportActionsList({ ), [sortedReportActions, isOffline], ); + + const newestVisibleReportAction = useMemo(() => { + const filteredReportActions = sortedVisibleReportActions.filter((reportAction) => !ReportActionsUtils.isWhisperAction(reportAction)); + + return filteredReportActions[0]; + }, + [sortedVisibleReportActions]); + const lastActionIndex = sortedVisibleReportActions[0]?.reportActionID; const reportActionSize = useRef(sortedVisibleReportActions.length); - const hasNewestReportAction = sortedVisibleReportActions?.[0]?.created === report.lastVisibleActionCreated; + const hasNewestReportAction = newestVisibleReportAction.created === report.lastVisibleActionCreated; const hasNewestReportActionRef = useRef(hasNewestReportAction); hasNewestReportActionRef.current = hasNewestReportAction; const previousLastIndex = useRef(lastActionIndex); From 5e39755539984c902be5115ab3a3e7226d4e9d59 Mon Sep 17 00:00:00 2001 From: burczu Date: Tue, 21 May 2024 07:18:35 +0200 Subject: [PATCH 2/8] prettier fixes --- src/pages/home/report/ReportActionsList.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index b83fc1fe01c..79bf64044c1 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -214,8 +214,7 @@ function ReportActionsList({ const filteredReportActions = sortedVisibleReportActions.filter((reportAction) => !ReportActionsUtils.isWhisperAction(reportAction)); return filteredReportActions[0]; - }, - [sortedVisibleReportActions]); + }, [sortedVisibleReportActions]); const lastActionIndex = sortedVisibleReportActions[0]?.reportActionID; const reportActionSize = useRef(sortedVisibleReportActions.length); From d0bb2f507ee8b75d9f78c5df0940d367bb1503c9 Mon Sep 17 00:00:00 2001 From: burczu Date: Tue, 21 May 2024 12:23:32 +0200 Subject: [PATCH 3/8] comment added --- src/pages/home/report/ReportActionsList.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index 79bf64044c1..0b354a28a16 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -210,6 +210,7 @@ function ReportActionsList({ [sortedReportActions, isOffline], ); + // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is newest report action const newestVisibleReportAction = useMemo(() => { const filteredReportActions = sortedVisibleReportActions.filter((reportAction) => !ReportActionsUtils.isWhisperAction(reportAction)); From 9b3e4d4ea2f8db92bf1bab952a2c5b04600d04f8 Mon Sep 17 00:00:00 2001 From: burczu Date: Wed, 22 May 2024 10:31:42 +0200 Subject: [PATCH 4/8] filtering out improved to just remove whispers from top --- src/pages/home/report/ReportActionsList.tsx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index 0b354a28a16..e6b91f7a3cb 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -2,6 +2,7 @@ import type {ListRenderItemInfo} from '@react-native/virtualized-lists/Lists/Vir import {useIsFocused, useRoute} from '@react-navigation/native'; import type {RouteProp} from '@react-navigation/native'; import type {DebouncedFunc} from 'lodash'; +import cloneDeep from 'lodash/cloneDeep'; import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'; import {DeviceEventEmitter, InteractionManager} from 'react-native'; import type {LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent, StyleProp, ViewStyle} from 'react-native'; @@ -210,12 +211,21 @@ function ReportActionsList({ [sortedReportActions, isOffline], ); - // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is newest report action + const filterOutLastWhisperAction = useCallback((actions: OnyxTypes.ReportAction[]): OnyxTypes.ReportAction[] => { + if (actions.length > 1 && ReportActionsUtils.isWhisperAction(actions[0])) { + actions.shift(); + return filterOutLastWhisperAction(actions); + } + + return actions; + }, []); + const newestVisibleReportAction = useMemo(() => { - const filteredReportActions = sortedVisibleReportActions.filter((reportAction) => !ReportActionsUtils.isWhisperAction(reportAction)); + // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is the newest report action + const filteredActions = filterOutLastWhisperAction(cloneDeep(sortedVisibleReportActions)); - return filteredReportActions[0]; - }, [sortedVisibleReportActions]); + return filteredActions[0]; + }, [filterOutLastWhisperAction, sortedVisibleReportActions]); const lastActionIndex = sortedVisibleReportActions[0]?.reportActionID; const reportActionSize = useRef(sortedVisibleReportActions.length); From ea78e1c0ca557f1e6fcbd0bdd3b732d0141f51fe Mon Sep 17 00:00:00 2001 From: burczu Date: Wed, 22 May 2024 10:36:17 +0200 Subject: [PATCH 5/8] additional comment added --- src/pages/home/report/ReportActionsList.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index e6b91f7a3cb..8652a6ed412 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -214,6 +214,7 @@ function ReportActionsList({ const filterOutLastWhisperAction = useCallback((actions: OnyxTypes.ReportAction[]): OnyxTypes.ReportAction[] => { if (actions.length > 1 && ReportActionsUtils.isWhisperAction(actions[0])) { actions.shift(); + // repeat the process until the first action is not a whisper action return filterOutLastWhisperAction(actions); } From 1103fa0fb86bf69514b215cfa5aa0a3212336f1b Mon Sep 17 00:00:00 2001 From: burczu Date: Wed, 22 May 2024 12:09:17 +0200 Subject: [PATCH 6/8] switched to different approach with while instead of recurrence and shift --- src/pages/home/report/ReportActionsList.tsx | 25 +++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index 8652a6ed412..27aa8520936 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -211,26 +211,23 @@ function ReportActionsList({ [sortedReportActions, isOffline], ); - const filterOutLastWhisperAction = useCallback((actions: OnyxTypes.ReportAction[]): OnyxTypes.ReportAction[] => { - if (actions.length > 1 && ReportActionsUtils.isWhisperAction(actions[0])) { - actions.shift(); - // repeat the process until the first action is not a whisper action - return filterOutLastWhisperAction(actions); - } - - return actions; - }, []); - const newestVisibleReportAction = useMemo(() => { // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is the newest report action - const filteredActions = filterOutLastWhisperAction(cloneDeep(sortedVisibleReportActions)); + let index = 0; + const size = sortedVisibleReportActions.length; + while (index < size) { + if (!ReportActionsUtils.isWhisperAction(sortedVisibleReportActions[index])) { + return sortedVisibleReportActions[index]; + } + index += 1; + } - return filteredActions[0]; - }, [filterOutLastWhisperAction, sortedVisibleReportActions]); + return null; + }, [sortedVisibleReportActions]); const lastActionIndex = sortedVisibleReportActions[0]?.reportActionID; const reportActionSize = useRef(sortedVisibleReportActions.length); - const hasNewestReportAction = newestVisibleReportAction.created === report.lastVisibleActionCreated; + const hasNewestReportAction = newestVisibleReportAction?.created === report.lastVisibleActionCreated; const hasNewestReportActionRef = useRef(hasNewestReportAction); hasNewestReportActionRef.current = hasNewestReportAction; const previousLastIndex = useRef(lastActionIndex); From 14a3f221054c5c7577cfbef82900b76c4010e2f8 Mon Sep 17 00:00:00 2001 From: burczu Date: Wed, 22 May 2024 13:09:59 +0200 Subject: [PATCH 7/8] unnecessary import removed --- src/pages/home/report/ReportActionsList.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index 27aa8520936..1308593b44d 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -2,7 +2,6 @@ import type {ListRenderItemInfo} from '@react-native/virtualized-lists/Lists/Vir import {useIsFocused, useRoute} from '@react-navigation/native'; import type {RouteProp} from '@react-navigation/native'; import type {DebouncedFunc} from 'lodash'; -import cloneDeep from 'lodash/cloneDeep'; import React, {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'; import {DeviceEventEmitter, InteractionManager} from 'react-native'; import type {LayoutChangeEvent, NativeScrollEvent, NativeSyntheticEvent, StyleProp, ViewStyle} from 'react-native'; From 6dfb6fdd877d0c38781083b5698ae709824e18d3 Mon Sep 17 00:00:00 2001 From: burczu Date: Thu, 23 May 2024 09:45:48 +0200 Subject: [PATCH 8/8] code simplified --- src/pages/home/report/ReportActionsList.tsx | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.tsx b/src/pages/home/report/ReportActionsList.tsx index 1308593b44d..c26623c7575 100644 --- a/src/pages/home/report/ReportActionsList.tsx +++ b/src/pages/home/report/ReportActionsList.tsx @@ -210,19 +210,8 @@ function ReportActionsList({ [sortedReportActions, isOffline], ); - const newestVisibleReportAction = useMemo(() => { - // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is the newest report action - let index = 0; - const size = sortedVisibleReportActions.length; - while (index < size) { - if (!ReportActionsUtils.isWhisperAction(sortedVisibleReportActions[index])) { - return sortedVisibleReportActions[index]; - } - index += 1; - } - - return null; - }, [sortedVisibleReportActions]); + // whisper action doesn't affect lastVisibleActionCreated, so we should not take it into account while checking if there is the newest report action + const newestVisibleReportAction = useMemo(() => sortedVisibleReportActions.find((item) => !ReportActionsUtils.isWhisperAction(item)) ?? null, [sortedVisibleReportActions]); const lastActionIndex = sortedVisibleReportActions[0]?.reportActionID; const reportActionSize = useRef(sortedVisibleReportActions.length);