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

@uppy/status-bar: refactor to typescript #4839

Merged
merged 13 commits into from
Jan 22, 2024
Merged

@uppy/status-bar: refactor to typescript #4839

merged 13 commits into from
Jan 22, 2024

Conversation

aduh95
Copy link
Contributor

@aduh95 aduh95 commented Dec 27, 2023

No description provided.

@aduh95 aduh95 force-pushed the status-bar-refactor branch from 48f4ca5 to 4fa2cda Compare December 30, 2023 23:15
@aduh95 aduh95 force-pushed the status-bar-refactor branch from 5a587ff to c2894a5 Compare January 3, 2024 18:15
@aduh95 aduh95 requested a review from Murderlon January 3, 2024 18:22
packages/@uppy/status-bar/src/Components.tsx Show resolved Hide resolved
packages/@uppy/status-bar/src/Components.tsx Outdated Show resolved Hide resolved
packages/@uppy/status-bar/src/Components.tsx Show resolved Hide resolved
const { progress } = props
const { value, mode, message } = progress
const roundedValue = Math.round(value * 100)
const roundedValue = Math.round(value! * 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const roundedValue = Math.round(value! * 100)
const roundedValue = value != null ? Math.round(value * 100) : 0

to avoid NaN

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR is about adding types, we should refrain from making any change that would affect the JS output

Copy link
Contributor

Choose a reason for hiding this comment

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

But I thought that rule was to not introduce breaking changes. In this case, does it matter that the JS output changes? I think we’re missing on the critical benefit of a TS refactor, which is improving our code to be more stable along the way, like in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we fix be fixing bugs that we discover while refactoring to TS, even though it changes the JS output? At the very least we should add a TODO to fix such discovered bugs in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO non-null assertions are an implicit TODO. We should go through them and remove them on the 4.x branch

Copy link
Contributor

Choose a reason for hiding this comment

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

I think non-null assertions are fine to use in some cases, when the alternative is to check for nullish and throw an error (like maybeNull!.something). But in this case, the bug will render NaN, and in this case the correct solution is probably not to throw an error, but instead render something like 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

{mode === 'determinate' ? `${roundedValue}% ${dot} ` : ''}

As you can see, if the value is indeterminate, that roundedValue will not be read.

Copy link
Contributor

@mifi mifi Jan 18, 2024

Choose a reason for hiding this comment

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

then it's better to put the round after the determinate check, then we don't need the non-null assertion, because typescript already knows that it's not nullish:

      {mode === 'determinate' ? `${Math.round(value * 100)}% ${dot} ` : ''}

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 agree, but I'd prefer to not introduce runtime changes when we can avoid it. My wish is we'd be able to get rid of all non-null assertions on the 4.x branch, and turn the linter warning to an error.

packages/@uppy/status-bar/src/Components.tsx Outdated Show resolved Hide resolved
packages/@uppy/status-bar/src/StatusBar.tsx Show resolved Hide resolved
packages/@uppy/status-bar/src/StatusBar.tsx Show resolved Hide resolved
packages/@uppy/status-bar/src/StatusBar.tsx Show resolved Hide resolved
@@ -45,5 +47,5 @@ export default {
1: '%{smart_count} more files added',
},
showErrorDetails: 'Show error details',
},
}
} as Locale<0 | 1>['strings'],
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure what's the purpose of this

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 to force TS to validate the object to be compatible with the defined type.

Copy link
Member

@Murderlon Murderlon left a comment

Choose a reason for hiding this comment

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

Overall looks good. There are some eslint warnings which would be nice to fix.

packages/@uppy/status-bar/src/Components.tsx Outdated Show resolved Hide resolved
packages/@uppy/status-bar/src/StatusBarUI.tsx Outdated Show resolved Hide resolved
@aduh95 aduh95 force-pushed the status-bar-refactor branch from 60788f3 to e21dd23 Compare January 16, 2024 17:03
@aduh95 aduh95 changed the base branch from 4.x to main January 16, 2024 17:24
@aduh95 aduh95 force-pushed the status-bar-refactor branch 3 times, most recently from d1ec649 to c42448b Compare January 16, 2024 20:48
@mifi
Copy link
Contributor

mifi commented Jan 17, 2024

force push messed up my local branch :/

this.#previousUploadedBytes = Object.values(recoveredState.files)
.reduce((pv, { progress }) => pv + progress.bytesUploaded, 0)
this.#previousUploadedBytes = Object.values(recoveredState.files).reduce(
(pv, { progress }) => pv + (progress.bytesUploaded as number),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
(pv, { progress }) => pv + (progress.bytesUploaded as number),
(pv, { progress }) => pv + (progress.bytesUploaded === false ? 0 : progress.bytesUploaded),

this.#previousUploadedBytes = Object.values(recoveredState.files)
.reduce((pv, { progress }) => pv + progress.bytesUploaded, 0)
this.#previousUploadedBytes = Object.values(recoveredState.files).reduce(
(pv, { progress }) => pv + (progress.bytesUploaded as number),
Copy link
Contributor

Choose a reason for hiding this comment

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

alternatively

Suggested change
(pv, { progress }) => pv + (progress.bytesUploaded as number),
(pv, { progress }) => pv + (Number(progress.bytesUploaded)),

@aduh95 aduh95 force-pushed the status-bar-refactor branch from c42448b to 4854bba Compare January 17, 2024 11:20
Copy link
Contributor

github-actions bot commented Jan 17, 2024

Diff output files
diff --git a/packages/@uppy/status-bar/lib/Components.js b/packages/@uppy/status-bar/lib/Components.js
index 77f7929..3f867a0 100644
--- a/packages/@uppy/status-bar/lib/Components.js
+++ b/packages/@uppy/status-bar/lib/Components.js
@@ -133,14 +133,16 @@ function PauseResumeButton(props) {
   } = props;
   const title = isAllPaused ? i18n("resume") : i18n("pause");
   function togglePauseResume() {
-    if (isAllComplete) return null;
+    if (isAllComplete) return;
     if (!resumableUploads) {
-      return uppy.cancelAll();
+      uppy.cancelAll();
+      return;
     }
     if (isAllPaused) {
-      return uppy.resumeAll();
+      uppy.resumeAll();
+      return;
     }
-    return uppy.pauseAll();
+    uppy.pauseAll();
   }
   return h(
     "button",
diff --git a/packages/@uppy/status-bar/lib/StatusBar.js b/packages/@uppy/status-bar/lib/StatusBar.js
index 28c2148..a0656ee 100644
--- a/packages/@uppy/status-bar/lib/StatusBar.js
+++ b/packages/@uppy/status-bar/lib/StatusBar.js
@@ -38,12 +38,10 @@ function getUploadingState(error, isAllComplete, recoveredState, files) {
     if (progress.uploadStarted && !progress.uploadComplete) {
       return statusBarStates.STATE_UPLOADING;
     }
-    if (progress.preprocess && state !== statusBarStates.STATE_UPLOADING) {
+    if (progress.preprocess) {
       state = statusBarStates.STATE_PREPROCESSING;
     }
-    if (
-      progress.postprocess && state !== statusBarStates.STATE_UPLOADING && state !== statusBarStates.STATE_PREPROCESSING
-    ) {
+    if (progress.postprocess && state !== statusBarStates.STATE_PREPROCESSING) {
       state = statusBarStates.STATE_POSTPROCESSING;
     }
   }
diff --git a/packages/@uppy/status-bar/lib/StatusBarUI.js b/packages/@uppy/status-bar/lib/StatusBarUI.js
index 933ed3a..ea8eba7 100644
--- a/packages/@uppy/status-bar/lib/StatusBarUI.js
+++ b/packages/@uppy/status-bar/lib/StatusBarUI.js
@@ -137,8 +137,8 @@ export default function StatusBar(props) {
       role: "progressbar",
       "aria-label": `${width}%`,
       "aria-valuetext": `${width}%`,
-      "aria-valuemin": "0",
-      "aria-valuemax": "100",
+      "aria-valuemin": 0,
+      "aria-valuemax": 100,
       "aria-valuenow": progressValue,
     }),
     (() => {
@@ -226,3 +226,12 @@ export default function StatusBar(props) {
     ),
   );
 }
+StatusBar.defaultProps = {
+  doneButtonHandler: undefined,
+  hideAfterFinish: false,
+  hideCancelButton: false,
+  hidePauseResumeButton: false,
+  hideRetryButton: false,
+  hideUploadButton: undefined,
+  showProgressDetails: undefined,
+};
diff --git a/packages/@uppy/status-bar/lib/calculateProcessingProgress.js b/packages/@uppy/status-bar/lib/calculateProcessingProgress.js
index 5c71cd4..52dae93 100644
--- a/packages/@uppy/status-bar/lib/calculateProcessingProgress.js
+++ b/packages/@uppy/status-bar/lib/calculateProcessingProgress.js
@@ -1,6 +1,6 @@
 export default function calculateProcessingProgress(files) {
   const values = [];
-  let mode;
+  let mode = "indeterminate";
   let message;
   for (
     const {

Co-authored-by: Mikael Finstad <finstaden@gmail.com>
@aduh95
Copy link
Contributor Author

aduh95 commented Jan 17, 2024

force push messed up my local branch :/

You can't mess up your local branches if you don't have local branches

Smart guy meme

@aduh95 aduh95 merged commit 6b30fff into main Jan 22, 2024
16 checks passed
@aduh95 aduh95 deleted the status-bar-refactor branch January 22, 2024 14:03
@github-actions github-actions bot mentioned this pull request Feb 19, 2024
github-actions bot added a commit that referenced this pull request Feb 19, 2024
| Package                   | Version | Package                   | Version |
| ------------------------- | ------- | ------------------------- | ------- |
| @uppy/audio               |   1.1.5 | @uppy/remote-sources      |   1.1.1 |
| @uppy/aws-s3              |   3.6.1 | @uppy/status-bar          |   3.2.6 |
| @uppy/aws-s3-multipart    |  3.10.1 | @uppy/store-default       |   3.2.1 |
| @uppy/companion           |  4.12.1 | @uppy/store-redux         |   3.0.6 |
| @uppy/companion-client    |   3.7.1 | @uppy/svelte              |   3.1.2 |
| @uppy/compressor          |   1.1.0 | @uppy/thumbnail-generator |   3.0.7 |
| @uppy/core                |   3.9.0 | @uppy/transloadit         |   3.5.0 |
| @uppy/dashboard           |   3.7.2 | @uppy/tus                 |   3.5.1 |
| @uppy/drop-target         |   2.0.3 | @uppy/utils               |   5.7.1 |
| @uppy/form                |   3.1.0 | @uppy/vue                 |   1.1.1 |
| @uppy/golden-retriever    |   3.1.2 | @uppy/webcam              |   3.3.5 |
| @uppy/image-editor        |   2.4.1 | @uppy/xhr-upload          |   3.6.1 |
| @uppy/locales             |   3.5.1 | uppy                      |  3.22.0 |
| @uppy/provider-views      |   3.9.0 |                           |         |

-  @uppy/aws-s3-multipart,@uppy/aws-s3,@uppy/companion-client,@uppy/tus,@uppy/xhr-upload: update `uppyfile` objects before emitting events (antoine du hamel / #4928)
- @uppy/transloadit: add `clientname` option (marius / #4920)
- @uppy/thumbnail-generator: fix broken previews after cropping (evgenia karunus / #4926)
- @uppy/compressor: upgrade compressorjs (merlijn vos / #4924)
- @uppy/companion: fix companion dns and allow redirects from http->https again (mikael finstad / #4895)
- @uppy/dashboard: autoopenfileeditor - rename "edit file" to "edit image" (evgenia karunus / #4925)
- meta: resolve jsx to preact in shared tsconfig (merlijn vos / #4923)
- @uppy/image-editor: image editor: make compressor work after the image editor, too (evgenia karunus / #4918)
- meta: exclude `tsconfig` files from npm bundles (antoine du hamel / #4916)
- @uppy/compressor: migrate to ts (mikael finstad / #4907)
- @uppy/provider-views: update uppy-providerbrowser-viewtype--list.scss (aditya patadia / #4913)
- @uppy/tus: migrate to ts (merlijn vos / #4899)
- meta: bump yarn version (antoine du hamel / #4906)
- meta: validate `defaultoptions` for stricter option types (antoine du hamel / #4901)
- @uppy/dashboard: Uncouple native camera and video buttons from the `disableLocalFiles` option (jake mcallister / #4894)
- meta: put experimental ternaries in .prettierrc.js (merlijn vos / #4900)
- @uppy/xhr-upload: migrate to ts (merlijn vos / #4892)
- @uppy/drop-target: refactor to typescript (artur paikin / #4863)
- meta: fix missing line return in js2ts script (antoine du hamel)
- meta: disable `@typescript-eslint/no-empty-function` lint rule (antoine du hamel / #4891)
- @uppy/companion-client: fix tests and linter (antoine du hamel / #4890)
- @uppy/companion-client: migrate to ts (merlijn vos / #4864)
- meta: prettier 3.0.3 -> 3.2.4 (antoine du hamel / #4889)
- @uppy/image-editor: migrate to ts (merlijn vos / #4880)
- meta: fix race condition in `e2e.yml` (antoine du hamel)
- @uppy/core: add utility type to help define plugin option types (antoine du hamel / #4885)
- meta: merge `output-watcher` and `e2e` workflows (antoine du hamel / #4886)
- @uppy/status-bar: fix `statusbaroptions` type (antoine du hamel / #4883)
- @uppy/core: improve types of .use() (merlijn vos / #4882)
- @uppy/audio: fix `audiooptions` (antoine du hamel / #4884)
- meta: upgrade vite and vitest (antoine du hamel / #4881)
- meta: fix `yarn build:clean` (antoine du hamel)
- @uppy/audio: refactor to typescript (antoine du hamel / #4860)
- @uppy/status-bar: refactor to typescript (antoine du hamel / #4839)
- @uppy/core: add `plugintarget` type and mark options as optional (antoine du hamel / #4874)
- meta: improve output watcher diff (antoine du hamel / #4876)
- meta: minify the output watcher diff further (antoine du hamel)
- meta: remove comments from output watcher (mikael finstad / #4875)
- @uppy/utils: improve types for `finddomelement` (antoine du hamel / #4873)
- @uppy/code: allow plugins to type `pluginstate` (antoine du hamel / #4872)
- meta: build(deps): bump follow-redirects from 1.15.1 to 1.15.4 (dependabot[bot] / #4862)
- meta: add `output-watcher` gha to help check output diff (antoine du hamel / #4868)
- meta: generate locale pack from output file (antoine du hamel / #4867)
- meta: comment on what we want to do about close, resetprogress, clearuploadedfiles, etc in the next major (artur paikin / #4865)
- meta: fix `yarn build:clean` (antoine du hamel / #4866)
- meta: use `explicit-module-boundary-types` lint rule (antoine du hamel / #4858)
- @uppy/form: use requestsubmit (merlijn vos / #4852)
- @uppy/provider-views: add referrerpolicy to images (merlijn vos / #4853)
- @uppy/core: add `debuglogger` as export in manual types (antoine du hamel / #4831)
- meta: fix `js2ts` script (antoine du hamel / #4846)
- @uppy/xhr-upload: show remove button (merlijn vos / #4851)
- meta: upgrade `@transloadit/prettier-bytes` (antoine du hamel / #4850)
- @uppy/core: add missing requiredmetafields key in restrictions (darthf1 / #4819)
- @uppy/companion,@uppy/tus: bump `tus-js-client` version range (merlijn vos / #4848)
- meta: build(deps): bump aws/aws-sdk-php from 3.272.1 to 3.288.1 in /examples/aws-php (dependabot[bot] / #4838)
- @uppy/dashboard: fix `typeerror` when `file.remote` is nullish (antoine du hamel / #4825)
- meta: fix `js2ts` script (antoine du hamel / #4844)
- @uppy/locales: fix "save" button translation in hr_hr.ts (žan žlender / #4830)
- meta: fix linting of `.tsx` files (antoine du hamel / #4843)
- @uppy/core: fix types (antoine du hamel / #4842)
- @uppy/utils: improve `preprocess` and `postprocess` types (antoine du hamel / #4841)
- meta: fix `yarn build:clean` (mikael finstad / #4840)
- meta: dev: remove extensions from vite aliases (antoine du hamel)
- meta: fix `"e2e"` script (antoine du hamel)
- @uppy/core: refactor to ts (murderlon)
- meta: fix typescript ci (antoine du hamel)
- meta: fix clean script (mikael finstad / #4820)
- @uppy/companion-client: fix `typeerror` (antoine du hamel)
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.

4 participants