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

221 lines
7.5 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';
import { ISource } 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
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;
}
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);
const [mangas, setMangas] = useState<any>({});
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-18 13:25:19 +02:00
const { data: unsortedSources = [], isLoading: FetchedSources } = requestManager.useGetSourceList();
const sources = useMemo(
() =>
unsortedSources.sort((a: { displayName: string }, b: { displayName: string }) => {
if (a.displayName < b.displayName) {
return -1;
}
if (a.displayName > b.displayName) {
return 1;
}
return 0;
}),
[unsortedSources],
);
2022-03-12 04:46:55 +00:00
const [fetched, setFetched] = useState<any>({});
const [lastPageNum, setLastPageNum] = useState<number>(1);
const [ResetUI, setResetUI] = useState<number>(0);
const limit = new PQueue({ concurrency: 5 });
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
async function doIT(elem: any[]) {
2023-02-06 10:06:33 +01:00
elem.map((ele) =>
limit.add(async () => {
2023-05-18 13:25:19 +02:00
const response = await requestManager
.getClient()
.get(`/api/v1/source/${ele.id}/search?searchTerm=${query || ''}&pageNum=1`);
2023-02-06 10:06:33 +01:00
const data = await response.data;
const tmp = mangas;
tmp[ele.id] = data.mangaList;
setMangas(tmp);
const tmp2 = fetched;
tmp2[ele.id] = true;
setFetched(tmp2);
setResetUI(1);
}),
);
2022-03-12 04:46:55 +00:00
}
useEffect(() => {
if (triggerUpdate === 2) {
return;
}
if (triggerUpdate === 0) {
setTriggerUpdate(1);
return;
}
setFetched({});
setMangas({});
2023-02-06 10:06:33 +01:00
doIT(
sources
.filter(({ lang }) => shownLangs.indexOf(lang) !== -1)
.filter((source) => showNsfw || !source.isNsfw),
);
2022-03-12 04:46:55 +00:00
}, [triggerUpdate]);
useEffect(() => {
if (ResetUI === 1) {
setResetUI(0);
}
}, [ResetUI]);
useEffect(() => {
if (query && FetchedSources) {
const delayDebounceFn = setTimeout(() => {
setTriggerUpdate(0);
}, 1000);
return () => clearTimeout(delayDebounceFn);
}
return () => {};
}, [query, shownLangs, sources]);
useEffect(() => {
// make sure all of forcedDefaultLangs() exists in shownLangs
sourceForcedDefaultLangs().forEach((forcedLang) => {
let hasLang = false;
shownLangs.forEach((lang) => {
if (lang === forcedLang) hasLang = true;
});
if (!hasLang) {
setShownLangs((shownLangsCopy) => {
shownLangsCopy.push(forcedLang);
return shownLangsCopy;
});
}
});
}, []);
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}
allLangs={sourceToLangList(sources)}
forcedLangs={sourceForcedDefaultLangs()}
/>
</>,
);
}, [t, shownLangs, sources]);
2022-03-12 04:46:55 +00:00
if (query) {
return (
<>
2023-02-06 10:06:33 +01:00
{sources
.filter(({ lang }) => shownLangs.indexOf(lang) !== -1)
.filter((source) => showNsfw || !source.isNsfw)
.sort((a, b) => {
const af = fetched[a.id];
const bf = fetched[b.id];
if (af && !bf) {
return -1;
}
if (!af && bf) {
return 1;
}
if (!af && !bf) {
return 0;
}
2022-03-12 04:46:55 +00:00
2023-02-06 10:06:33 +01:00
const al = mangas[a.id].length === 0;
const bl = mangas[b.id].length === 0;
if (al && !bl) {
return 1;
}
if (bl && !al) {
return -1;
}
return 0;
})
.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
mangas={mangas[id] || []}
isLoading={!fetched[id]}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
horizontal
2022-03-12 04:46:55 +00:00
noFaces
2023-03-23 13:59:32 +01:00
message={fetched[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;