2020-04-10 05:08:09 +00:00
|
|
|
import React, { useState } from 'react';
|
2021-05-04 04:26:09 +00:00
|
|
|
import {
|
|
|
|
faBoxOpen,
|
|
|
|
faCloudDownloadAlt,
|
|
|
|
faEllipsisH,
|
|
|
|
faLock,
|
|
|
|
faTrashAlt,
|
|
|
|
faUnlock,
|
|
|
|
} 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 SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import deleteBackup from '@/api/server/backups/deleteBackup';
|
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';
|
2021-05-04 04:26:09 +00:00
|
|
|
import http, { httpErrorToHuman } from '@/api/http';
|
2022-06-20 15:17:33 +00:00
|
|
|
import { Dialog } from '@/components/elements/dialog';
|
2020-04-10 05:08:09 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
backup: ServerBackup;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ({ backup }: Props) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
|
|
|
const setServerFromState = ServerContext.useStoreActions((actions) => actions.server.setServerFromState);
|
|
|
|
const [modal, setModal] = useState('');
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [truncate, setTruncate] = 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)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((url) => {
|
2022-06-26 19:30:05 +00:00
|
|
|
// @ts-expect-error this is valid
|
2020-04-10 05:08:09 +00:00
|
|
|
window.location = url;
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2020-04-10 05:08:09 +00:00
|
|
|
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)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(() =>
|
|
|
|
mutate(
|
|
|
|
(data) => ({
|
|
|
|
...data,
|
|
|
|
items: data.items.filter((b) => b.uuid !== backup.uuid),
|
|
|
|
backupCount: data.backupCount - 1,
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.catch((error) => {
|
2020-04-10 05:08:09 +00:00
|
|
|
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-03-12 22:40:45 +00:00
|
|
|
const doRestorationAction = () => {
|
2021-01-31 02:01:32 +00:00
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('backups');
|
2021-03-12 21:47:49 +00:00
|
|
|
restoreServerBackup(uuid, backup.uuid, truncate)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(() =>
|
|
|
|
setServerFromState((s) => ({
|
|
|
|
...s,
|
|
|
|
status: 'restoring_backup',
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
.catch((error) => {
|
2021-01-31 02:01:32 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-05-04 04:26:09 +00:00
|
|
|
const onLockToggle = () => {
|
|
|
|
if (backup.isLocked && modal !== 'unlock') {
|
|
|
|
return setModal('unlock');
|
|
|
|
}
|
|
|
|
|
|
|
|
http.post(`/api/client/servers/${uuid}/backups/${backup.uuid}/lock`)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(() =>
|
|
|
|
mutate(
|
|
|
|
(data) => ({
|
|
|
|
...data,
|
|
|
|
items: data.items.map((b) =>
|
|
|
|
b.uuid !== backup.uuid
|
|
|
|
? b
|
|
|
|
: {
|
|
|
|
...b,
|
|
|
|
isLocked: !b.isLocked,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.catch((error) => alert(httpErrorToHuman(error)))
|
2021-05-04 04:26:09 +00:00
|
|
|
.then(() => setModal(''));
|
|
|
|
};
|
|
|
|
|
2020-04-10 05:08:09 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-06-20 15:17:33 +00:00
|
|
|
<Dialog.Confirm
|
|
|
|
open={modal === 'unlock'}
|
|
|
|
onClose={() => setModal('')}
|
|
|
|
title={`Unlock "${backup.name}"`}
|
2021-05-04 04:26:09 +00:00
|
|
|
onConfirmed={onLockToggle}
|
|
|
|
>
|
2022-06-20 15:17:33 +00:00
|
|
|
This backup will no longer be protected from automated or accidental deletions.
|
|
|
|
</Dialog.Confirm>
|
|
|
|
<Dialog.Confirm
|
|
|
|
open={modal === 'restore'}
|
|
|
|
onClose={() => setModal('')}
|
|
|
|
confirm={'Restore'}
|
|
|
|
title={`Restore "${backup.name}"`}
|
2021-03-12 22:40:45 +00:00
|
|
|
onConfirmed={() => doRestorationAction()}
|
2021-01-18 02:25:13 +00:00
|
|
|
>
|
2022-06-20 15:17:33 +00:00
|
|
|
<p>
|
|
|
|
Your server will be stopped. You will not be able to control the power state, access the file
|
|
|
|
manager, or create additional backups until completed.
|
2021-01-18 02:25:13 +00:00
|
|
|
</p>
|
2022-06-20 15:17:33 +00:00
|
|
|
<p css={tw`mt-4 -mb-2 bg-gray-700 p-3 rounded`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<label htmlFor={'restore_truncate'} css={tw`text-base 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'}
|
2021-03-12 21:47:49 +00:00
|
|
|
checked={truncate}
|
2022-06-26 19:13:52 +00:00
|
|
|
onChange={() => setTruncate((s) => !s)}
|
2021-01-18 02:25:13 +00:00
|
|
|
/>
|
2022-06-20 15:17:33 +00:00
|
|
|
Delete all files before restoring backup.
|
2021-01-18 02:25:13 +00:00
|
|
|
</label>
|
|
|
|
</p>
|
2022-06-20 15:17:33 +00:00
|
|
|
</Dialog.Confirm>
|
|
|
|
<Dialog.Confirm
|
|
|
|
title={`Delete "${backup.name}"`}
|
|
|
|
confirm={'Continue'}
|
|
|
|
open={modal === 'delete'}
|
|
|
|
onClose={() => setModal('')}
|
|
|
|
onConfirmed={doDeletion}
|
2020-04-10 05:08:09 +00:00
|
|
|
>
|
2022-06-20 15:17:33 +00:00
|
|
|
This is a permanent operation. The backup cannot be recovered once deleted.
|
|
|
|
</Dialog.Confirm>
|
2022-06-26 19:13:52 +00:00
|
|
|
<SpinnerOverlay visible={loading} fixed />
|
|
|
|
{backup.isSuccessful ? (
|
2020-08-21 04:44:33 +00:00
|
|
|
<DropdownMenu
|
2022-06-26 19:13:52 +00:00
|
|
|
renderToggle={(onClick) => (
|
2020-08-21 04:44:33 +00:00
|
|
|
<button
|
|
|
|
onClick={onClick}
|
2022-06-20 15:17:33 +00:00
|
|
|
css={tw`text-gray-200 transition-colors duration-150 hover:text-gray-100 p-2`}
|
2020-08-21 04:44:33 +00:00
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faEllipsisH} />
|
2020-08-21 04:44:33 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<div css={tw`text-sm`}>
|
|
|
|
<Can action={'backup.download'}>
|
2021-01-31 02:01:32 +00:00
|
|
|
<DropdownButtonRow onClick={doDownload}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faCloudDownloadAlt} css={tw`text-xs`} />
|
2020-08-21 04:44:33 +00:00
|
|
|
<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')}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon fixedWidth icon={faBoxOpen} css={tw`text-xs`} />
|
2021-01-18 02:25:13 +00:00
|
|
|
<span css={tw`ml-2`}>Restore</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
</Can>
|
2020-08-21 04:44:33 +00:00
|
|
|
<Can action={'backup.delete'}>
|
2021-05-04 04:26:09 +00:00
|
|
|
<>
|
|
|
|
<DropdownButtonRow onClick={onLockToggle}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
fixedWidth
|
|
|
|
icon={backup.isLocked ? faUnlock : faLock}
|
|
|
|
css={tw`text-xs mr-2`}
|
|
|
|
/>
|
|
|
|
{backup.isLocked ? 'Unlock' : 'Lock'}
|
|
|
|
</DropdownButtonRow>
|
2022-06-26 19:13:52 +00:00
|
|
|
{!backup.isLocked && (
|
|
|
|
<DropdownButtonRow danger onClick={() => setModal('delete')}>
|
|
|
|
<FontAwesomeIcon fixedWidth icon={faTrashAlt} css={tw`text-xs`} />
|
|
|
|
<span css={tw`ml-2`}>Delete</span>
|
|
|
|
</DropdownButtonRow>
|
|
|
|
)}
|
2021-05-04 04:26:09 +00:00
|
|
|
</>
|
2020-08-21 04:44:33 +00:00
|
|
|
</Can>
|
|
|
|
</div>
|
|
|
|
</DropdownMenu>
|
2022-06-26 19:13:52 +00:00
|
|
|
) : (
|
2020-08-21 04:44:33 +00:00
|
|
|
<button
|
2021-01-31 02:01:32 +00:00
|
|
|
onClick={() => setModal('delete')}
|
2022-06-20 15:17:33 +00:00
|
|
|
css={tw`text-gray-200 transition-colors duration-150 hover:text-gray-100 p-2`}
|
2020-08-21 04:44:33 +00:00
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FontAwesomeIcon icon={faTrashAlt} />
|
2020-08-21 04:44:33 +00:00
|
|
|
</button>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
2020-04-10 05:08:09 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|