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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
import { useContext, useEffect, useLayoutEffect, useState } from 'react';
|
2024-01-26 20:50:51 +01:00
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { requestManager } from '@/lib/requests/requests/RequestManager.ts';
|
2024-01-26 20:50:51 +01:00
|
|
|
import { TMigratableSource } from '@/components/MigrationCard.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
2024-01-26 20:50:51 +01:00
|
|
|
import { GridLayouts } from '@/components/source/GridLayouts.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
|
2024-10-05 19:33:09 +02:00
|
|
|
import { GridLayout } from '@/modules/library/contexts/LibraryOptionsContext.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-07-03 23:15:50 +02:00
|
|
|
import { GetSourceMigratableQuery, GetSourceMigratableQueryVariables } from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { GET_SOURCE_MIGRATABLE } from '@/lib/graphql/queries/SourceQuery.ts';
|
|
|
|
|
import { SOURCE_BASE_FIELDS } from '@/lib/graphql/fragments/SourceFragments.ts';
|
2024-07-08 18:02:50 +02:00
|
|
|
import { BaseMangaGrid } from '@/components/source/BaseMangaGrid.tsx';
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
export const Migrate = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
|
|
|
|
|
|
|
|
|
const { sourceId: paramSourceId } = useParams<{ sourceId: string }>();
|
|
|
|
|
|
|
|
|
|
const [gridLayout, setGridLayout] = useLocalStorage('migrateGridLayout', GridLayout.List);
|
|
|
|
|
|
|
|
|
|
const fragmentSource = requestManager.graphQLClient.client.cache.readFragment<
|
|
|
|
|
Pick<TMigratableSource, 'id' | 'name'>
|
|
|
|
|
>({
|
|
|
|
|
id: requestManager.graphQLClient.client.cache.identify({ __typename: 'SourceType', id: paramSourceId }),
|
2024-07-03 23:15:50 +02:00
|
|
|
fragment: SOURCE_BASE_FIELDS,
|
2024-01-26 20:50:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [isKnownSource, setIsKnownSource] = useState(fragmentSource !== null ? true : undefined);
|
|
|
|
|
const {
|
|
|
|
|
data: migratableSourceData,
|
|
|
|
|
loading: isSourceLoading,
|
|
|
|
|
error: sourceError,
|
2024-04-27 22:14:42 +02:00
|
|
|
refetch: refetchSource,
|
2024-07-03 23:15:50 +02:00
|
|
|
} = requestManager.useGetSource<GetSourceMigratableQuery, GetSourceMigratableQueryVariables>(
|
|
|
|
|
GET_SOURCE_MIGRATABLE,
|
|
|
|
|
paramSourceId,
|
|
|
|
|
{ skip: !!isKnownSource, notifyOnNetworkStatusChange: true },
|
|
|
|
|
);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
const { sourceId, name } = {
|
|
|
|
|
sourceId: paramSourceId,
|
|
|
|
|
name: paramSourceId,
|
|
|
|
|
...fragmentSource,
|
|
|
|
|
...migratableSourceData?.source,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
data: migratableSourceMangasData,
|
|
|
|
|
loading: areMangasLoading,
|
|
|
|
|
error: mangasError,
|
2024-04-27 22:14:42 +02:00
|
|
|
refetch: refetchMangas,
|
2024-01-26 20:50:51 +01:00
|
|
|
} = requestManager.useGetMigratableSourceMangas(sourceId, {
|
|
|
|
|
skip: !isKnownSource,
|
2024-04-27 22:14:42 +02:00
|
|
|
notifyOnNetworkStatusChange: true,
|
2024-01-26 20:50:51 +01:00
|
|
|
});
|
|
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2024-01-26 20:50:51 +01:00
|
|
|
setTitle(name ?? sourceId ?? t('migrate.title'));
|
|
|
|
|
setAction(<GridLayouts gridLayout={gridLayout} onChange={setGridLayout} />);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
|
|
|
|
}, [t, name, sourceId, gridLayout]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isSourceLoading || isKnownSource) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsKnownSource(
|
|
|
|
|
!!migratableSourceData ||
|
|
|
|
|
!!sourceError?.message.includes("The field at path '/source' was declared as a non null type"),
|
|
|
|
|
);
|
|
|
|
|
}, [isSourceLoading, sourceError]);
|
|
|
|
|
|
|
|
|
|
const isLoadingSource = isSourceLoading || (!sourceError && !isKnownSource);
|
|
|
|
|
const isLoading = isLoadingSource || areMangasLoading;
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasErrorSource = sourceError && isKnownSource === false;
|
|
|
|
|
const hasError = hasErrorSource || mangasError;
|
|
|
|
|
if (hasError) {
|
|
|
|
|
const error = (hasErrorSource ? sourceError : mangasError)!;
|
2024-04-27 22:14:42 +02:00
|
|
|
return (
|
2024-04-29 15:29:33 +02:00
|
|
|
<EmptyViewAbsoluteCentered
|
2024-04-27 22:14:42 +02:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
|
|
|
|
messageExtra={error.message}
|
|
|
|
|
retry={() => {
|
|
|
|
|
if (hasErrorSource) {
|
|
|
|
|
refetchSource().catch(defaultPromiseErrorHandler('Migrate::refetchSource'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mangasError) {
|
|
|
|
|
refetchMangas().catch(defaultPromiseErrorHandler('Migrate::refetchMangas'));
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2024-01-26 20:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-08 18:02:50 +02:00
|
|
|
<BaseMangaGrid
|
2024-01-26 20:50:51 +01:00
|
|
|
hasNextPage={false}
|
|
|
|
|
loadMore={() => {}}
|
|
|
|
|
isLoading={areMangasLoading}
|
2024-07-08 18:02:50 +02:00
|
|
|
mangas={migratableSourceMangasData?.mangas.nodes ?? []}
|
2024-01-26 20:50:51 +01:00
|
|
|
gridLayout={gridLayout}
|
|
|
|
|
mode="migrate.search"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|