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

Dashboard - convert some files to typescript - 2 #5367

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

lakesare
Copy link
Contributor

@lakesare lakesare commented Jul 25, 2024

  • converts AddFiles.tsx
  • converts Dashboard.tsx

Only merge after #5381, #5380

Copy link
Contributor

github-actions bot commented Jul 25, 2024

Diff output files
diff --git a/packages/@uppy/dashboard/lib/Dashboard.js b/packages/@uppy/dashboard/lib/Dashboard.js
index a4e8014..929c8e2 100644
--- a/packages/@uppy/dashboard/lib/Dashboard.js
+++ b/packages/@uppy/dashboard/lib/Dashboard.js
@@ -71,6 +71,7 @@ const defaultOptions = {
   autoOpen: null,
   disabled: false,
   disableLocalFiles: false,
+  nativeCameraFacingMode: "",
   doneButtonHandler: undefined,
   onRequestCloseModal: null,
 };
diff --git a/packages/@uppy/dashboard/lib/components/AddFiles.js b/packages/@uppy/dashboard/lib/components/AddFiles.js
index 8a0edf9..68d3ebf 100644
--- a/packages/@uppy/dashboard/lib/components/AddFiles.js
+++ b/packages/@uppy/dashboard/lib/components/AddFiles.js
@@ -2,21 +2,29 @@ import { Component, Fragment, h } from "preact";
 class AddFiles extends Component {
   constructor() {
     super(...arguments);
+    this.fileInput = null;
+    this.folderInput = null;
+    this.mobilePhotoFileInput = null;
+    this.mobileVideoFileInput = null;
     this.triggerFileInputClick = () => {
-      this.fileInput.click();
+      var _this$fileInput;
+      (_this$fileInput = this.fileInput) == null || _this$fileInput.click();
     };
     this.triggerFolderInputClick = () => {
-      this.folderInput.click();
+      var _this$folderInput;
+      (_this$folderInput = this.folderInput) == null || _this$folderInput.click();
     };
     this.triggerVideoCameraInputClick = () => {
-      this.mobileVideoFileInput.click();
+      var _this$mobileVideoFile;
+      (_this$mobileVideoFile = this.mobileVideoFileInput) == null || _this$mobileVideoFile.click();
     };
     this.triggerPhotoCameraInputClick = () => {
-      this.mobilePhotoFileInput.click();
+      var _this$mobilePhotoFile;
+      (_this$mobilePhotoFile = this.mobilePhotoFileInput) == null || _this$mobilePhotoFile.click();
     };
     this.onFileInputChange = event => {
       this.props.handleInputChange(event);
-      event.target.value = null;
+      event.target.value = "";
     };
     this.renderHiddenInput = (isFolder, refCallback) => {
       var _this$props$allowedFi;
@@ -305,16 +313,6 @@ class AddFiles extends Component {
       if (hasOnlyMyDevice) list = [];
       const listWithoutLastTwo = [...list];
       const lastTwo = listWithoutLastTwo.splice(list.length - 2, list.length);
-      const renderList = l =>
-        l.map(_ref => {
-          let {
-            key,
-            elements,
-          } = _ref;
-          return h(Fragment, {
-            key: key,
-          }, elements);
-        });
       return h(
         Fragment,
         null,
@@ -325,13 +323,33 @@ class AddFiles extends Component {
             className: "uppy-Dashboard-AddFiles-list",
             role: "tablist",
           },
-          renderList(listWithoutLastTwo),
-          h("span", {
-            role: "presentation",
-            style: {
-              "white-space": "nowrap",
+          listWithoutLastTwo.map(_ref => {
+            let {
+              key,
+              elements,
+            } = _ref;
+            return h(Fragment, {
+              key: key,
+            }, elements);
+          }),
+          h(
+            "span",
+            {
+              role: "presentation",
+              style: {
+                "white-space": "nowrap",
+              },
             },
-          }, renderList(lastTwo)),
+            lastTwo.map(_ref2 => {
+              let {
+                key,
+                elements,
+              } = _ref2;
+              return h(Fragment, {
+                key: key,
+              }, elements);
+            }),
+          ),
         ),
       );
     };
@@ -413,7 +431,7 @@ class AddFiles extends Component {
         this.props.note && h("div", {
           className: "uppy-Dashboard-note",
         }, this.props.note),
-        this.props.proudlyDisplayPoweredByUppy && this.renderPoweredByUppy(this.props),
+        this.props.proudlyDisplayPoweredByUppy && this.renderPoweredByUppy(),
       ),
     );
   }
diff --git a/packages/@uppy/dashboard/lib/components/Dashboard.js b/packages/@uppy/dashboard/lib/components/Dashboard.js
index 1fb0233..189b65d 100644
--- a/packages/@uppy/dashboard/lib/components/Dashboard.js
+++ b/packages/@uppy/dashboard/lib/components/Dashboard.js
@@ -55,7 +55,7 @@ export default function Dashboard(props) {
   const numberOfFilesForRecovery = props.recoveredState ? Object.keys(props.recoveredState.files).length : null;
   const numberOfGhosts = props.files
     ? Object.keys(props.files).filter(fileID => props.files[fileID].isGhost).length
-    : null;
+    : 0;
   const renderRestoredText = () => {
     if (numberOfGhosts > 0) {
       return props.i18n("recoveredXFiles", {
@@ -191,12 +191,23 @@ export default function Dashboard(props) {
             containerWidth: props.containerWidth,
             containerHeight: props.containerHeight,
           })
-          : h(
-            AddFiles,
-            _extends({}, props, {
-              isSizeMD: isSizeMD,
-            }),
-          ),
+          : h(AddFiles, {
+            i18n: props.i18n,
+            i18nArray: props.i18nArray,
+            acquirers: props.acquirers,
+            handleInputChange: props.handleInputChange,
+            maxNumberOfFiles: props.maxNumberOfFiles,
+            allowedFileTypes: props.allowedFileTypes,
+            showNativePhotoCameraButton: props.showNativePhotoCameraButton,
+            showNativeVideoCameraButton: props.showNativeVideoCameraButton,
+            nativeCameraFacingMode: props.nativeCameraFacingMode,
+            showPanel: props.showPanel,
+            activePickerPanel: props.activePickerPanel,
+            disableLocalFiles: props.disableLocalFiles,
+            fileManagerSelectionType: props.fileManagerSelectionType,
+            note: props.note,
+            proudlyDisplayPoweredByUppy: props.proudlyDisplayPoweredByUppy,
+          }),
         h(
           Slide,
           null,

@lakesare lakesare marked this pull request as ready for review July 25, 2024 03:50
@lakesare lakesare changed the title Dashboard - convert some files to typescript 2 Dashboard - convert some files to typescript Jul 30, 2024
@lakesare lakesare changed the title Dashboard - convert some files to typescript Dashboard - convert some files to typescript - 2 Jul 30, 2024

folderInput: $TSFixMe
folderInput: HTMLInputElement | null = null
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
folderInput: HTMLInputElement | null = null
folderInput!: HTMLInputElement

And let's remove the optional chaining below

Copy link
Contributor Author

@lakesare lakesare Jul 31, 2024

Choose a reason for hiding this comment

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

🤔 why
I understand ref is unlilkely to be null when triggerFileInputClick() is called, but it is certainly null by default. So, if we want to say "we know this is not null now", we should instead do this.fileInput!.click().

But I wouldn't do that either, I think we shouldn't rely on react rendering cycles too much.

Copy link
Member

Choose a reason for hiding this comment

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

To reduce the output diff

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 mean - reducing the output diff is clearly not the value in itself, and we certainly shouldn't ! values in order to reduce the output diff.

I think your logic is "when I'm viewing the git history of the file, I should be able to ignore typescript commits, because they are highly unlikely to introduce real changes"?
If so - would you like me to subdivide this PR into some chunks, especially for changes that do change output js?

Copy link
Member

Choose a reason for hiding this comment

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

I guess there are several points for which it makes sense to me to keep the output diff as close to 0:

  • I believe there is value in not artificially increasing the output files size when (IMO in this case) it does not bring any value to our users, and very little to ourselves.
  • It makes reviewing so much easier.
  • As you said, it makes git blame crawling easier because the TS commit can be safely discarded.

Now I don't feel strongly about either of those points, feel free to ignore, but if you think it does make sense, I'm happy to help split the PR so we can land the uncontroversial TS-only changes first.

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 believe there is value in not artificially increasing the output files size when (IMO in this case) it does not bring any value to our users, and very little to ourselves.

I think we should certainly defend against null refs here - file size increase is a negligible cost for safety and a sense of "this will certainly not error out, no matter the react internal life cycles" for developers. To be clear - file size increase is not always a negligible cost, but this file size increase is certainly a negligible cost.

I'm pro doing:

  • ;(event.target as HTMLInputElement).value = '' as a separate PR
  • accept={this.props.allowedFileTypes?.join(', ')} as a separate PR

Because this is actual functionality change, and it's useful to create PRs that are focused on one change specifically (more motivation to actually test it in multiple browsers for both the author and reviewers; better git blames).

I'll PR those separately 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two PRs:

PRing these separately turned out to be a good idea, we needed to change these not only in Dashboard, but in several other plugins too.

@aduh95
Copy link
Member

aduh95 commented Jul 30, 2024

Let's try to reduce the changes in #5367 (comment) as close to 0 as possible

@lakesare
Copy link
Contributor Author

@aduh95, as far as I see the only change that I can remove there is to return the renderList function.

I don't believe it's difficult to review here - if you think the version with the renderList was an easier read, I can revert it.

@aduh95
Copy link
Member

aduh95 commented Jul 30, 2024

@lakesare also the changes in AddFiles. If we have a PR with passing tests and no output diff, the reviewing is very straight forward, and it can land almost immediately. The changes that require changing the output/distributed files require more scrutiny.

@lakesare
Copy link
Contributor Author

@lakesare also the changes in AddFiles. If we have a PR with passing tests and no output diff, the reviewing is very straight forward, and it can land almost immediately. The changes that require changing the output/distributed files require more scrutiny.

We don't need to land it immediately, these are genuine changes! Please review it as if the errors can crawl in, and then I'll throw my eye over it again before merging.
I understand the value of this diff when we're converting files that do not necessitate the change to functionality, but dashboard was relatively a mess type-wise, we did need to change stuff there.

Copy link
Contributor

@mifi mifi left a comment

Choose a reason for hiding this comment

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

This pr needs to be altered slightly after #5381 is merged, but other than that LGTM. Improving our flawed typings and readability outweighs bigger git diffs and a slight bundle size increase imo.

in the future we can probably increase our target browser supported versions so we don't have to transpile things like this:

-      this.folderInput?.click();
+      var _this$folderInput;
+      (_this$folderInput = this.folderInput) == null || _this$folderInput.click();

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.

3 participants