2020-04-10 05:08:09 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-01-18 02:25:13 +00:00
|
|
|
import { faBoxOpen, faCloudDownloadAlt, faEllipsisH, faLock, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
2020-04-10 05:08:09 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import DropdownMenu, { DropdownButtonRow } from '@/components/elements/DropdownMenu';
|
|
|
|
import getBackupDownloadUrl from '@/api/server/backups/getBackupDownloadUrl';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import ChecksumModal from '@/components/server/backups/ChecksumModal';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import deleteBackup from '@/api/server/backups/deleteBackup';
|
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
2020-04-10 17:11:15 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-07-04 23:26:07 +00:00
|
|
|
import tw from 'twin.macro';
|
2020-08-21 04:44:33 +00:00
|
|
|
import getServerBackups from '@/api/swr/getServerBackups';
|
|
|
|
import { ServerBackup } from '@/api/server/types';
|
2020-08-26 05:09:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2021-01-18 02:25:13 +00:00
|
|
|
import Input from '@/components/elements/Input';
|
2021-01-31 02:01:32 +00:00
|
|
|
import { restoreServerBackup } from '@/api/server/backups';
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
backup: ServerBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ backup }: Props) => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2021-01-31 02:01:32 +00:00
|
|
|
const setServerFromState = ServerContext.useStoreActions(actions => actions.server.setServerFromState);
|
|
|
|
const [ modal, setModal ] = useState('');
|
2020-04-10 05:08:09 +00:00
|
|
|
const [ loading, setLoading ] = useState(false);
|
2020-08-21 04:44:33 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
|
|
|
const { mutate } = getServerBackups();
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
const doDownload = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('backups');
|
|
|
|
getBackupDownloadUrl(uuid, backup.uuid)
|
|
|
|
.then(url => {
|
|
|
|
// @ts-ignore
|
|
|
|
window.location = url;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-08-21 04:44:33 +00:00
|
|
|
clearAndAddHttpError({ key: 'backups', error });
|
2020-04-10 05:08:09 +00:00
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
|
|
|
const doDeletion = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('backups');
|
|
|
|
deleteBackup(uuid, backup.uuid)
|
2021-01-31 02:01:32 +00:00
|
|
|
.then(() => mutate(data => ({
|
|
|
|
...data,
|
|
|
|
items: data.items.filter(b => b.uuid !== backup.uuid),
|
|
|
|
}), false))
|
2020-04-10 05:08:09 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2020-08-21 04:44:33 +00:00
|
|
|
clearAndAddHttpError({ key: 'backups', error });
|
2020-04-10 05:08:09 +00:00
|
|
|
setLoading(false);
|
2021-01-31 02:01:32 +00:00
|
|
|
setModal('');
|
2020-04-10 05:08:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-31 02:01:32 +00:00
|
|
|
const doRestorationAction = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('backups');
|
|
|
|
restoreServerBackup(uuid, backup.uuid)
|
|
|
|
.then(() => setServerFromState(s => ({
|
|
|
|
...s,
|
|
|
|
status: 'restoring_backup',
|
|
|
|
})))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
clearAndAddHttpError({ key: 'backups', error });
|
|
|
|
})
|
2021-01-31 02:07:48 +00:00
|
|
|
.then(() => setLoading(false))
|
|
|
|
.then(() => setModal(''));
|
2021-01-31 02:01:32 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ChecksumModal
|
2020-07-04 23:26:07 +00:00
|
|
|
appear
|
2021-01-31 02:01:32 +00:00
|
|
|
visible={modal === 'checksum'}
|
|
|
|
onDismissed={() => setModal('')}
|
2020-08-24 01:06:47 +00:00
|
|
|
checksum={backup.checksum}
|
2020-04-10 05:08:09 +00:00
|
|
|
/>
|
2021-01-18 02:25:13 +00:00
|
|
|
<ConfirmationModal
|
2021-01-31 02:01:32 +00:00
|
|
|
visible={modal === 'restore'}
|
2021-01-18 02:25:13 +00:00
|
|
|
title={'Restore this backup?'}
|
|
|
|
buttonText={'Restore backup'}
|
2021-01-31 02:01:32 +00:00
|
|
|
onConfirmed={() => doRestorationAction()}
|
|
|
|
onModalDismissed={() => setModal('')}
|
2021-01-18 02:25:13 +00:00
|
|
|
>
|
|
|
|
<p css={tw`text-neutral-300`}>
|
|
|
|
This server will be stopped in order to restore the backup. Once the backup has started you will
|
|
|
|
not be able to control the server power state, access the file manager, or create additional backups
|
|
|
|
until it has completed.
|
|
|
|
</p>
|
|
|
|
<p css={tw`text-neutral-300 mt-4`}>
|
|
|
|
Are you sure you want to continue?
|
|
|
|
</p>
|
|
|
|
<p css={tw`mt-4 -mb-2 bg-neutral-900 p-3 rounded`}>
|
2021-01-31 02:01:32 +00:00
|
|
|
<label
|
|
|
|
htmlFor={'restore_truncate'}
|
|
|
|
css={tw`text-base text-neutral-200 flex items-center cursor-pointer`}
|
|
|
|
>
|
2021-01-18 02:25:13 +00:00
|
|
|
<Input
|
|
|
|
type={'checkbox'}
|
|
|
|
css={tw`text-red-500! w-5! h-5! mr-2`}
|
|
|
|
id={'restore_truncate'}
|
|
|
|
value={'true'}
|
|
|
|
/>
|
|
|
|
Remove all files and folders before restoring this backup.
|
|
|
|
</label>
|
|
|
|
</p>
|
|
|
|
</ConfirmationModal>
|
2020-04-10 05:08:09 +00:00
|
|
|
<ConfirmationModal
|
2021-01-31 02:01:32 +00:00
|
|
|
visible={modal === 'delete'}
|
2020-04-10 05:08:09 +00:00
|
|
|
title={'Delete this backup?'}
|
|
|
|
buttonText={'Yes, delete backup'}
|
|
|
|
onConfirmed={() => doDeletion()}
|
2021-01-31 02:01:32 +00:00
|
|
|
onModalDismissed={() => setModal('')}
|
2020-04-10 05:08:09 +00:00
|
|
|
>
|
|
|
|
Are you sure you wish to delete this backup? This is a permanent operation and the backup cannot
|
|
|
|
be recovered once deleted.
|
|
|
|
</ConfirmationModal>
|
2020-07-05 01:30:50 +00:00
|
|
|
<SpinnerOverlay visible={loading} fixed/>
|
2020-08-21 04:44:33 +00:00
|
|
|
{backup.isSuccessful ?
|
|
|
|
<DropdownMenu
|
|
|
|
renderToggle={onClick => (
|
|
|
|
<button
|
|
|
|
onClick={onClick}
|
|
|
|
css={tw`text-neutral-200 transition-colors duration-150 hover:text-neutral-100 p-2`}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faEllipsisH}/>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<div css={tw`text-sm`}>
|
|
|
|
<Can action={'backup.download'}>
|
2021-01-31 02:01:32 +00:00
|
|
|
<DropdownButtonRow onClick={doDownload}>
|
2020-08-21 04:44:33 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faCloudDownloadAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Download</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
2021-01-18 02:25:13 +00:00
|
|
|
<Can action={'backup.restore'}>
|
2021-01-31 02:01:32 +00:00
|
|
|
<DropdownButtonRow onClick={() => setModal('restore')}>
|
2021-01-18 02:25:13 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faBoxOpen} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Restore</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
2021-01-31 02:01:32 +00:00
|
|
|
<DropdownButtonRow onClick={() => setModal('checksum')}>
|
2020-08-21 04:44:33 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faLock} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Checksum</span>
|
2020-04-10 17:11:15 +00:00
|
|
|
</DropdownButtonRow>
|
2020-08-21 04:44:33 +00:00
|
|
|
<Can action={'backup.delete'}>
|
2021-01-31 02:01:32 +00:00
|
|
|
<DropdownButtonRow danger onClick={() => setModal('delete')}>
|
2020-08-21 04:44:33 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faTrashAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Delete</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
</DropdownMenu>
|
|
|
|
:
|
|
|
|
<button
|
2021-01-31 02:01:32 +00:00
|
|
|
onClick={() => setModal('delete')}
|
2020-08-21 04:44:33 +00:00
|
|
|
css={tw`text-neutral-200 transition-colors duration-150 hover:text-neutral-100 p-2`}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faTrashAlt}/>
|
|
|
|
</button>
|
|
|
|
}
|
2020-04-10 05:08:09 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|