From fa4989410355cecd1a8a7372b8f2f1f1b14c7da9 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Wed, 22 Jun 2022 12:59:46 +0400 Subject: [PATCH 1/7] Refactor markasunread --- src/libs/actions/Report.js | 44 ++++++++++++++++--- .../report/ContextMenu/ContextMenuActions.js | 2 +- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index f529931b5c80..fbd061f48789 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -118,6 +118,21 @@ function getUnreadActionCount(report) { return Math.max(0, unreadActionCount); } +/** + * Returns the number of unread actions for a reportID + * + * @param {Number} reportID + * @param {Number} sequenceNumber + * @returns {Number} + */ +function getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber) { + const reportMaxSequenceNumber = reportMaxSequenceNumbers[reportID]; + + // Determine the number of unread actions by deducting the last read sequence from the total. If, for some reason, + // the last read sequence is higher than the actual last sequence, let's just assume all actions are read + return Math.max(reportMaxSequenceNumber - sequenceNumber - ReportActions.getDeletedCommentsCount(reportID, sequenceNumber), 0); +} + /** * @param {Object} report * @return {String[]} @@ -411,15 +426,10 @@ function setLocalIOUReportData(iouReportObject) { */ function setLocalLastRead(reportID, lastReadSequenceNumber) { lastReadSequenceNumbers[reportID] = lastReadSequenceNumber; - const reportMaxSequenceNumber = reportMaxSequenceNumbers[reportID]; - - // Determine the number of unread actions by deducting the last read sequence from the total. If, for some reason, - // the last read sequence is higher than the actual last sequence, let's just assume all actions are read - const unreadActionCount = Math.max(reportMaxSequenceNumber - lastReadSequenceNumber - ReportActions.getDeletedCommentsCount(reportID, lastReadSequenceNumber), 0); // Update the report optimistically. Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, { - unreadActionCount, + unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, lastReadSequenceNumber), lastVisitedTimestamp: Date.now(), }); } @@ -1111,6 +1121,27 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false }); } +function markCommentAsUnread(reportID, sequenceNumber) { + API.write('MarkCommentAsUnread', + { + markAsUnread: true, + reportID, + sequenceNumber, + }, + { + optimisticData: [{ + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: { + lastVisitedTimestamp: Date.now(), + unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), + }, + }], + successData: [], + failureData: [], + }); +} + /** * Toggles the pinned state of the report. * @@ -1546,4 +1577,5 @@ export { renameReport, getLastReadSequenceNumber, setIsComposerFullSize, + markCommentAsUnread, }; diff --git a/src/pages/home/report/ContextMenu/ContextMenuActions.js b/src/pages/home/report/ContextMenu/ContextMenuActions.js index c06d0fea408b..2419e9d6e7b7 100644 --- a/src/pages/home/report/ContextMenu/ContextMenuActions.js +++ b/src/pages/home/report/ContextMenu/ContextMenuActions.js @@ -131,7 +131,7 @@ export default [ successIcon: Expensicons.Checkmark, shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION, onPress: (closePopover, {reportAction, reportID}) => { - Report.updateLastReadActionID(reportID, reportAction.sequenceNumber, true); + Report.markCommentAsUnread(reportID, reportAction.sequenceNumber); Report.setNewMarkerPosition(reportID, reportAction.sequenceNumber); if (closePopover) { hideContextMenu(true, ReportActionComposeFocusManager.focus); From 52cb17c834e1800b33d7c5d87605a1c46ea6e481 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Thu, 23 Jun 2022 11:23:00 +0400 Subject: [PATCH 2/7] Read newest action --- src/libs/actions/Report.js | 34 +++++++++++++++++++++- src/pages/home/report/ReportActionsView.js | 6 ++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index fbd061f48789..3ded4aadb45d 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1121,10 +1121,41 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false }); } +/** + * Marks the new report actions as read + * + * @param {Number} reportID + */ +function readNewestAction(reportID) { + const sequenceNumber = reportMaxSequenceNumbers[reportID]; + API.write('ReadNewestAction', + { + reportID, + sequenceNumber, + }, + { + optimisticData: [{ + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: { + lastVisitedTimestamp: Date.now(), + unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), + }, + }], + successData: [], + failureData: [], + }); +} + +/** + * Sets the last read comment on a report + * + * @param {Number} reportID + * @param {Number} sequenceNumber + */ function markCommentAsUnread(reportID, sequenceNumber) { API.write('MarkCommentAsUnread', { - markAsUnread: true, reportID, sequenceNumber, }, @@ -1578,4 +1609,5 @@ export { getLastReadSequenceNumber, setIsComposerFullSize, markCommentAsUnread, + readNewestAction, }; diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 1e919028393f..d51b36c3b6c9 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -242,14 +242,14 @@ class ReportActionsView extends React.Component { // When the last action changes, record the max action // This will make the NEW marker line go away if you receive comments in the same chat you're looking at if (shouldRecordMaxAction) { - Report.updateLastReadActionID(this.props.reportID); + Report.readNewestAction(this.props.reportID); } } // Update the new marker position and last read action when we are closing the sidebar or moving from a small to large screen size if (shouldRecordMaxAction && reportBecomeVisible) { this.updateNewMarkerPosition(this.props.report.unreadActionCount); - Report.updateLastReadActionID(this.props.reportID); + Report.readNewestAction(this.props.reportID); } } @@ -301,7 +301,7 @@ class ReportActionsView extends React.Component { */ scrollToBottomAndUpdateLastRead() { ReportScrollManager.scrollToBottom(); - Report.updateLastReadActionID(this.props.reportID); + Report.readNewestAction(this.props.reportID); } /** From f16573a1099fc5fafe926f92d216020cfccfd95d Mon Sep 17 00:00:00 2001 From: madmax330 Date: Wed, 29 Jun 2022 14:57:34 +0400 Subject: [PATCH 3/7] add openreport --- package-lock.json | 2 +- src/libs/actions/Report.js | 18 ++++++++++++++++++ src/pages/home/report/ReportActionsView.js | 6 ++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8f76fac6dd5b..6104d3d3a6b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25270,7 +25270,7 @@ "good-listener": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/good-listener/-/good-listener-1.2.2.tgz", - "integrity": "sha512-goW1b+d9q/HIwbVYZzZ6SsTr4IgE+WA44A0GmPIQstuOrgsFcT7VEJ48nmr9GaRtNu0XTKacFLGnBPAM6Afouw==", + "integrity": "sha1-1TswzfkxPf+33JoNR3CWqm0UXFA=", "requires": { "delegate": "^3.1.2" } diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 3ded4aadb45d..a35874fdd88c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1121,6 +1121,23 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false }); } +/** + * Gets all the report actions and updates the last read message + * + * @param {Number} reportID + */ +function openReport(reportID) { + API.write('OpenReport', + { + reportID, + }, + { + optimisticData: [], + successData: [], + failureData: [], + }); +} + /** * Marks the new report actions as read * @@ -1610,4 +1627,5 @@ export { setIsComposerFullSize, markCommentAsUnread, readNewestAction, + openReport, }; diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index d51b36c3b6c9..0f771d05cbc0 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -125,7 +125,7 @@ class ReportActionsView extends React.Component { return; } - Report.updateLastReadActionID(this.props.reportID); + Report.openReport(this.props.reportID); }); // If the reportID is not found then we have either not loaded this chat or the user is unable to access it. @@ -145,6 +145,7 @@ class ReportActionsView extends React.Component { this.updateNewMarkerAndMarkReadOnce(); } + Report.openReport(); this.fetchData(); } @@ -201,6 +202,7 @@ class ReportActionsView extends React.Component { componentDidUpdate(prevProps) { if (prevProps.network.isOffline && !this.props.network.isOffline) { + Report.openReport(); this.fetchData(); } @@ -353,7 +355,7 @@ class ReportActionsView extends React.Component { // Only mark as read if the report is open if (!this.props.isDrawerOpen) { - Report.updateLastReadActionID(this.props.reportID); + Report.readNewestAction(this.props.reportID); } } From dda655e70ddb126c3d3f903a9ef05c992a3b6912 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Wed, 29 Jun 2022 17:37:19 +0400 Subject: [PATCH 4/7] polish + style --- src/libs/actions/Report.js | 17 +++++++++++++---- src/pages/home/report/ReportActionsView.js | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index d3f9965ea673..5a926b44e41c 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -120,9 +120,9 @@ function getUnreadActionCount(report) { /** * Returns the number of unread actions for a reportID - * - * @param {Number} reportID - * @param {Number} sequenceNumber + * + * @param {Number} reportID + * @param {Number} sequenceNumber * @returns {Number} */ function getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber) { @@ -1125,12 +1125,21 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false * @param {Number} reportID */ function openReport(reportID) { + const sequenceNumber = reportMaxSequenceNumbers[reportID]; API.write('OpenReport', { reportID, + sequenceNumber, }, { - optimisticData: [], + optimisticData: [{ + onyxMethod: 'merge', + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, + value: { + lastVisitedTimestamp: Date.now(), + unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), + }, + }], successData: [], failureData: [], }); diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 0f771d05cbc0..6b74ab01cd4a 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -145,7 +145,7 @@ class ReportActionsView extends React.Component { this.updateNewMarkerAndMarkReadOnce(); } - Report.openReport(); + Report.openReport(this.props.reportID); this.fetchData(); } @@ -202,7 +202,7 @@ class ReportActionsView extends React.Component { componentDidUpdate(prevProps) { if (prevProps.network.isOffline && !this.props.network.isOffline) { - Report.openReport(); + Report.openReport(this.props.reportID); this.fetchData(); } From df64eae9afa7b43c044f3e97a0d2c96145bac888 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Wed, 29 Jun 2022 17:42:32 +0400 Subject: [PATCH 5/7] revert package lock --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 32a3f6516362..7173d498089c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25087,7 +25087,7 @@ "good-listener": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/good-listener/-/good-listener-1.2.2.tgz", - "integrity": "sha1-1TswzfkxPf+33JoNR3CWqm0UXFA=", + "integrity": "sha512-goW1b+d9q/HIwbVYZzZ6SsTr4IgE+WA44A0GmPIQstuOrgsFcT7VEJ48nmr9GaRtNu0XTKacFLGnBPAM6Afouw==", "requires": { "delegate": "^3.1.2" } From d4d41d33b5dd41c9d2d92f6aca3bd85145f66b42 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Wed, 6 Jul 2022 22:50:05 +0200 Subject: [PATCH 6/7] Address comments --- src/libs/actions/Report.js | 13 +++++-------- src/pages/home/report/ReportActionsView.js | 3 +-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index e9a1fa6ceeb3..d9735a94dc80 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -1095,12 +1095,13 @@ function updateLastReadActionID(reportID, sequenceNumber, manuallyMarked = false } /** - * Gets all the report actions and updates the last read message + * Gets the latest page of report actions and updates the last read message * * @param {Number} reportID */ function openReport(reportID) { const sequenceNumber = reportMaxSequenceNumbers[reportID]; + lastReadSequenceNumbers[reportID] = sequenceNumber; API.write('OpenReport', { reportID, @@ -1115,8 +1116,6 @@ function openReport(reportID) { unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), }, }], - successData: [], - failureData: [], }); } @@ -1127,6 +1126,7 @@ function openReport(reportID) { */ function readNewestAction(reportID) { const sequenceNumber = reportMaxSequenceNumbers[reportID]; + lastReadSequenceNumbers[reportID] = sequenceNumber; API.write('ReadNewestAction', { reportID, @@ -1141,8 +1141,6 @@ function readNewestAction(reportID) { unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), }, }], - successData: [], - failureData: [], }); } @@ -1153,6 +1151,7 @@ function readNewestAction(reportID) { * @param {Number} sequenceNumber */ function markCommentAsUnread(reportID, sequenceNumber) { + lastReadSequenceNumbers[reportID] = sequenceNumber; API.write('MarkCommentAsUnread', { reportID, @@ -1163,12 +1162,10 @@ function markCommentAsUnread(reportID, sequenceNumber) { onyxMethod: 'merge', key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - lastVisitedTimestamp: Date.now(), + lastVisitedTimestamp: Math.round(Date.now() / 1000), unreadActionCount: getUnreadActionCountFromSequenceNumber(reportID, sequenceNumber), }, }], - successData: [], - failureData: [], }); } diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index 6b74ab01cd4a..d0747403d64b 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -145,7 +145,6 @@ class ReportActionsView extends React.Component { this.updateNewMarkerAndMarkReadOnce(); } - Report.openReport(this.props.reportID); this.fetchData(); } @@ -251,7 +250,7 @@ class ReportActionsView extends React.Component { // Update the new marker position and last read action when we are closing the sidebar or moving from a small to large screen size if (shouldRecordMaxAction && reportBecomeVisible) { this.updateNewMarkerPosition(this.props.report.unreadActionCount); - Report.readNewestAction(this.props.reportID); + Report.openReport(this.props.reportID); } } From 112c9862525ca8b2ccf202ff071e0f76edffa332 Mon Sep 17 00:00:00 2001 From: madmax330 Date: Thu, 7 Jul 2022 22:15:48 +0200 Subject: [PATCH 7/7] Address comments again --- src/pages/home/report/ReportActionsView.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index d0747403d64b..4f517bb3b902 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -302,7 +302,6 @@ class ReportActionsView extends React.Component { */ scrollToBottomAndUpdateLastRead() { ReportScrollManager.scrollToBottom(); - Report.readNewestAction(this.props.reportID); } /** @@ -354,7 +353,7 @@ class ReportActionsView extends React.Component { // Only mark as read if the report is open if (!this.props.isDrawerOpen) { - Report.readNewestAction(this.props.reportID); + Report.openReport(this.props.reportID); } }