2024-05-04 15:41:21 +02: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 { useTranslation } from 'react-i18next';
|
2024-09-26 15:33:22 +02:00
|
|
|
import { useContext, useLayoutEffect, useMemo } from 'react';
|
2024-05-08 15:06:47 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import SettingsIcon from '@mui/icons-material/Settings';
|
|
|
|
|
import PopupState, { bindMenu, bindTrigger } from 'material-ui-popup-state';
|
|
|
|
|
import Menu from '@mui/material/Menu';
|
|
|
|
|
import MenuItem from '@mui/material/MenuItem';
|
2024-05-08 18:51:09 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { requestManager } from '@/lib/requests/requests/RequestManager.ts';
|
2024-05-04 15:41:21 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.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-05-04 15:41:21 +02:00
|
|
|
import { GridLayouts } from '@/components/source/GridLayouts.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { CheckboxInput } from '@/modules/core/components/inputs/CheckboxInput.tsx';
|
|
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-05 19:21:26 +02:00
|
|
|
import { MangaCard } from '@/modules/manga/components/cards/MangaCard.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { StyledGroupedVirtuoso } from '@/modules/core/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/modules/core/components/virtuoso/StyledGroupHeader.tsx';
|
2024-07-08 18:02:50 +02:00
|
|
|
import {
|
|
|
|
|
GetMangasDuplicatesQuery,
|
|
|
|
|
GetMangasDuplicatesQueryVariables,
|
|
|
|
|
MangaType,
|
|
|
|
|
} from '@/lib/graphql/generated/graphql.ts';
|
|
|
|
|
import { GET_MANGAS_DUPLICATES } from '@/lib/graphql/queries/MangaQuery.ts';
|
|
|
|
|
import { BaseMangaGrid } from '@/components/source/BaseMangaGrid.tsx';
|
2024-10-05 19:21:26 +02:00
|
|
|
import { IMangaGridProps } from '@/modules/manga/components/MangaGrid.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { StyledGroupItemWrapper } from '@/modules/core/components/virtuoso/StyledGroupItemWrapper.tsx';
|
2024-08-14 14:03:53 +02:00
|
|
|
import { enhancedCleanup } from '@/lib/data/Strings.ts';
|
2024-07-13 15:54:43 +02:00
|
|
|
|
2024-07-08 18:02:50 +02:00
|
|
|
const findDuplicatesByTitle = <Manga extends Pick<MangaType, 'title'>>(
|
|
|
|
|
libraryMangas: Manga[],
|
|
|
|
|
): Record<string, Manga[]> => {
|
2024-08-14 14:03:53 +02:00
|
|
|
const titleToMangas = Object.groupBy(libraryMangas, ({ title }) => enhancedCleanup(title));
|
2024-05-08 15:06:47 +02:00
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
return Object.fromEntries(
|
|
|
|
|
Object.entries(titleToMangas)
|
2024-07-08 18:02:50 +02:00
|
|
|
.filter((titleToMangaMap): titleToMangaMap is [string, Manga[]] => (titleToMangaMap[1]?.length ?? 0) > 1)
|
2024-05-08 18:51:09 +02:00
|
|
|
.map(([, mangas]) => [mangas[0].title, mangas]),
|
|
|
|
|
);
|
2024-05-08 15:06:47 +02:00
|
|
|
};
|
|
|
|
|
|
2024-07-08 18:02:50 +02:00
|
|
|
type TMangaDuplicate = Pick<MangaType, 'id' | 'title' | 'description'>;
|
|
|
|
|
const findDuplicatesByTitleAndAlternativeTitles = <Manga extends TMangaDuplicate>(
|
|
|
|
|
libraryMangas: Manga[],
|
|
|
|
|
): Record<string, Manga[]> => {
|
2024-07-13 16:51:51 +02:00
|
|
|
const titleToMangas: Record<string, Set<Manga>> = {};
|
|
|
|
|
const titleToAlternativeTitleMatches: Record<string, Set<Manga>> = {};
|
|
|
|
|
|
|
|
|
|
libraryMangas.forEach((mangaToCheck) => {
|
2024-08-14 14:03:53 +02:00
|
|
|
const titleToCheck = enhancedCleanup(mangaToCheck.title);
|
2024-07-13 16:51:51 +02:00
|
|
|
|
|
|
|
|
titleToMangas[titleToCheck] ??= new Set();
|
|
|
|
|
titleToMangas[titleToCheck].add(mangaToCheck);
|
|
|
|
|
|
|
|
|
|
titleToAlternativeTitleMatches[titleToCheck] ??= new Set();
|
|
|
|
|
titleToAlternativeTitleMatches[titleToCheck].add(mangaToCheck);
|
2024-05-08 15:06:47 +02:00
|
|
|
|
|
|
|
|
libraryMangas.forEach((libraryManga) => {
|
|
|
|
|
const isDifferentManga = mangaToCheck.id !== libraryManga.id;
|
|
|
|
|
if (!isDifferentManga) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 14:03:53 +02:00
|
|
|
const doesTitleMatch = enhancedCleanup(libraryManga.title) === titleToCheck;
|
|
|
|
|
const doesAlternativeTitleMatch = enhancedCleanup(libraryManga?.description ?? '').includes(titleToCheck);
|
2024-05-08 15:06:47 +02:00
|
|
|
|
|
|
|
|
const isDuplicate = doesTitleMatch || doesAlternativeTitleMatch;
|
|
|
|
|
if (!isDuplicate) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-13 16:51:51 +02:00
|
|
|
if (doesTitleMatch) {
|
|
|
|
|
titleToMangas[titleToCheck].add(libraryManga);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doesAlternativeTitleMatch) {
|
|
|
|
|
titleToAlternativeTitleMatches[titleToCheck].add(libraryManga);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const titleToDuplicatesEntries = Object.entries(titleToMangas)
|
|
|
|
|
.map(([title, titleMatches]) => {
|
|
|
|
|
const originalTitle = [...titleMatches][0].title;
|
|
|
|
|
|
|
|
|
|
const combinedDuplicates = [...titleMatches, ...(titleToAlternativeTitleMatches[title] ?? [])];
|
|
|
|
|
const duplicates = [...new Set([...combinedDuplicates])];
|
|
|
|
|
|
|
|
|
|
const noDuplicatesFound = duplicates.length === 1;
|
|
|
|
|
if (noDuplicatesFound) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [originalTitle, duplicates];
|
|
|
|
|
})
|
|
|
|
|
.filter((entry) => !!entry);
|
|
|
|
|
|
|
|
|
|
return Object.fromEntries(titleToDuplicatesEntries);
|
2024-05-08 15:06:47 +02:00
|
|
|
};
|
2024-05-04 15:41:21 +02:00
|
|
|
|
|
|
|
|
export const LibraryDuplicates = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-05-04 15:52:16 +02:00
|
|
|
const [gridLayout, setGridLayout] = useLocalStorage('libraryDuplicatesGridLayout', GridLayout.List);
|
2024-05-08 15:06:47 +02:00
|
|
|
const [checkAlternativeTitles, setCheckAlternativeTitles] = useLocalStorage(
|
|
|
|
|
'libraryDuplicatesCheckAlternativeTitles',
|
|
|
|
|
false,
|
|
|
|
|
);
|
2024-05-04 15:41:21 +02:00
|
|
|
|
|
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2024-05-04 15:41:21 +02:00
|
|
|
setTitle(t('library.settings.advanced.duplicates.label.title'));
|
2024-05-08 15:06:47 +02:00
|
|
|
setAction(
|
|
|
|
|
<>
|
|
|
|
|
<GridLayouts gridLayout={gridLayout} onChange={setGridLayout} />
|
|
|
|
|
<PopupState variant="popover" popupId="library-dupliactes-settings">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<>
|
2024-09-06 16:59:27 +02:00
|
|
|
<IconButton {...bindTrigger(popupState)} color="inherit">
|
2024-05-08 15:06:47 +02:00
|
|
|
<SettingsIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<Menu {...bindMenu(popupState)}>
|
|
|
|
|
<MenuItem>
|
|
|
|
|
<CheckboxInput
|
|
|
|
|
label={t(
|
|
|
|
|
'library.settings.advanced.duplicates.settings.label.check_description',
|
|
|
|
|
)}
|
|
|
|
|
checked={checkAlternativeTitles}
|
|
|
|
|
onChange={(_, checked) => setCheckAlternativeTitles(checked)}
|
|
|
|
|
/>
|
|
|
|
|
</MenuItem>
|
|
|
|
|
</Menu>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
|
|
|
|
</>,
|
|
|
|
|
);
|
2024-05-04 15:41:21 +02:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2024-05-08 15:06:47 +02:00
|
|
|
}, [t, gridLayout, checkAlternativeTitles]);
|
2024-05-04 15:41:21 +02:00
|
|
|
|
2024-07-08 18:02:50 +02:00
|
|
|
const { data, loading, error, refetch } = requestManager.useGetMangas<
|
|
|
|
|
GetMangasDuplicatesQuery,
|
|
|
|
|
GetMangasDuplicatesQueryVariables
|
|
|
|
|
>(GET_MANGAS_DUPLICATES, { condition: { inLibrary: true } });
|
2024-05-04 15:41:21 +02:00
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
const mangasByTitle = useMemo(() => {
|
2024-07-08 18:02:50 +02:00
|
|
|
const libraryMangas: TMangaDuplicate[] = data?.mangas.nodes ?? [];
|
2024-05-04 15:41:21 +02:00
|
|
|
|
2024-05-08 15:06:47 +02:00
|
|
|
if (checkAlternativeTitles) {
|
|
|
|
|
return findDuplicatesByTitleAndAlternativeTitles(libraryMangas);
|
|
|
|
|
}
|
2024-05-04 15:41:21 +02:00
|
|
|
|
2024-05-08 15:06:47 +02:00
|
|
|
return findDuplicatesByTitle(libraryMangas);
|
|
|
|
|
}, [data?.mangas.nodes, checkAlternativeTitles]);
|
2024-05-04 15:41:21 +02:00
|
|
|
|
2024-08-26 16:28:44 +02:00
|
|
|
const duplicatedTitles = useMemo(
|
|
|
|
|
() => Object.keys(mangasByTitle).toSorted((titleA, titleB) => titleA.localeCompare(titleB)),
|
|
|
|
|
[mangasByTitle],
|
|
|
|
|
);
|
|
|
|
|
const duplicatedMangas = useMemo(
|
|
|
|
|
() => duplicatedTitles.map((title) => mangasByTitle[title]).flat(),
|
|
|
|
|
[mangasByTitle],
|
|
|
|
|
);
|
2024-05-08 18:51:09 +02:00
|
|
|
const mangasCountByTitle = useMemo(
|
2024-08-26 16:28:44 +02:00
|
|
|
() => duplicatedTitles.map((title) => mangasByTitle[title]).map((mangas) => mangas.length),
|
2024-05-08 18:51:09 +02:00
|
|
|
[mangasByTitle],
|
2024-05-04 15:41:21 +02:00
|
|
|
);
|
2024-05-08 18:51:09 +02:00
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyViewAbsoluteCentered
|
2024-05-24 14:04:19 +02:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
2024-05-08 18:51:09 +02:00
|
|
|
messageExtra={error.message}
|
|
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('LibraryDuplicates::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gridLayout === GridLayout.List) {
|
|
|
|
|
return (
|
|
|
|
|
<StyledGroupedVirtuoso
|
|
|
|
|
groupCounts={mangasCountByTitle}
|
|
|
|
|
groupContent={(index) => (
|
|
|
|
|
<StyledGroupHeader variant="h5" isFirstItem={index === 0}>
|
|
|
|
|
{duplicatedTitles[index]}
|
|
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
|
|
|
|
itemContent={(index) => (
|
2024-07-11 17:22:52 +02:00
|
|
|
<StyledGroupItemWrapper key={duplicatedMangas[index].id}>
|
2024-05-08 18:59:01 +02:00
|
|
|
<MangaCard
|
2024-07-08 18:02:50 +02:00
|
|
|
manga={duplicatedMangas[index] as IMangaGridProps['mangas'][number]}
|
2024-05-08 18:59:01 +02:00
|
|
|
gridLayout={gridLayout}
|
|
|
|
|
selected={null}
|
|
|
|
|
mode="duplicate"
|
|
|
|
|
/>
|
2024-07-11 17:22:52 +02:00
|
|
|
</StyledGroupItemWrapper>
|
2024-05-08 18:51:09 +02:00
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return duplicatedTitles.map((title, index) => (
|
|
|
|
|
<Box key={title}>
|
|
|
|
|
<StyledGroupHeader sx={{ pt: index === 0 ? undefined : 0, pb: 0 }} variant="h5" isFirstItem={false}>
|
|
|
|
|
{title}
|
|
|
|
|
</StyledGroupHeader>
|
2024-07-08 18:02:50 +02:00
|
|
|
<BaseMangaGrid
|
|
|
|
|
mangas={mangasByTitle[title] as IMangaGridProps['mangas']}
|
2024-05-08 18:51:09 +02:00
|
|
|
hasNextPage={false}
|
|
|
|
|
loadMore={() => {}}
|
|
|
|
|
isLoading={false}
|
|
|
|
|
gridLayout={gridLayout}
|
2024-07-08 18:02:50 +02:00
|
|
|
inLibraryIndicator={false}
|
2024-05-08 18:51:09 +02:00
|
|
|
horizontal
|
2024-05-08 18:59:01 +02:00
|
|
|
mode="duplicate"
|
2024-05-08 18:51:09 +02:00
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
));
|
2024-05-04 15:41:21 +02:00
|
|
|
};
|