2022-03-12 04:46:55 +00:00
|
|
|
/*
|
2023-02-06 10:06:33 +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/.
|
|
|
|
|
*/
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2024-04-14 15:50:12 +02:00
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import CardActionArea from '@mui/material/CardActionArea';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2024-09-26 15:33:22 +02:00
|
|
|
import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react';
|
2024-01-26 20:50:51 +01:00
|
|
|
import { Link, useLocation } from 'react-router-dom';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { StringParam, useQueryParam } from 'use-query-params';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-09-14 03:01:07 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { requestManager } from '@/lib/requests/requests/RequestManager.ts';
|
|
|
|
|
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
|
|
|
|
|
import { langSortCmp, sourceDefualtLangs, sourceForcedDefaultLangs } from '@/lib/Languages.tsx';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { translateExtensionLanguage } from '@/screens/util/Extensions';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { AppbarSearch } from '@/modules/core/components/AppbarSearch.tsx';
|
|
|
|
|
import { LangSelect } from '@/modules/core/components/inputs/LangSelect.tsx';
|
|
|
|
|
import { useDebounce } from '@/modules/core/hooks/useDebounce.ts';
|
2024-06-05 23:04:13 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2024-10-05 19:21:26 +02:00
|
|
|
import { MangaCardProps } from '@/modules/manga/MangaCard.types.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { EmptyView } from '@/modules/core/components/placeholder/EmptyView.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
|
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
2024-07-03 23:15:50 +02:00
|
|
|
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
2024-10-05 21:22:12 +02:00
|
|
|
import { BaseMangaGrid } from '@/modules/manga/components/BaseMangaGrid.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
type SourceLoadingState = { isLoading: boolean; hasResults: boolean; emptySearch: boolean };
|
|
|
|
|
type SourceToLoadingStateMap = Map<string, SourceLoadingState>;
|
2023-05-20 13:19:03 +02:00
|
|
|
|
2024-07-03 23:15:50 +02:00
|
|
|
function sourceToLangList(sources: Pick<SourceType, 'lang'>[]) {
|
2022-03-12 04:46:55 +00:00
|
|
|
const result: string[] = [];
|
|
|
|
|
|
|
|
|
|
sources.forEach((source) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
if (result.indexOf(source.lang) === -1) {
|
|
|
|
|
result.push(source.lang);
|
|
|
|
|
}
|
2022-03-12 04:46:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result.sort(langSortCmp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-03 23:15:50 +02:00
|
|
|
const compareSourceByName = (
|
|
|
|
|
sourceA: Pick<SourceType, 'displayName'>,
|
|
|
|
|
sourceB: Pick<SourceType, 'displayName'>,
|
|
|
|
|
): -1 | 0 | 1 => {
|
2023-05-20 13:19:03 +02:00
|
|
|
if (sourceA.displayName < sourceB.displayName) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (sourceA.displayName > sourceB.displayName) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const compareSourcesBySearchResult = (
|
2024-07-03 23:15:50 +02:00
|
|
|
sourceA: Pick<SourceType, 'id'>,
|
|
|
|
|
sourceB: Pick<SourceType, 'id'>,
|
2023-05-20 13:23:30 +02:00
|
|
|
sourceToFetchedStateMap: SourceToLoadingStateMap,
|
2023-05-20 13:19:03 +02:00
|
|
|
): -1 | 0 | 1 => {
|
2023-11-25 18:13:51 +01:00
|
|
|
const isSourceAFetched = !sourceToFetchedStateMap.get(sourceA.id)?.isLoading;
|
|
|
|
|
const isSourceBFetched = !sourceToFetchedStateMap.get(sourceB.id)?.isLoading;
|
2023-05-20 13:19:03 +02:00
|
|
|
if (isSourceAFetched && !isSourceBFetched) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (!isSourceAFetched && isSourceBFetched) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (!isSourceAFetched && !isSourceBFetched) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
const isSourceASearchResultEmpty = !sourceToFetchedStateMap.get(sourceA.id)?.hasResults;
|
|
|
|
|
const isSourceBSearchResultEmpty = !sourceToFetchedStateMap.get(sourceB.id)?.hasResults;
|
2023-05-20 13:19:03 +02:00
|
|
|
if (isSourceASearchResultEmpty && !isSourceBSearchResultEmpty) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (isSourceBSearchResultEmpty && !isSourceASearchResultEmpty) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-05-20 13:23:30 +02:00
|
|
|
|
2023-05-20 13:19:03 +02:00
|
|
|
return 0;
|
|
|
|
|
};
|
2023-05-20 13:23:30 +02:00
|
|
|
const TRIGGER_SEARCH_THRESHOLD = 1000; // ms
|
|
|
|
|
|
2023-05-23 13:35:09 +02:00
|
|
|
const SourceSearchPreview = React.memo(
|
|
|
|
|
({
|
|
|
|
|
source,
|
|
|
|
|
onSearchRequestFinished,
|
|
|
|
|
searchString,
|
|
|
|
|
emptyQuery,
|
2024-01-26 20:50:51 +01:00
|
|
|
mode,
|
2023-05-23 13:35:09 +02:00
|
|
|
}: {
|
2024-07-03 23:15:50 +02:00
|
|
|
source: Pick<SourceType, 'id' | 'displayName' | 'lang'>;
|
2023-05-23 13:35:09 +02:00
|
|
|
onSearchRequestFinished: (
|
2024-07-03 23:15:50 +02:00
|
|
|
source: Pick<SourceType, 'id'>,
|
2023-05-23 13:35:09 +02:00
|
|
|
isLoading: boolean,
|
|
|
|
|
hasResults: boolean,
|
|
|
|
|
emptySearch: boolean,
|
|
|
|
|
) => void;
|
|
|
|
|
searchString: string | null | undefined;
|
|
|
|
|
emptyQuery: boolean;
|
2024-01-26 20:50:51 +01:00
|
|
|
} & Pick<MangaCardProps, 'mode'>) => {
|
2023-05-23 13:35:09 +02:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const { id, displayName, lang } = source;
|
2024-04-29 14:26:33 +02:00
|
|
|
const [refetch, results] = requestManager.useSourceSearch(id, searchString ?? '', undefined, 1, {
|
2023-09-12 02:36:11 +02:00
|
|
|
skipRequest: !searchString,
|
2023-11-11 23:42:58 +01:00
|
|
|
addAbortSignal: true,
|
2023-09-12 02:36:11 +02:00
|
|
|
});
|
|
|
|
|
const { data: searchResult, isLoading, error, abortRequest } = results[0]!;
|
2024-06-02 15:24:29 +02:00
|
|
|
const mangas = searchResult?.fetchSourceManga?.mangas ?? [];
|
2023-05-23 13:35:09 +02:00
|
|
|
const noMangasFound = !isLoading && !mangas.length;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onSearchRequestFinished(source, isLoading, !noMangasFound, !searchString);
|
|
|
|
|
}, [isLoading, noMangasFound, searchString]);
|
|
|
|
|
|
|
|
|
|
let errorMessage: string | undefined;
|
|
|
|
|
if (error) {
|
|
|
|
|
errorMessage = t('search.error.label.source_search_failed');
|
|
|
|
|
} else if (noMangasFound) {
|
|
|
|
|
errorMessage = t('manga.error.label.no_mangas_found');
|
|
|
|
|
}
|
2023-05-20 13:23:30 +02:00
|
|
|
|
2023-05-23 13:35:09 +02:00
|
|
|
useEffect(
|
|
|
|
|
() => () => {
|
2023-06-10 00:42:26 +02:00
|
|
|
// INFO:
|
|
|
|
|
// with strict mode + dev mode the first request will be aborted. due to using SWR there won't be an
|
|
|
|
|
// immediate second request since it's the same key. instead the "second" request will be the error handling of SWR
|
2024-07-03 23:15:50 +02:00
|
|
|
abortRequest(new Error(`SourceSearchPreview(${id}, ${displayName}): search string changed`));
|
2023-05-23 13:35:09 +02:00
|
|
|
},
|
|
|
|
|
[searchString],
|
|
|
|
|
);
|
2023-05-20 13:23:30 +02:00
|
|
|
|
2023-05-23 13:35:09 +02:00
|
|
|
if ((!isLoading && !searchString) || emptyQuery) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-20 13:25:10 +02:00
|
|
|
|
2023-05-23 13:35:09 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
2024-09-14 03:01:07 +02:00
|
|
|
<Card sx={{ mb: 1 }}>
|
2023-05-28 12:16:06 +02:00
|
|
|
<CardActionArea component={Link} to={`/sources/${id}?query=${searchString}`} sx={{ p: 3 }}>
|
2023-05-23 13:35:09 +02:00
|
|
|
<Typography variant="h5">{displayName}</Typography>
|
|
|
|
|
<Typography variant="caption">{translateExtensionLanguage(lang)}</Typography>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
2024-04-29 15:34:12 +02:00
|
|
|
{errorMessage ? (
|
2024-04-29 14:26:33 +02:00
|
|
|
<EmptyView
|
2024-07-11 17:22:52 +02:00
|
|
|
sx={{ alignItems: 'start', height: undefined }}
|
2024-04-29 14:26:33 +02:00
|
|
|
noFaces
|
|
|
|
|
message={errorMessage}
|
|
|
|
|
messageExtra={error && error.message}
|
|
|
|
|
retry={
|
|
|
|
|
error
|
|
|
|
|
? () =>
|
|
|
|
|
refetch(1).catch(
|
|
|
|
|
defaultPromiseErrorHandler(`SourceSearchPreview(${source.id})::refetch`),
|
|
|
|
|
)
|
|
|
|
|
: undefined
|
|
|
|
|
}
|
|
|
|
|
/>
|
2024-04-29 15:34:12 +02:00
|
|
|
) : (
|
2024-07-08 18:02:50 +02:00
|
|
|
<BaseMangaGrid
|
2024-09-14 03:01:07 +02:00
|
|
|
gridWrapperProps={{ sx: { px: 0 } }}
|
2024-04-29 15:34:12 +02:00
|
|
|
mangas={mangas}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
hasNextPage={false}
|
|
|
|
|
loadMore={() => undefined}
|
|
|
|
|
horizontal
|
|
|
|
|
noFaces
|
|
|
|
|
message={errorMessage}
|
|
|
|
|
inLibraryIndicator
|
|
|
|
|
mode={mode}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-05-23 13:35:09 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-05-20 13:19:03 +02:00
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const SearchAll: React.FC = () => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2024-01-26 20:50:51 +01:00
|
|
|
const { pathname, state } = useLocation<{ mangaTitle?: string }>();
|
|
|
|
|
const isMigrateMode = pathname.startsWith('/migrate/source');
|
|
|
|
|
|
|
|
|
|
const mangaTitle = state?.mangaTitle;
|
|
|
|
|
|
2023-05-23 13:35:09 +02:00
|
|
|
const [query] = useQueryParam('query', StringParam);
|
|
|
|
|
const searchString = useDebounce(query, TRIGGER_SEARCH_THRESHOLD);
|
|
|
|
|
|
2023-02-08 18:19:26 +01:00
|
|
|
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', sourceDefualtLangs());
|
2022-03-12 04:46:55 +00:00
|
|
|
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
|
|
|
|
|
|
2024-05-05 01:17:18 +02:00
|
|
|
const { data, loading, error, refetch } = requestManager.useGetSourceList({ notifyOnNetworkStatusChange: true });
|
2023-09-01 20:29:19 +02:00
|
|
|
const sources = data?.sources.nodes ?? [];
|
2023-05-20 13:23:30 +02:00
|
|
|
const [sourceToLoadingStateMap, setSourceToLoadingStateMap] = useState<SourceToLoadingStateMap>(new Map());
|
2023-05-23 13:35:09 +02:00
|
|
|
const debouncedSourceToLoadingStateMap = useDebounce(sourceToLoadingStateMap, 500);
|
2023-05-20 13:23:30 +02:00
|
|
|
|
|
|
|
|
const sourcesSortedByName = useMemo(() => [...sources].sort(compareSourceByName), [sources]);
|
|
|
|
|
const sourcesFilteredByLang = useMemo(
|
|
|
|
|
() => sourcesSortedByName.filter((source) => shownLangs.includes(source.lang)),
|
|
|
|
|
[sourcesSortedByName, shownLangs],
|
|
|
|
|
);
|
|
|
|
|
const sourcesFilteredByNsfw = useMemo(
|
|
|
|
|
() => sourcesFilteredByLang.filter((source) => showNsfw || !source.isNsfw),
|
|
|
|
|
[sourcesFilteredByLang, showNsfw],
|
|
|
|
|
);
|
|
|
|
|
const sourcesSortedByResult = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
[...sourcesFilteredByNsfw].sort((sourceA, sourceB) =>
|
2023-05-23 13:35:09 +02:00
|
|
|
compareSourcesBySearchResult(sourceA, sourceB, debouncedSourceToLoadingStateMap),
|
2023-05-20 13:23:30 +02:00
|
|
|
),
|
2023-05-23 13:35:09 +02:00
|
|
|
[sourcesFilteredByNsfw, debouncedSourceToLoadingStateMap],
|
2023-05-20 13:23:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const updateSourceLoadingState = useCallback(
|
2024-07-03 23:15:50 +02:00
|
|
|
({ id }: Pick<SourceType, 'id'>, isLoading: boolean, hasResults: boolean, emptySearch: boolean) => {
|
2023-05-20 13:23:30 +02:00
|
|
|
setSourceToLoadingStateMap((currentMap) => {
|
|
|
|
|
const mapCopy = new Map(currentMap);
|
|
|
|
|
mapCopy.set(id, { isLoading, hasResults, emptySearch });
|
|
|
|
|
return mapCopy;
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-05-23 13:35:09 +02:00
|
|
|
[setSourceToLoadingStateMap],
|
2023-05-20 13:23:30 +02:00
|
|
|
);
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2024-01-26 20:50:51 +01:00
|
|
|
setTitle(t(isMigrateMode ? 'migrate.search.title' : 'search.title.global_search', { title: mangaTitle }));
|
2022-03-12 04:46:55 +00:00
|
|
|
setAction(
|
|
|
|
|
<>
|
2024-04-07 15:22:03 +02:00
|
|
|
<AppbarSearch isClosable={false} />
|
2022-03-12 04:46:55 +00:00
|
|
|
<LangSelect
|
|
|
|
|
shownLangs={shownLangs}
|
|
|
|
|
setShownLangs={setShownLangs}
|
2023-05-20 13:23:30 +02:00
|
|
|
allLangs={sourceToLangList(sources)}
|
2022-03-12 04:46:55 +00:00
|
|
|
forcedLangs={sourceForcedDefaultLangs()}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
|
|
|
|
);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2023-05-20 13:23:30 +02:00
|
|
|
}, [t, shownLangs, setShownLangs, sources]);
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
// make sure all of forcedDefaultLangs() exists in shownLangs
|
|
|
|
|
const missingDefaultLangs = sourceForcedDefaultLangs().filter(
|
|
|
|
|
(defaultLang) => !shownLangs.includes(defaultLang),
|
2022-03-12 04:46:55 +00:00
|
|
|
);
|
2023-05-20 13:23:30 +02:00
|
|
|
setShownLangs([...shownLangs, ...missingDefaultLangs]);
|
|
|
|
|
}, []);
|
2023-03-11 18:05:58 +01:00
|
|
|
|
2024-05-05 01:17:18 +02:00
|
|
|
if (loading) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyViewAbsoluteCentered
|
|
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
|
|
|
|
messageExtra={error.message}
|
|
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('SearchAll::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
return (
|
2024-09-14 03:01:07 +02:00
|
|
|
<Box sx={{ p: 1 }}>
|
|
|
|
|
{sourcesSortedByResult.map((source, index) => (
|
|
|
|
|
<Box key={source.id} sx={{ pb: index + 1 !== sourcesSortedByResult.length ? 2 : 0 }}>
|
|
|
|
|
<SourceSearchPreview
|
|
|
|
|
source={source}
|
|
|
|
|
onSearchRequestFinished={updateSourceLoadingState}
|
|
|
|
|
searchString={searchString}
|
|
|
|
|
emptyQuery={!query}
|
|
|
|
|
mode={isMigrateMode ? 'migrate.select' : 'source'}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
2023-05-20 13:23:30 +02:00
|
|
|
))}
|
2024-09-14 03:01:07 +02:00
|
|
|
</Box>
|
2023-05-20 13:23:30 +02:00
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|