From 94406b7f13f6f49afbadf3caa82378b2053b7d0d Mon Sep 17 00:00:00 2001 From: Juan Di Toro Date: Tue, 12 Mar 2024 15:32:00 +0100 Subject: [PATCH] fix: state and rendering --- app/components/dashboard-layout.tsx | 173 ++++++++++++++-------------- app/components/lib/uppy.ts | 52 ++++++--- 2 files changed, 125 insertions(+), 100 deletions(-) diff --git a/app/components/dashboard-layout.tsx b/app/components/dashboard-layout.tsx index 422d765..66c6c01 100644 --- a/app/components/dashboard-layout.tsx +++ b/app/components/dashboard-layout.tsx @@ -54,7 +54,17 @@ export const DashboardLayout = ({ children }: React.PropsWithChildren<{}>) => {

Privacy

Ownership

- + + + + + + + + {children} @@ -89,97 +99,92 @@ export const DashboardLayout = ({ children }: React.PropsWithChildren<{}>) => { ) } -const UploadFileModal = () => { - const { getRootProps, getInputProps, files, upload, removeFile, cancelAll } = - useUppy({ - uploader: "tus", - endpoint: import.meta.env.VITE_PUBLIC_TUS_ENDPOINT - }) +const UploadFileForm = () => { + const { + getRootProps, + getInputProps, + getFiles, + upload, + state, + removeFile, + cancelAll + } = useUppy({ + uploader: "tus", + endpoint: import.meta.env.VITE_PUBLIC_TUS_ENDPOINT + }) - const isUploading = - files.length > 0 - ? files.map((file) => file.progress?.uploadStarted != null).includes(true) - : false - const isCompleted = - files.length > 0 - ? files - .map((file) => file.progress?.uploadComplete) - .reduce((acc, cur) => acc && cur, true) - : false - const hasStarted = isUploading || isCompleted + console.log({ state, files: getFiles() }) + + const isUploading = state === "uploading" + const isCompleted = state === "completed" + const hasStarted = state !== "idle" && state !== "initializing" return ( - - - - - - - Upload Files - - {!hasStarted ? ( -
- - -

Drag & Drop Files or Browse

-
- ) : null} - -
- {files.map((file) => ( - { - removeFile(id) - }} - /> - ))} + <> + + Upload Files + + {!hasStarted ? ( +
+ + +

Drag & Drop Files or Browse

+ ) : null} - {hasStarted ? ( -
- - {isCompleted - ? "Upload completed" - : `${files.length} being uploaded`} -
- ) : null} +
+ {getFiles().map((file) => ( + { + removeFile(id) + }} + /> + ))} +
- {hasStarted ? ( - - - - ) : ( - - )} + + ) : null} - {hasStarted && isCompleted ? ( - - - - ) : null} - -
+ {isCompleted ? ( + + + + ) : null} + + {!hasStarted && !isCompleted && !isUploading ? ( + + ) : null} + ) } diff --git a/app/components/lib/uppy.ts b/app/components/lib/uppy.ts index 807b695..9c06abb 100644 --- a/app/components/lib/uppy.ts +++ b/app/components/lib/uppy.ts @@ -36,7 +36,10 @@ export function useUppy({ (element: HTMLElement | null) => _setTargetRef(element), [] ) - const [state, setState] = useState() + const [uppyState, setUppyState] = useState() + const [state, setState] = useState< + "completed" | "idle" | "initializing" | "error" | "uploading" + >("initializing") const [inputProps, setInputProps] = useState< | { @@ -71,7 +74,7 @@ export function useUppy({ [targetRef, uppyInstance] ) const cancelAll = useCallback( - () => uppyInstance.current?.cancelAll(), + () => uppyInstance.current?.cancelAll({ reason: "user" }), // eslint-disable-next-line react-hooks/exhaustive-deps [targetRef, uppyInstance] ) @@ -116,36 +119,53 @@ export function useUppy({ uppy.on("complete", (result) => { if (result.failed.length === 0) { console.log("Upload successful üòÄ") + setState("completed") } else { console.warn("Upload failed üòû") + setState("error") } console.log("successful files:", result.successful) console.log("failed files:", result.failed) }) - const setStateCb = () => { - setState(uppy.getState()) + const setStateCb = (event: (typeof LISTENING_EVENTS)[number]) => { + switch (event) { + case "upload": + setState("uploading") + break + case "upload-error": + setState("error") + break + default: + break + } + setUppyState(uppy.getState()) } for (const event of LISTENING_EVENTS) { - uppy.on(event, setStateCb) + uppy.on(event, function cb() { + setStateCb(event) + }) } + setState("idle") return () => { - for (const event of ["complete", ...LISTENING_EVENTS]) { - uppyInstance.current?.off( - event as "complete" & keyof typeof LISTENING_EVENTS, - //@ts-expect-error -- huh? typescript wtf - setStateCb - ) - } - uppyInstance.current?.close() - uppyInstance.current = undefined + // for (const event of ["complete", ...LISTENING_EVENTS]) { + // uppyInstance.current?.off( + // event as "complete" & keyof typeof LISTENING_EVENTS, + // //@ts-expect-error -- huh? typescript wtf + // setStateCb + // ) + // } + // uppyInstance.current?.cancelAll({ reason: "unmount" }) + // uppyInstance.current?.logout() + // uppyInstance.current?.close() + // uppyInstance.current = undefined } }, [targetRef, endpoint, uploader]) return { - files: uppyInstance.current?.getFiles() ?? [], + getFiles: () => uppyInstance.current?.getFiles() ?? [], error: uppyInstance.current?.getState, state, upload: () => @@ -154,6 +174,6 @@ export function useUppy({ getInputProps: () => inputProps, getRootProps, removeFile, - cancelAll, + cancelAll } }