Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Scope file input null-guard fix to ensu only

Prateek Sunal6bbe7ecfc42a
diff --git a/web/apps/ensu/src/components/utils/use-file-input.ts b/web/apps/ensu/src/components/utils/use-file-input.ts
new file mode 100644
index 0000000000..671f8efafc
--- /dev/null
+++ b/web/apps/ensu/src/components/utils/use-file-input.ts
@@ -0,0 +1,123 @@
+import { useCallback, useRef } from "react";
+
+interface UseFileInputParams {
+ /**
+ * If `true`, the file open dialog will ask the user to select directories.
+ * Otherwise it'll ask the user to select files (default).
+ */
+ directory?: boolean;
+ /**
+ * If specified, it'll restrict the type of files that the user can select
+ * by setting the "accept" attribute of the underlying HTML input element we
+ * use to surface the file selector dialog. For value of accept can be an
+ * extension or a MIME type (See
+ * https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept).
+ */
+ accept?: string;
+ /**
+ * A callback that is invoked when the user selects files.
+ *
+ * It will be passed the list of {@link File}s that the user selected.
+ *
+ * This will be a list even if the user selected directories - in that case,
+ * it will be the recursive list of files within this directory.
+ *
+ * If the user selected no items, then {@link onCancel} will be invoked.
+ */
+ onSelect: (selectedFiles: File[]) => void;
+ /**
+ * A callback that is invoked when the user cancels on the file / directory
+ * dialog.
+ */
+ onCancel: () => void;
+}
+
+interface UseFileInputResult {
+ /**
+ * A function to call to get the properties that should be passed to a dummy
+ * `input` element that needs to be created to anchor the select file
+ * dialog. This input HTML element is not going to be visible, but it needs
+ * to be part of the DOM for {@link openSelector} to have effect.
+ */
+ getInputProps: () => React.HTMLAttributes<HTMLInputElement>;
+ /**
+ * A function that can be called to open the select file / directory dialog.
+ */
+ openSelector: () => void;
+}
+
+/**
+ * Local ensu copy of file input hook to avoid affecting other apps.
+ */
+export const useFileInput = ({
+ directory,
+ accept,
+ onSelect,
+ onCancel,
+}: UseFileInputParams): UseFileInputResult => {
+ const inputRef = useRef<HTMLInputElement | null>(null);
+
+ const handleInputRef = useCallback(
+ (node: HTMLInputElement | null) => {
+ if (inputRef.current) {
+ inputRef.current.removeEventListener("cancel", onCancel);
+ }
+
+ inputRef.current = node;
+
+ if (inputRef.current) {
+ inputRef.current.addEventListener("cancel", onCancel);
+ }
+ },
+ [onCancel],
+ );
+
+ const openSelector = useCallback(() => {
+ const input = inputRef.current;
+ if (!input) return;
+
+ input.value = "";
+ input.click();
+ }, []);
+
+ const handleChange: React.ChangeEventHandler<HTMLInputElement> = (
+ event,
+ ) => {
+ const files = event.target.files;
+ if (files?.length) {
+ onSelect([...files]);
+ } else {
+ onCancel();
+ }
+ };
+
+ // [Note: webkitRelativePath]
+ //
+ // If the webkitdirectory attribute of an <input> HTML element is set then
+ // the File objects that we get will have `webkitRelativePath` property
+ // containing the relative path to the selected directory.
+ //
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
+ //
+ // These paths use the POSIX path separator ("/").
+ // https://stackoverflow.com/questions/62806233/when-using-webkitrelativepath-is-the-path-separator-operating-system-specific
+ //
+ const directoryOpts = directory
+ ? { directory: "", webkitdirectory: "" }
+ : {};
+
+ const getInputProps = useCallback(
+ () => ({
+ type: "file",
+ multiple: true,
+ style: { display: "none" },
+ ...directoryOpts,
+ ref: handleInputRef,
+ onChange: handleChange,
+ ...(accept && { accept }),
+ }),
+ [directoryOpts, accept, handleChange, handleInputRef],
+ );
+
+ return { getInputProps, openSelector };
+};
diff --git a/web/apps/ensu/src/pages/chat.tsx b/web/apps/ensu/src/pages/chat.tsx
index 383bee2b76..ab1cb9d63d 100644
--- a/web/apps/ensu/src/pages/chat.tsx
+++ b/web/apps/ensu/src/pages/chat.tsx
@@ -14,6 +14,7 @@ import { ChatComposer } from "components/chat/ChatComposer";
import { ChatDialogs } from "components/chat/ChatDialogs";
import { ChatMessageList } from "components/chat/ChatMessageList";
import { ChatSidebar } from "components/chat/ChatSidebar";
+import { useFileInput } from "components/utils/use-file-input";
import { savedLocalUser } from "ente-accounts/services/accounts-db";
import { openAccountsManagePasskeysPage } from "ente-accounts/services/passkey";
import { NavbarBase } from "ente-base/components/Navbar";
@@ -22,7 +23,6 @@ import log from "ente-base/log";
import { savedLogs } from "ente-base/log-web";
import { savedAuthToken } from "ente-base/token";
import { saveStringAsFile } from "ente-base/utils/web";
-import { useFileInput } from "ente-gallery/components/utils/use-file-input";
import { type NotificationAttributes } from "ente-new/photos/components/Notification";
import { useRouter } from "next/router";
import React, {
diff --git a/web/packages/gallery/components/utils/use-file-input.ts b/web/packages/gallery/components/utils/use-file-input.ts
index 3ac6ca9915..a137b34ad3 100644
--- a/web/packages/gallery/components/utils/use-file-input.ts
+++ b/web/packages/gallery/components/utils/use-file-input.ts
@@ -1,4 +1,4 @@
-import { useCallback, useRef } from "react";
+import { useCallback, useEffect, useRef } from "react";
interface UseFileInputParams {
/**
@@ -50,7 +50,7 @@ interface UseFileInputResult {
* Wrap a open file selector into an easy to use package.
*
* Returns a {@link UseFileInputResult} which contains a function to get the
- * properties for an input element, a function to open the file selector, and the
+ * props for an input element, a function to open the file selector, and the
* list of selected files.
*
* See the documentation of {@link UseFileInputParams} and
@@ -64,27 +64,21 @@ export const useFileInput = ({
}: UseFileInputParams): UseFileInputResult => {
const inputRef = useRef<HTMLInputElement | null>(null);
- const handleInputRef = useCallback(
- (node: HTMLInputElement | null) => {
- if (inputRef.current) {
- inputRef.current.removeEventListener("cancel", onCancel);
- }
-
- inputRef.current = node;
-
- if (inputRef.current) {
- inputRef.current.addEventListener("cancel", onCancel);
- }
- },
- [onCancel],
- );
+ useEffect(() => {
+ // React (as of 19) doesn't support attaching the onCancel event handler
+ // via props, so do it using its ref.
+ //
+ // https://github.com/facebook/react/issues/27858
+ inputRef.current!.addEventListener("cancel", onCancel);
+ return () => {
+ // Use optional chaining to avoid spurious errors during HMR.
+ inputRef.current?.removeEventListener("cancel", onCancel);
+ };
+ }, [onCancel]);
const openSelector = useCallback(() => {
- const input = inputRef.current;
- if (!input) return;
-
- input.value = "";
- input.click();
+ inputRef.current!.value = "";
+ inputRef.current!.click();
}, []);
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (
@@ -119,11 +113,11 @@ export const useFileInput = ({
multiple: true,
style: { display: "none" },
...directoryOpts,
- ref: handleInputRef,
+ ref: inputRef,
onChange: handleChange,
...(accept && { accept }),
}),
- [directoryOpts, accept, handleChange, handleInputRef],
+ [directoryOpts, accept, handleChange],
);
return { getInputProps, openSelector };