Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Get_ReportHistory #9613

Merged
merged 43 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f2f4740
start
sketchydroide Jun 28, 2022
963fcc9
remove unneeded import
sketchydroide Jun 29, 2022
9777a88
updating OpenReport
sketchydroide Jun 30, 2022
9bcfda0
Merge branch 'maxence-rrulr' into afonseca_rrgh
sketchydroide Jun 30, 2022
ff8bb0f
duplicated OpenReport
sketchydroide Jun 30, 2022
bdc16db
updating
sketchydroide Jun 30, 2022
926d388
adding is loading
sketchydroide Jun 30, 2022
1f6fbe4
improving on is loading
sketchydroide Jun 30, 2022
0a0cf9a
I don't think there is optimistic data in this case, it just loads
sketchydroide Jun 30, 2022
3fffea6
I was editing the wrong method
sketchydroide Jun 30, 2022
01269a3
remove offset, I'm not sure it is needed
sketchydroide Jul 1, 2022
72e896e
Merge branch 'main' into afonseca_rrgh
sketchydroide Jul 12, 2022
55835a6
no message
sketchydroide Jul 12, 2022
ef81dbb
export method
sketchydroide Jul 12, 2022
7ba25f1
replace fetchData
sketchydroide Jul 12, 2022
c6b0ccc
loadMoreChats
sketchydroide Jul 12, 2022
4e01c35
linter requests
sketchydroide Jul 12, 2022
4a3bcd3
on the timer part as well
sketchydroide Jul 12, 2022
38e73a3
removing the old timer section as it should no longer be needed
sketchydroide Jul 13, 2022
ff7cd6b
also removing the timers import
sketchydroide Jul 13, 2022
0d66a1f
renaming offset to oldestActionSequenceNumber
sketchydroide Jul 13, 2022
dd5d4b6
was the same but forgot about it
sketchydroide Jul 13, 2022
8b17188
reverting
sketchydroide Jul 13, 2022
3f06e7a
collections is loading report actions
sketchydroide Jul 14, 2022
2740fde
fixing linter;
sketchydroide Jul 15, 2022
dc1cf4c
missed one
sketchydroide Jul 15, 2022
65bf517
this no longes seems to be used
sketchydroide Jul 19, 2022
24d7b31
nothing uses offset on fetchActions anymore
sketchydroide Jul 19, 2022
61354ef
Merge branch 'main' into afonseca_rrgh
sketchydroide Jul 19, 2022
5287bc7
this makes more sense
sketchydroide Jul 19, 2022
9f3fab1
removing errors
sketchydroide Jul 20, 2022
d829272
this was done by mistake, reverting.
sketchydroide Jul 20, 2022
0eece64
no need for value
sketchydroide Jul 20, 2022
92ddb4a
changing the ONYX key for the loading
sketchydroide Jul 20, 2022
17769b9
offset is always a number
sketchydroide Jul 20, 2022
a3592e2
correcting the onyx key
sketchydroide Jul 20, 2022
048751d
style
sketchydroide Jul 20, 2022
00e0dba
Merge branch 'main' into afonseca_rrgh
sketchydroide Jul 20, 2022
700792a
commit new package
sketchydroide Jul 20, 2022
92c619e
remove object for is loading,
sketchydroide Jul 20, 2022
b9e84f6
reseting the file
sketchydroide Jul 21, 2022
ec6c141
version is wrong?
sketchydroide Jul 21, 2022
a016a61
Updated the description a bit
sketchydroide Jul 21, 2022
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
4 changes: 1 addition & 3 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
103 changes: 35 additions & 68 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
marcaaron marked this conversation as resolved.
Show resolved Hide resolved

return DeprecatedAPI.Report_GetHistory({
reportID,
Expand All @@ -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({
Expand Down Expand Up @@ -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));
});
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -1683,12 +1650,12 @@ export {
navigateToConciergeChat,
handleInaccessibleReport,
setReportWithDraft,
fetchActionsWithLoadingState,
createPolicyRoom,
renameReport,
setIsComposerFullSize,
markCommentAsUnread,
readNewestAction,
readOldestAction,
openReport,
openPaymentDetailsPage,
createOptimisticReport,
Expand Down
6 changes: 3 additions & 3 deletions src/pages/home/report/ReportActionsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
},
}),
Expand Down