mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
Co-authored-by: Anthony <anthony@infraly.co> Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
151 lines
6.8 KiB
TypeScript
151 lines
6.8 KiB
TypeScript
import { useCallback, useMemo } from 'react';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
import { getCoreRowModel, useReactTable, type OnChangeFn, type PaginationState } from '@tanstack/react-table';
|
|
import { useNavigate, useParams } from '@tanstack/react-router';
|
|
import { Archive, ArrowLeft } from 'lucide-react';
|
|
import Spinner from '@/components/elements/Spinner';
|
|
import ListToolbar from '@/components/elements/ListToolbar';
|
|
import { NewButton } from '@/components/elements/NewButton';
|
|
import CreateBackupButton from '@/components/server/backups/CreateBackupButton';
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
|
import { useCurrentServer } from '@/api/server/queries';
|
|
import { updateServerBackup, useServerBackups } from '@/api/server/backups/queries';
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
import { ServerError } from '@/components/elements/ScreenBlock';
|
|
import { getPageSearch, usePageSearch } from '@/router/search';
|
|
import DataTable from '@/components/elements/table/DataTable';
|
|
import DataTablePagination from '@/components/elements/table/DataTablePagination';
|
|
import { backupColumns } from '@/components/server/backups/BackupTable';
|
|
import { completeBackup, parseBackupCompletedPayload } from '@/components/server/backups/backupCompletion';
|
|
import { SocketEvent } from '@/components/server/events';
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
|
import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from '@/components/ui/empty';
|
|
import { emptyCompactClass } from '@/components/ui/styles';
|
|
|
|
export default function BackupContainer() {
|
|
const navigate = useNavigate();
|
|
const { id } = useParams({ from: '/authenticated/server/$id' });
|
|
const page = usePageSearch();
|
|
const server = useCurrentServer()!;
|
|
const [canCreate] = usePermissions('backup.create');
|
|
const queryClient = useQueryClient();
|
|
const { data: backups, error, refetch } = useServerBackups(server.attributes.uuid, page);
|
|
const columns = useMemo(() => backupColumns(page), [page]);
|
|
const data = useMemo(() => backups?.data ?? [], [backups?.data]);
|
|
const pagination = useMemo<PaginationState>(
|
|
() => ({ pageIndex: page - 1, pageSize: backups?.meta.pagination.per_page ?? 20 }),
|
|
[backups?.meta.pagination.per_page, page]
|
|
);
|
|
const onPaginationChange = useCallback<OnChangeFn<PaginationState>>(
|
|
(updater) => {
|
|
const next = updater instanceof Function ? updater(pagination) : updater;
|
|
if (next.pageIndex === pagination.pageIndex) return;
|
|
|
|
navigate({
|
|
to: '/server/$id/backups',
|
|
params: { id },
|
|
search: getPageSearch(next.pageIndex + 1),
|
|
replace: true,
|
|
viewTransition: false,
|
|
});
|
|
},
|
|
[id, navigate, pagination]
|
|
);
|
|
const table = useReactTable({
|
|
data,
|
|
columns,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getRowId: (backup) => backup.attributes.uuid,
|
|
manualPagination: true,
|
|
rowCount: backups?.meta.pagination.total ?? 0,
|
|
state: { pagination },
|
|
onPaginationChange,
|
|
});
|
|
|
|
useWebsocketEvent(SocketEvent.BACKUP_COMPLETED, (data) => {
|
|
const payload = parseBackupCompletedPayload(data);
|
|
if (!payload) {
|
|
console.warn('Ignoring a malformed backup completion event.', data);
|
|
return;
|
|
}
|
|
|
|
updateServerBackup(queryClient, server.attributes.uuid, page, payload.uuid, (backup) =>
|
|
completeBackup(backup, payload, new Date().toISOString())
|
|
);
|
|
});
|
|
|
|
const backupLimit = server.attributes.feature_limits.backups ?? 0;
|
|
|
|
if (error && !backups) {
|
|
return <ServerError message={httpErrorToHuman(error)} onRetry={() => refetch()} />;
|
|
}
|
|
|
|
if (!backups) {
|
|
return <Spinner size={'large'} centered />;
|
|
}
|
|
|
|
const backupCount = backups.meta.backup_count;
|
|
const canAddBackup = canCreate && backupLimit > backupCount;
|
|
const usage =
|
|
backupLimit === 0 && backups.data.length > 0
|
|
? 'Backups cannot be created for this server because the backup limit is set to 0.'
|
|
: canCreate && backupLimit > 0 && backupCount > 0
|
|
? `${backupCount} of ${backupLimit} backups have been created for this server.`
|
|
: null;
|
|
|
|
return (
|
|
<ServerContentBlock title={'Backups'}>
|
|
{(usage || canAddBackup) && (
|
|
<ListToolbar summary={usage}>{canAddBackup && <CreateBackupButton />}</ListToolbar>
|
|
)}
|
|
<DataTable
|
|
table={table}
|
|
emptyState={
|
|
page > 1 ? (
|
|
<Empty className={emptyCompactClass}>
|
|
<EmptyHeader>
|
|
<EmptyMedia variant={'icon'}>
|
|
<Archive />
|
|
</EmptyMedia>
|
|
<EmptyTitle>No backups on this page</EmptyTitle>
|
|
<EmptyDescription>This page is past the end of the backup list.</EmptyDescription>
|
|
</EmptyHeader>
|
|
<EmptyContent>
|
|
<NewButton isSecondary icon={ArrowLeft} onClick={() => table.setPageIndex(0)}>
|
|
First page
|
|
</NewButton>
|
|
</EmptyContent>
|
|
</Empty>
|
|
) : (
|
|
<Empty className={emptyCompactClass}>
|
|
<EmptyHeader>
|
|
<EmptyMedia variant={'icon'}>
|
|
<Archive />
|
|
</EmptyMedia>
|
|
<EmptyTitle>No backups</EmptyTitle>
|
|
<EmptyDescription>
|
|
{backupLimit > 0
|
|
? "This server doesn't have any backups yet."
|
|
: "Backups can't be created because this server's backup limit is 0."}
|
|
</EmptyDescription>
|
|
</EmptyHeader>
|
|
{canAddBackup && (
|
|
<EmptyContent>
|
|
<CreateBackupButton />
|
|
</EmptyContent>
|
|
)}
|
|
</Empty>
|
|
)
|
|
}
|
|
/>
|
|
<DataTablePagination
|
|
table={table}
|
|
total={backups.meta.pagination.total}
|
|
count={backups.meta.pagination.count}
|
|
itemLabel={'backups'}
|
|
/>
|
|
</ServerContentBlock>
|
|
);
|
|
}
|