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
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Card, CardActionArea, Typography } from '@mui/material';
|
2023-05-20 13:23:30 +02:00
|
|
|
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
import { StringParam, useQueryParam } from 'use-query-params';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { ISource } from '@/typings';
|
2023-08-15 13:54:29 +02:00
|
|
|
import requestManager from '@/lib/requests/RequestManager.ts';
|
2023-06-05 01:42:39 +02:00
|
|
|
import useLocalStorage from '@/util/useLocalStorage';
|
|
|
|
|
import { langSortCmp, sourceDefualtLangs, sourceForcedDefaultLangs } from '@/util/language';
|
|
|
|
|
import { translateExtensionLanguage } from '@/screens/util/Extensions';
|
|
|
|
|
import AppbarSearch from '@/components/util/AppbarSearch';
|
|
|
|
|
import LangSelect from '@/components/navbar/action/LangSelect';
|
|
|
|
|
import MangaGrid from '@/components/MangaGrid';
|
|
|
|
|
import NavbarContext from '@/components/context/NavbarContext';
|
|
|
|
|
import { useDebounce } from '@/components/manga/hooks';
|
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
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
function sourceToLangList(sources: ISource[]) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 13:19:03 +02:00
|
|
|
const compareSourceByName = (sourceA: ISource, sourceB: ISource): -1 | 0 | 1 => {
|
|
|
|
|
if (sourceA.displayName < sourceB.displayName) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (sourceA.displayName > sourceB.displayName) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const compareSourcesBySearchResult = (
|
|
|
|
|
sourceA: ISource,
|
|
|
|
|
sourceB: ISource,
|
2023-05-20 13:23:30 +02:00
|
|
|
sourceToFetchedStateMap: SourceToLoadingStateMap,
|
2023-05-20 13:19:03 +02:00
|
|
|
): -1 | 0 | 1 => {
|
2023-05-20 13:23:30 +02:00
|
|
|
const isSourceAFetched = !sourceToFetchedStateMap.get(sourceA.id)?.isLoading ?? true;
|
|
|
|
|
const isSourceBFetched = !sourceToFetchedStateMap.get(sourceB.id)?.isLoading ?? true;
|
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,
|
|
|
|
|
}: {
|
|
|
|
|
source: ISource;
|
|
|
|
|
onSearchRequestFinished: (
|
|
|
|
|
source: ISource,
|
|
|
|
|
isLoading: boolean,
|
|
|
|
|
hasResults: boolean,
|
|
|
|
|
emptySearch: boolean,
|
|
|
|
|
) => void;
|
|
|
|
|
searchString: string | null | undefined;
|
|
|
|
|
emptyQuery: boolean;
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const skipRequest = !searchString;
|
|
|
|
|
|
|
|
|
|
const { id, displayName, lang } = source;
|
|
|
|
|
const {
|
|
|
|
|
data: searchResult,
|
|
|
|
|
isLoading,
|
|
|
|
|
error,
|
|
|
|
|
abortRequest,
|
2023-06-17 15:45:06 +02:00
|
|
|
} = requestManager.useSourceQuickSearch(id, searchString ?? '', [], 1, { skipRequest });
|
2023-05-23 13:35:09 +02:00
|
|
|
const mangas = !isLoading ? searchResult?.[0]?.mangaList ?? [] : [];
|
|
|
|
|
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
|
2023-05-23 13:35:09 +02:00
|
|
|
abortRequest(
|
|
|
|
|
new Error(`SourceSearchPreview(${source.id}, ${source.displayName}): search string changed`),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
[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 (
|
|
|
|
|
<>
|
|
|
|
|
<Card sx={{ margin: '10px' }}>
|
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>
|
|
|
|
|
<MangaGrid
|
|
|
|
|
mangas={mangas}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
hasNextPage={false}
|
2023-06-17 16:50:17 +02:00
|
|
|
loadMore={() => undefined}
|
2023-05-23 13:35:09 +02:00
|
|
|
horizontal
|
|
|
|
|
noFaces
|
|
|
|
|
message={errorMessage}
|
|
|
|
|
inLibraryIndicator
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-05-20 13:19:03 +02:00
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
const SearchAll: React.FC = () => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2022-03-12 04:46:55 +00:00
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
|
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);
|
|
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
const { data: sources = [] } = requestManager.useGetSourceList();
|
|
|
|
|
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(
|
|
|
|
|
({ id }: ISource, isLoading: boolean, hasResults: boolean, emptySearch: boolean) => {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('search.title.global_search'));
|
2022-03-12 04:46:55 +00:00
|
|
|
setAction(
|
|
|
|
|
<>
|
2023-02-06 10:06:33 +01:00
|
|
|
<AppbarSearch autoOpen />
|
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()}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
|
|
|
|
);
|
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
|
|
|
|
2023-05-20 13:23:30 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{sourcesSortedByResult.map((source) => (
|
|
|
|
|
<SourceSearchPreview
|
|
|
|
|
key={source.id}
|
|
|
|
|
source={source}
|
|
|
|
|
onSearchRequestFinished={updateSourceLoadingState}
|
2023-05-23 13:35:09 +02:00
|
|
|
searchString={searchString}
|
|
|
|
|
emptyQuery={!query}
|
2023-05-20 13:23:30 +02:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SearchAll;
|