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';
|
|
|
|
|
import { useContext, useEffect, 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-05-04 15:41:21 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { MangaGrid } from '@/components/MangaGrid.tsx';
|
|
|
|
|
import { TManga } from '@/typings';
|
|
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
|
|
|
|
import { useLocalStorage } from '@/util/useStorage.tsx';
|
|
|
|
|
import { GridLayout } from '@/components/context/LibraryOptionsContext.tsx';
|
|
|
|
|
import { GridLayouts } from '@/components/source/GridLayouts.tsx';
|
2024-05-08 15:06:47 +02:00
|
|
|
import { CheckboxInput } from '@/components/atoms/CheckboxInput.tsx';
|
2024-05-08 18:51:09 +02:00
|
|
|
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
|
|
|
|
import { MangaCard } from '@/components/MangaCard.tsx';
|
|
|
|
|
import { StyledGroupedVirtuoso } from '@/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/components/virtuoso/StyledGroupHeader.tsx';
|
2024-05-08 15:06:47 +02:00
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
const findDuplicatesByTitle = (libraryMangas: TManga[]): Record<string, TManga[]> => {
|
2024-05-08 15:06:47 +02:00
|
|
|
const titleToMangas = Object.groupBy(libraryMangas, ({ title }) => title.toLowerCase().trim());
|
|
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
return Object.fromEntries(
|
|
|
|
|
Object.entries(titleToMangas)
|
|
|
|
|
.filter((titleToMangaMap): titleToMangaMap is [string, TManga[]] => (titleToMangaMap[1]?.length ?? 0) > 1)
|
|
|
|
|
.map(([, mangas]) => [mangas[0].title, mangas]),
|
|
|
|
|
);
|
2024-05-08 15:06:47 +02:00
|
|
|
};
|
|
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
const findDuplicatesByTitleAndAlternativeTitles = (libraryMangas: TManga[]): Record<string, TManga[]> => {
|
2024-05-08 15:06:47 +02:00
|
|
|
const idToDuplicateStatus: Record<TManga['id'], boolean> = {};
|
2024-05-08 18:51:09 +02:00
|
|
|
const titleToMangas: Record<string, TManga[]> = {};
|
2024-05-08 15:06:47 +02:00
|
|
|
|
|
|
|
|
libraryMangas.forEach((mangaToCheck) =>
|
|
|
|
|
libraryMangas.forEach((libraryManga) => {
|
|
|
|
|
const isDifferentManga = mangaToCheck.id !== libraryManga.id;
|
|
|
|
|
if (!isDifferentManga) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const titleToCheck = mangaToCheck.title.toLowerCase().trim();
|
|
|
|
|
|
|
|
|
|
const doesTitleMatch = libraryManga.title.toLowerCase().trim() === titleToCheck;
|
|
|
|
|
const doesAlternativeTitleMatch = !!libraryManga.description?.toLowerCase().trim().includes(titleToCheck);
|
|
|
|
|
|
|
|
|
|
const isDuplicate = doesTitleMatch || doesAlternativeTitleMatch;
|
|
|
|
|
if (!isDuplicate) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const markMangaToCheckAsDuplicate = !idToDuplicateStatus[mangaToCheck.id];
|
|
|
|
|
if (markMangaToCheckAsDuplicate) {
|
|
|
|
|
idToDuplicateStatus[mangaToCheck.id] = true;
|
2024-05-08 18:51:09 +02:00
|
|
|
titleToMangas[mangaToCheck.title] = [...(titleToMangas[mangaToCheck.title] ?? []), mangaToCheck];
|
2024-05-08 15:06:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wasAlreadyDetected = idToDuplicateStatus[libraryManga.id];
|
|
|
|
|
if (wasAlreadyDetected) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
idToDuplicateStatus[libraryManga.id] = true;
|
2024-05-08 18:51:09 +02:00
|
|
|
titleToMangas[mangaToCheck.title] = [...(titleToMangas[mangaToCheck.title] ?? []), libraryManga];
|
2024-05-08 15:06:47 +02:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
return titleToMangas;
|
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);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
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) => (
|
|
|
|
|
<>
|
|
|
|
|
<IconButton {...bindTrigger(popupState)}>
|
|
|
|
|
<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
|
|
|
|
|
|
|
|
const { data, loading, error, refetch } = requestManager.useGetMangas({ condition: { inLibrary: true } });
|
|
|
|
|
|
2024-05-08 18:51:09 +02:00
|
|
|
const mangasByTitle = useMemo(() => {
|
2024-05-04 15:41:21 +02:00
|
|
|
const libraryMangas: TManga[] = data?.mangas.nodes ?? [];
|
|
|
|
|
|
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-05-08 18:51:09 +02:00
|
|
|
const duplicatedTitles = useMemo(() => Object.keys(mangasByTitle), [mangasByTitle]);
|
|
|
|
|
const duplicatedMangas = useMemo(() => Object.values(mangasByTitle).flat(), [mangasByTitle]);
|
|
|
|
|
const mangasCountByTitle = useMemo(
|
|
|
|
|
() => Object.values(mangasByTitle).map((mangas) => mangas.length),
|
|
|
|
|
[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
|
|
|
|
|
style={{
|
|
|
|
|
// override Virtuoso default values and set them with class
|
|
|
|
|
height: 'undefined',
|
|
|
|
|
}}
|
|
|
|
|
groupCounts={mangasCountByTitle}
|
|
|
|
|
groupContent={(index) => (
|
|
|
|
|
<StyledGroupHeader variant="h5" isFirstItem={index === 0}>
|
|
|
|
|
{duplicatedTitles[index]}
|
|
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
|
|
|
|
itemContent={(index) => (
|
|
|
|
|
<Box key={duplicatedMangas[index].id} sx={{ px: 1, pb: 1 }}>
|
2024-05-08 18:59:01 +02:00
|
|
|
<MangaCard
|
|
|
|
|
manga={duplicatedMangas[index]}
|
|
|
|
|
gridLayout={gridLayout}
|
|
|
|
|
selected={null}
|
|
|
|
|
mode="duplicate"
|
|
|
|
|
/>
|
2024-05-08 18:51:09 +02:00
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return duplicatedTitles.map((title, index) => (
|
|
|
|
|
<Box key={title}>
|
|
|
|
|
<StyledGroupHeader sx={{ pt: index === 0 ? undefined : 0, pb: 0 }} variant="h5" isFirstItem={false}>
|
|
|
|
|
{title}
|
|
|
|
|
</StyledGroupHeader>
|
|
|
|
|
<MangaGrid
|
|
|
|
|
mangas={mangasByTitle[title]}
|
|
|
|
|
hasNextPage={false}
|
|
|
|
|
loadMore={() => {}}
|
|
|
|
|
isLoading={false}
|
|
|
|
|
gridLayout={gridLayout}
|
|
|
|
|
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
|
|
|
};
|