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
|
|
|
|
2020-08-26 05:09:54 +00:00
|
|
|
const backupLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.backups);
|
|
|
|
|
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' });
|
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
if (!backups || (error && isValidating)) {
|
2020-07-04 23:26:07 +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'}>
|
2020-07-04 23:26:07 +00:00
|
|
|
<FlashMessageRender byKey={'backups'} css={tw`mb-4`}/>
|
2020-12-06 20:01:42 +00:00
|
|
|
<Pagination data={backups} onPageSelect={setPage}>
|
|
|
|
{({ items }) => (
|
|
|
|
!items.length ?
|
|
|
|
// Don't show any error messages if the server has no backups and the user cannot
|
|
|
|
// create additional ones for the server.
|
|
|
|
!backupLimit ?
|
|
|
|
null
|
|
|
|
:
|
|
|
|
<p css={tw`text-center text-sm text-neutral-300`}>
|
|
|
|
{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.'
|
|
|
|
}
|
|
|
|
</p>
|
|
|
|
:
|
|
|
|
items.map((backup, index) => <BackupRow
|
|
|
|
key={backup.uuid}
|
|
|
|
backup={backup}
|
|
|
|
css={index > 0 ? tw`mt-2` : undefined}
|
|
|
|
/>)
|
|
|
|
)}
|
|
|
|
</Pagination>
|
2020-08-26 05:09:54 +00:00
|
|
|
{backupLimit === 0 &&
|
2020-11-09 02:09:22 +00:00
|
|
|
<p css={tw`text-center text-sm text-neutral-300`}>
|
2020-08-21 04:44:33 +00:00
|
|
|
Backups cannot be created for this server.
|
|
|
|
</p>
|
2020-06-03 03:14:16 +00:00
|
|
|
}
|
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`}>
|
2021-03-07 00:57:02 +00:00
|
|
|
{(backupLimit > 0 && backups.pagination.total > 0) &&
|
2020-11-09 02:09:22 +00:00
|
|
|
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
|
2021-03-07 00:57:02 +00:00
|
|
|
{backups.pagination.total} of {backupLimit} backups have been created for this server.
|
2020-11-09 02:09:22 +00:00
|
|
|
</p>
|
|
|
|
}
|
2021-03-07 00:57:02 +00:00
|
|
|
{backupLimit > 0 && backupLimit !== backups.pagination.total &&
|
2020-12-06 20:01:42 +00:00
|
|
|
<CreateBackupButton css={tw`w-full sm:w-auto`}/>
|
2020-11-09 02:09:22 +00:00
|
|
|
}
|
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 () => {
|
|
|
|
const [ page, setPage ] = useState<number>(1);
|
|
|
|
return (
|
|
|
|
<ServerBackupContext.Provider value={{ page, setPage }}>
|
|
|
|
<BackupContainer/>
|
|
|
|
</ServerBackupContext.Provider>
|
|
|
|
);
|
|
|
|
};
|