2024-01-26 20:50:51 +01:00
|
|
|
/*
|
|
|
|
|
* 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 Dialog from '@mui/material/Dialog';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Stack from '@mui/material/Stack';
|
2024-01-26 20:50:51 +01:00
|
|
|
import { Link, useNavigate, useParams } from 'react-router-dom';
|
|
|
|
|
import { useState } from 'react';
|
|
|
|
|
import FormGroup from '@mui/material/FormGroup';
|
|
|
|
|
import { CheckboxInput } from '@/components/atoms/CheckboxInput.tsx';
|
|
|
|
|
import { Mangas, MigrateMode } from '@/lib/data/Mangas.ts';
|
|
|
|
|
import { makeToast } from '@/components/util/Toast.tsx';
|
2024-03-16 16:21:15 +01:00
|
|
|
import {
|
|
|
|
|
createUpdateMetadataServerSettings,
|
|
|
|
|
useMetadataServerSettings,
|
|
|
|
|
} from '@/lib/metadata/metadataServerSettings.ts';
|
|
|
|
|
import { MetadataMigrationSettings } from '@/typings.ts';
|
2024-03-16 14:02:54 +01:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
|
|
|
|
|
2024-01-26 20:50:51 +01:00
|
|
|
export const MigrateDialog = ({ mangaIdToMigrateTo, onClose }: { mangaIdToMigrateTo: number; onClose: () => void }) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
|
|
const { mangaId: mangaIdAsString } = useParams<{ mangaId: string }>();
|
|
|
|
|
const mangaId = Number(mangaIdAsString);
|
|
|
|
|
|
2024-03-16 14:02:54 +01:00
|
|
|
const {
|
|
|
|
|
settings: { includeChapters, includeCategories, deleteChapters },
|
|
|
|
|
} = useMetadataServerSettings();
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
const [isMigrationInProcess, setIsMigrationInProcess] = useState(false);
|
|
|
|
|
|
2024-03-16 16:21:15 +01:00
|
|
|
const setMigrationFlag = createUpdateMetadataServerSettings<keyof MetadataMigrationSettings>(
|
|
|
|
|
defaultPromiseErrorHandler('MigrateDialog::updateSetting'),
|
|
|
|
|
);
|
2024-03-16 14:02:54 +01:00
|
|
|
|
2024-01-26 20:50:51 +01:00
|
|
|
const migrate = async (mode: MigrateMode) => {
|
|
|
|
|
if (mangaId == null) {
|
|
|
|
|
throw new Error(`MigrateDialog::migrate: unexpected mangaId "${mangaId}"`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
makeToast(t('migrate.label.info'), 'info');
|
|
|
|
|
|
|
|
|
|
setIsMigrationInProcess(true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await Mangas.migrate(mangaId, mangaIdToMigrateTo, {
|
|
|
|
|
mode,
|
|
|
|
|
migrateChapters: includeChapters,
|
|
|
|
|
migrateCategories: includeCategories,
|
2024-03-16 13:45:54 +01:00
|
|
|
deleteChapters,
|
2024-01-26 20:50:51 +01:00
|
|
|
});
|
|
|
|
|
|
2024-03-16 13:45:47 +01:00
|
|
|
navigate(`/manga/${mangaIdToMigrateTo}`, { replace: true });
|
2024-01-26 20:50:51 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
setIsMigrationInProcess(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open fullWidth onClose={onClose}>
|
|
|
|
|
<DialogTitle>{t('migrate.dialog.title')}</DialogTitle>
|
|
|
|
|
<DialogContent dividers>
|
|
|
|
|
<FormGroup>
|
|
|
|
|
<CheckboxInput
|
|
|
|
|
disabled={isMigrationInProcess}
|
|
|
|
|
label={t('chapter.title')}
|
|
|
|
|
checked={includeChapters}
|
2024-03-16 14:02:54 +01:00
|
|
|
onChange={(_, checked) => setMigrationFlag('includeChapters', checked)}
|
2024-01-26 20:50:51 +01:00
|
|
|
/>
|
|
|
|
|
<CheckboxInput
|
|
|
|
|
disabled={isMigrationInProcess}
|
|
|
|
|
label={t('category.title.category_one')}
|
|
|
|
|
checked={includeCategories}
|
2024-03-16 14:02:54 +01:00
|
|
|
onChange={(_, checked) => setMigrationFlag('includeCategories', checked)}
|
2024-01-26 20:50:51 +01:00
|
|
|
/>
|
2024-03-16 13:45:54 +01:00
|
|
|
<CheckboxInput
|
|
|
|
|
disabled={isMigrationInProcess}
|
|
|
|
|
label={t('migrate.dialog.label.delete_downloaded')}
|
|
|
|
|
checked={deleteChapters}
|
2024-03-16 14:02:54 +01:00
|
|
|
onChange={(_, checked) => setMigrationFlag('deleteChapters', checked)}
|
2024-03-16 13:45:54 +01:00
|
|
|
/>
|
2024-01-26 20:50:51 +01:00
|
|
|
</FormGroup>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
<Stack sx={{ width: '100%' }} direction="row" justifyContent="space-between">
|
|
|
|
|
<Button disabled={isMigrationInProcess} component={Link} to={`/manga/${mangaIdToMigrateTo}`}>
|
|
|
|
|
{t('migrate.dialog.action.button.show_entry')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Stack direction="row">
|
|
|
|
|
<Button disabled={isMigrationInProcess} onClick={onClose}>
|
|
|
|
|
{t('global.button.cancel')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button disabled={isMigrationInProcess} onClick={() => migrate('copy')}>
|
|
|
|
|
{t('global.button.copy')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button disabled={isMigrationInProcess} onClick={() => migrate('migrate')}>
|
|
|
|
|
{t('global.button.migrate')}
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|