2022-11-25 20:25:03 +00:00
|
|
|
import { CloudUploadIcon } from '@heroicons/react/outline';
|
|
|
|
import { useSignal } from '@preact/signals-react';
|
2020-07-12 22:42:32 +00:00
|
|
|
import axios from 'axios';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useRef } from 'react';
|
2020-07-12 21:20:37 +00:00
|
|
|
import tw from 'twin.macro';
|
2022-11-25 20:25:03 +00:00
|
|
|
|
|
|
|
import getFileUploadUrl from '@/api/server/files/getFileUploadUrl';
|
2022-06-20 18:16:42 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-08-23 05:10:16 +00:00
|
|
|
import { ModalMask } from '@/components/elements/Modal';
|
2022-11-25 20:25:03 +00:00
|
|
|
import Portal from '@/components/elements/Portal';
|
|
|
|
import FadeTransition from '@/components/elements/transitions/FadeTransition';
|
|
|
|
import type { WithClassname } from '@/components/types';
|
2020-08-23 05:10:16 +00:00
|
|
|
import useEventListener from '@/plugins/useEventListener';
|
2022-07-24 22:22:50 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
2020-08-23 05:36:53 +00:00
|
|
|
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
2020-08-24 17:26:05 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2022-07-24 21:18:32 +00:00
|
|
|
function isFileOrDirectory(event: DragEvent): boolean {
|
|
|
|
if (!event.dataTransfer?.types) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
return event.dataTransfer.types.some(value => value.toLowerCase() === 'files');
|
2022-07-24 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 17:33:12 +00:00
|
|
|
export default ({ className }: WithClassname) => {
|
2020-09-10 04:07:57 +00:00
|
|
|
const fileUploadInput = useRef<HTMLInputElement>(null);
|
2022-11-21 20:58:55 +00:00
|
|
|
|
|
|
|
const visible = useSignal(false);
|
|
|
|
const timeouts = useSignal<NodeJS.Timeout[]>([]);
|
|
|
|
|
2020-08-23 05:36:53 +00:00
|
|
|
const { mutate } = useFileManagerSwr();
|
2022-07-24 22:22:50 +00:00
|
|
|
const { addError, clearAndAddHttpError } = useFlashKey('files');
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2022-11-21 20:58:55 +00:00
|
|
|
const { clearFileUploads, removeFileUpload, pushFileUpload, setUploadProgress } = ServerContext.useStoreActions(
|
2022-11-25 20:25:03 +00:00
|
|
|
actions => actions.files,
|
2022-07-24 22:50:47 +00:00
|
|
|
);
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEventListener(
|
|
|
|
'dragenter',
|
2022-11-25 20:25:03 +00:00
|
|
|
e => {
|
2022-07-24 22:22:50 +00:00
|
|
|
e.preventDefault();
|
2022-06-26 19:13:52 +00:00
|
|
|
e.stopPropagation();
|
2022-07-24 22:22:50 +00:00
|
|
|
if (isFileOrDirectory(e)) {
|
2022-11-21 20:58:55 +00:00
|
|
|
visible.value = true;
|
2022-07-24 21:18:32 +00:00
|
|
|
}
|
2022-06-26 19:13:52 +00:00
|
|
|
},
|
2022-11-25 20:25:03 +00:00
|
|
|
{ capture: true },
|
2022-06-26 19:13:52 +00:00
|
|
|
);
|
2020-07-12 22:42:32 +00:00
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
useEventListener('dragexit', () => (visible.value = false), { capture: true });
|
2022-07-24 22:22:50 +00:00
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
useEventListener('keydown', () => (visible.value = false));
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2022-07-24 21:18:32 +00:00
|
|
|
useEffect(() => {
|
2022-11-21 20:58:55 +00:00
|
|
|
return () => timeouts.value.forEach(clearTimeout);
|
2022-07-24 21:18:32 +00:00
|
|
|
}, []);
|
2020-07-12 22:42:32 +00:00
|
|
|
|
2022-07-24 22:50:47 +00:00
|
|
|
const onUploadProgress = (data: ProgressEvent, name: string) => {
|
2022-11-21 20:58:55 +00:00
|
|
|
setUploadProgress({ name, loaded: data.loaded });
|
2022-07-24 22:50:47 +00:00
|
|
|
};
|
2022-07-24 22:22:50 +00:00
|
|
|
|
2022-07-24 22:50:47 +00:00
|
|
|
const onFileSubmission = (files: FileList) => {
|
2022-07-24 22:22:50 +00:00
|
|
|
clearAndAddHttpError();
|
|
|
|
const list = Array.from(files);
|
2022-11-25 20:25:03 +00:00
|
|
|
if (list.some(file => !file.size || (!file.type && file.size === 4096))) {
|
2022-07-24 22:22:50 +00:00
|
|
|
return addError('Folder uploads are not supported at this time.', 'Error');
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const uploads = list.map(file => {
|
2022-11-21 20:58:55 +00:00
|
|
|
const controller = new AbortController();
|
2022-12-04 23:36:53 +00:00
|
|
|
pushFileUpload({
|
|
|
|
name: file.name,
|
|
|
|
data: { abort: controller, loaded: 0, total: file.size },
|
|
|
|
});
|
2022-11-21 20:58:55 +00:00
|
|
|
|
2022-07-24 22:50:47 +00:00
|
|
|
return () =>
|
2022-12-12 21:09:22 +00:00
|
|
|
getFileUploadUrl(uuid).then(url =>
|
2022-12-04 23:36:53 +00:00
|
|
|
axios
|
|
|
|
.post(
|
|
|
|
url,
|
|
|
|
{ files: file },
|
|
|
|
{
|
|
|
|
signal: controller.signal,
|
|
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
|
|
params: { directory },
|
2022-12-12 21:09:22 +00:00
|
|
|
onUploadProgress: data => onUploadProgress(data, file.name),
|
|
|
|
},
|
2022-12-04 23:36:53 +00:00
|
|
|
)
|
2022-12-12 21:09:22 +00:00
|
|
|
.then(() => timeouts.value.push(setTimeout(() => removeFileUpload(file.name), 500))),
|
2022-07-24 22:50:47 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
Promise.all(uploads.map(fn => fn()))
|
2020-08-23 05:36:53 +00:00
|
|
|
.then(() => mutate())
|
2022-11-25 20:25:03 +00:00
|
|
|
.catch(error => {
|
2022-07-24 22:50:47 +00:00
|
|
|
clearFileUploads();
|
|
|
|
clearAndAddHttpError(error);
|
|
|
|
});
|
2020-07-12 21:20:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-20 18:16:42 +00:00
|
|
|
<Portal>
|
2022-11-25 20:25:03 +00:00
|
|
|
<FadeTransition show={visible.value} duration="duration-75" key="upload_modal_mask" appear unmount>
|
2022-06-20 18:16:42 +00:00
|
|
|
<ModalMask
|
2022-11-21 20:58:55 +00:00
|
|
|
onClick={() => (visible.value = false)}
|
2022-11-25 20:25:03 +00:00
|
|
|
onDragOver={e => e.preventDefault()}
|
|
|
|
onDrop={e => {
|
2022-06-20 18:16:42 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-09-10 04:07:57 +00:00
|
|
|
|
2022-11-21 20:58:55 +00:00
|
|
|
visible.value = false;
|
2022-11-25 20:25:03 +00:00
|
|
|
if (!e.dataTransfer?.files.length) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-10 04:07:57 +00:00
|
|
|
|
2022-06-20 18:16:42 +00:00
|
|
|
onFileSubmission(e.dataTransfer.files);
|
|
|
|
}}
|
|
|
|
>
|
2022-07-24 22:59:20 +00:00
|
|
|
<div className={'w-full flex items-center justify-center pointer-events-none'}>
|
|
|
|
<div
|
|
|
|
className={
|
|
|
|
'flex items-center space-x-4 bg-black w-full ring-4 ring-blue-200 ring-opacity-60 rounded p-6 mx-10 max-w-sm'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<CloudUploadIcon className={'w-10 h-10 flex-shrink-0'} />
|
|
|
|
<p className={'font-header flex-1 text-lg text-neutral-100 text-center'}>
|
|
|
|
Drag and drop files to upload.
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-06-20 18:16:42 +00:00
|
|
|
</div>
|
|
|
|
</ModalMask>
|
2022-11-25 20:25:03 +00:00
|
|
|
</FadeTransition>
|
2022-06-20 18:16:42 +00:00
|
|
|
</Portal>
|
2020-09-10 04:07:57 +00:00
|
|
|
<input
|
|
|
|
type={'file'}
|
|
|
|
ref={fileUploadInput}
|
|
|
|
css={tw`hidden`}
|
2022-11-25 20:25:03 +00:00
|
|
|
onChange={e => {
|
2020-09-10 04:07:57 +00:00
|
|
|
if (!e.currentTarget.files) return;
|
|
|
|
|
|
|
|
onFileSubmission(e.currentTarget.files);
|
|
|
|
if (fileUploadInput.current) {
|
|
|
|
fileUploadInput.current.files = null;
|
|
|
|
}
|
|
|
|
}}
|
2022-09-29 23:43:20 +00:00
|
|
|
multiple
|
2020-09-10 04:07:57 +00:00
|
|
|
/>
|
2022-07-24 21:18:32 +00:00
|
|
|
<Button className={className} onClick={() => fileUploadInput.current && fileUploadInput.current.click()}>
|
2020-07-12 21:20:37 +00:00
|
|
|
Upload
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|