parent
a4f6870518
commit
df9a7f71f9
6 changed files with 92 additions and 50 deletions
|
@ -1,11 +1,12 @@
|
|||
import React, { useContext, useEffect, useState } from 'react';
|
||||
import React, { useContext, useEffect } from 'react';
|
||||
import { ServerContext } from '@/state/server';
|
||||
import { CloudUploadIcon } from '@heroicons/react/solid';
|
||||
import { CloudUploadIcon, XIcon } from '@heroicons/react/solid';
|
||||
import asDialog from '@/hoc/asDialog';
|
||||
import { Dialog, DialogWrapperContext } from '@/components/elements/dialog';
|
||||
import { Button } from '@/components/elements/button/index';
|
||||
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
||||
import Code from '@/components/elements/Code';
|
||||
import { useSignal } from '@preact/signals-react';
|
||||
|
||||
const svgProps = {
|
||||
cx: 16,
|
||||
|
@ -31,23 +32,34 @@ const Spinner = ({ progress, className }: { progress: number; className?: string
|
|||
|
||||
const FileUploadList = () => {
|
||||
const { close } = useContext(DialogWrapperContext);
|
||||
const removeFileUpload = ServerContext.useStoreActions((actions) => actions.files.removeFileUpload);
|
||||
const clearFileUploads = ServerContext.useStoreActions((actions) => actions.files.clearFileUploads);
|
||||
const uploads = ServerContext.useStoreState((state) =>
|
||||
state.files.uploads.sort((a, b) => a.name.localeCompare(b.name))
|
||||
Object.entries(state.files.uploads).sort(([a], [b]) => a.localeCompare(b))
|
||||
);
|
||||
|
||||
return (
|
||||
<div className={'space-y-2 mt-6'}>
|
||||
{uploads.map((file) => (
|
||||
<div key={file.name} className={'flex items-center space-x-3 bg-gray-700 p-3 rounded'}>
|
||||
{uploads.map(([name, file]) => (
|
||||
<div key={name} className={'flex items-center space-x-3 bg-gray-700 p-3 rounded'}>
|
||||
<Tooltip content={`${Math.floor((file.loaded / file.total) * 100)}%`} placement={'left'}>
|
||||
<div className={'flex-shrink-0'}>
|
||||
<Spinner progress={(file.loaded / file.total) * 100} className={'w-6 h-6'} />
|
||||
</div>
|
||||
</Tooltip>
|
||||
<Code>{file.name}</Code>
|
||||
<Code className={'flex-1 truncate'}>{name}</Code>
|
||||
<button
|
||||
onClick={removeFileUpload.bind(this, name)}
|
||||
className={'text-gray-500 hover:text-gray-200 transition-colors duration-75'}
|
||||
>
|
||||
<XIcon className={'w-5 h-5'} />
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
<Dialog.Footer>
|
||||
<Button.Danger variant={Button.Variants.Secondary} onClick={() => clearFileUploads()}>
|
||||
Cancel Uploads
|
||||
</Button.Danger>
|
||||
<Button.Text onClick={close}>Close</Button.Text>
|
||||
</Dialog.Footer>
|
||||
</div>
|
||||
|
@ -60,17 +72,17 @@ const FileUploadListDialog = asDialog({
|
|||
})(FileUploadList);
|
||||
|
||||
export default () => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const open = useSignal(false);
|
||||
|
||||
const count = ServerContext.useStoreState((state) => state.files.uploads.length);
|
||||
const count = ServerContext.useStoreState((state) => Object.keys(state.files.uploads).length);
|
||||
const progress = ServerContext.useStoreState((state) => ({
|
||||
uploaded: state.files.uploads.reduce((count, file) => count + file.loaded, 0),
|
||||
total: state.files.uploads.reduce((count, file) => count + file.total, 0),
|
||||
uploaded: Object.values(state.files.uploads).reduce((count, file) => count + file.loaded, 0),
|
||||
total: Object.values(state.files.uploads).reduce((count, file) => count + file.total, 0),
|
||||
}));
|
||||
|
||||
useEffect(() => {
|
||||
if (count === 0) {
|
||||
setOpen(false);
|
||||
open.value = false;
|
||||
}
|
||||
}, [count]);
|
||||
|
||||
|
@ -78,13 +90,16 @@ export default () => {
|
|||
<>
|
||||
{count > 0 && (
|
||||
<Tooltip content={`${count} files are uploading, click to view`}>
|
||||
<button className={'flex items-center justify-center w-10 h-10'} onClick={setOpen.bind(this, true)}>
|
||||
<button
|
||||
className={'flex items-center justify-center w-10 h-10'}
|
||||
onClick={() => (open.value = true)}
|
||||
>
|
||||
<Spinner progress={(progress.uploaded / progress.total) * 100} className={'w-8 h-8'} />
|
||||
<CloudUploadIcon className={'h-3 absolute mx-auto animate-pulse'} />
|
||||
</button>
|
||||
</Tooltip>
|
||||
)}
|
||||
<FileUploadListDialog open={open} onClose={setOpen.bind(this, false)} />
|
||||
<FileUploadListDialog open={open.value} onClose={() => (open.value = false)} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@ import axios from 'axios';
|
|||
import getFileUploadUrl from '@/api/server/files/getFileUploadUrl';
|
||||
import tw from 'twin.macro';
|
||||
import { Button } from '@/components/elements/button/index';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import React, { useEffect, useRef } from 'react';
|
||||
import { ModalMask } from '@/components/elements/Modal';
|
||||
import Fade from '@/components/elements/Fade';
|
||||
import useEventListener from '@/plugins/useEventListener';
|
||||
|
@ -12,6 +12,7 @@ import { ServerContext } from '@/state/server';
|
|||
import { WithClassname } from '@/components/types';
|
||||
import Portal from '@/components/elements/Portal';
|
||||
import { CloudUploadIcon } from '@heroicons/react/outline';
|
||||
import { useSignal } from '@preact/signals-react';
|
||||
|
||||
function isFileOrDirectory(event: DragEvent): boolean {
|
||||
if (!event.dataTransfer?.types) {
|
||||
|
@ -23,14 +24,16 @@ function isFileOrDirectory(event: DragEvent): boolean {
|
|||
|
||||
export default ({ className }: WithClassname) => {
|
||||
const fileUploadInput = useRef<HTMLInputElement>(null);
|
||||
const [timeouts, setTimeouts] = useState<NodeJS.Timeout[]>([]);
|
||||
const [visible, setVisible] = useState(false);
|
||||
|
||||
const visible = useSignal(false);
|
||||
const timeouts = useSignal<NodeJS.Timeout[]>([]);
|
||||
|
||||
const { mutate } = useFileManagerSwr();
|
||||
const { addError, clearAndAddHttpError } = useFlashKey('files');
|
||||
|
||||
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
||||
const directory = ServerContext.useStoreState((state) => state.files.directory);
|
||||
const { clearFileUploads, appendFileUpload, removeFileUpload } = ServerContext.useStoreActions(
|
||||
const { clearFileUploads, removeFileUpload, pushFileUpload, setUploadProgress } = ServerContext.useStoreActions(
|
||||
(actions) => actions.files
|
||||
);
|
||||
|
||||
|
@ -40,27 +43,24 @@ export default ({ className }: WithClassname) => {
|
|||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (isFileOrDirectory(e)) {
|
||||
return setVisible(true);
|
||||
visible.value = true;
|
||||
}
|
||||
},
|
||||
{ capture: true }
|
||||
);
|
||||
|
||||
useEventListener('dragexit', () => setVisible(false), { capture: true });
|
||||
useEventListener('dragexit', () => (visible.value = false), { capture: true });
|
||||
|
||||
useEventListener('keydown', () => {
|
||||
visible && setVisible(false);
|
||||
});
|
||||
useEventListener('keydown', () => (visible.value = false));
|
||||
|
||||
useEffect(() => {
|
||||
return () => timeouts.forEach(clearTimeout);
|
||||
return () => timeouts.value.forEach(clearTimeout);
|
||||
}, []);
|
||||
|
||||
const onUploadProgress = (data: ProgressEvent, name: string) => {
|
||||
appendFileUpload({ name, loaded: data.loaded, total: data.total });
|
||||
setUploadProgress({ name, loaded: data.loaded });
|
||||
if (data.loaded >= data.total) {
|
||||
const timeout = setTimeout(() => removeFileUpload(name), 500);
|
||||
setTimeouts((t) => [...t, timeout]);
|
||||
timeouts.value.push(setTimeout(() => removeFileUpload(name), 500));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -71,23 +71,20 @@ export default ({ className }: WithClassname) => {
|
|||
return addError('Folder uploads are not supported at this time.', 'Error');
|
||||
}
|
||||
|
||||
if (!list.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
const uploads = list.map((file) => {
|
||||
appendFileUpload({ name: file.name, loaded: 0, total: file.size });
|
||||
const controller = new AbortController();
|
||||
pushFileUpload({ name: file.name, data: { abort: controller, loaded: 0, total: file.size } });
|
||||
|
||||
return () =>
|
||||
getFileUploadUrl(uuid).then((url) =>
|
||||
axios.post(
|
||||
url,
|
||||
{ files: file },
|
||||
{
|
||||
signal: controller.signal,
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
params: { directory },
|
||||
onUploadProgress: (data) => {
|
||||
onUploadProgress(data, file.name);
|
||||
},
|
||||
onUploadProgress: (data) => onUploadProgress(data, file.name),
|
||||
}
|
||||
)
|
||||
);
|
||||
|
@ -104,15 +101,15 @@ export default ({ className }: WithClassname) => {
|
|||
return (
|
||||
<>
|
||||
<Portal>
|
||||
<Fade appear in={visible} timeout={75} key={'upload_modal_mask'} unmountOnExit>
|
||||
<Fade appear in={visible.value} timeout={75} key={'upload_modal_mask'} unmountOnExit>
|
||||
<ModalMask
|
||||
onClick={() => setVisible(false)}
|
||||
onClick={() => (visible.value = false)}
|
||||
onDragOver={(e) => e.preventDefault()}
|
||||
onDrop={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
setVisible(false);
|
||||
visible.value = false;
|
||||
if (!e.dataTransfer?.files.length) return;
|
||||
|
||||
onFileSubmission(e.dataTransfer.files);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue