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

Update Sidebar to only listen to specific properties on Onyx connections to speed up performance #12083

Merged
merged 16 commits into from
Nov 8, 2022
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
63 changes: 63 additions & 0 deletions src/pages/home/sidebar/SidebarLinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,65 @@ class SidebarLinks extends React.Component {
SidebarLinks.propTypes = propTypes;
SidebarLinks.defaultProps = defaultProps;

/**
* This function (and the few below it), narrow down the data from Onyx to just the properties that we want to trigger a re-render of the component. This helps minimize re-rendering
* and makes the entire component more performant because it's not re-rendering when a bunch of properties change which aren't ever used in the UI.
* @param {Object} [report]
* @returns {Object|undefined}
*/
const reportSelector = report => report && ({
reportID: report.reportID,
participants: report.participants,
hasDraft: report.hasDraft,
isPinned: report.isPinned,
errorFields: {
addWorkspaceRoom: report.errorFields && report.errorFields.addWorkspaceRoom,
},
maxSequenceNumber: report.maxSequenceNumber,
lastReadSequenceNumber: report.lastReadSequenceNumber,
lastMessageText: report.lastMessageText,
lastMessageTimestamp: report.lastMessageTimestamp,
iouReportID: report.iouReportID,
hasOutstandingIOU: report.hasOutstandingIOU,
statusNum: report.statusNum,
stateNum: report.stateNum,
chatType: report.chatType,
policyID: report.policyID,
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey 👋, this PR caused this regression where room names are not updated in the sidebar when renamed while offline. The problem is that reportName was not included in the reportSelector.

});

/**
* @param {Object} [personalDetails]
* @returns {Object|undefined}
*/
const personalDetailsSelector = personalDetails => _.reduce(personalDetails, (finalPersonalDetails, personalData, login) => {
// It's OK to do param-reassignment in _.reduce() because we absolutely know the starting state of finalPersonalDetails
// eslint-disable-next-line no-param-reassign
finalPersonalDetails[login] = {
login: personalData.login,
displayName: personalData.displayName,
firstName: personalData.firstName,
avatar: personalData.avatar,
};
return finalPersonalDetails;
}, {});

/**
* @param {Object} [reportActions]
* @returns {Object|undefined}
*/
const reportActionsSelector = reportActions => reportActions && _.map(reportActions, reportAction => ({
errors: reportAction.errors,
}));

/**
* @param {Object} [policy]
* @returns {Object|undefined}
*/
const policySelector = policy => policy && ({
type: policy.type,
name: policy.name,
Copy link
Member

Choose a reason for hiding this comment

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

We missed listening for avatar property on policies which caused #18226

});

export default compose(
withLocalize,
withCurrentUserPersonalDetails,
Expand All @@ -176,9 +235,11 @@ export default compose(
// with 10,000 withOnyx() connections, it would have unknown performance implications.
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
selector: reportSelector,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
selector: personalDetailsSelector,
},
priorityMode: {
key: ONYXKEYS.NVP_PRIORITY_MODE,
Expand All @@ -188,9 +249,11 @@ export default compose(
},
reportActions: {
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
selector: reportActionsSelector,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: policySelector,
},
preferredLocale: {
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
Expand Down