Skip to content

Refactor Get_ReportHistory #9613

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

Merged
merged 43 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 34 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
1 change: 1 addition & 0 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
2 changes: 1 addition & 1 deletion src/libs/actions/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function getAppData(shouldSyncPolicyList = true) {
// We should update the syncing indicator when personal details and reports are both done fetching.
return Promise.all([
PersonalDetails.fetchPersonalDetails(),
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
102 changes: 42 additions & 60 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,11 +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;
function fetchActions(reportID) {
const reportActionsOffset = -1;

if (!_.isNumber(reportActionsOffset)) {
Log.alert('[Report] Offset provided is not a number', {
Expand All @@ -720,28 +718,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 +760,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 @@ -1132,6 +1075,45 @@ function openReport(reportID) {
});
}

/**
* Gets the actions from the oldest unread action.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we clarify: Does this get actions from the oldest unread action and older? or and newer?

Not sure if my proposed addition is 100% clear even, but just from reading this I think ReadOldestAction is singular so we're only getting 1 action. If that's true, great - let's update this comment so it is clear we're only getting 1 action. Otherwise can we think about making the command ReadOldestActions and clarifying if we're getting oldest unread action & newer or & older?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this reads in to the older actions/messages, basically when you scroll up, so older and older

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about the plural part though. @marcaaron do you have thoughts on that?
We would need to change Web as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We (the user) are reading the "oldest action". This is synonymous with scrolling up to the oldest action stored locally - a.k.a. triggering a call to paginate the actions and load the next set. It should not be plural and we are not "only getting 1 action" we are getting the next set of historical actions.

*
* @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: {isLoading: true},
}],
successData: [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`,
value: {
isLoading: false,
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
},

],
failureData: [{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS}${reportID}`,
value: {isLoading: false},
}],
});
}

/**
* Gets the IOUReport and the associated report actions.
*
Expand Down Expand Up @@ -1655,12 +1637,12 @@ export {
navigateToConciergeChat,
handleInaccessibleReport,
setReportWithDraft,
fetchActionsWithLoadingState,
createPolicyRoom,
renameReport,
setIsComposerFullSize,
markCommentAsUnread,
readNewestAction,
readOldestAction,
openReport,
openPaymentDetailsPage,
};
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: ONYXKEYS.COLLECTION.IS_LOADING_REPORT_ACTIONS,
initWithStoredValues: false,
},
}),
Expand Down