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

Fix LHN undefined reports #19219

Merged
merged 3 commits into from
May 19, 2023
Merged
Changes from all commits
Commits
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
34 changes: 11 additions & 23 deletions src/libs/SidebarUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,15 @@ import * as LocalePhoneNumber from './LocalePhoneNumber';
// Note: It is very important that the keys subscribed to here are the same
// keys that are connected to SidebarLinks withOnyx(). If there was a key missing from SidebarLinks and it's data was updated
// for that key, then there would be no re-render and the options wouldn't reflect the new data because SidebarUtils.getOrderedReportIDs() wouldn't be triggered.
// There are a couple of keys here which are OK to have stale data. iouReports for example, doesn't need to exist in withOnyx() because
// when IOUs change, it also triggers a change on the reports collection. Having redundant subscriptions causes more re-renders which should be avoided.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is no longer relevant since we now render IOU reports in the LHN

// There are a couple of keys here which are OK to have stale data. Having redundant subscriptions causes more re-renders which should be avoided.
// Session also can remain stale because the only way for the current user to change is to sign out and sign in, which would clear out all the Onyx
// data anyway and cause SidebarLinks to rerender.

const chatReports = {};
const moneyRequestReports = {};
let allReports;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
callback: (report, key) => {
if (!report) {
delete moneyRequestReports[key];
delete chatReports[key];
} else if (ReportUtils.isMoneyRequestReport(report)) {
moneyRequestReports[key] = report;
} else {
chatReports[key] = report;
}
},
waitForCollectionCallback: true,
callback: (val) => (allReports = val),
});

let personalDetails;
Expand Down Expand Up @@ -108,9 +98,7 @@ function getOrderedReportIDs(reportIDFromRoute) {
const isInDefaultMode = !isInGSDMode;

// Filter out all the reports that shouldn't be displayed
const reportsToDisplay = _.filter({...chatReports, ...moneyRequestReports}, (report) =>
ReportUtils.shouldReportBeInOptionList(report, reportIDFromRoute, isInGSDMode, currentUserLogin, moneyRequestReports, betas, policies),
);
const reportsToDisplay = _.filter(allReports, (report) => ReportUtils.shouldReportBeInOptionList(report, reportIDFromRoute, isInGSDMode, currentUserLogin, allReports, betas, policies));

// There are a few properties that need to be calculated for the report which are used when sorting reports.
_.each(reportsToDisplay, (report) => {
Expand All @@ -121,7 +109,7 @@ function getOrderedReportIDs(reportIDFromRoute) {
report.displayName = ReportUtils.getReportName(report);

// eslint-disable-next-line no-param-reassign
report.iouReportAmount = ReportUtils.getMoneyRequestTotal(report, moneyRequestReports);
report.iouReportAmount = ReportUtils.getMoneyRequestTotal(report, allReports);
});

// The LHN is split into five distinct groups, and each group is sorted a little differently. The groups will ALWAYS be in this order:
Expand All @@ -146,7 +134,7 @@ function getOrderedReportIDs(reportIDFromRoute) {
return;
}

if (report.hasOutstandingIOU && !ReportUtils.isIOUOwnedByCurrentUser(report, moneyRequestReports)) {
if (report.hasOutstandingIOU && !ReportUtils.isIOUOwnedByCurrentUser(report, allReports)) {
outstandingIOUReports.push(report);
return;
}
Expand Down Expand Up @@ -196,7 +184,7 @@ function getOrderedReportIDs(reportIDFromRoute) {
*/
function getOptionData(reportID) {
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
const report = chatReports[reportKey] || moneyRequestReports[reportKey];
const report = allReports[reportKey];

// When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for
// this method to be called after the Onyx data has been cleared out. In that case, it's fine to do
Expand Down Expand Up @@ -260,7 +248,7 @@ function getOptionData(reportID) {
result.tooltipText = ReportUtils.getReportParticipantsTitle(report.participants || []);
result.hasOutstandingIOU = report.hasOutstandingIOU;
result.parentReportID = report.parentReportID || null;
const parentReport = result.parentReportID ? chatReports[`${ONYXKEYS.COLLECTION.REPORT}${result.parentReportID}`] : null;
const parentReport = result.parentReportID ? allReports[`${ONYXKEYS.COLLECTION.REPORT}${result.parentReportID}`] : null;
const hasMultipleParticipants = participantPersonalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat;
const subtitle = ReportUtils.getChatRoomSubtitle(report);

Expand Down Expand Up @@ -329,8 +317,8 @@ function getOptionData(reportID) {
result.alternateText = lastMessageText || formattedLogin;
}

result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result, moneyRequestReports);
result.iouReportAmount = ReportUtils.getMoneyRequestTotal(result, moneyRequestReports);
result.isIOUReportOwner = ReportUtils.isIOUOwnedByCurrentUser(result, allReports);
result.iouReportAmount = ReportUtils.getMoneyRequestTotal(result, allReports);

if (!hasMultipleParticipants) {
result.login = personalDetail.login;
Expand Down