diff --git a/src/ONYXKEYS.js b/src/ONYXKEYS.js index d5c05583b581..40675a1a6a89 100755 --- a/src/ONYXKEYS.js +++ b/src/ONYXKEYS.js @@ -103,6 +103,7 @@ export default { POLICY: 'policy_', REPORTS_WITH_DRAFT: 'reportWithDraft_', REPORT_IS_COMPOSER_FULL_SIZE: 'reportIsComposerFullSize_', + IS_LOADING_REPORT_ACTIONS: 'isLoadingReportActions_', }, // Indicates which locale should be used @@ -162,9 +163,6 @@ export default { // The number of minutes a user has to wait for a call. INBOX_CALL_USER_WAIT_TIME: 'inboxCallUserWaitTime', - // Are report actions loading? - IS_LOADING_REPORT_ACTIONS: 'isLoadingReportActions', - // Is report data loading? IS_LOADING_REPORT_DATA: 'isLoadingReportData', diff --git a/src/libs/actions/App.js b/src/libs/actions/App.js index 051507090007..46b003d5418b 100644 --- a/src/libs/actions/App.js +++ b/src/libs/actions/App.js @@ -104,7 +104,7 @@ function getAppData() { // We should update the syncing indicator when personal details and reports are both done fetching. return Promise.all([ - Report.fetchAllReports(true, true), + Report.fetchAllReports(true), ]); } diff --git a/src/libs/actions/Policy.js b/src/libs/actions/Policy.js index c4117360bd7e..fc9b6088228d 100644 --- a/src/libs/actions/Policy.js +++ b/src/libs/actions/Policy.js @@ -190,7 +190,7 @@ function deletePolicy(policyID) { // Removing the workspace data from Onyx as well return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, null); }) - .then(() => Report.fetchAllReports(false, true)) + .then(() => Report.fetchAllReports(false)) .then(() => { Navigation.goBack(); return Promise.resolve(); diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 1311f9f23cb0..e101ef0c2b74 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -21,7 +21,6 @@ import CONST from '../../CONST'; import Log from '../Log'; import * as LoginUtils from '../LoginUtils'; import * as ReportUtils from '../ReportUtils'; -import Timers from '../Timers'; import * as ReportActions from './ReportActions'; import Growl from '../Growl'; import * as Localize from '../Localize'; @@ -691,19 +690,10 @@ function fetchOrCreateChatReport(participants, shouldNavigate = true) { * Get the actions of a report * * @param {Number} reportID - * @param {Number} [offset] * @returns {Promise} */ -function fetchActions(reportID, offset) { - const reportActionsOffset = !_.isUndefined(offset) ? offset : -1; - - if (!_.isNumber(reportActionsOffset)) { - Log.alert('[Report] Offset provided is not a number', { - offset, - reportActionsOffset, - }); - return; - } +function fetchActions(reportID) { + const reportActionsOffset = -1; return DeprecatedAPI.Report_GetHistory({ reportID, @@ -720,28 +710,14 @@ function fetchActions(reportID, offset) { }); } -/** - * Get the actions of a report - * - * @param {Number} reportID - * @param {Number} [offset] - */ -function fetchActionsWithLoadingState(reportID, offset) { - Onyx.set(ONYXKEYS.IS_LOADING_REPORT_ACTIONS, true); - fetchActions(reportID, offset) - .finally(() => Onyx.set(ONYXKEYS.IS_LOADING_REPORT_ACTIONS, false)); -} - /** * Get all of our reports * * @param {Boolean} shouldRecordHomePageTiming whether or not performance timing should be measured - * @param {Boolean} shouldDelayActionsFetch when the app loads we want to delay the fetching of additional actions * @returns {Promise} */ function fetchAllReports( shouldRecordHomePageTiming = false, - shouldDelayActionsFetch = false, ) { Onyx.set(ONYXKEYS.IS_LOADING_REPORT_DATA, true); return DeprecatedAPI.Get({ @@ -776,47 +752,6 @@ function fetchAllReports( if (shouldRecordHomePageTiming) { Timing.end(CONST.TIMING.HOMEPAGE_REPORTS_LOADED); } - - // Delay fetching report history as it significantly increases sign in to interactive time. - // Register the timer so we can clean it up if the user quickly logs out after logging in. If we don't - // cancel the timer we'll make unnecessary API requests from the sign in page. - Timers.register(setTimeout(() => { - // Filter reports to see which ones have actions we need to fetch so we can preload Onyx with new - // content and improve chat switching experience by only downloading content we don't have yet. - // This improves performance significantly when reconnecting by limiting API requests and unnecessary - // data processing by Onyx. - const reportIDsWithMissingActions = _.chain(returnedReports) - .map(report => report.reportID) - .filter(reportID => ReportActions.isReportMissingActions(reportID, lodashGet(allReports, [reportID, 'maxSequenceNumber']))) - .value(); - - // Once we have the reports that are missing actions we will find the intersection between the most - // recently accessed reports and reports missing actions. Then we'll fetch the history for a small - // set to avoid making too many network requests at once. - const reportIDsToFetchActions = _.chain(ReportUtils.sortReportsByLastVisited(allReports)) - .map(report => report.reportID) - .reverse() - .intersection(reportIDsWithMissingActions) - .slice(0, 10) - .value(); - - if (_.isEmpty(reportIDsToFetchActions)) { - Log.info('[Report] Local reportActions up to date. Not fetching additional actions.'); - return; - } - - Log.info('[Report] Fetching reportActions for reportIDs: ', false, { - reportIDs: reportIDsToFetchActions, - }); - _.each(reportIDsToFetchActions, (reportID) => { - const offset = ReportActions.dangerouslyGetReportActionsMaxSequenceNumber(reportID, false); - fetchActions(reportID, offset); - }); - - // We are waiting a set amount of time to allow the UI to finish loading before bogging it down with - // more requests and operations. Startup delay is longer since there is a lot more work done to build - // up the UI when the app first initializes. - }, shouldDelayActionsFetch ? CONST.FETCH_ACTIONS_DELAY.STARTUP : CONST.FETCH_ACTIONS_DELAY.RECONNECT)); }); } @@ -1160,6 +1095,38 @@ function openReport(reportID) { }); } +/** + * 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. + * + * @param {Number} reportID + * @param {Number} oldestActionSequenceNumber + */ +function readOldestAction(reportID, oldestActionSequenceNumber) { + API.read('ReadOldestAction', + { + reportID, + reportActionsOffset: oldestActionSequenceNumber, + }, + { + optimisticData: [{ + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`, + value: true, + }], + successData: [{ + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`, + value: false, + }], + failureData: [{ + onyxMethod: CONST.ONYX.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`, + value: false, + }], + }); +} + /** * Gets the IOUReport and the associated report actions. * @@ -1683,12 +1650,12 @@ export { navigateToConciergeChat, handleInaccessibleReport, setReportWithDraft, - fetchActionsWithLoadingState, createPolicyRoom, renameReport, setIsComposerFullSize, markCommentAsUnread, readNewestAction, + readOldestAction, openReport, openPaymentDetailsPage, createOptimisticReport, diff --git a/src/pages/home/report/ReportActionsView.js b/src/pages/home/report/ReportActionsView.js index ef288001ce0e..c09c2e0ec431 100755 --- a/src/pages/home/report/ReportActionsView.js +++ b/src/pages/home/report/ReportActionsView.js @@ -299,8 +299,8 @@ class ReportActionsView extends React.Component { // Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments, unless we're near the beginning, in which // case just get everything starting from 0. - const offset = Math.max(minSequenceNumber - CONST.REPORT.ACTIONS.LIMIT, 0); - Report.fetchActionsWithLoadingState(this.props.reportID, offset); + const oldestActionSequenceNumber = Math.max(minSequenceNumber - CONST.REPORT.ACTIONS.LIMIT, 0); + Report.readOldestAction(this.props.reportID, oldestActionSequenceNumber); } scrollToBottomAndMarkReportAsRead() { @@ -458,7 +458,7 @@ export default compose( key: ONYXKEYS.IS_LOADING_REPORT_DATA, }, isLoadingReportActions: { - key: ONYXKEYS.IS_LOADING_REPORT_ACTIONS, + key: ({reportID}) => `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`, initWithStoredValues: false, }, }),