Files
suwayomi-material-you-webui/src/screens/Sources.tsx

169 lines
6.2 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-01-26 23:32:12 +03:30
* 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/.
*/
2021-01-26 23:32:12 +03:30
import { Fragment, useContext, useEffect, useLayoutEffect, useMemo } from 'react';
import IconButton from '@mui/material/IconButton';
import Tooltip from '@mui/material/Tooltip';
import Typography from '@mui/material/Typography';
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
2023-10-28 00:32:02 +02:00
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { useLocalStorage } from '@/util/useStorage.tsx';
import { sourceDefualtLangs, sourceForcedDefaultLangs, langSortCmp } from '@/util/language';
import { translateExtensionLanguage } from '@/screens/util/Extensions';
2023-10-28 00:32:02 +02:00
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder';
import { SourceCard } from '@/components/SourceCard';
import { LangSelect } from '@/components/navbar/action/LangSelect';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
2024-04-29 15:29:33 +02:00
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
2024-04-27 22:28:55 +02:00
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
2021-03-09 16:44:09 +03:30
function sourceToLangList(sources: Pick<SourceType, 'lang'>[]) {
2021-03-09 16:44:09 +03:30
const result: string[] = [];
sources.forEach((source) => {
if (result.indexOf(source.lang) === -1) {
result.push(source.lang);
}
2021-03-09 16:44:09 +03:30
});
result.sort(langSortCmp);
return result;
}
function groupByLang<Source extends Pick<SourceType, 'lang'>>(sources: Source[]): Record<string, Source[]> {
const result: Record<string, Source[]> = {};
2021-03-09 16:44:09 +03:30
sources.forEach((source) => {
if (result[source.lang] === undefined) {
result[source.lang] = [];
}
2021-03-09 16:44:09 +03:30
result[source.lang].push(source);
});
return result;
}
2021-01-19 14:47:07 +03:30
2023-10-28 00:32:02 +02:00
export function Sources() {
const { t } = useTranslation();
const { setAction } = useContext(NavBarContext);
2021-03-09 16:44:09 +03:30
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', sourceDefualtLangs());
2021-09-05 01:25:37 +04:30
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
2021-03-09 16:44:09 +03:30
2024-04-27 22:28:55 +02:00
const {
data,
loading: isLoading,
error,
refetch,
} = requestManager.useGetSourceList({ notifyOnNetworkStatusChange: true });
2023-09-01 20:29:19 +02:00
const sources = data?.sources.nodes;
2021-01-19 14:47:07 +03:30
const areSourcesFromDifferentRepos = useMemo(() => {
if (!sources || !!sources?.length) {
return false;
}
const { repo } = sources[0].extension;
return sources.some((source) => source.extension.repo !== repo);
}, [sources]);
const navigate = useNavigate();
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;
});
}
});
2021-10-18 14:22:33 +03:30
}, []);
useLayoutEffect(() => {
2021-03-09 16:44:09 +03:30
setAction(
<>
2023-11-05 17:22:29 +01:00
<Tooltip title={t('search.title.global_search')}>
2024-09-06 16:59:27 +02:00
<IconButton onClick={() => navigate('/sources/all/search/')} size="large" color="inherit">
2023-11-05 17:22:29 +01:00
<TravelExploreIcon />
</IconButton>
</Tooltip>
<LangSelect
shownLangs={shownLangs}
setShownLangs={setShownLangs}
allLangs={sourceToLangList(sources ?? [])}
forcedLangs={sourceForcedDefaultLangs()}
/>
</>,
2021-03-09 16:44:09 +03:30
);
return () => {
setAction(null);
};
}, [t, shownLangs, sources]);
2021-03-09 16:44:09 +03:30
if (isLoading) return <LoadingPlaceholder />;
2021-01-19 14:47:07 +03:30
2024-04-27 22:28:55 +02:00
if (error) {
return (
2024-04-29 15:29:33 +02:00
<EmptyViewAbsoluteCentered
2024-04-27 22:28:55 +02:00
message={t('global.error.label.failed_to_load_data')}
messageExtra={error.message}
retry={() => refetch().catch(defaultPromiseErrorHandler('Sources::refetch'))}
/>
);
}
if (sources?.length === 0) {
2024-04-29 15:29:33 +02:00
return <EmptyViewAbsoluteCentered message={t('source.error.label.no_sources_found')} />;
2021-01-19 14:47:07 +03:30
}
2021-03-09 16:44:09 +03:30
return (
<>
{Object.entries(groupByLang(sources ?? []))
.sort((a, b) => langSortCmp(a[0], b[0]))
.map(
([lang, list]) =>
shownLangs.indexOf(lang) !== -1 && (
<Fragment key={lang}>
<Typography
key={lang}
2024-08-30 21:54:11 +02:00
variant="h5"
component="h2"
sx={{
paddingLeft: 3,
paddingTop: 1,
paddingBottom: 2,
fontWeight: 'bold',
}}
>
{translateExtensionLanguage(lang)}
</Typography>
{list
.filter((source) => showNsfw || !source.isNsfw)
.map((source) => (
<SourceCard
key={source.id}
source={source}
showSourceRepo={areSourcesFromDifferentRepos}
/>
))}
</Fragment>
),
)}
2021-03-09 16:44:09 +03:30
</>
);
2021-01-19 14:47:07 +03:30
}