2022-11-21 20:58:55 +00:00
|
|
|
import { CloudUploadIcon, XIcon } from '@heroicons/react/solid';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { useSignal } from '@preact/signals-react';
|
|
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
|
2022-07-24 22:03:45 +00:00
|
|
|
import { Button } from '@/components/elements/button/index';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { Dialog, DialogWrapperContext } from '@/components/elements/dialog';
|
2022-07-24 22:03:45 +00:00
|
|
|
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
|
|
|
import Code from '@/components/elements/Code';
|
2022-11-25 20:25:03 +00:00
|
|
|
import asDialog from '@/hoc/asDialog';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-07-24 22:03:45 +00:00
|
|
|
const svgProps = {
|
|
|
|
cx: 16,
|
|
|
|
cy: 16,
|
|
|
|
r: 14,
|
|
|
|
strokeWidth: 3,
|
|
|
|
fill: 'none',
|
|
|
|
stroke: 'currentColor',
|
|
|
|
};
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-07-24 22:03:45 +00:00
|
|
|
const Spinner = ({ progress, className }: { progress: number; className?: string }) => (
|
|
|
|
<svg viewBox={'0 0 32 32'} className={className}>
|
|
|
|
<circle {...svgProps} className={'opacity-25'} />
|
|
|
|
<circle
|
|
|
|
{...svgProps}
|
|
|
|
stroke={'white'}
|
|
|
|
strokeDasharray={28 * Math.PI}
|
|
|
|
className={'rotate-[-90deg] origin-[50%_50%] transition-[stroke-dashoffset] duration-300'}
|
|
|
|
style={{ strokeDashoffset: ((100 - progress) / 100) * 28 * Math.PI }}
|
|
|
|
/>
|
|
|
|
</svg>
|
|
|
|
);
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-07-24 22:03:45 +00:00
|
|
|
const FileUploadList = () => {
|
|
|
|
const { close } = useContext(DialogWrapperContext);
|
2022-12-12 21:06:52 +00:00
|
|
|
const cancelFileUpload = ServerContext.useStoreActions(actions => actions.files.cancelFileUpload);
|
2022-11-25 20:25:03 +00:00
|
|
|
const clearFileUploads = ServerContext.useStoreActions(actions => actions.files.clearFileUploads);
|
|
|
|
const uploads = ServerContext.useStoreState(state =>
|
|
|
|
Object.entries(state.files.uploads).sort(([a], [b]) => a.localeCompare(b)),
|
2022-07-24 21:18:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-07-24 22:03:45 +00:00
|
|
|
<div className={'space-y-2 mt-6'}>
|
2022-11-21 20:58:55 +00:00
|
|
|
{uploads.map(([name, file]) => (
|
|
|
|
<div key={name} className={'flex items-center space-x-3 bg-gray-700 p-3 rounded'}>
|
2022-07-24 22:03:45 +00:00
|
|
|
<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>
|
2022-11-21 20:58:55 +00:00
|
|
|
<Code className={'flex-1 truncate'}>{name}</Code>
|
|
|
|
<button
|
2022-12-04 23:36:53 +00:00
|
|
|
onClick={cancelFileUpload.bind(this, name)}
|
2022-11-21 20:58:55 +00:00
|
|
|
className={'text-gray-500 hover:text-gray-200 transition-colors duration-75'}
|
|
|
|
>
|
|
|
|
<XIcon className={'w-5 h-5'} />
|
|
|
|
</button>
|
2022-07-24 21:18:32 +00:00
|
|
|
</div>
|
2022-07-24 22:03:45 +00:00
|
|
|
))}
|
|
|
|
<Dialog.Footer>
|
2022-11-21 20:58:55 +00:00
|
|
|
<Button.Danger variant={Button.Variants.Secondary} onClick={() => clearFileUploads()}>
|
|
|
|
Cancel Uploads
|
|
|
|
</Button.Danger>
|
2022-07-24 22:03:45 +00:00
|
|
|
<Button.Text onClick={close}>Close</Button.Text>
|
|
|
|
</Dialog.Footer>
|
2022-07-24 21:18:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
2022-07-24 22:03:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FileUploadListDialog = asDialog({
|
|
|
|
title: 'File Uploads',
|
|
|
|
description: 'The following files are being uploaded to your server.',
|
|
|
|
})(FileUploadList);
|
|
|
|
|
|
|
|
export default () => {
|
2022-11-21 20:58:55 +00:00
|
|
|
const open = useSignal(false);
|
2022-07-24 21:18:32 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
const count = ServerContext.useStoreState(state => Object.keys(state.files.uploads).length);
|
|
|
|
const progress = ServerContext.useStoreState(state => ({
|
2022-11-21 20:58:55 +00:00
|
|
|
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),
|
2022-07-24 22:03:45 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (count === 0) {
|
2022-11-21 20:58:55 +00:00
|
|
|
open.value = false;
|
2022-07-24 22:03:45 +00:00
|
|
|
}
|
|
|
|
}, [count]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{count > 0 && (
|
|
|
|
<Tooltip content={`${count} files are uploading, click to view`}>
|
2022-11-21 20:58:55 +00:00
|
|
|
<button
|
|
|
|
className={'flex items-center justify-center w-10 h-10'}
|
|
|
|
onClick={() => (open.value = true)}
|
|
|
|
>
|
2022-07-24 22:03:45 +00:00
|
|
|
<Spinner progress={(progress.uploaded / progress.total) * 100} className={'w-8 h-8'} />
|
|
|
|
<CloudUploadIcon className={'h-3 absolute mx-auto animate-pulse'} />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
)}
|
2022-11-21 20:58:55 +00:00
|
|
|
<FileUploadListDialog open={open.value} onClose={() => (open.value = false)} />
|
2022-07-24 22:03:45 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|