2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-01-26 23:32:12 +03:30
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
import { Fragment, useContext, useEffect, useLayoutEffect, useMemo } from 'react';
|
2024-04-14 15:50:12 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import Tooltip from '@mui/material/Tooltip';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2022-03-12 04:46:55 +00:00
|
|
|
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
|
2023-06-04 21:30:15 +02:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { requestManager } from '@/lib/requests/requests/RequestManager.ts';
|
|
|
|
|
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
|
2024-10-05 23:22:42 +02:00
|
|
|
import {
|
|
|
|
|
sourceDefualtLangs,
|
|
|
|
|
sourceForcedDefaultLangs,
|
|
|
|
|
langSortCmp,
|
|
|
|
|
DefaultLanguage,
|
|
|
|
|
} from '@/modules/core/utils/Languages.ts';
|
2024-10-05 21:29:50 +02:00
|
|
|
import { translateExtensionLanguage } from '@/modules/extension/services/Extensions.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
2024-10-05 21:22:12 +02:00
|
|
|
import { SourceCard } from '@/modules/source/components/SourceCard.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { LangSelect } from '@/modules/core/components/inputs/LangSelect.tsx';
|
2024-10-05 22:17:23 +02:00
|
|
|
import { NavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-07-03 23:15:50 +02:00
|
|
|
import { SourceType } from '@/lib/graphql/generated/graphql.ts';
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2024-09-30 03:38:49 +02:00
|
|
|
function sourceToLangList(sources: Pick<SourceType, 'id' | 'lang'>[]) {
|
|
|
|
|
const result = new Set<string>();
|
2021-03-09 16:44:09 +03:30
|
|
|
|
|
|
|
|
sources.forEach((source) => {
|
2024-09-30 03:38:49 +02:00
|
|
|
const isLocalSource = Number(source.id) === 0;
|
|
|
|
|
const lang = isLocalSource ? DefaultLanguage.OTHER : source.lang;
|
|
|
|
|
|
|
|
|
|
result.add(lang);
|
2021-03-09 16:44:09 +03:30
|
|
|
});
|
|
|
|
|
|
2024-09-30 03:38:49 +02:00
|
|
|
return [...result].sort(langSortCmp);
|
2021-03-09 16:44:09 +03:30
|
|
|
}
|
|
|
|
|
|
2024-09-30 03:45:54 +02:00
|
|
|
function groupByLang<Source extends Pick<SourceType, 'id' | 'name' | 'lang'>>(
|
|
|
|
|
sources: Source[],
|
|
|
|
|
): Record<string, Source[]> {
|
2024-07-03 23:15:50 +02:00
|
|
|
const result: Record<string, Source[]> = {};
|
2024-09-30 03:38:49 +02:00
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
sources.forEach((source) => {
|
2024-09-30 03:38:49 +02:00
|
|
|
const isLocalSource = Number(source.id) === 0;
|
|
|
|
|
const lang = isLocalSource ? DefaultLanguage.OTHER : source.lang;
|
|
|
|
|
|
|
|
|
|
result[lang] ??= [];
|
|
|
|
|
result[lang].push(source);
|
2021-03-09 16:44:09 +03:30
|
|
|
});
|
|
|
|
|
|
2024-09-30 03:45:54 +02:00
|
|
|
Object.values(result).forEach((langSources) => langSources.sort((a, b) => a.name.localeCompare(b.name)));
|
|
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
return result;
|
|
|
|
|
}
|
2021-01-19 14:47:07 +03:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function Sources() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2024-01-26 20:50:51 +01:00
|
|
|
const { setAction } = useContext(NavBarContext);
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-02-08 18:19:26 +01:00
|
|
|
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
|
|
|
|
2024-01-09 22:09:31 +01:00
|
|
|
const areSourcesFromDifferentRepos = useMemo(() => {
|
|
|
|
|
if (!sources || !!sources?.length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { repo } = sources[0].extension;
|
|
|
|
|
return sources.some((source) => source.extension.repo !== repo);
|
|
|
|
|
}, [sources]);
|
|
|
|
|
|
2023-06-04 21:30:15 +02:00
|
|
|
const navigate = useNavigate();
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2021-10-18 14:02:51 +03:30
|
|
|
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
|
|
|
}, []);
|
2021-10-18 14:02:51 +03:30
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2021-03-09 16:44:09 +03:30
|
|
|
setAction(
|
2022-03-12 04:46:55 +00:00
|
|
|
<>
|
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>
|
2022-03-12 04:46:55 +00:00
|
|
|
<LangSelect
|
|
|
|
|
shownLangs={shownLangs}
|
|
|
|
|
setShownLangs={setShownLangs}
|
2022-11-02 21:48:22 +01:00
|
|
|
allLangs={sourceToLangList(sources ?? [])}
|
2022-03-12 04:46:55 +00:00
|
|
|
forcedLangs={sourceForcedDefaultLangs()}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
2021-03-09 16:44:09 +03:30
|
|
|
);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2023-03-28 23:14:03 +02:00
|
|
|
}, [t, shownLangs, sources]);
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-05-15 11:56:55 +02:00
|
|
|
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'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 21:48:22 +01:00
|
|
|
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
|
|
|
}
|
2022-11-02 21:48:22 +01:00
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
return (
|
|
|
|
|
<>
|
2023-02-06 10:06:33 +01:00
|
|
|
{Object.entries(groupByLang(sources ?? []))
|
|
|
|
|
.sort((a, b) => langSortCmp(a[0], b[0]))
|
|
|
|
|
.map(
|
|
|
|
|
([lang, list]) =>
|
2024-09-30 03:38:49 +02:00
|
|
|
(lang === DefaultLanguage.OTHER || shownLangs.includes(lang)) && (
|
2023-08-14 21:43:06 +02:00
|
|
|
<Fragment key={lang}>
|
2024-01-02 23:32:49 +01:00
|
|
|
<Typography
|
|
|
|
|
key={lang}
|
2024-08-30 21:54:11 +02:00
|
|
|
variant="h5"
|
|
|
|
|
component="h2"
|
|
|
|
|
sx={{
|
|
|
|
|
paddingLeft: 3,
|
|
|
|
|
paddingTop: 1,
|
|
|
|
|
paddingBottom: 2,
|
2024-01-02 23:32:49 +01:00
|
|
|
fontWeight: 'bold',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-04-21 00:27:42 +02:00
|
|
|
{translateExtensionLanguage(lang)}
|
2024-01-02 23:32:49 +01:00
|
|
|
</Typography>
|
2024-07-03 23:15:50 +02:00
|
|
|
{list
|
2024-09-30 03:38:49 +02:00
|
|
|
.filter((source) => {
|
|
|
|
|
const isLangOther = lang === DefaultLanguage.OTHER;
|
|
|
|
|
if (isLangOther) {
|
|
|
|
|
const isLocalSource = Number(source.id) === 0;
|
|
|
|
|
const isLangShown = shownLangs.includes(lang);
|
|
|
|
|
|
|
|
|
|
const isLangOtherSourceShown = isLangShown || isLocalSource;
|
|
|
|
|
if (!isLangOtherSourceShown) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return showNsfw || !source.isNsfw;
|
|
|
|
|
})
|
2023-02-06 10:06:33 +01:00
|
|
|
.map((source) => (
|
2024-01-09 22:09:31 +01:00
|
|
|
<SourceCard
|
|
|
|
|
key={source.id}
|
|
|
|
|
source={source}
|
|
|
|
|
showSourceRepo={areSourcesFromDifferentRepos}
|
|
|
|
|
/>
|
2023-02-06 10:06:33 +01:00
|
|
|
))}
|
2023-08-14 21:43:06 +02:00
|
|
|
</Fragment>
|
2023-02-06 10:06:33 +01:00
|
|
|
),
|
|
|
|
|
)}
|
2021-03-09 16:44:09 +03:30
|
|
|
</>
|
|
|
|
|
);
|
2021-01-19 14:47:07 +03:30
|
|
|
}
|