refactor: remove uploader from uppy hook and check by the upload size limit if we use tus plugin or just file post. additionally add a onBeforeUpload filter to set the custom uploader property.

This commit is contained in:
Derrick Hammer 2024-03-18 10:12:27 -04:00
parent 988780b25f
commit 3a4b40ef27
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 48 additions and 20 deletions

View File

@ -1,17 +1,13 @@
import Uppy, { type State, debugLogger } from "@uppy/core" import Uppy, {debugLogger, type State, UppyFile} from "@uppy/core"
import Tus from "@uppy/tus" import Tus from "@uppy/tus"
import toArray from "@uppy/utils/lib/toArray" import toArray from "@uppy/utils/lib/toArray"
import { import {type ChangeEvent, useCallback, useEffect, useMemo, useRef, useState} from "react"
type ChangeEvent, import DropTarget, {type DropTargetOptions} from "./uppy-dropzone"
useCallback, import {useSdk} from "~/components/lib/sdk-context.js";
useEffect, import UppyFileUpload from "~/components/lib/uppy-file-upload.js";
useMemo, import {Sdk} from "@lumeweb/portal-sdk";
useRef,
useState
} from "react"
import DropTarget, { type DropTargetOptions } from "./uppy-dropzone"
const LISTENING_EVENTS = [ const LISTENING_EVENTS = [
"upload", "upload",
@ -23,12 +19,26 @@ const LISTENING_EVENTS = [
] as const ] as const
export function useUppy({ export function useUppy({
uploader,
endpoint endpoint
}: { }: {
uploader: "tus"
endpoint: string endpoint: string
}) { }) {
const sdk = useSdk()
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();
}, []);
const inputRef = useRef<HTMLInputElement>(null) const inputRef = useRef<HTMLInputElement>(null)
const [targetRef, _setTargetRef] = useState<HTMLElement | null>(null) const [targetRef, _setTargetRef] = useState<HTMLElement | null>(null)
const uppyInstance = useRef<Uppy>() const uppyInstance = useRef<Uppy>()
@ -82,7 +92,17 @@ export function useUppy({
useEffect(() => { useEffect(() => {
if (!targetRef) return if (!targetRef) return
const uppy = new Uppy({ logger: debugLogger }).use(DropTarget, { const uppy = new Uppy({
logger: debugLogger,
onBeforeUpload: (files) => {
for (const file of Object.entries(files)) {
// @ts-ignore
file[1].uploader = file[1].size > uploadLimit ? "tus" : "file";
}
return true;
},
}).use(DropTarget, {
target: targetRef target: targetRef
} as DropTargetOptions) } as DropTargetOptions)
@ -109,11 +129,19 @@ export function useUppy({
} }
}) })
switch (uploader) {
case "tus": uppy.use(UppyFileUpload, { sdk: sdk as Sdk })
let useTus = false;
uppyInstance.current?.getFiles().forEach((file) => {
if (file.size > uploadLimit) {
useTus = true;
}
})
if (useTus) {
uppy.use(Tus, { endpoint: endpoint, limit: 6 }) uppy.use(Tus, { endpoint: endpoint, limit: 6 })
break
default:
} }
uppy.on("complete", (result) => { uppy.on("complete", (result) => {
@ -148,7 +176,7 @@ export function useUppy({
}) })
} }
setState("idle") setState("idle")
}, [targetRef, endpoint, uploader]) }, [targetRef, endpoint, uploadLimit])
useEffect(() => { useEffect(() => {
return () => { return () => {