Automatically update the backup view when the backup is completed
This commit is contained in:
parent
b1e7e0b8b0
commit
7f2b477538
3 changed files with 45 additions and 2 deletions
|
@ -44,6 +44,9 @@ export default () => {
|
||||||
{backups.map((backup, index) => <BackupRow
|
{backups.map((backup, index) => <BackupRow
|
||||||
key={backup.uuid}
|
key={backup.uuid}
|
||||||
backup={backup}
|
backup={backup}
|
||||||
|
onBackupUpdated={data => setBackups(
|
||||||
|
s => ([ ...s.map(b => b.uuid === data.uuid ? data : b) ]),
|
||||||
|
)}
|
||||||
className={index !== (backups.length - 1) ? 'mb-2' : undefined}
|
className={index !== (backups.length - 1) ? 'mb-2' : undefined}
|
||||||
/>)}
|
/>)}
|
||||||
</div>
|
</div>
|
||||||
|
@ -51,7 +54,7 @@ export default () => {
|
||||||
<Can action={'backup.create'}>
|
<Can action={'backup.create'}>
|
||||||
<div className={'mt-6 flex justify-end'}>
|
<div className={'mt-6 flex justify-end'}>
|
||||||
<CreateBackupButton
|
<CreateBackupButton
|
||||||
onBackupGenerated={backup => setBackups(s => [...s, backup])}
|
onBackupGenerated={backup => setBackups(s => [ ...s, backup ])}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</Can>
|
</Can>
|
||||||
|
|
|
@ -14,9 +14,11 @@ import getBackupDownloadUrl from '@/api/server/backups/getBackupDownloadUrl';
|
||||||
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
||||||
import useFlash from '@/plugins/useFlash';
|
import useFlash from '@/plugins/useFlash';
|
||||||
import { httpErrorToHuman } from '@/api/http';
|
import { httpErrorToHuman } from '@/api/http';
|
||||||
|
import useWebsocketEvent from '@/plugins/useWebsocketEvent';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
backup: ServerBackup;
|
backup: ServerBackup;
|
||||||
|
onBackupUpdated: (backup: ServerBackup) => void;
|
||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,12 +34,26 @@ const DownloadModal = ({ checksum, ...props }: RequiredModalProps & { checksum:
|
||||||
</Modal>
|
</Modal>
|
||||||
);
|
);
|
||||||
|
|
||||||
export default ({ backup, className }: Props) => {
|
export default ({ backup, onBackupUpdated, className }: Props) => {
|
||||||
const { uuid } = useServer();
|
const { uuid } = useServer();
|
||||||
const { addError, clearFlashes } = useFlash();
|
const { addError, clearFlashes } = useFlash();
|
||||||
const [ loading, setLoading ] = useState(false);
|
const [ loading, setLoading ] = useState(false);
|
||||||
const [ visible, setVisible ] = useState(false);
|
const [ visible, setVisible ] = useState(false);
|
||||||
|
|
||||||
|
useWebsocketEvent(`backup completed:${backup.uuid}`, data => {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(data);
|
||||||
|
onBackupUpdated({
|
||||||
|
...backup,
|
||||||
|
sha256Hash: parsed.sha256_hash || '',
|
||||||
|
bytes: parsed.file_size || 0,
|
||||||
|
completedAt: new Date(),
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const getBackupLink = () => {
|
const getBackupLink = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
clearFlashes('backups');
|
clearFlashes('backups');
|
||||||
|
|
24
resources/scripts/plugins/useWebsocketEvent.ts
Normal file
24
resources/scripts/plugins/useWebsocketEvent.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { ServerContext } from '@/state/server';
|
||||||
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
|
const useWebsocketEvent = (event: string, callback: (data: string) => void) => {
|
||||||
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
||||||
|
const savedCallback = useRef<any>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
savedCallback.current = callback;
|
||||||
|
}, [callback]);
|
||||||
|
|
||||||
|
return useEffect(() => {
|
||||||
|
const eventListener = (event: any) => savedCallback.current(event);
|
||||||
|
if (connected && instance) {
|
||||||
|
instance.addListener(event, eventListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
instance && instance.removeListener(event, eventListener);
|
||||||
|
};
|
||||||
|
}, [ event, connected, instance ]);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useWebsocketEvent;
|
Loading…
Add table
Reference in a new issue