Files
suwayomi-material-you-webui/src/screens/settings/Backup.tsx
schroda 58eb2fead3 Feature/enforce license notice in each file via eslint rule (#304)
* [ESLint] Add "eslint-plugin-header" as dev dependency

* [ESLint::eslint-plugin-header] Setup rule

* [ESLint::eslint-plugin-header] Fix errors
2023-05-18 13:17:41 +02:00

96 lines
3.5 KiB
TypeScript

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import React, { useContext, useEffect } from 'react';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import { fromEvent } from 'file-selector';
import client from 'util/client';
import makeToast from 'components/util/Toast';
import ListItemLink from 'components/util/ListItemLink';
import NavbarContext from 'components/context/NavbarContext';
import { useTranslation } from 'react-i18next';
export default function Backup() {
const { t } = useTranslation();
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => {
setTitle(t('settings.backup.title'));
setAction(null);
}, [t]);
const { baseURL } = client.defaults;
const submitBackup = (file: File) => {
if (file.name.toLowerCase().endsWith('proto.gz')) {
const formData = new FormData();
formData.append('backup.proto.gz', file);
makeToast(t('settings.backup.label.restoring_backup'), 'info');
client
.post('/api/v1/backup/import/file', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
.then(() => makeToast(t('settings.backup.label.restored_backup'), 'success'))
.catch(() => makeToast(t('settings.backup.label.backup_restore_failed'), 'error'));
} else if (file.name.toLowerCase().endsWith('json')) {
makeToast(t('settings.backup.label.legacy_backup_unsupported'), 'error');
} else {
makeToast(t('global.error.label.invalid_file_type'), 'error');
}
};
const dropHandler = async (e: Event) => {
e.preventDefault();
const files = await fromEvent(e);
submitBackup(files[0] as File);
};
const dragOverHandler = (e: Event) => {
e.preventDefault();
};
useEffect(() => {
document.addEventListener('drop', dropHandler);
document.addEventListener('dragover', dragOverHandler);
const input = document.getElementById('backup-file');
input?.addEventListener('change', async (evt) => {
const files = await fromEvent(evt);
submitBackup(files[0] as File);
});
return () => {
document.removeEventListener('drop', dropHandler);
document.removeEventListener('dragover', dragOverHandler);
};
}, []);
return (
<>
<List sx={{ padding: 0 }}>
<ListItemLink to={`${baseURL}/api/v1/backup/export/file`} directLink>
<ListItemText
primary={t('settings.backup.label.create_backup')}
secondary={t('settings.backup.label.create_backup_info')}
/>
</ListItemLink>
<ListItem button onClick={() => document.getElementById('backup-file')?.click()}>
<ListItemText
primary={t('settings.backup.label.restore_backup')}
secondary={t('settings.backup.label.restore_backup_info')}
/>
</ListItem>
</List>
<input type="file" id="backup-file" style={{ display: 'none' }} />
</>
);
}