2020-04-10 05:08:09 +00:00
|
|
|
import React, { useState } from 'react';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { 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 useServer from '@/plugins/useServer';
|
|
|
|
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-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
backup: ServerBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ backup }: Props) => {
|
|
|
|
const { uuid } = useServer();
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const [ deleteVisible, setDeleteVisible ] = 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)
|
2020-08-21 04:44:33 +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);
|
|
|
|
setDeleteVisible(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{visible &&
|
|
|
|
<ChecksumModal
|
2020-07-04 23:26:07 +00:00
|
|
|
appear
|
2020-04-10 05:08:09 +00:00
|
|
|
visible={visible}
|
|
|
|
onDismissed={() => setVisible(false)}
|
2020-08-24 01:06:47 +00:00
|
|
|
checksum={backup.checksum}
|
2020-04-10 05:08:09 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
<ConfirmationModal
|
2020-08-18 04:35:11 +00:00
|
|
|
visible={deleteVisible}
|
2020-04-10 05:08:09 +00:00
|
|
|
title={'Delete this backup?'}
|
|
|
|
buttonText={'Yes, delete backup'}
|
|
|
|
onConfirmed={() => doDeletion()}
|
2020-08-18 04:35:11 +00:00
|
|
|
onModalDismissed={() => setDeleteVisible(false)}
|
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'}>
|
|
|
|
<DropdownButtonRow onClick={() => doDownload()}>
|
|
|
|
<FontAwesomeIcon fixedWidth icon={faCloudDownloadAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Download</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
|
|
|
<DropdownButtonRow onClick={() => setVisible(true)}>
|
|
|
|
<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'}>
|
|
|
|
<DropdownButtonRow danger onClick={() => setDeleteVisible(true)}>
|
|
|
|
<FontAwesomeIcon fixedWidth icon={faTrashAlt} css={tw`text-xs`}/>
|
|
|
|
<span css={tw`ml-2`}>Delete</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
</DropdownMenu>
|
|
|
|
:
|
|
|
|
<button
|
|
|
|
onClick={() => setDeleteVisible(true)}
|
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|