2024-03-26 13:42:18 +00:00
|
|
|
import Uppy, { debugLogger, FailedUppyFile, type State, type UppyFile } from "@uppy/core";
|
2024-03-22 12:47:50 +00:00
|
|
|
|
|
|
|
import Tus from "@uppy/tus";
|
|
|
|
import toArray from "@uppy/utils/lib/toArray";
|
|
|
|
|
|
|
|
import {
|
|
|
|
type ChangeEvent,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from "react";
|
|
|
|
import DropTarget, { type DropTargetOptions } from "./uppy-dropzone";
|
|
|
|
import { useSdk } from "~/components/lib/sdk-context";
|
|
|
|
import UppyFileUpload from "~/components/lib/uppy-file-upload";
|
|
|
|
import { PROTOCOL_S5, type Sdk } from "@lumeweb/portal-sdk";
|
|
|
|
import type { S5Client, HashProgressEvent } from "@lumeweb/s5-js";
|
2024-03-27 06:46:12 +00:00
|
|
|
import { useInvalidate } from "@refinedev/core";
|
2024-03-12 12:42:17 +00:00
|
|
|
|
|
|
|
const LISTENING_EVENTS = [
|
2024-03-28 23:14:35 +00:00
|
|
|
"upload-progress",
|
2024-03-12 12:42:17 +00:00
|
|
|
"upload",
|
|
|
|
"upload-success",
|
|
|
|
"upload-error",
|
|
|
|
"file-added",
|
|
|
|
"file-removed",
|
2024-03-22 12:47:50 +00:00
|
|
|
"files-added",
|
2024-03-29 00:13:20 +00:00
|
|
|
"preprocess-progress"
|
2024-03-22 12:47:50 +00:00
|
|
|
] as const;
|
2024-03-12 12:42:17 +00:00
|
|
|
|
2024-03-19 11:18:31 +00:00
|
|
|
export function useUppy() {
|
2024-03-27 06:46:12 +00:00
|
|
|
const invalidate = useInvalidate()
|
2024-03-22 12:47:50 +00:00
|
|
|
const sdk = useSdk();
|
2024-03-18 14:12:27 +00:00
|
|
|
|
2024-03-22 12:47:50 +00:00
|
|
|
const [uploadLimit, setUploadLimit] = useState<number>(0);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
async function getUploadLimit() {
|
|
|
|
try {
|
|
|
|
const limit = await sdk.account!().uploadLimit();
|
|
|
|
setUploadLimit(limit);
|
|
|
|
} catch (err) {
|
|
|
|
console.log("Error occured while fetching upload limit", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getUploadLimit();
|
|
|
|
}, [sdk.account]);
|
|
|
|
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
|
|
const [targetRef, _setTargetRef] = useState<HTMLElement | null>(null);
|
|
|
|
const uppyInstance = useRef<Uppy>();
|
2024-03-12 12:42:17 +00:00
|
|
|
const setRef = useCallback(
|
|
|
|
(element: HTMLElement | null) => _setTargetRef(element),
|
2024-03-22 12:47:50 +00:00
|
|
|
[],
|
|
|
|
);
|
|
|
|
const [, setUppyState] = useState<State>();
|
2024-03-12 14:32:00 +00:00
|
|
|
const [state, setState] = useState<
|
|
|
|
"completed" | "idle" | "initializing" | "error" | "uploading"
|
2024-03-22 12:47:50 +00:00
|
|
|
>("initializing");
|
2024-03-12 12:42:17 +00:00
|
|
|
|
2024-03-28 23:09:14 +00:00
|
|
|
const [inputProps, setInputProps] = useState<{
|
2024-03-22 12:47:50 +00:00
|
|
|
ref: typeof inputRef;
|
|
|
|
type: "file";
|
|
|
|
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|
2024-03-28 23:09:14 +00:00
|
|
|
>();
|
2024-03-26 13:42:18 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
const [failedFiles, setFailedFiles] = useState<FailedUppyFile<Record<string, any>, Record<string, any>>[]>([])
|
2024-03-12 12:42:17 +00:00
|
|
|
const getRootProps = useMemo(
|
|
|
|
() => () => {
|
|
|
|
return {
|
|
|
|
ref: setRef,
|
|
|
|
onClick: () => {
|
|
|
|
if (inputRef.current) {
|
|
|
|
//@ts-expect-error -- dumb html
|
2024-03-22 12:47:50 +00:00
|
|
|
inputRef.current.value = null;
|
|
|
|
inputRef.current.click();
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|
|
|
|
},
|
2024-03-22 12:47:50 +00:00
|
|
|
role: "presentation",
|
|
|
|
};
|
2024-03-12 12:42:17 +00:00
|
|
|
},
|
2024-03-22 12:47:50 +00:00
|
|
|
[setRef],
|
|
|
|
);
|
2024-03-12 12:42:17 +00:00
|
|
|
const removeFile = useCallback(
|
|
|
|
(id: string) => {
|
2024-03-22 12:47:50 +00:00
|
|
|
uppyInstance.current?.removeFile(id);
|
2024-03-12 12:42:17 +00:00
|
|
|
},
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-03-22 12:47:50 +00:00
|
|
|
[targetRef, uppyInstance],
|
|
|
|
);
|
2024-03-12 12:42:17 +00:00
|
|
|
const cancelAll = useCallback(
|
2024-03-12 14:32:00 +00:00
|
|
|
() => uppyInstance.current?.cancelAll({ reason: "user" }),
|
2024-03-12 12:42:17 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2024-03-22 12:47:50 +00:00
|
|
|
[targetRef, uppyInstance],
|
|
|
|
);
|
2024-03-12 12:42:17 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-03-22 12:47:50 +00:00
|
|
|
if (!targetRef) return;
|
|
|
|
|
|
|
|
const tusPreprocessor = async (fileIDs: string[]) => {
|
|
|
|
for (const fileID of fileIDs) {
|
|
|
|
const file = uppyInstance.current?.getFile(fileID) as UppyFile;
|
|
|
|
// @ts-ignore
|
|
|
|
if (file.uploader === "tus") {
|
|
|
|
const hashProgressCb = (event: HashProgressEvent) => {
|
|
|
|
uppyInstance.current?.emit("preprocess-progress", file, {
|
|
|
|
mode: "determinate",
|
|
|
|
message: "Hashing file...",
|
2024-03-29 00:38:31 +00:00
|
|
|
value: Math.round((event.bytes / event.total) * 100),
|
2024-03-22 12:47:50 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
const options = await sdk.protocols!()
|
|
|
|
.get<S5Client>(PROTOCOL_S5)
|
|
|
|
.getSdk()
|
|
|
|
.getTusOptions(
|
|
|
|
file.data as File,
|
|
|
|
{},
|
|
|
|
{ onHashProgress: hashProgressCb },
|
|
|
|
);
|
|
|
|
uppyInstance.current?.setFileState(fileID, {
|
|
|
|
tus: options,
|
|
|
|
meta: {
|
|
|
|
...options.metadata,
|
|
|
|
...file.meta,
|
|
|
|
},
|
|
|
|
});
|
2024-03-18 19:32:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-22 12:47:50 +00:00
|
|
|
};
|
2024-03-18 19:32:47 +00:00
|
|
|
|
2024-03-18 14:12:27 +00:00
|
|
|
const uppy = new Uppy({
|
2024-03-22 12:47:50 +00:00
|
|
|
logger: debugLogger,
|
|
|
|
onBeforeUpload: (files) => {
|
|
|
|
for (const file of Object.entries(files)) {
|
|
|
|
// @ts-ignore
|
|
|
|
file[1].uploader = file[1].size > uploadLimit ? "tus" : "file";
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2024-03-18 14:12:27 +00:00
|
|
|
}).use(DropTarget, {
|
2024-03-22 12:47:50 +00:00
|
|
|
target: targetRef,
|
|
|
|
} as DropTargetOptions);
|
2024-03-12 12:42:17 +00:00
|
|
|
|
2024-03-22 12:47:50 +00:00
|
|
|
uppyInstance.current = uppy;
|
2024-03-12 12:42:17 +00:00
|
|
|
setInputProps({
|
|
|
|
ref: inputRef,
|
|
|
|
type: "file",
|
|
|
|
onChange: (event) => {
|
2024-03-22 12:47:50 +00:00
|
|
|
const files = toArray(event.target.files);
|
2024-03-12 12:42:17 +00:00
|
|
|
if (files.length > 0) {
|
2024-03-22 12:47:50 +00:00
|
|
|
uppyInstance.current?.log("[DragDrop] Files selected through input");
|
|
|
|
uppyInstance.current?.addFiles(files);
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 16:37:22 +00:00
|
|
|
uppy.iteratePlugins((plugin) => {
|
2024-03-22 12:47:50 +00:00
|
|
|
uppy.removePlugin(plugin);
|
2024-03-18 16:37:22 +00:00
|
|
|
});
|
|
|
|
|
2024-03-22 12:47:50 +00:00
|
|
|
uppy.use(UppyFileUpload, { sdk: sdk as Sdk });
|
2024-03-18 16:37:22 +00:00
|
|
|
|
2024-03-22 12:47:50 +00:00
|
|
|
let useTus = false;
|
2024-03-18 16:37:22 +00:00
|
|
|
|
2024-03-22 12:47:50 +00:00
|
|
|
uppyInstance.current?.getFiles().forEach((file) => {
|
|
|
|
if (file.size > uploadLimit) {
|
|
|
|
useTus = true;
|
2024-03-18 16:37:22 +00:00
|
|
|
}
|
2024-03-22 12:47:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (useTus) {
|
2024-03-29 03:39:46 +00:00
|
|
|
uppy.use(Tus, { limit: 1, parallelUploads: 1 });
|
2024-03-22 12:47:50 +00:00
|
|
|
uppy.addPreProcessor(tusPreprocessor);
|
|
|
|
}
|
2024-03-18 16:37:22 +00:00
|
|
|
|
2024-03-12 12:42:17 +00:00
|
|
|
// We clear the input after a file is selected, because otherwise
|
|
|
|
// change event is not fired in Chrome and Safari when a file
|
|
|
|
// with the same name is selected.
|
|
|
|
// ___Why not use value="" on <input/> instead?
|
|
|
|
// Because if we use that method of clearing the input,
|
|
|
|
// Chrome will not trigger change if we drop the same file twice (Issue #768).
|
|
|
|
// @ts-expect-error TS freaks out, but this is fine
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
2024-03-22 12:47:50 +00:00
|
|
|
event.target.value = null;
|
|
|
|
},
|
|
|
|
});
|
2024-03-18 19:32:47 +00:00
|
|
|
|
2024-03-12 12:42:17 +00:00
|
|
|
uppy.on("complete", (result) => {
|
|
|
|
if (result.failed.length === 0) {
|
2024-03-22 12:47:50 +00:00
|
|
|
console.log("Upload successful üòÄ");
|
|
|
|
setState("completed");
|
2024-03-12 12:42:17 +00:00
|
|
|
} else {
|
2024-03-22 12:47:50 +00:00
|
|
|
console.warn("Upload failed üòû");
|
|
|
|
setState("error");
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|
2024-03-22 12:47:50 +00:00
|
|
|
console.log("successful files:", result.successful);
|
|
|
|
console.log("failed files:", result.failed);
|
2024-03-26 13:42:18 +00:00
|
|
|
setFailedFiles(result.failed);
|
2024-03-27 06:46:12 +00:00
|
|
|
invalidate({
|
|
|
|
resource: "file",
|
|
|
|
invalidates: ["list"]
|
|
|
|
})
|
2024-03-22 12:47:50 +00:00
|
|
|
});
|
2024-03-12 12:42:17 +00:00
|
|
|
|
2024-03-12 14:32:00 +00:00
|
|
|
const setStateCb = (event: (typeof LISTENING_EVENTS)[number]) => {
|
|
|
|
switch (event) {
|
|
|
|
case "upload":
|
2024-03-28 23:14:35 +00:00
|
|
|
case "upload-progress":
|
2024-03-29 00:13:20 +00:00
|
|
|
case "preprocess-progress":
|
2024-03-22 12:47:50 +00:00
|
|
|
setState("uploading");
|
|
|
|
break;
|
2024-03-12 14:32:00 +00:00
|
|
|
case "upload-error":
|
2024-03-22 12:47:50 +00:00
|
|
|
setState("error");
|
|
|
|
break;
|
2024-03-28 23:14:35 +00:00
|
|
|
case "upload-success":
|
|
|
|
setState("completed");
|
|
|
|
break;
|
2024-03-12 14:32:00 +00:00
|
|
|
default:
|
2024-03-22 12:47:50 +00:00
|
|
|
break;
|
2024-03-12 14:32:00 +00:00
|
|
|
}
|
2024-03-22 12:47:50 +00:00
|
|
|
setUppyState(uppy.getState());
|
|
|
|
};
|
2024-03-12 12:42:17 +00:00
|
|
|
|
|
|
|
for (const event of LISTENING_EVENTS) {
|
2024-03-12 14:32:00 +00:00
|
|
|
uppy.on(event, function cb() {
|
2024-03-22 12:47:50 +00:00
|
|
|
setStateCb(event);
|
|
|
|
});
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|
2024-03-22 12:47:50 +00:00
|
|
|
setState("idle");
|
2024-03-28 23:28:44 +00:00
|
|
|
}, [targetRef, invalidate, sdk, uploadLimit]);
|
2024-03-12 12:42:17 +00:00
|
|
|
|
2024-03-12 14:36:44 +00:00
|
|
|
useEffect(() => {
|
2024-03-12 12:42:17 +00:00
|
|
|
return () => {
|
2024-03-22 12:47:50 +00:00
|
|
|
uppyInstance.current?.cancelAll({ reason: "unmount" });
|
|
|
|
uppyInstance.current?.logout();
|
|
|
|
uppyInstance.current?.close();
|
|
|
|
uppyInstance.current = undefined;
|
|
|
|
};
|
|
|
|
}, []);
|
2024-03-12 12:42:17 +00:00
|
|
|
return {
|
2024-03-12 14:32:00 +00:00
|
|
|
getFiles: () => uppyInstance.current?.getFiles() ?? [],
|
2024-03-12 12:42:17 +00:00
|
|
|
error: uppyInstance.current?.getState,
|
2024-03-26 13:42:18 +00:00
|
|
|
failedFiles,
|
2024-03-12 12:42:17 +00:00
|
|
|
state,
|
|
|
|
upload: () =>
|
|
|
|
uppyInstance.current?.upload() ??
|
|
|
|
new Error("[useUppy] Uppy has not initialized yet."),
|
|
|
|
getInputProps: () => inputProps,
|
|
|
|
getRootProps,
|
|
|
|
removeFile,
|
2024-03-22 12:47:50 +00:00
|
|
|
cancelAll,
|
|
|
|
};
|
2024-03-12 12:42:17 +00:00
|
|
|
}
|