2020-07-12 22:42:32 +00:00
|
|
|
import axios from 'axios';
|
|
|
|
import getFileUploadUrl from '@/api/server/files/getFileUploadUrl';
|
2020-07-12 21:20:37 +00:00
|
|
|
import tw from 'twin.macro';
|
2022-06-20 18:16:42 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2020-09-10 04:07:57 +00:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2020-07-12 21:20:37 +00:00
|
|
|
import styled from 'styled-components/macro';
|
2020-08-23 05:10:16 +00:00
|
|
|
import { ModalMask } from '@/components/elements/Modal';
|
|
|
|
import Fade from '@/components/elements/Fade';
|
|
|
|
import useEventListener from '@/plugins/useEventListener';
|
2020-08-23 05:35:53 +00:00
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import useFlash 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-09-13 17:33:12 +00:00
|
|
|
import { WithClassname } from '@/components/types';
|
2022-06-20 18:16:42 +00:00
|
|
|
import Portal from '@/components/elements/Portal';
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2020-08-23 05:10:16 +00:00
|
|
|
const InnerContainer = styled.div`
|
2022-06-26 19:13:52 +00:00
|
|
|
max-width: 600px;
|
|
|
|
${tw`bg-black w-full border-4 border-primary-500 border-dashed rounded p-10 mx-10`}
|
2020-07-12 21:20:37 +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-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
2020-08-23 05:36:53 +00:00
|
|
|
const { mutate } = useFileManagerSwr();
|
2020-08-23 05:35:53 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const directory = ServerContext.useStoreState((state) => state.files.directory);
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEventListener(
|
|
|
|
'dragenter',
|
|
|
|
(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
setVisible(true);
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2020-08-23 05:10:16 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
useEventListener(
|
|
|
|
'dragexit',
|
|
|
|
(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
setVisible(false);
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2020-07-12 22:42:32 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-08-23 05:10:16 +00:00
|
|
|
if (!visible) return;
|
2020-07-12 22:42:32 +00:00
|
|
|
|
2020-08-23 05:10:16 +00:00
|
|
|
const hide = () => setVisible(false);
|
2020-07-12 22:42:32 +00:00
|
|
|
|
2020-08-23 05:10:16 +00:00
|
|
|
window.addEventListener('keydown', hide);
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('keydown', hide);
|
|
|
|
};
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [visible]);
|
2020-07-12 21:20:37 +00:00
|
|
|
|
2020-09-10 04:07:57 +00:00
|
|
|
const onFileSubmission = (files: FileList) => {
|
2020-08-23 05:35:53 +00:00
|
|
|
const form = new FormData();
|
2022-06-26 19:13:52 +00:00
|
|
|
Array.from(files).forEach((file) => form.append('files', file));
|
2020-07-12 22:42:32 +00:00
|
|
|
|
2020-08-23 05:35:53 +00:00
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('files');
|
2020-07-12 22:42:32 +00:00
|
|
|
getFileUploadUrl(uuid)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((url) =>
|
|
|
|
axios.post(`${url}&directory=${directory}`, form, {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data',
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
2020-08-23 05:36:53 +00:00
|
|
|
.then(() => mutate())
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-07-12 22:42:32 +00:00
|
|
|
console.error(error);
|
2020-08-23 05:35:53 +00:00
|
|
|
clearAndAddHttpError({ error, key: 'files' });
|
|
|
|
})
|
|
|
|
.then(() => setVisible(false))
|
|
|
|
.then(() => setLoading(false));
|
2020-07-12 21:20:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-06-20 18:16:42 +00:00
|
|
|
<Portal>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Fade appear in={visible} timeout={75} key={'upload_modal_mask'} unmountOnExit>
|
2022-06-20 18:16:42 +00:00
|
|
|
<ModalMask
|
|
|
|
onClick={() => setVisible(false)}
|
2022-06-26 19:13:52 +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-06-20 18:16:42 +00:00
|
|
|
setVisible(false);
|
|
|
|
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);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div css={tw`w-full flex items-center justify-center`} style={{ pointerEvents: 'none' }}>
|
|
|
|
<InnerContainer>
|
2022-06-26 19:13:52 +00:00
|
|
|
<p css={tw`text-lg text-neutral-200 text-center`}>Drag and drop files to upload.</p>
|
2022-06-20 18:16:42 +00:00
|
|
|
</InnerContainer>
|
|
|
|
</div>
|
|
|
|
</ModalMask>
|
|
|
|
</Fade>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay visible={loading} size={'large'} fixed />
|
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-06-26 19:13:52 +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;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Button
|
2020-09-13 17:33:12 +00:00
|
|
|
className={className}
|
2020-09-10 04:07:57 +00:00
|
|
|
onClick={() => {
|
2022-06-26 19:13:52 +00:00
|
|
|
fileUploadInput.current ? fileUploadInput.current.click() : setVisible(true);
|
2020-09-10 04:07:57 +00:00
|
|
|
}}
|
|
|
|
>
|
2020-07-12 21:20:37 +00:00
|
|
|
Upload
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|