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): reorganize shared/utils.ts #13339

Merged
merged 5 commits into from
Sep 16, 2024

Conversation

agilgur5
Copy link

@agilgur5 agilgur5 commented Jul 12, 2024

While writing various PRs, I've had some of these changes in a stash for a while. When working on some recent namespace fix PRs, this refactor felt particularly necessary given all the unrelated functions to namespaces in this file

Motivation

  • this file's main purpose is for namespace manipulation, so rename to shared/namespaces.ts

    • and rename Utils to nsUtils for explicitness as well
    • generally speaking, "dumping grounds" are not good to have and organizing them when they get large is best practice
  • these changes also make these functions easier to unit test and give them proper encapsulation

    • private functions are no longer exported

Modifications

  • shared/utils.ts -> shared/namespaces.ts

    • Utils -> nsUtils
  • several util functions were used in exactly one place, so just move them to that place

    • statusIconClasses was only used in phase-icon.tsx
    • isWorkflowRunning, isWorkflowSuspended were only used in workflow-operations-map.tsx
  • some were only used together with exactly one component, so move to that component

    • getValueFromParameter was only used with ParametersInput
  • others were only used for one section of the UI, e.g. Events or Workflows

    • so move them to a utils.ts file there; they are not shared between sections
    • similarly queryParams was only used in services/
  • also simplify some of these functions:

    • with optional chaining
    • by inverting unnecessarily nested conditionals
    • by removing unnecessary singleton structures

Verification

  • Purely stylistic changes, no semantic changes
  • yarn lint, test & build all pass

Comment on lines -9 to -24
let isRunning = false;
let hasFailed = false;
conditions.map(condition => {
if (condition.status === 'False') {
hasFailed = true;
} else if (condition.status === 'Unknown') {
isRunning = true;
}
});
if (hasFailed) {
classes = [icon, 'status-icon--failed'];
} else if (isRunning) {
classes = [icon, 'status-icon--spin'];
} else {
classes = [icon, 'status-icon--running'];
}
Copy link
Author

@agilgur5 agilgur5 Jul 12, 2024

Choose a reason for hiding this comment

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

I simplified this quite a bit as the two flags, hasFailed and isRunning, are mutually exclusive (by the logic here but also by definition), so can just return as soon as one of them is hit

@agilgur5 agilgur5 marked this pull request as draft July 16, 2024 02:01
Anton Gilgur added 3 commits August 1, 2024 00:13
- its main purpose is for namespace manipulation, so rename to `shared/namespaces.ts`
  - and rename `Utils` to `nsUtils` for explicitness as well
    - this singleton may not be necessary in the future, but keeping it as-is right now for simpler diff
  - generally speaking, "dumping grounds" are not good to have and organizing them when they get large is best practice

- several util functions were used in exactly one place, so just move them to that place
  - `statusIconClasses` was only used in `phase-icon.tsx`
  - `isWorkflowRunning`, `isWorkflowSuspended` were only used in `workflow-operations-map.tsx`

- some were only used together with exactly one component, so move to that component
  - `getValueFromParameter` was only used with `ParametersInput`

- others were only used for one section of the UI, e.g. Events or Workflows
  - so move them to a `utils.ts` file there; they are not shared between sections
  - also `queryParams` was only used in `services/`

- also simplify some of these functions:
  - with optional chaining
  - by inverting unnecesarily nested conditionals
  - by removing unnecessary singleton structures

- these changes also make these functions easier to unit test and give them proper encapsulation
  - private functions are no longer exported

Signed-off-by: Anton Gilgur <[email protected]>
Signed-off-by: Anton Gilgur <[email protected]>
@agilgur5 agilgur5 force-pushed the refactor-ui-shared-utils branch from 0918598 to 2ab845e Compare August 1, 2024 04:15
@agilgur5 agilgur5 marked this pull request as ready for review August 1, 2024 04:15
@isubasinghe isubasinghe self-assigned this Sep 8, 2024
Copy link
Member

@isubasinghe isubasinghe left a comment

Choose a reason for hiding this comment

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

Looks okay, can you please make the CI pass?

@agilgur5
Copy link
Author

Yea, looks like I accidentally added an extra new line during conflict resolution

@agilgur5
Copy link
Author

Should pass now

@agilgur5 agilgur5 enabled auto-merge (squash) September 10, 2024 00:39
Copy link
Member

@isubasinghe isubasinghe left a comment

Choose a reason for hiding this comment

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

Code looks fine, I am unsure if we should merge this personally though, just due to future potential git conflicts.

@agilgur5 agilgur5 merged commit 860c862 into argoproj:main Sep 16, 2024
15 checks passed
@agilgur5 agilgur5 deleted the refactor-ui-shared-utils branch September 16, 2024 08:42
@agilgur5
Copy link
Author

agilgur5 commented Sep 20, 2024

I am unsure if we should merge this personally though, just due to future potential git conflicts.

Same as I wrote in #12539 (comment), this logic applies to any refactor, so this is a strawman, as we ofc don't avoid refactors.
Same as there, conflicts are (much) worse for dep upgrades than code changes as well, but we don't avoid those either.

MasonM added a commit to MasonM/argo-workflows that referenced this pull request Oct 9, 2024
Attempting to build the UI on `main` currently fails with the following
error:
```
ERROR in ./src/app/workflows/components/retry-workflow-node-panel.tsx 26:0-43
Module not found: Error: Can't resolve '../../shared/utils' in '/home/vscode/go/src/github.com/argoproj/argo-workflows/ui/src/app/workflows/components'
```

This is happening due to a merge conflict between
argoproj#13343 and
argoproj#13339. The latter was
merged first and deleted `ui/src/app/shared/utils.ts`, which the former
is importing. The reason the CI build didn't fail for the former PR is that
the branch wasn't up-to-date with `main` at the time of merge.

Signed-off-by: Mason Malone <[email protected]>
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.

2 participants