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

107 lines
3.6 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-22 17:00:33 +03:30
import React, { useContext, useEffect, useState } from 'react';
import LangSelect from 'components/manga/LangSelect';
import SourceCard from 'components/manga/SourceCard';
import NavbarContext from 'context/NavbarContext';
import client from 'util/client';
import {
sourceDefualtLangs, sourceForcedDefaultLangs, langCodeToName, langSortCmp,
} from 'util/language';
import useLocalStorage from 'util/useLocalStorage';
2021-03-09 16:44:09 +03:30
function sourceToLangList(sources: ISource[]) {
const result: string[] = [];
sources.forEach((source) => {
2021-03-09 18:05:34 +03:30
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) => {
if (result[source.lang] === undefined) { result[source.lang] = [] as ISource[]; }
result[source.lang].push(source);
});
return result;
}
2021-01-19 14:47:07 +03:30
2021-05-27 17:13:22 +04:30
export default function MangaSources() {
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
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
2021-01-19 14:47:07 +03:30
const [sources, setSources] = useState<ISource[]>([]);
2021-03-07 16:27:13 +03:30
const [fetched, setFetched] = useState<boolean>(false);
2021-01-19 14:47:07 +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-03-09 16:44:09 +03:30
useEffect(() => {
setTitle('Sources');
setAction(
<LangSelect
2021-03-09 16:44:09 +03:30
shownLangs={shownLangs}
setShownLangs={setShownLangs}
allLangs={sourceToLangList(sources)}
2021-10-18 14:22:33 +03:30
forcedLangs={sourceForcedDefaultLangs()}
2021-03-09 16:44:09 +03:30
/>,
);
}, [shownLangs, sources]);
2021-01-19 14:47:07 +03:30
useEffect(() => {
client.get('/api/v1/source/list')
.then((response) => response.data)
2021-03-07 16:27:13 +03:30
.then((data) => { setSources(data); setFetched(true); });
2021-01-19 14:47:07 +03:30
}, []);
if (sources.length === 0) {
2021-03-07 16:27:13 +03:30
if (fetched) return (<h3>No sources found. Install Some Extensions first.</h3>);
return (<h3>loading...</h3>);
2021-01-19 14:47:07 +03:30
}
2021-03-09 16:44:09 +03:30
return (
<>
{/* eslint-disable-next-line max-len */}
{Object.entries(groupByLang(sources)).sort((a, b) => langSortCmp(a[0], b[0])).map(([lang, list]) => (
shownLangs.indexOf(lang) !== -1 && (
<React.Fragment key={lang}>
<h1 key={lang} style={{ marginLeft: 25 }}>{langCodeToName(lang)}</h1>
2021-09-03 04:47:02 +04:30
{(list as ISource[])
.filter((source) => showNsfw || !source.isNsfw)
.map((source) => (
<SourceCard
key={source.id}
source={source}
/>
))}
2021-03-09 16:44:09 +03:30
</React.Fragment>
)
))}
</>
);
2021-01-19 14:47:07 +03:30
}