Files
pterodactyl-panel/resources/scripts/components/server/files/UploadButton.tsx
T

100 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import { NewButton } from '@/components/elements/NewButton';
import { useRef } from 'react';
2020-08-22 22:10:16 -07:00
import useEventListener from '@/plugins/useEventListener';
2026-10-05 18:10:18 -04:00
import type { WithClassname } from '@/components/types';
2022-06-20 14:16:42 -04:00
import Portal from '@/components/elements/Portal';
2026-10-05 18:10:18 -04:00
import { CloudUpload } from 'lucide-react';
import { useDialogState } from '@/components/elements/dialog';
import useFileUploader from '@/components/server/files/useFileUploader';
2020-07-12 15:20:37 -06:00
2022-07-24 23:18:32 +02:00
function isFileOrDirectory(event: DragEvent): boolean {
if (!event.dataTransfer?.types) {
return false;
}
return event.dataTransfer.types.some((value) => value.toLowerCase() === 'files');
2022-07-24 23:18:32 +02:00
}
2026-10-05 18:10:18 -04:00
export default function UploadButton({ className }: WithClassname) {
const fileUploadInput = useRef<HTMLInputElement>(null);
2022-11-21 12:58:55 -08:00
2026-10-05 18:10:18 -04:00
const dropOverlay = useDialogState();
const upload = useFileUploader();
2020-07-12 15:20:37 -06:00
useEventListener(
'dragenter',
2026-10-05 18:10:18 -04:00
(e: DragEvent) => {
e.preventDefault();
e.stopPropagation();
if (isFileOrDirectory(e)) {
2026-10-05 18:10:18 -04:00
dropOverlay.show();
2022-07-24 23:18:32 +02:00
}
},
{ capture: true }
);
2020-07-12 16:42:32 -06:00
2026-10-05 18:10:18 -04:00
useEventListener('dragexit', dropOverlay.hide, { capture: true });
2020-07-12 16:42:32 -06:00
2026-10-05 18:10:18 -04:00
useEventListener('keydown', dropOverlay.hide);
2022-07-24 18:50:47 -04:00
const onFileSubmission = (files: FileList) => {
2026-10-05 18:10:18 -04:00
// Failures have already been reported with error toasts.
upload(Array.from(files)).catch(() => {});
2020-07-12 15:20:37 -06:00
};
return (
<>
2022-06-20 14:16:42 -04:00
<Portal>
2026-10-05 18:10:18 -04:00
{dropOverlay.open && (
<div
className={'fixed z-50 overflow-auto flex w-full inset-0 bg-background/70'}
onDragOver={(e) => e.preventDefault()}
onDrop={(e) => {
2022-06-20 14:16:42 -04:00
e.preventDefault();
e.stopPropagation();
2026-10-05 18:10:18 -04:00
dropOverlay.hide();
2022-06-20 14:16:42 -04:00
if (!e.dataTransfer?.files.length) return;
2022-06-20 14:16:42 -04:00
onFileSubmission(e.dataTransfer.files);
}}
>
2022-07-24 18:59:20 -04:00
<div className={'w-full flex items-center justify-center pointer-events-none'}>
<div
className={
2026-10-05 18:10:18 -04:00
'flex items-center space-x-4 bg-muted w-full ring-4 ring-ring/50 rounded-sm p-6 mx-10 max-w-sm'
2022-07-24 18:59:20 -04:00
}
>
2026-10-05 18:10:18 -04:00
<CloudUpload className={'w-10 h-10 shrink-0'} />
<p className={'font-header flex-1 text-lg text-foreground text-center'}>
2022-07-24 18:59:20 -04:00
Drag and drop files to upload.
</p>
</div>
2022-06-20 14:16:42 -04:00
</div>
2026-10-05 18:10:18 -04:00
</div>
)}
2022-06-20 14:16:42 -04:00
</Portal>
<input
type={'file'}
ref={fileUploadInput}
2026-10-05 18:10:18 -04:00
className={'hidden'}
aria-label={'Upload files'}
onChange={(e) => {
if (!e.currentTarget.files) return;
onFileSubmission(e.currentTarget.files);
2026-10-05 18:10:18 -04:00
e.currentTarget.value = '';
}}
multiple
/>
2026-10-05 18:10:18 -04:00
<NewButton
icon={CloudUpload}
className={className}
onClick={() => fileUploadInput.current && fileUploadInput.current.click()}
>
2020-07-12 15:20:37 -06:00
Upload
2026-10-05 18:10:18 -04:00
</NewButton>
2020-07-12 15:20:37 -06:00
</>
);
2026-10-05 18:10:18 -04:00
}