From 2149ace46adcafc26d37adf39e5980ca19843dc0 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:38:30 -0800 Subject: [PATCH 1/8] Include filtered actions when computing oldest and newest reportAction on a report --- src/libs/actions/Report.ts | 30 +++++++++++++++------- src/pages/home/report/ReportActionsView.js | 5 ++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 7ad12cf3e1ed..a44f4dc36edc 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -73,7 +73,7 @@ import INPUT_IDS from '@src/types/form/NewRoomForm'; import type {PersonalDetails, PersonalDetailsList, PolicyReportField, RecentlyUsedReportFields, ReportActionReactions, ReportMetadata, ReportUserIsTyping} from '@src/types/onyx'; import type {Decision, OriginalMessageIOU} from '@src/types/onyx/OriginalMessage'; import type {NotificationPreference, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; -import type Report from '@src/types/onyx/Report'; +import Report from '@src/types/onyx/Report'; import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/ReportAction'; import type ReportAction from '@src/types/onyx/ReportAction'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; @@ -113,15 +113,27 @@ Onyx.connect({ }, }); +// map of reportID to all reportActions for that report const allReportActions: OnyxCollection = {}; + +// map of reportID to the oldest reportAction for that report +const oldestReportActions: Record = {}; + +// map of report to the ID of the newest action for that report +const newestReportActions = Report = {}; + Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, - callback: (action, key) => { - if (!key || !action) { + callback: (actions, key) => { + if (!key || !actions) { return; } const reportID = CollectionUtils.extractCollectionItemID(key); - allReportActions[reportID] = action; + allReportActions[reportID] = actions; + const sortedActions = ReportActionsUtils.getSortedReportActions(Object.values(actions)); + + oldestReportActions[reportID] = sortedActions[0].reportActionID; + newestReportActions[reportID] = sortedActions.at(-1).reportActionID; }, }); @@ -879,7 +891,7 @@ function reconnect(reportID: string) { * Gets the older actions that have not been read yet. * Normally happens when you scroll up on a chat, and the actions have not been read yet. */ -function getOlderActions(reportID: string, reportActionID: string) { +function getOlderActions(reportID: string) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -912,7 +924,7 @@ function getOlderActions(reportID: string, reportActionID: string) { const parameters: GetOlderActionsParams = { reportID, - reportActionID, + reportActionID: oldestReportActions[reportID], }; API.read(READ_COMMANDS.GET_OLDER_ACTIONS, parameters, {optimisticData, successData, failureData}); @@ -922,7 +934,7 @@ function getOlderActions(reportID: string, reportActionID: string) { * Gets the newer actions that have not been read yet. * Normally happens when you are not located at the bottom of the list and scroll down on a chat. */ -function getNewerActions(reportID: string, reportActionID: string) { +function getNewerActions(reportID: string) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -955,7 +967,7 @@ function getNewerActions(reportID: string, reportActionID: string) { const parameters: GetNewerActionsParams = { reportID, - reportActionID, + reportActionID: newestReportActions[reportID], }; API.read(READ_COMMANDS.GET_NEWER_ACTIONS, parameters, {optimisticData, successData, failureData}); @@ -2343,7 +2355,7 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal API.write(WRITE_COMMANDS.LEAVE_ROOM, parameters, {optimisticData, successData, failureData}); - const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}) as Report[], reportMetadata); + const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}), reportMetadata); // We want to filter out the current report, hidden reports and empty chats const filteredReportsByLastRead = sortedReportsByLastRead.filter( diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 5e9d863dd62d..7cbf7f41e42f 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -194,7 +194,7 @@ function ReportActionsView(props) { return; } // Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments - Report.getOlderActions(reportID, oldestReportAction.reportActionID); + Report.getOlderActions(reportID); }, [props.isLoadingOlderReportActions, props.network.isOffline, oldestReportAction, reportID]); /** @@ -223,8 +223,7 @@ function ReportActionsView(props) { return; } - const newestReportAction = _.first(props.reportActions); - Report.getNewerActions(reportID, newestReportAction.reportActionID); + Report.getNewerActions(reportID); }, 500), [props.isLoadingNewerReportActions, props.isLoadingInitialReportActions, props.reportActions, reportID, hasNewestReportAction], ); From 9c6291fb507ec1f8b71470158d845fb9ea9c3410 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:40:26 -0800 Subject: [PATCH 2/8] fix typo --- src/libs/actions/Report.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index a44f4dc36edc..5455e8dec8e7 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -73,7 +73,7 @@ import INPUT_IDS from '@src/types/form/NewRoomForm'; import type {PersonalDetails, PersonalDetailsList, PolicyReportField, RecentlyUsedReportFields, ReportActionReactions, ReportMetadata, ReportUserIsTyping} from '@src/types/onyx'; import type {Decision, OriginalMessageIOU} from '@src/types/onyx/OriginalMessage'; import type {NotificationPreference, RoomVisibility, WriteCapability} from '@src/types/onyx/Report'; -import Report from '@src/types/onyx/Report'; +import type Report from '@src/types/onyx/Report'; import type {Message, ReportActionBase, ReportActions} from '@src/types/onyx/ReportAction'; import type ReportAction from '@src/types/onyx/ReportAction'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; @@ -120,7 +120,7 @@ const allReportActions: OnyxCollection = {}; const oldestReportActions: Record = {}; // map of report to the ID of the newest action for that report -const newestReportActions = Report = {}; +const newestReportActions = Record = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, From 0f784f92514377dec5d812adfcccd8a632f1bac9 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:42:02 -0800 Subject: [PATCH 3/8] Fix another typo --- src/libs/actions/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 5455e8dec8e7..39f9a958c881 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -120,7 +120,7 @@ const allReportActions: OnyxCollection = {}; const oldestReportActions: Record = {}; // map of report to the ID of the newest action for that report -const newestReportActions = Record = {}; +const newestReportActions: Record = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, From faa7ba86fdcca9b496171be3f0d5f09c54702ec4 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:44:48 -0800 Subject: [PATCH 4/8] Fix type error --- src/libs/actions/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 39f9a958c881..ec090bce491b 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -133,7 +133,7 @@ Onyx.connect({ const sortedActions = ReportActionsUtils.getSortedReportActions(Object.values(actions)); oldestReportActions[reportID] = sortedActions[0].reportActionID; - newestReportActions[reportID] = sortedActions.at(-1).reportActionID; + newestReportActions[reportID] = sortedActions[sortedActions.length - 1].reportActionID; }, }); From 141acedd627ba57cb18f3448d96385da4bad8c73 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:45:48 -0800 Subject: [PATCH 5/8] Add early return --- src/libs/actions/Report.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index ec090bce491b..795d2b90edc6 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -132,6 +132,10 @@ Onyx.connect({ allReportActions[reportID] = actions; const sortedActions = ReportActionsUtils.getSortedReportActions(Object.values(actions)); + if (sortedActions.length === 0) { + return; + } + oldestReportActions[reportID] = sortedActions[0].reportActionID; newestReportActions[reportID] = sortedActions[sortedActions.length - 1].reportActionID; }, From c1c70624f71037a68b1a753722b2ddec8d6deee7 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:50:04 -0800 Subject: [PATCH 6/8] Remove extraneous change --- src/libs/actions/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 795d2b90edc6..54d4de663b11 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -2359,7 +2359,7 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal API.write(WRITE_COMMANDS.LEAVE_ROOM, parameters, {optimisticData, successData, failureData}); - const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}), reportMetadata); + const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}) as Report[], reportMetadata); // We want to filter out the current report, hidden reports and empty chats const filteredReportsByLastRead = sortedReportsByLastRead.filter( From 91711f10265abb72c2f8dd0b1d966470b7de9e46 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 11:59:53 -0800 Subject: [PATCH 7/8] Remove unnecessary dependency from hook --- src/pages/home/report/ReportActionsView.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 7cbf7f41e42f..ca3ee7d2ab6a 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -225,7 +225,7 @@ function ReportActionsView(props) { Report.getNewerActions(reportID); }, 500), - [props.isLoadingNewerReportActions, props.isLoadingInitialReportActions, props.reportActions, reportID, hasNewestReportAction], + [props.isLoadingNewerReportActions, props.isLoadingInitialReportActions, reportID, hasNewestReportAction], ); /** From 42b20a48db5a6c2178497406067f1b9a48c2a512 Mon Sep 17 00:00:00 2001 From: rory Date: Wed, 6 Mar 2024 12:04:06 -0800 Subject: [PATCH 8/8] Clarify comment --- src/libs/actions/Report.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 54d4de663b11..94fe324d306a 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -116,7 +116,7 @@ Onyx.connect({ // map of reportID to all reportActions for that report const allReportActions: OnyxCollection = {}; -// map of reportID to the oldest reportAction for that report +// map of reportID to the ID of the oldest reportAction for that report const oldestReportActions: Record = {}; // map of report to the ID of the newest action for that report