Files
suwayomi-material-you-webui/src/screens/SearchAll.tsx
T

225 lines
8.1 KiB
TypeScript
Raw Normal View History

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
import { Card, CardActionArea, Typography } from '@mui/material';
2022-03-12 04:46:55 +00:00
import NavbarContext from 'components/context/NavbarContext';
import MangaGrid from 'components/MangaGrid';
import LangSelect from 'components/navbar/action/LangSelect';
import AppbarSearch from 'components/util/AppbarSearch';
2023-03-11 18:05:58 +01:00
import PQueue from 'p-queue';
2023-05-18 13:25:19 +02:00
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import { StringParam, useQueryParam } from 'use-query-params';
2023-04-21 00:27:42 +02:00
import { langSortCmp, sourceDefualtLangs, sourceForcedDefaultLangs } from 'util/language';
2022-03-12 04:46:55 +00:00
import useLocalStorage from 'util/useLocalStorage';
2023-05-20 13:19:03 +02:00
import { IManga, ISource, SourceSearchResult } from 'typings';
2023-03-23 13:59:32 +01:00
import { useTranslation } from 'react-i18next';
2023-04-21 00:27:42 +02:00
import { translateExtensionLanguage } from 'screens/util/Extensions';
2023-05-18 13:25:19 +02:00
import requestManager from 'lib/RequestManager';
2022-03-12 04:46:55 +00:00
2023-05-20 13:19:03 +02:00
type SourceToMangasMap = { [source: string]: IManga[] };
type SourceToFetchedStateMap = { [source: string]: boolean };
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,
sourceToFetchedStateMap: SourceToFetchedStateMap,
sourceToMangasMap: SourceToMangasMap,
): -1 | 0 | 1 => {
const isSourceAFetched = sourceToFetchedStateMap[sourceA.id];
const isSourceBFetched = sourceToFetchedStateMap[sourceB.id];
if (isSourceAFetched && !isSourceBFetched) {
return -1;
}
if (!isSourceAFetched && isSourceBFetched) {
return 1;
}
if (!isSourceAFetched && !isSourceBFetched) {
return 0;
}
const isSourceASearchResultEmpty = sourceToMangasMap[sourceA.id].length === 0;
const isSourceBSearchResultEmpty = sourceToMangasMap[sourceB.id].length === 0;
if (isSourceASearchResultEmpty && !isSourceBSearchResultEmpty) {
return 1;
}
if (isSourceBSearchResultEmpty && !isSourceASearchResultEmpty) {
return -1;
}
return 0;
};
const SearchAll: React.FC = () => {
2023-03-23 13:59:32 +01:00
const { t } = useTranslation();
2022-03-12 04:46:55 +00:00
const [query] = useQueryParam('query', StringParam);
const { setTitle, setAction } = useContext(NavbarContext);
const [triggerUpdate, setTriggerUpdate] = useState<number>(2);
2023-05-20 13:19:03 +02:00
const [sourceToMangasMap, setSourceToMangasMap] = useState<SourceToMangasMap>({});
2022-03-12 04:46:55 +00:00
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:19:03 +02:00
const { data: sources = [], isLoading: isLoadingSources } = requestManager.useGetSourceList();
const sortedSources = useMemo(() => [...sources].sort(compareSourceByName), [sources]);
2023-05-18 13:25:19 +02:00
2023-05-20 13:19:03 +02:00
const [sourceToFetchedStateMap, setSourceToFetchedStateMap] = useState<SourceToFetchedStateMap>({});
2022-03-12 04:46:55 +00:00
const [lastPageNum, setLastPageNum] = useState<number>(1);
2023-05-20 13:19:03 +02:00
const [resetUI, setResetUI] = useState<number>(0);
2022-03-12 04:46:55 +00:00
2023-05-20 13:19:03 +02:00
const searchRequestsQueue = new PQueue({ concurrency: 5 });
2022-03-12 04:46:55 +00:00
useEffect(() => {
2023-03-23 13:59:32 +01:00
setTitle(t('search.title.global_search'));
2023-03-11 18:05:58 +01:00
setAction(<AppbarSearch />);
}, [t]);
2022-03-12 04:46:55 +00:00
2023-05-20 13:19:03 +02:00
async function performSearch(sourcesToSearchIn: ISource[]) {
sourcesToSearchIn.map((source) =>
searchRequestsQueue.add(async () => {
2023-05-18 13:25:19 +02:00
const response = await requestManager
.getClient()
2023-05-20 13:19:03 +02:00
.get<SourceSearchResult>(`/api/v1/source/${source.id}/search?searchTerm=${query || ''}&pageNum=1`);
const searchResult = await response.data;
const tmpMangas = sourceToMangasMap;
tmpMangas[source.id] = searchResult.mangaList;
setSourceToMangasMap(tmpMangas);
const tmpFetched = sourceToFetchedStateMap;
tmpFetched[source.id] = true;
setSourceToFetchedStateMap(tmpFetched);
2023-02-06 10:06:33 +01:00
setResetUI(1);
}),
);
2022-03-12 04:46:55 +00:00
}
useEffect(() => {
if (triggerUpdate === 2) {
return;
}
if (triggerUpdate === 0) {
setTriggerUpdate(1);
return;
}
2023-05-20 13:19:03 +02:00
setSourceToFetchedStateMap({});
setSourceToMangasMap({});
performSearch(
sortedSources
2023-02-06 10:06:33 +01:00
.filter(({ lang }) => shownLangs.indexOf(lang) !== -1)
.filter((source) => showNsfw || !source.isNsfw),
);
2022-03-12 04:46:55 +00:00
}, [triggerUpdate]);
useEffect(() => {
2023-05-20 13:19:03 +02:00
if (resetUI === 1) {
2022-03-12 04:46:55 +00:00
setResetUI(0);
}
2023-05-20 13:19:03 +02:00
}, [resetUI]);
2022-03-12 04:46:55 +00:00
useEffect(() => {
2023-05-18 22:28:53 +02:00
if (query && !isLoadingSources) {
2022-03-12 04:46:55 +00:00
const delayDebounceFn = setTimeout(() => {
setTriggerUpdate(0);
}, 1000);
return () => clearTimeout(delayDebounceFn);
}
return () => {};
2023-05-20 13:19:03 +02:00
}, [query, shownLangs, sortedSources]);
2022-03-12 04:46:55 +00:00
useEffect(() => {
// make sure all of forcedDefaultLangs() exists in shownLangs
2023-05-20 13:19:03 +02:00
const missingDefaultLangs = sourceForcedDefaultLangs().filter(
(defaultLang) => !shownLangs.includes(defaultLang),
);
setShownLangs([...shownLangs, ...missingDefaultLangs]);
2022-03-12 04:46:55 +00:00
}, []);
useEffect(() => {
2023-03-23 13:59:32 +01:00
setTitle(t('source.title'));
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:19:03 +02:00
allLangs={sourceToLangList(sortedSources)}
2022-03-12 04:46:55 +00:00
forcedLangs={sourceForcedDefaultLangs()}
/>
</>,
);
2023-05-20 13:19:03 +02:00
}, [t, shownLangs, sortedSources]);
2022-03-12 04:46:55 +00:00
if (query) {
return (
<>
2023-05-20 13:19:03 +02:00
{sortedSources
2023-02-06 10:06:33 +01:00
.filter(({ lang }) => shownLangs.indexOf(lang) !== -1)
.filter((source) => showNsfw || !source.isNsfw)
2023-05-20 13:19:03 +02:00
.sort((sourceA, sourceB) =>
compareSourcesBySearchResult(sourceA, sourceB, sourceToFetchedStateMap, sourceToMangasMap),
)
2023-02-06 10:06:33 +01:00
.map(({ lang, id, displayName }) => (
2022-03-12 04:46:55 +00:00
<>
<Card sx={{ margin: '10px' }}>
<CardActionArea
component={Link}
to={`/sources/${id}/popular/?R&query=${query}`}
sx={{ p: 3 }}
2022-03-12 04:46:55 +00:00
>
2023-02-06 10:06:33 +01:00
<Typography variant="h5">{displayName}</Typography>
2023-04-21 00:27:42 +02:00
<Typography variant="caption">{translateExtensionLanguage(lang)}</Typography>
</CardActionArea>
2022-03-12 04:46:55 +00:00
</Card>
<MangaGrid
2023-05-20 13:19:03 +02:00
mangas={sourceToMangasMap[id] || []}
isLoading={!sourceToFetchedStateMap[id]}
2022-03-12 04:46:55 +00:00
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
horizontal
2022-03-12 04:46:55 +00:00
noFaces
2023-05-20 13:19:03 +02:00
message={
sourceToFetchedStateMap[id] ? t('manga.error.label.no_mangas_found') : undefined
}
2022-11-24 21:04:30 +01:00
inLibraryIndicator
2022-03-12 04:46:55 +00:00
/>
</>
2023-02-06 10:06:33 +01:00
))}
2022-03-12 04:46:55 +00:00
</>
);
}
2023-03-11 18:05:58 +01:00
return null;
};
export default SearchAll;