2020-04-05 02:54:59 +00:00
|
|
|
import React, { useState } from 'react';
|
2020-04-04 20:24:03 +00:00
|
|
|
import { ServerBackup } from '@/api/server/backups/getServerBackups';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faArchive } from '@fortawesome/free-solid-svg-icons/faArchive';
|
|
|
|
import format from 'date-fns/format';
|
2020-04-05 02:54:59 +00:00
|
|
|
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
|
2020-04-04 20:24:03 +00:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import { faCloudDownloadAlt } from '@fortawesome/free-solid-svg-icons/faCloudDownloadAlt';
|
2020-04-05 02:54:59 +00:00
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
|
|
|
import { bytesToHuman } from '@/helpers';
|
|
|
|
import Can from '@/components/elements/Can';
|
|
|
|
import useServer from '@/plugins/useServer';
|
2020-04-07 03:28:14 +00:00
|
|
|
import getBackupDownloadUrl from '@/api/server/backups/getBackupDownloadUrl';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2020-04-07 04:22:57 +00:00
|
|
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
2020-04-07 05:25:54 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-04-04 20:24:03 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
backup: ServerBackup;
|
|
|
|
className?: string;
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:54:59 +00:00
|
|
|
const DownloadModal = ({ checksum, ...props }: RequiredModalProps & { checksum: string }) => (
|
|
|
|
<Modal {...props}>
|
|
|
|
<h3 className={'mb-6'}>Verify file checksum</h3>
|
|
|
|
<p className={'text-sm'}>
|
|
|
|
The SHA256 checksum of this file is:
|
|
|
|
</p>
|
|
|
|
<pre className={'mt-2 text-sm p-2 bg-neutral-900 rounded'}>
|
|
|
|
<code className={'block font-mono'}>{checksum}</code>
|
|
|
|
</pre>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
|
2020-04-07 05:25:54 +00:00
|
|
|
export default ({ backup, className }: Props) => {
|
2020-04-05 02:54:59 +00:00
|
|
|
const { uuid } = useServer();
|
2020-04-07 03:28:14 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
|
|
|
const [ loading, setLoading ] = useState(false);
|
2020-04-05 02:54:59 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
|
2020-04-07 05:25:54 +00:00
|
|
|
const appendBackup = ServerContext.useStoreActions(actions => actions.backups.appendBackup);
|
|
|
|
|
2020-04-07 04:22:57 +00:00
|
|
|
useWebsocketEvent(`backup completed:${backup.uuid}`, data => {
|
|
|
|
try {
|
|
|
|
const parsed = JSON.parse(data);
|
2020-04-07 05:25:54 +00:00
|
|
|
appendBackup({
|
2020-04-07 04:22:57 +00:00
|
|
|
...backup,
|
|
|
|
sha256Hash: parsed.sha256_hash || '',
|
|
|
|
bytes: parsed.file_size || 0,
|
|
|
|
completedAt: new Date(),
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-07 03:28:14 +00:00
|
|
|
const getBackupLink = () => {
|
|
|
|
setLoading(true);
|
|
|
|
clearFlashes('backups');
|
|
|
|
getBackupDownloadUrl(uuid, backup.uuid)
|
|
|
|
.then(url => {
|
|
|
|
// @ts-ignore
|
|
|
|
window.location = url;
|
|
|
|
setVisible(true);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'backups', message: httpErrorToHuman(error) });
|
|
|
|
})
|
|
|
|
.then(() => setLoading(false));
|
|
|
|
};
|
|
|
|
|
2020-04-04 20:24:03 +00:00
|
|
|
return (
|
|
|
|
<div className={`grey-row-box flex items-center ${className}`}>
|
2020-04-07 03:28:14 +00:00
|
|
|
<SpinnerOverlay visible={loading} fixed={true}/>
|
2020-04-05 02:54:59 +00:00
|
|
|
{visible &&
|
|
|
|
<DownloadModal
|
|
|
|
visible={visible}
|
|
|
|
appear={true}
|
|
|
|
onDismissed={() => setVisible(false)}
|
|
|
|
checksum={backup.sha256Hash}
|
|
|
|
/>
|
|
|
|
}
|
2020-04-04 20:24:03 +00:00
|
|
|
<div className={'mr-4'}>
|
2020-04-05 02:54:59 +00:00
|
|
|
{backup.completedAt ?
|
|
|
|
<FontAwesomeIcon icon={faArchive} className={'text-neutral-300'}/>
|
|
|
|
:
|
|
|
|
<Spinner size={'tiny'}/>
|
|
|
|
}
|
2020-04-04 20:24:03 +00:00
|
|
|
</div>
|
|
|
|
<div className={'flex-1'}>
|
2020-04-05 02:54:59 +00:00
|
|
|
<p className={'text-sm mb-1'}>
|
|
|
|
{backup.name}
|
|
|
|
{backup.completedAt &&
|
|
|
|
<span className={'ml-3 text-neutral-300 text-xs font-thin'}>{bytesToHuman(backup.bytes)}</span>
|
|
|
|
}
|
|
|
|
</p>
|
|
|
|
<p className={'text-xs text-neutral-400 font-mono'}>
|
|
|
|
{backup.uuid}
|
|
|
|
</p>
|
2020-04-04 20:24:03 +00:00
|
|
|
</div>
|
2020-04-05 02:54:59 +00:00
|
|
|
<div className={'ml-8 text-center'}>
|
2020-04-04 20:24:03 +00:00
|
|
|
<p
|
|
|
|
title={format(backup.createdAt, 'ddd, MMMM Do, YYYY HH:mm:ss Z')}
|
|
|
|
className={'text-sm'}
|
|
|
|
>
|
|
|
|
{distanceInWordsToNow(backup.createdAt, { includeSeconds: true, addSuffix: true })}
|
|
|
|
</p>
|
|
|
|
<p className={'text-2xs text-neutral-500 uppercase mt-1'}>Created</p>
|
|
|
|
</div>
|
2020-04-05 02:54:59 +00:00
|
|
|
<Can action={'backup.download'}>
|
|
|
|
<div className={'ml-6'} style={{ marginRight: '-0.5rem' }}>
|
|
|
|
{!backup.completedAt ?
|
|
|
|
<div className={'p-2 invisible'}>
|
|
|
|
<FontAwesomeIcon icon={faCloudDownloadAlt}/>
|
|
|
|
</div>
|
|
|
|
:
|
2020-04-07 03:28:14 +00:00
|
|
|
<button
|
|
|
|
onClick={() => getBackupLink()}
|
2020-04-05 02:54:59 +00:00
|
|
|
className={'text-sm text-neutral-300 p-2 transition-colors duration-250 hover:text-cyan-400'}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faCloudDownloadAlt}/>
|
2020-04-07 03:28:14 +00:00
|
|
|
</button>
|
2020-04-05 02:54:59 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</Can>
|
2020-04-04 20:24:03 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|