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

Pay Header improvements #18883

Merged
merged 12 commits into from
May 15, 2023
Merged

Pay Header improvements #18883

merged 12 commits into from
May 15, 2023

Conversation

roryabraham
Copy link
Contributor

Details

Fixed Issues

$ GH_LINK
PROPOSAL: GH_LINK_ISSUE(COMMENT)

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
    • 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 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

@@ -27,10 +27,6 @@ const propTypes = {
vertical: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_VERTICAL)),
}),

/** A function with content to measure. This component will use this.props.children by default,
but in the case the children are not displayed, the measurement will not work. */
measureContent: PropTypes.func.isRequired,
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 clear to me that this was not needed. It also said it would use this.props.children by default, but it did not. Everywhere this component was used we had measureContent be an exact or near-exact duplicate of the component's children.

Furthermore, children and measureContent were swapped, such that the children would be rendered invisibly + measured, then whatever was returned by measureContent would be rendered visibly with the adjusted position.

@roryabraham
Copy link
Contributor Author

roryabraham commented May 13, 2023

@best-lucky1030 WIP stands for "work in progress"

@mountiny mountiny self-requested a review May 13, 2023 21:24
@iwiznia iwiznia marked this pull request as ready for review May 15, 2023 16:16
@iwiznia iwiznia requested a review from a team as a code owner May 15, 2023 16:16
@iwiznia iwiznia changed the title [WIP] Pay Header improvements Pay Header improvements May 15, 2023
@iwiznia iwiznia merged commit e082b99 into ionatan_payheader May 15, 2023
@iwiznia iwiznia deleted the Rory-PayHeader branch May 15, 2023 16:16
@melvin-bot melvin-bot bot removed the request for review from a team May 15, 2023 16:16
@melvin-bot
Copy link

melvin-bot bot commented May 15, 2023

@arosiclair 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]

@melvin-bot melvin-bot bot requested a review from arosiclair May 15, 2023 16:17
@mountiny
Copy link
Contributor

How was this merged without an approval

@iwiznia
Copy link
Contributor

iwiznia commented May 15, 2023

It was merged into my PR, we will do reviews, tests and everything in the actual PR that is targeting main

@mountiny
Copy link
Contributor

oh I see, always miss that nuance thanks

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/iwiznia in version: 1.3.16-0 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/chiragsalian in version: 1.3.16-7 🚀

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

2 similar comments
@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/chiragsalian in version: 1.3.16-7 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/chiragsalian in version: 1.3.16-7 🚀

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

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/chiragsalian in version: 1.3.16-7 🚀

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

Comment on lines +11 to +37
export default function useKeyboardShortcut(shortcut, callback, config = {}) {
const {captureOnInputs = true, shouldBubble = false, priority = 0, shouldPreventDefault = true, excludedNodes = [], isActive = true} = config;

const subscription = useRef(null);
const subscribe = useCallback(
() =>
KeyboardShortcut.subscribe(
shortcut.shortcutKey,
callback,
shortcut.descriptionKey,
shortcut.modifiers,
captureOnInputs,
shouldBubble,
priority,
shouldPreventDefault,
excludedNodes,
),
[callback, captureOnInputs, excludedNodes, priority, shortcut.descriptionKey, shortcut.modifiers, shortcut.shortcutKey, shouldBubble, shouldPreventDefault],
);

useEffect(() => {
const unsubscribe = subscription.current || (() => {});
unsubscribe();
subscription.current = isActive ? subscribe() : null;
return isActive ? subscription.current : () => {};
}, [isActive, subscribe]);
}
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 not sure why are we calling manually unsubscribe(); instead of letting the unsubscribe be called during cleanup. I also don't think the reference is needed or the useCallback.

I think this could have been simple to implement like this:

export default function useKeyboardShortcut(shortcut, callback, config = {}) {
    const {captureOnInputs = true, shouldBubble = false, priority = 0, shouldPreventDefault = true, excludedNodes = [], isActive = true} = config;
    useEffect(() => {
        if (isActive) {
            return KeyboardShortcut.subscribe(
                shortcut.shortcutKey,
                callback,
                shortcut.descriptionKey,
                shortcut.modifiers,
                captureOnInputs,
                shouldBubble,
                priority,
                shouldPreventDefault,
                excludedNodes,
            );
        }
        return undefined;
    }, [isActive, callback, captureOnInputs, excludedNodes, priority, shortcut.descriptionKey, shortcut.modifiers, shortcut.shortcutKey, shouldBubble, shouldPreventDefault]);
}

unless I'm missing something?

Also, where we use the hook here:

useKeyboardShortcut(
CONST.KEYBOARD_SHORTCUTS.ENTER,
() => {
if (focusedIndex === -1) {
return;
}
selectItem(focusedIndex);
setFocusedIndex(-1); // Reset the focusedIndex on selecting any menu
},
{isActive: props.isVisible},
);

We didn't use useCallback for keeping the same reference of the function. We re-create a new function every render which causes the useEffect inside useKeyboardShortcut to run every single time after every render.

You can see this problem if you add some console logs:

Screen.Recording.2023-05-25.at.12.56.27.PM.mov

Copy link
Contributor

Choose a reason for hiding this comment

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

I spent some time trying to fix this by using useCallback and useMemo to try to keep the same references as much as possible, but I died at ReportActionCompose. The size and it being a class component doesn't help at all with it :(

Copy link
Contributor

@mountiny mountiny May 29, 2023

Choose a reason for hiding this comment

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

@aldo-expensify Should we try to export this to expert agency? They might have more luck with this and it will save your valuable time

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think this can be pretty time consuming, it would be great to have their help 🙏 . Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

btw, the constant re-subscribe of the shortcut causes this bug: #18883 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@aldo-expensify it looks like your implementation would cause a duplicate subscription to be set up every time any of the dependencies to the useEffect hook change, without cleaning up the previous subscription. That's why we're manually calling unsubscribe in the hook instead of only doing it during the cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems like a potential use case for the new useEvent React hook.

Copy link
Contributor

@aldo-expensify aldo-expensify Jun 1, 2023

Choose a reason for hiding this comment

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

@aldo-expensify it looks like your implementation would cause a duplicate subscription to be set up every time any of the dependencies to the useEffect hook change, without cleaning up the previous subscription. That's why we're manually calling unsubscribe in the hook instead of only doing it during the cleanup.

hmm I don't think so, my implementation returns the unsubscribe function (return KeyboardShortcut.subscribe(...)). The useEffect calls the function during clean up, so it shouldn't be subscribed twice.

The useEffect does clean up before executing the callback you pass to it again.

  1. useEffect runs a first time, we return the unsubscribe callback
  2. Watched values change
  3. useEffect executes the unsubscribe callback we returned in 1. (this is the useEffect clean up)
  4. useEffect runs again, and we return a new unsubscribe callback
  5. Watched values change
  6. useEffect executes the unsubscribe callback we returned in 4. (this is the useEffect clean up)
  7. useEffect runs again, and we return a new unsubscribe callback

... and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see why I'm wrong here. The cleanup function is called every time the dependencies to the effect change. So if we need to resubscribe, the cleanup (unsubscribe) will be called first, then the effect will run and resubscribe

Copy link
Contributor

@aldo-expensify aldo-expensify Jun 2, 2023

Choose a reason for hiding this comment

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

That is my understanding, I did some testing adding console.logs

Comment on lines +51 to +61
useKeyboardShortcut(
CONST.KEYBOARD_SHORTCUTS.ENTER,
() => {
if (focusedIndex === -1) {
return;
}
selectItem(focusedIndex);
setFocusedIndex(-1); // Reset the focusedIndex on selecting any menu
},
{isActive: props.isVisible},
);
Copy link
Contributor

@aldo-expensify aldo-expensify May 25, 2023

Choose a reason for hiding this comment

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

I think the changes in the code that cause this event listener to constantly resubscribe (more details), make it climb to the top in the list of event handlers with the same priority. Since this event doesn't allow bubbling, it prevents any other handler. This causes this bug:

  1. Open the panel to invite new members to a workspace (/workspace/<workspace id>/invite)
  2. Verify that arrows up and down work and they change the selected row
  3. Select something with the mouse
  4. Click Next
  5. Go back
  6. BUG: The arrow keys don't work anymore

Listeners before re-subscribe:
image

Listeners after re-subscribe:

image

Context: I'm coming from debugging this other bug related to competing listeners: #18419

*/
export default function useArrowKeyFocusManager({maxIndex, onFocusedIndexChange = () => {}, initialFocusedIndex = 0, disabledIndexes = [], shouldExcludeTextAreaNodes = true}) {
const [focusedIndex, setFocusedIndex] = useState(initialFocusedIndex);
useEffect(() => onFocusedIndexChange(focusedIndex), [focusedIndex, onFocusedIndexChange]);
Copy link
Member

Choose a reason for hiding this comment

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

We might have caused a bug here.

More details - #40483 (comment)

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.

6 participants