/* * 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/. */ import React, { useContext, useEffect, useState } from 'react'; import ExtensionLangSelect from '../components/ExtensionLangSelect'; import SourceCard from '../components/SourceCard'; import NavbarContext from '../context/NavbarContext'; import client from '../util/client'; import { defualtLangs, langCodeToName, langSortCmp } from '../util/language'; import useLocalStorage from '../util/useLocalStorage'; function sourceToLangList(sources: ISource[]) { const result: string[] = []; sources.forEach((source) => { if (result.indexOf(source.lang) === -1) { result.push(source.lang); } }); 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; } export default function Sources() { const { setTitle, setAction } = useContext(NavbarContext); const [shownLangs, setShownLangs] = useLocalStorage('shownSourceLangs', defualtLangs()); const [sources, setSources] = useState([]); const [fetched, setFetched] = useState(false); useEffect(() => { setTitle('Sources'); setAction( , ); }, [shownLangs, sources]); useEffect(() => { client.get('/api/v1/source/list') .then((response) => response.data) .then((data) => { setSources(data); setFetched(true); }); }, []); if (sources.length === 0) { if (fetched) return (

No sources found. Install Some Extensions first.

); return (

loading...

); } 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 && (

{langCodeToName(lang)}

{(list as ISource[]).map((source) => ( ))}
) ))} ); }