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] preserve sidebar active report highlight between tabs #8227

Closed
wants to merge 4 commits into from
Closed

[Fix] preserve sidebar active report highlight between tabs #8227

wants to merge 4 commits into from

Conversation

mdneyazahmad
Copy link
Contributor

Details

When opened multiple report in different tabs, preserve the active report highlighting on sidebar (web only).

Fixed Issues

$ #7792

Tests

  1. Open a chat in tab 1
  2. Open a different chat in tab 2
  3. Verify that when selecting different chat does not change (highlight) it on another tab.
  • Verify that no errors appear in the JS console

PR Review Checklist

Contributor (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 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 included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • 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 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 was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct english and approved by marketing by tagging the marketing team 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
  • I verified any variables that can be defined as constants (ie. in CONST.js) 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 */
    • Any functional components have the displayName property
    • 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
    • Any internal methods are bound to “this” properly so there are no scoping issues
    • Any internal methods bound to “this” are necessary to be bound
    • 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
  • 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)

PR Reviewer Checklist

  • 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 verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • 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 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 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 was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct english and approved by marketing by tagging the marketing team 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 are not impacted by changes in this PR (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
  • I verified any variables that can be defined as constants (ie. in CONST.js) 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 */
    • Any functional components have the displayName property
    • 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
    • Any internal methods are bound to “this” properly so there are no scoping issues
    • Any internal methods bound to “this” are necessary to be bound
    • 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 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)

QA Steps

  1. Open a chat in tab 1
  2. Open a different chat in tab 2
  3. Verify that when selecting different chat does not change (highlight) it on another tab.
  • Verify that no errors appear in the JS console

Screenshots

Web

web.mp4

Mobile Web

mweb.mp4

Desktop

desktop.mp4

iOS

ios.mp4

Android

android.mp4

@mdneyazahmad mdneyazahmad requested a review from a team as a code owner March 18, 2022 17:55
@AndrewGable AndrewGable requested review from a team and removed request for a team March 21, 2022 22:25
@melvin-bot melvin-bot bot requested review from Beamanator and rushatgabhane and removed request for a team March 21, 2022 22:25
@rushatgabhane
Copy link
Member

rushatgabhane commented Mar 22, 2022

PR Reviewer Checklist

  • 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 verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • 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 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 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 was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team 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 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 */
    • Any functional components have the displayName property
    • 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 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 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.

@rushatgabhane
Copy link
Member

Working on finding any performance related issues

// This is usually needed after login/create account and re-launches
return (
<BaseDrawerNavigator
drawerContent={() => (
Copy link
Member

Choose a reason for hiding this comment

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

Avoid using arrow function callbacks in components that are expensive to re-render. React will re-render this component since each time the parent renders it creates a new instance of that function. Alternative: Bind the method in the constructor instead - https://github.com/Expensify/App/blob/main/PERFORMANCE.md#react-performance-tips

@mdneyazahmad Let's remove the arrow function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can not remove it because we have to pass currentlyViewedReportID. The other option is to use Context. May be, we can make SidebarScreen PureComponent.

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 need your help here. What do you think @rushatgabhane ?

Copy link
Member

Choose a reason for hiding this comment

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

If I'm not wrong, making it a pure component won't help because we have an arrow function.

Anyway, regarding removing the arrow function. It might not make a difference. (I'll verify this once again)

So let's do nothing here

@@ -87,7 +90,7 @@ function getReportID(route) {
return Number.parseInt(params.reportID, 10);
}

class ReportScreen extends React.Component {
class ReportScreen extends React.PureComponent {
Copy link
Member

@rushatgabhane rushatgabhane Mar 25, 2022

Choose a reason for hiding this comment

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

I wanna know why you've made this a PureComponent.

We should only bother with PureComponent if we're noticing performance problems and have determined the reason of unnecessary re-renders.

Performance.diffObject() in componentDidUpdate() should help you determine this.
ref: https://github.com/Expensify/App/blob/main/PERFORMANCE.md#reconciliation

Copy link
Contributor Author

@mdneyazahmad mdneyazahmad Mar 28, 2022

Choose a reason for hiding this comment

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

https://reactnavigation.org/docs/screen#children

Note: By default, React Navigation applies optimizations to screen components to prevent unnecessary renders. Using a render callback removes those optimizations. So if you use a render callback, you'll need to ensure that you use React.memo or React.PureComponent for your screen components to avoid performance issues.

Copy link
Member

@rushatgabhane rushatgabhane Mar 30, 2022

Choose a reason for hiding this comment

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

@mdneyazahmad I don't find that satisfactory.

Again, we should only bother with PureComponent if we're noticing performance problems and have determined the reason of unnecessary re-renders.

  • Does PureComponent reduce number of re-renders? Please provide some kind of verification for this
  • PureComponent only does a shallow comparison, so what side-effects (if any) are expected? https://60devs.com/pure-component-in-react.html

Copy link
Member

Choose a reason for hiding this comment

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

@Beamanator can you please buddy check what I've said above #8227 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed @rushatgabhane - The main port of using React.PureComponent would be "to avoid performance issues" right? But if there's no performance gains we don't need to make that change. So please only make this change if we know (if you can prove) there's some benefits 👍

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 will remove PureComponent here.

@rushatgabhane
Copy link
Member

rushatgabhane commented Mar 25, 2022

@mdneyazahmad Do we still need currentlyViewedReportID in Onyx?

@mdneyazahmad
Copy link
Contributor Author

I think, we need currentlyViewedReportID we are using that variable here

const redirectRoute = isLoggedIn ? ROUTES.getReportRoute(currentlyViewedReportID) : ROUTES.HOME;

@@ -296,9 +296,6 @@ export default compose(
network: {
key: ONYXKEYS.NETWORK,
},
currentlyViewedReportID: {
key: ONYXKEYS.CURRENTLY_VIEWED_REPORTID,
Copy link
Member

@rushatgabhane rushatgabhane Mar 31, 2022

Choose a reason for hiding this comment

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

Rajat pointed out in 1:1 that currentlyViewedReportID both in Onyx and state is confusing.

@mdneyazahmad when using a single tab, the Onyx key currentlyViewedReportID is in sync with the state.
But when using multiple tabs, this Onyx key has the value of the most recently viewed report.

What do you think of renaming this key to something like mostRecentlyViewedReportID.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree I also figured out this issue when proposing the solution.

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 think renaming currentlyViewedReportID to mostRecentlyViewedReportID in onyx and currentlyViewedReportID is fine in state. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

sounds good

Copy link
Contributor

Choose a reason for hiding this comment

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

We should be adding "rename" migration anytime we change an Onyx key so the old value transfers to the new key name. But maybe not worth the effort in this case...

Copy link
Member

@rushatgabhane rushatgabhane Apr 7, 2022

Choose a reason for hiding this comment

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

Thanks! TIL about migrations
We'll get it done once the performance issue is addressed.

@mdneyazahmad mdneyazahmad requested a review from marcaaron as a code owner April 3, 2022 15:20
@mdneyazahmad
Copy link
Contributor Author

Updated

@rushatgabhane
Copy link
Member

Sorry for the delay, I'll review this tomorrow.

Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

Just passing through, but these changes look potentially dangerous as they are changing our lower level navigation code. Can we think of alternatives...?

@@ -71,9 +71,10 @@ class BaseDrawerNavigator extends Component {
<Drawer.Screen
key={screen.name}
name={screen.name}
component={screen.component}
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty sure this is the recommended practice for rendering a screen component. Why are we changing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case we have to pass currentlyViewedReportID across ReportScreen and Sidebar. If we use component={Screen} we can not pass this variables, react-navigation provides one more option for screen component that is to use render props. This way we can pass this state variables to the ReportScreen but this is not the recommended way to pass variables it instead recommends to use Context api for the same. If we use render props we will lose the optimizations applied by the library and we have to manually handle re-rendering of the screen component and also it suggests to use PureComponent in this case.

Copy link
Member

@rushatgabhane rushatgabhane Apr 6, 2022

Choose a reason for hiding this comment

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

source: https://reactnavigation.org/docs/screen/#children
@mdneyazahmad makes sense. Thanks for the context.

Copy link
Member

@rushatgabhane rushatgabhane Apr 6, 2022

Choose a reason for hiding this comment

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

@Beamanator what should we do here? Is it okay to use Context API? That's the closest thing to our previous implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm kinda lost here. Why does this all need to happen at such a low level and why are we duplicating Onyx data in the MainDrawerNavigator state instead of just referencing the value that is stored in Onyx and have the SidebarLinks update when it changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

It works without changing this name but lastViewedReportID makes much sense here. User can open multiple reports but it will contain the id of last report opened.

Please do not make this change, thanks!

Copy link
Member

Choose a reason for hiding this comment

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

I think solution 3 is a good one. It might be needed in the future. Onyx is always the easiest way to manage updates.

Copy link
Member

@rushatgabhane rushatgabhane Apr 10, 2022

Choose a reason for hiding this comment

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

Thanks for the solution 3 and all the help, it is exactly what we need.
@mdneyazahmad let's get it done! :)

(Also, P/S format for the win! Will def use it more)

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 works!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need only this change

App/src/setup/index.js

Lines 24 to 26 in 43910fe

safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
captureMetrics: Metrics.canCaptureOnyxMetrics(),
initialKeyStates: {

  captureMetrics: Metrics.canCaptureOnyxMetrics(),
+ keysToDisableSyncEvents: [ONYXKEYS.CURRENTLY_VIEWED_REPORTID],

I will create a pr to react-native-onyx to add the feature as suggested my @marcaaron

Comment on lines +83 to +91
children: props => (
<ReportScreen
setCurrentlyViewedReportID={this.setCurrentlyViewedReportID}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
/>
),
initialParams,
},
Copy link
Member

@parasharrajat parasharrajat Apr 8, 2022

Choose a reason for hiding this comment

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

IMO, Really a bad way of doing things. No prop updates should be passed to the Screen components this way. It will create a performance issue.

@mdneyazahmad
Copy link
Contributor Author

mdneyazahmad commented Apr 27, 2022

We have decided to close this PR because implementation has changed and this is now handled here #8759

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

Successfully merging this pull request may close these issues.

5 participants