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
|
|
|
|
2023-08-14 21:43:06 +02:00
|
|
|
import { Fragment, useContext, useEffect } from 'react';
|
2022-03-12 04:46:55 +00:00
|
|
|
import { IconButton } from '@mui/material';
|
|
|
|
|
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';
|
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 { sourceDefualtLangs, sourceForcedDefaultLangs, langSortCmp } from '@/util/language';
|
|
|
|
|
import { translateExtensionLanguage } from '@/screens/util/Extensions';
|
|
|
|
|
import LoadingPlaceholder from '@/components/util/LoadingPlaceholder';
|
|
|
|
|
import NavbarContext from '@/components/context/NavbarContext';
|
|
|
|
|
import SourceCard from '@/components/SourceCard';
|
|
|
|
|
import LangSelect from '@/components/navbar/action/LangSelect';
|
2021-03-09 16:44:09 +03:30
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2021-03-09 16:44:09 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
result.sort(langSortCmp);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function groupByLang(sources: ISource[]) {
|
|
|
|
|
const result = {} as any;
|
|
|
|
|
sources.forEach((source) => {
|
2023-02-06 10:06:33 +01:00
|
|
|
if (result[source.lang] === undefined) {
|
|
|
|
|
result[source.lang] = [] as ISource[];
|
|
|
|
|
}
|
2021-03-09 16:44:09 +03:30
|
|
|
result[source.lang].push(source);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-01-19 14:47:07 +03:30
|
|
|
|
2021-10-26 14:57:11 +03:30
|
|
|
export default function Sources() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2021-03-09 16:44:09 +03:30
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
|
|
|
|
|
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
|
|
|
|
2023-05-18 13:25:19 +02:00
|
|
|
const { data: sources, isLoading } = requestManager.useGetSourceList();
|
2021-01-19 14:47:07 +03:30
|
|
|
|
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
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
useEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('source.title'));
|
2021-03-09 16:44:09 +03:30
|
|
|
setAction(
|
2022-03-12 04:46:55 +00:00
|
|
|
<>
|
2023-06-04 21:30:15 +02:00
|
|
|
<IconButton onClick={() => navigate('/sources/all/search/')} size="large">
|
2022-03-12 04:46:55 +00:00
|
|
|
<TravelExploreIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<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
|
|
|
);
|
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
|
|
|
|
2022-11-02 21:48:22 +01:00
|
|
|
if (sources?.length === 0) {
|
2023-03-23 13:59:32 +01:00
|
|
|
return <h3>{t('source.error.label.no_sources_found')}</h3>;
|
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]) =>
|
|
|
|
|
shownLangs.indexOf(lang) !== -1 && (
|
2023-08-14 21:43:06 +02:00
|
|
|
<Fragment key={lang}>
|
2023-02-06 10:06:33 +01:00
|
|
|
<h1 key={lang} style={{ marginLeft: 25 }}>
|
2023-04-21 00:27:42 +02:00
|
|
|
{translateExtensionLanguage(lang)}
|
2023-02-06 10:06:33 +01:00
|
|
|
</h1>
|
|
|
|
|
{(list as ISource[])
|
|
|
|
|
.filter((source) => showNsfw || !source.isNsfw)
|
|
|
|
|
.map((source) => (
|
|
|
|
|
<SourceCard key={source.id} source={source} />
|
|
|
|
|
))}
|
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
|
|
|
}
|