2020-12-06 20:01:42 +00:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
2020-04-04 17:59:25 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import CreateBackupButton from '@/components/server/backups/CreateBackupButton';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-04-04 20:24:03 +00:00
|
|
|
import BackupRow from '@/components/server/backups/BackupRow';
|
2020-07-04 23:26:07 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-12-06 20:01:42 +00:00
|
|
|
import getServerBackups, { Context as ServerBackupContext } from '@/api/swr/getServerBackups';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
2020-12-06 20:01:42 +00:00
|
|
|
import Pagination from '@/components/elements/Pagination';
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2020-12-06 20:01:42 +00:00
|
|
|
const BackupContainer = () => {
|
|
|
|
const { page, setPage } = useContext(ServerBackupContext);
|
2020-08-21 04:44:33 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const { data: backups, error, isValidating } = getServerBackups();
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
const backupLimit = ServerContext.useStoreState((state) => state.server.data!.featureLimits.backups);
|
2020-08-26 05:09:54 +00:00
|
|
|
|
2020-04-04 17:59:25 +00:00
|
|
|
useEffect(() => {
|
2020-08-21 04:44:33 +00:00
|
|
|
if (!error) {
|
|
|
|
clearFlashes('backups');
|
2020-04-04 17:59:25 +00:00
|
|
|
|
2020-08-21 04:44:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearAndAddHttpError({ error, key: 'backups' });
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [error]);
|
2020-08-21 04:44:33 +00:00
|
|
|
|
|
|
|
if (!backups || (error && isValidating)) {
|
2022-06-26 19:13:52 +00:00
|
|
|
return <Spinner size={'large'} centered />;
|
2020-04-04 17:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-08-26 05:09:54 +00:00
|
|
|
<ServerContentBlock title={'Backups'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender byKey={'backups'} css={tw`mb-4`} />
|
2020-12-06 20:01:42 +00:00
|
|
|
<Pagination data={backups} onPageSelect={setPage}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{({ items }) =>
|
|
|
|
!items.length ? (
|
2020-12-06 20:01:42 +00:00
|
|
|
// Don't show any error messages if the server has no backups and the user cannot
|
|
|
|
// create additional ones for the server.
|
2022-06-26 19:13:52 +00:00
|
|
|
!backupLimit ? null : (
|
2020-12-06 20:01:42 +00:00
|
|
|
<p css={tw`text-center text-sm text-neutral-300`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{page > 1
|
|
|
|
? "Looks like we've run out of backups to show you, try going back a page."
|
|
|
|
: 'It looks like there are no backups currently stored for this server.'}
|
2020-12-06 20:01:42 +00:00
|
|
|
</p>
|
2022-06-26 19:13:52 +00:00
|
|
|
)
|
|
|
|
) : (
|
|
|
|
items.map((backup, index) => (
|
|
|
|
<BackupRow key={backup.uuid} backup={backup} css={index > 0 ? tw`mt-2` : undefined} />
|
|
|
|
))
|
|
|
|
)
|
|
|
|
}
|
2020-12-06 20:01:42 +00:00
|
|
|
</Pagination>
|
2022-06-26 19:13:52 +00:00
|
|
|
{backupLimit === 0 && (
|
|
|
|
<p css={tw`text-center text-sm text-neutral-300`}>
|
|
|
|
Backups cannot be created for this server because the backup limit is set to 0.
|
|
|
|
</p>
|
|
|
|
)}
|
2020-04-04 17:59:25 +00:00
|
|
|
<Can action={'backup.create'}>
|
2020-11-09 02:09:22 +00:00
|
|
|
<div css={tw`mt-6 sm:flex items-center justify-end`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
{backupLimit > 0 && backups.backupCount > 0 && (
|
|
|
|
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
|
|
|
|
{backups.backupCount} of {backupLimit} backups have been created for this server.
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
{backupLimit > 0 && backupLimit > backups.backupCount && (
|
|
|
|
<CreateBackupButton css={tw`w-full sm:w-auto`} />
|
|
|
|
)}
|
2020-04-04 17:59:25 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2020-08-26 05:09:54 +00:00
|
|
|
</ServerContentBlock>
|
2020-04-04 17:59:25 +00:00
|
|
|
);
|
|
|
|
};
|
2020-12-06 20:01:42 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const [page, setPage] = useState<number>(1);
|
2020-12-06 20:01:42 +00:00
|
|
|
return (
|
|
|
|
<ServerBackupContext.Provider value={{ page, setPage }}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<BackupContainer />
|
2020-12-06 20:01:42 +00:00
|
|
|
</ServerBackupContext.Provider>
|
|
|
|
);
|
|
|
|
};
|