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

Use initialValue in withOnyx and other optimizations for ReportScreen #26772

Merged

Conversation

ospfranco
Copy link
Contributor

Details

This PR makes further improvements to the ReportScreen. It is based on the changes of the previous PR.

A summary of the changes:

  • A lot of re-renders were caused by react-native-onyx delaying the rendering because it had to fetch data from the DB. This was solved by adding a initialValue to the withOnyx HOC. This allows components to mount ASAP. This had to be manually added to the components that were causing a re-render.
  • Large components with a lot of onyx connections would also block rendering as data was being fetched/hydrated to the component. This was a particular case of ReportScreen. A solution was implemented where we tell onyx to delay updates until the component has been mounted. This allows React to perform a render instead of blocking completely.
  • ReportScreenWrapper was using an internal hook to update route params. This was moved into its own component to avoid a large tree-rerender
  • HeaderView.js had unused onyx connections and was returning a new object every time. Causing a re-render
  • Some other minor optimizations like skipping some hooks on first render.

This PR is also dependant on changes we made to react-native-onyx in this PR those need to be merged and published first.

Fixed Issues

$ #26087

Tests

  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG))
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@ospfranco ospfranco requested a review from a team as a code owner September 5, 2023 12:49
@melvin-bot melvin-bot bot requested review from situchan and removed request for a team September 5, 2023 12:49
@melvin-bot
Copy link

melvin-bot bot commented Sep 5, 2023

@situchan Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@janicduplessis
Copy link
Contributor

I did another round on profiling on this branch and found some more improvements

Prevent SidebarLinks from re-rendering on opening report

currentReportID is passed as a prop down to SidebarLinks and causes a re-render of the whole list when opening a new report. Turns out SidebarLinks only uses currentReportID in the showReportPage function which is invoked when clicking a report and never in the render path so we can use Navigation.getTopmostReportId to get it without having to pass it as prop and causing a re-render. We also have onyx connected to fetch the current report which is never used so we can remove that too. After that SidebarLinks won't re-render on opening a report since it is a pure component and props do not change even if parent SidebarLinksData re-renders.

Before:

image

After:

image

The perf impact is pretty minimal, but it is also a good change to cleanup SidebarLinks. Ideally SidebarLinksData would not re-render at all but getOrderedReportIDs is dependent on the currentReportID.

Diff: janicduplessis@b47663e

Reduce SidebarLinksData render time

I was able to get this render down more significantly by changing the memoization to separate getOrderedReportIDs from currentReportID, but this will cause slight behaviour change since if for some reason the opened report is not in the reports list, it will be added at the start instead of being added where it should be according to the sorting logic. I feel like the case where the opened report is not visible in the report list should be very rare, but I am not familiar enough with the product to know for sure. Another option to preserve 100% behaviour at the cost of performance would be to compute the reports list a 2nd time if the current report is not visible. This will result in 2x worst perf for the edge case of the current report not being visible, but great improvement in the common case. I think someone with more product knowledge could make the call on this one.

image

Diff: janicduplessis@2c14b62

Will update this as I find more things

- Add initial values to different components to allow immediate mounting/rendering
- Delay the initialization of certain values to allow React to flush the queue immediately
- Remove unnecessary Onyx values
@ospfranco
Copy link
Contributor Author

I applied the first performance update suggested by @janicduplessis, tested it and everything is working fine.

I will wait for the green light from someone in expensify to approve the change in the second suggestion.

@mountiny
Copy link
Contributor

@ospfranco can you also sync up with main please?

@fedirjh
Copy link
Contributor

fedirjh commented Sep 25, 2023

I think we have a weird bug, I've been testing some reports , and sometimes, instead of the skeleton view, the "not found" view is displayed. This issue has occurred with older reports when opening reports with old tasks tasks.

This bug is also affects distance requests and money requests.

Edit : Affects threads as well, I guess it affects all nested reports.

Steps to reproduce

  1. Fresh login
  2. Open an old report that have some old tasks / Open an old IOU/Expense report
  3. Open a task report / Open an old money request
CleanShot.2023-09-25.at.18.57.04.mp4


const isOptimisticDelete = lodashGet(report, 'statusNum') === CONST.REPORT.STATUS.CLOSED;

const shouldHideReport = !ReportUtils.canAccessReport(report, policies, betas);

const isLoading = !reportID || !isSidebarLoaded || _.isEmpty(personalDetails) || firstRenderRef.current;
Copy link
Contributor

@fedirjh fedirjh Sep 25, 2023

Choose a reason for hiding this comment

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

I am suspecting this change has caused this bug #26772 (comment) , isLoading is used inside shouldShowNotFoundPage which display the not found view. I guess before this change isLoading was initially set as true but it has changed to be set initially as false and that will affect the value of shouldShowNotFoundPage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem with that first render is that it would delay the rendering one extra cycle at which point onyx would be already trying to shove data into the component and would delay the rendering again. However I don't think there is a way around it at this point. I will just revert it and we can revisit this again later.

Copy link
Contributor

Choose a reason for hiding this comment

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

However I don't think there is a way around it at this point.

@ospfranco Instead of reverting and losing the optimisation effect , why don't we just update shouldShowNotFoundPage to account for the first render ?

@ospfranco
Copy link
Contributor Author

@fedirjh I've reverted the change to the isLoading flag. I've tested it with my account but I'm not sure if I can reproduce it with any of the chats I have. Can you take a look and confirm it is working?

@fedirjh
Copy link
Contributor

fedirjh commented Sep 26, 2023

Can you take a look and confirm it is working?

@ospfranco Yes that fixes the bug , but it brings back the skeleton bug , when you navigate between opened reports, the skeleton is briefly displayed.

@fedirjh
Copy link
Contributor

fedirjh commented Sep 26, 2023

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
CleanShot.2023-09-26.at.13.08.44.mp4
Mobile Web - Chrome
CleanShot.2023-09-26.at.13.23.15.mp4
Mobile Web - Safari
CleanShot.2023-09-26.at.13.18.57.mp4
Desktop
CleanShot.2023-09-26.at.14.41.41.mp4
iOS
CleanShot.2023-09-26.at.13.59.24.mp4
Android
CleanShot.2023-09-26.at.14.39.11.mp4

Copy link
Contributor

@fedirjh fedirjh left a comment

Choose a reason for hiding this comment

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

All yours @mountiny.

* Get the most recently accessed report for the user
*
* @param {Object} reports
* @param {Boolean} [ignoreDefaultRooms]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does not seem to have a default value set, also there are required params coming after

Suggested change
* @param {Boolean} [ignoreDefaultRooms]
* @param {Boolean} ignoreDefaultRooms

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand. This is just an internal function and doesn't require a default value, it should always be called with a boolean value:

CleanShot 2023-09-27 at 08 15 37

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah but the [] in js docs means optional parameter, but this is not optional parameter but the looks of things

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, this wasn't my code, just moved it from the wrapper. But it is fixed.

src/libs/Navigation/AppNavigator/ReportScreenIDSetter.js Outdated Show resolved Hide resolved
@@ -68,5 +68,6 @@ SilentCommentUpdater.displayName = 'SilentCommentUpdater';
export default withOnyx({
comment: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`,
initialValue: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you forget to apply this suggestion?

Comment on lines 176 to 181
if (didManuallyMarkReportAsUnread) {
// Clearing the current unread marker so that it can be recalculated
setCurrentUnreadMarker(null);
setMessageManuallyMarkedUnread(new Date().getTime());
} else {
setMessageManuallyMarkedUnread(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should have an early return here as that is preferred

};

// This wrapper is reponsible for opening the last accessed report if there is no reportID specified in the route params
function ReportScreenIDSetter(props) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a new component and we prefer destructuring the props, can you please do that here?

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Thanks! looking great!

@ospfranco
Copy link
Contributor Author

Ah, unit test is failing, let me take a look

@mountiny mountiny merged commit 86c0658 into Expensify:main Sep 28, 2023
10 checks passed
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@ospfranco ospfranco deleted the osp/further-report-screen-optimizations branch September 28, 2023 05:34
@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 1.3.75-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@@ -242,6 +242,7 @@ const ONYXKEYS = {
POLICY_RECENTLY_USED_TAGS: 'policyRecentlyUsedTags_',
WORKSPACE_INVITE_MEMBERS_DRAFT: 'workspaceInviteMembersDraft_',
REPORT: 'report_',
REPORT_METADATA: 'reportMetadata_',
Copy link
Contributor

Choose a reason for hiding this comment

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

Request: can we please add a comment to explain what belongs in report_ and what belongs in reportMetadata_. Without all the context the distinction isn't very clear.

Copy link
Contributor Author

@ospfranco ospfranco Oct 2, 2023

Choose a reason for hiding this comment

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

For now we've separated the loading states isLoadingReportActions and isLoadingMoreReportActions into reportMetadata_, as they were causing many re-renders on smaller components every time the report actions were being fetched. It makes sense to add it a comment for future reference.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, sounds good. Maybe the distinction is that REPORT_METADATA is for client-generated data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a performance optimization, not a hard cut between functionality/logic.

@OSBotify
Copy link
Contributor

OSBotify commented Oct 2, 2023

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.75-12 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Oct 2, 2023

🚀 Deployed to staging by https://github.com/mountiny in version: 1.3.76-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

OSBotify commented Oct 3, 2023

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.76-6 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 failure ❌
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.