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

112 lines
4.3 KiB
TypeScript
Raw Normal View History

2026-10-05 18:10:18 -04:00
import React from 'react';
import { useServerStore } from '@/state/server';
import { CloudUpload, X } from 'lucide-react';
import { Dialog } from '@/components/elements/dialog';
import Button from '@/components/elements/Button';
2022-07-24 18:03:45 -04:00
import Tooltip from '@/components/elements/tooltip/Tooltip';
import Code from '@/components/elements/Code';
2022-07-24 23:18:32 +02:00
2022-07-24 18:03:45 -04:00
const svgProps = {
cx: 16,
cy: 16,
r: 14,
strokeWidth: 3,
fill: 'none',
stroke: 'currentColor',
};
2022-07-24 23:18:32 +02:00
2022-07-24 18:03:45 -04:00
const Spinner = ({ progress, className }: { progress: number; className?: string }) => (
2026-10-08 16:53:20 -04:00
<svg viewBox='0 0 32 32' className={className}>
<circle {...svgProps} className='opacity-25' />
2022-07-24 18:03:45 -04:00
<circle
{...svgProps}
2026-10-08 16:53:20 -04:00
stroke='currentColor'
2022-07-24 18:03:45 -04:00
strokeDasharray={28 * Math.PI}
2026-10-08 16:53:20 -04:00
className='-rotate-90 origin-[50%_50%] transition-[stroke-dashoffset] duration-300'
2022-07-24 18:03:45 -04:00
style={{ strokeDashoffset: ((100 - progress) / 100) * 28 * Math.PI }}
/>
</svg>
);
2022-07-24 23:18:32 +02:00
2026-10-05 18:10:18 -04:00
const percent = (loaded: number, total: number) => (total > 0 ? (loaded / total) * 100 : 0);
const FileUploadList = ({ onClose }: { onClose: () => void }) => {
const cancelFileUpload = useServerStore((state) => state.files.cancelFileUpload);
const clearFileUploads = useServerStore((state) => state.files.clearFileUploads);
const uploadsById = useServerStore((state) => state.files.uploads);
const uploads = React.useMemo(
() =>
Object.entries(uploadsById).sort(
([a, first], [b, second]) => first.name.localeCompare(second.name) || a.localeCompare(b)
),
[uploadsById]
2022-07-24 23:18:32 +02:00
);
return (
2026-10-08 16:53:20 -04:00
<div className='space-y-2 mt-6'>
2026-10-05 18:10:18 -04:00
{uploads.map(([id, file]) => (
2026-10-08 16:53:20 -04:00
<div key={id} className='flex items-center space-x-3 bg-card p-3 rounded-sm'>
<Tooltip content={`${Math.floor(percent(file.loaded, file.total))}%`} placement='left'>
<div className='shrink-0'>
<Spinner progress={percent(file.loaded, file.total)} className='w-6 h-6' />
2022-07-24 18:03:45 -04:00
</div>
</Tooltip>
2026-10-08 16:53:20 -04:00
<Code className='flex-1 truncate'>{file.name}</Code>
2022-11-21 12:58:55 -08:00
<button
2026-10-08 16:53:20 -04:00
type='button'
2026-10-05 18:10:18 -04:00
aria-label={`Cancel upload of ${file.name}`}
onClick={() => cancelFileUpload(id)}
2026-10-08 16:53:20 -04:00
className='text-muted-foreground hover:text-foreground transition-colors duration-75'
2022-11-21 12:58:55 -08:00
>
2026-10-08 16:53:20 -04:00
<X className='w-5 h-5' />
2022-11-21 12:58:55 -08:00
</button>
2022-07-24 23:18:32 +02:00
</div>
2022-07-24 18:03:45 -04:00
))}
<Dialog.Footer>
2026-10-05 18:10:18 -04:00
<Button.Danger isSecondary onClick={() => clearFileUploads()}>
2022-11-21 12:58:55 -08:00
Cancel Uploads
</Button.Danger>
2026-10-05 18:10:18 -04:00
<Button.Text onClick={onClose}>Close</Button.Text>
2022-07-24 18:03:45 -04:00
</Dialog.Footer>
2022-07-24 23:18:32 +02:00
</div>
);
2022-07-24 18:03:45 -04:00
};
2026-10-05 18:10:18 -04:00
export default function FileManagerStatus() {
const count = useServerStore((state) => Object.keys(state.files.uploads).length);
const uploaded = useServerStore((state) =>
Object.values(state.files.uploads).reduce((count, file) => count + file.loaded, 0)
);
const total = useServerStore((state) =>
Object.values(state.files.uploads).reduce((count, file) => count + file.total, 0)
);
2022-07-24 18:03:45 -04:00
2026-10-05 18:10:18 -04:00
if (count === 0) {
return null;
}
2022-07-24 18:03:45 -04:00
return (
2026-10-05 18:10:18 -04:00
<Dialog.Trigger
trigger={({ onClick }) => (
2022-07-24 18:03:45 -04:00
<Tooltip content={`${count} files are uploading, click to view`}>
2026-10-08 16:53:20 -04:00
<button type='button' className='flex items-center justify-center w-10 h-10' onClick={onClick}>
<Spinner progress={percent(uploaded, total)} className='w-8 h-8' />
<CloudUpload className='h-3 absolute mx-auto animate-pulse' />
2022-07-24 18:03:45 -04:00
</button>
</Tooltip>
)}
2026-10-05 18:10:18 -04:00
>
{({ open, onClose }) => (
<Dialog
open={open}
onClose={onClose}
2026-10-08 16:53:20 -04:00
title='File Uploads'
description='The following files are being uploaded to your server.'
2026-10-05 18:10:18 -04:00
>
<FileUploadList onClose={onClose} />
</Dialog>
)}
</Dialog.Trigger>
2022-07-24 18:03:45 -04:00
);
2026-10-05 18:10:18 -04:00
}