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

refactor(ui): convert a few components to use hooks #11800

Merged
merged 6 commits into from
Sep 21, 2023

Conversation

agilgur5
Copy link
Contributor

@agilgur5 agilgur5 commented Sep 11, 2023

Partial fix for #9810
Partial fix for #11740

Motivation

  • Use newer, modern React hooks syntax instead of legacy React component lifecycle hooks

  • Use newer, modern async/await syntax instead of Promise chains

Combined a few components together as previously requested. These are all components that make use of effects and other hooks (e.g. refs), but are not significantly complicated (unlike #11794, which is one complex component -- an entire page. and #11791 is all components without effects).

Modifications

Convert 3 components to use hooks: ResourceEditor, WorkflowDrawer, and TagsInput

  • this.props -> props

  • this.state -> useState

    • simplify some callbacks now too
  • ref -> useRef

    • rename variables to clarify that these are refs which are accessed via .current
  • componentDidMount -> useEffect

  • methods -> inner functions

    • also convert to async/await syntax as well
    • hoist child render functions into the main component
  • also shorten some logic by using modern optional chaining syntax

See commit messages for more details

Verification

  • Mostly no real semantic changes
  • Manually tested a solid bit for TagsInput (as the refs gave some errors for a bit due to an accidental incorrect renaming) and a bit for WorkflowDrawer. Screenshots of them working post-refactor:
    Screenshot 2023-09-11 at 5 33 48 PM
    Screenshot 2023-09-11 at 5 35 50 PM

Notes for Reviewers

Ignoring whitespace changes makes it a bit easier to read. Though there are still a good amount of other refactorings

- `this.props` -> `props`
- `this.state` -> `useState`
  - simplify some callbacks now too
- methods -> inner functions
  - also convert to async/await syntax as well
- hoist child render functions into the main component

- could possibly optimize this a bit with `useCallback`
- and there's bit of a logic bug in `submit` as it sets `value.metadata.namespace` directly
  - but this was present in the previous version already. and it probably gets quickly overwritten / re-rendered by `props.onSubmit`.
  - would be good to fix just in case though, but this component is rarely used too

Signed-off-by: Anton Gilgur <[email protected]>
- `this.state` -> `useState`
- `this.props` -> `props`
- `componentDidMount` -> `useEffect`
  - also convert to async/await syntax

Signed-off-by: Anton Gilgur <[email protected]>
- `this.props` -> `props`
- `this.state` -> `useState`
- methods -> inner functions
  - hoist `onTagsChange` to above render
  - also convert to async/await syntax
- `ref` -> `useRef`
  - rename variables to clarify that these are refs which are accessed via `.current`
  - simplify `inputRef` callback as we can now pass it directly
  - `autoCompleteRef` could not be passed directly as the `argo-ui` component does not quite support that
    - likely need to conver that to use `forwardRef`. I'm imaginging that `argo-ui` might all be `class` components? (as it hasn't been maintained in some time)

- rename `renderInput` render props to `inputProps` so as to not conflict with top-level `props` naming

- also shorten some logic by using optional chaining syntax

Signed-off-by: Anton Gilgur <[email protected]>
- consistent with other parts of the codebase and previous refactors, such as 25e1939

Signed-off-by: Anton Gilgur <[email protected]>
Signed-off-by: Anton Gilgur <[email protected]>
- without this, the input would throw an error when adding a label
- this matches the old class-based code as well
  - I think I misread the old code while refactoring and missed that it was `inputProps.ref` and _not_ `inputRef`
    - in old code, it was `props.ref` and `this.inputEl`, so the refactor changed up names that crossed each other a bit

Signed-off-by: Anton Gilgur <[email protected]>
Copy link
Member

@toyamagu-2021 toyamagu-2021 left a comment

Choose a reason for hiding this comment

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

I've double checked functionalities.

@terrytangyuan terrytangyuan merged commit 27132d9 into argoproj:master Sep 21, 2023
22 checks passed
@agilgur5 agilgur5 deleted the refactor-ui-hooks branch September 21, 2023 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants