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

Migrate ArrowKeyFocusManager to functional component #19812

Closed

Conversation

szebniok
Copy link
Contributor

Details

The ArrowKeyFocusManager is used by:

  • ReportActionCompose - for emoji and mention suggestions
  • OptionsSelector - used by many components, e.g. NewChatPage
  • AttachmentPicker/index.native.js

I was not able to get the keyboard keys to work in the iOS and Android simulator (but the same is true for new.expensify.com).
I couldn't get the OptionsSelector working on mWeb versions as well.

Fixed Issues

$ #16110

Tests

  1. Open the chat window
  2. Type in @
  3. Verify that we can use arrow keys to move around the suggestions
  4. Type in :sm
  5. Verify that we can use arrow keys to move around the suggestions
  6. (On desktop/web) Open the new chat window
  7. Verify that we can use arrow keys to move around the suggestions
  • 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
Screen.Recording.2023-05-30.at.13.58.06.mov
Mobile Web - Chrome
Screen.Recording.2023-05-30.at.14.36.48.mov
Mobile Web - Safari
Screen.Recording.2023-05-30.at.14.56.09.mov
Desktop
Screen.Recording.2023-05-30.at.13.58.48.mov
iOS

I was unable to get the keyboard keys to work on iOS

Android

I was unable to get the keyboard keys to work on Android

@szebniok szebniok requested a review from a team as a code owner May 30, 2023 13:10
@melvin-bot melvin-bot bot requested review from fedirjh and youssef-lr and removed request for a team May 30, 2023 13:11
@melvin-bot
Copy link

melvin-bot bot commented May 30, 2023

@fedirjh @youssef-lr One of you needs to 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]

});

const onArrowUpKey = useCallback(() => onArrowUpKeyRef.current(), []);
const onArrowDownKey = useCallback(() => onArrowDownKeyRef.current(), []);
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 way we set the key handlers is a bit weird, but I think it is necessary here, if we want to avoid unnecessary re-renders or resubscribing to the KeyboardShortcut inside useEffect below.

I am trying to accomplish something akin to the useEvent hook from this RFC

@fedirjh
Copy link
Contributor

fedirjh commented Jun 1, 2023

cc @marcaaron @youssef-lr @aldo-expensify @szebniok Given that we have created custom hooks useArrowKeyFocusManager.js and useKeyboardShortcut.js. I believe we should utilize those hooks in this migration. However, there is an open issue regarding the Arrow key shortcut not working, which affects the same hooks. Should we hold the PR until the issue is resolved ?

@szebniok, could you please take a look at the issue #19700 and assess if we can address it ?

Copy link
Contributor

@aldo-expensify aldo-expensify left a comment

Choose a reason for hiding this comment

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

I find the implementation using useRef and useEffects without watching anything hard to follow. I would be good to have some comments.

Comment on lines +37 to +73
useEffect(() => {
onArrowUpKeyRef.current = () => {
if (props.maxIndex < 0) {
return;
}

const currentFocusedIndex = props.focusedIndex > 0 ? props.focusedIndex - 1 : props.maxIndex;
let newFocusedIndex = currentFocusedIndex;

while (props.disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex > 0 ? newFocusedIndex - 1 : props.maxIndex;
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return; // no-op
}
}
props.onFocusedIndexChanged(newFocusedIndex);
};

onArrowDownKeyRef.current = () => {
if (props.maxIndex < 0) {
return;
}

const currentFocusedIndex = props.focusedIndex < props.maxIndex ? props.focusedIndex + 1 : 0;
let newFocusedIndex = currentFocusedIndex;

while (props.disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex < props.maxIndex ? newFocusedIndex + 1 : 0;
if (newFocusedIndex === currentFocusedIndex) {
// all indexes are disabled
return; // no-op
}
}
props.onFocusedIndexChanged(newFocusedIndex);
};
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the purpose of this code in a useEffect that will be run in every cycle after rendering. Why did we put it in a use effect and use references (useRef)?

const onArrowUpKey = useCallback(() => onArrowUpKeyRef.current(), []);
const onArrowDownKey = useCallback(() => onArrowDownKeyRef.current(), []);

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB: I think this would be a bit simpler with each subscribe in its own useEffect.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree

Comment on lines +104 to +107
useEffect(() => {
if (mounted.current) {
if (prevMaxIndex.current === props.maxIndex) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

This useEffect is not watching anything either, is that intended?

@aldo-expensify
Copy link
Contributor

I believe we should utilize those hooks in this migration. However, there is an open issue regarding the #19700, which affects the same hooks. Should we hold the PR until the issue is resolved ?

Maybe.. Those hooks are not broken, the implementation could be improved a bit to have cleaner code. The problem in #19700 comes from not being able to keep a stable reference of the callbacks we pass to these hooks, but that's not a problem of the hook itself.

Copy link
Contributor

@roryabraham roryabraham left a comment

Choose a reason for hiding this comment

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

I think at minimum we should use the useArrowKeyFocusManager hook inside this component, but more likely we should just remove this component altogether and standardize on just the hook

@fedirjh
Copy link
Contributor

fedirjh commented Jun 22, 2023

Little bump here , Since the useArrowKeyFocusManager hook was fixed with #20756, I think we can proceed with using the hook in this migration.

@roryabraham
Copy link
Contributor

Agree, let's close this out

@fabioh8010
Copy link
Contributor

fabioh8010 commented Dec 5, 2023

Hey @fedirjh @roryabraham 👋 , I'm coming here because of this TS issue, we must migrate this component to function one first before migrating to TS. I would like to know if the TS Migration team can do it now or, if not, who is going to continue this migration. Thanks!

@roryabraham
Copy link
Contributor

I think we can/should use the hook going forward

@fabioh8010
Copy link
Contributor

Thanks, I will leave this comment in the issue.

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