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

144 lines
4.5 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-02-13 21:12:18 +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-02-20 01:23:52 +03:30
import { Tab, Tabs } from '@material-ui/core';
2021-02-13 21:12:18 +03:30
import React, { useContext, useEffect, useState } from 'react';
import MangaGrid from 'components/manga/MangaGrid';
import NavbarContext from 'context/NavbarContext';
import client from 'util/client';
import cloneObject from 'util/cloneObject';
2021-02-13 21:12:18 +03:30
2021-02-20 01:23:52 +03:30
interface IMangaCategory {
category: ICategory
mangas: IManga[]
2021-03-07 16:27:13 +03:30
isFetched: boolean
2021-02-20 01:23:52 +03:30
}
interface TabPanelProps {
children: React.ReactNode;
index: any;
value: any;
}
function TabPanel(props: TabPanelProps) {
const {
2021-02-21 04:27:41 +03:30
children, value, index,
2021-02-20 01:23:52 +03:30
} = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
>
{value === index && children}
</div>
);
}
export default function Library() {
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => { setTitle('Library'); setAction(<></>); }, []);
2021-02-20 01:23:52 +03:30
const [tabs, setTabs] = useState<IMangaCategory[]>([]);
const [tabNum, setTabNum] = useState<number>(0);
2021-02-13 21:12:18 +03:30
2021-02-21 04:27:41 +03:30
// a hack so MangaGrid doesn't stop working. I won't change it in case
// if I do manga pagination for library..
const [lastPageNum, setLastPageNum] = useState<number>(1);
const handleTabChange = (newTab: number) => {
setTabNum(newTab);
};
2021-02-13 21:12:18 +03:30
useEffect(() => {
2021-08-21 03:46:01 +04:30
client.get('/api/v1/category')
.then((response) => response.data)
.then((categories: ICategory[]) => {
const categoryTabs = categories.map((category) => ({
category,
mangas: [] as IManga[],
isFetched: false,
}));
setTabs(categoryTabs);
});
2021-02-20 01:23:52 +03:30
}, []);
2021-03-07 16:27:13 +03:30
// fetch the current tab
useEffect(() => {
tabs.forEach((tab, index) => {
2021-08-21 03:46:01 +04:30
if (index === tabNum && !tab.isFetched) {
2021-03-07 16:27:13 +03:30
// eslint-disable-next-line @typescript-eslint/no-shadow
client.get(`/api/v1/category/${tab.category.id}`)
.then((response) => response.data)
2021-03-07 16:27:13 +03:30
.then((data: IManga[]) => {
const tabsClone = cloneObject(tabs);
2021-03-07 16:27:13 +03:30
tabsClone[index].mangas = data;
tabsClone[index].isFetched = true;
setTabs(tabsClone); // clone the object
});
}
});
2021-08-21 03:46:01 +04:30
}, [tabs.length, tabNum]);
if (tabs.length === 0) {
return <h3>Library is empty</h3>;
}
2021-03-07 16:27:13 +03:30
2021-02-20 01:23:52 +03:30
let toRender;
if (tabs.length > 1) {
2021-02-21 04:27:41 +03:30
// eslint-disable-next-line max-len
const tabDefines = tabs.map((tab) => (<Tab label={tab.category.name} value={tab.category.order} />));
2021-02-20 01:23:52 +03:30
2021-02-21 04:27:41 +03:30
const tabBodies = tabs.map((tab) => (
<TabPanel value={tabNum} index={tab.category.order}>
2021-02-20 01:23:52 +03:30
<MangaGrid
mangas={tab.mangas}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
2021-03-07 16:27:13 +03:30
message={tab.isFetched ? 'Category is Empty' : 'Loading...'}
2021-02-20 01:23:52 +03:30
/>
</TabPanel>
));
2021-02-20 02:28:55 +03:30
// Visual Hack: 160px is min-width for viewport width of >600
2021-02-20 02:28:55 +03:30
const scrollableTabs = window.innerWidth < tabs.length * 160;
2021-02-20 01:23:52 +03:30
toRender = (
<>
<Tabs
value={tabNum}
2021-02-21 04:27:41 +03:30
onChange={(e, newTab) => handleTabChange(newTab)}
2021-02-20 01:23:52 +03:30
indicatorColor="primary"
textColor="primary"
2021-02-20 02:28:55 +03:30
centered={!scrollableTabs}
variant={scrollableTabs ? 'scrollable' : 'fullWidth'}
scrollButtons="on"
2021-02-20 01:23:52 +03:30
>
{tabDefines}
</Tabs>
{tabBodies}
</>
);
} else {
const mangas = tabs.length === 1 ? tabs[0].mangas : [];
toRender = (
<MangaGrid
mangas={mangas}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
2021-03-07 16:27:13 +03:30
message={tabs.length > 0 ? 'Library is Empty' : undefined}
2021-02-20 01:23:52 +03:30
/>
);
}
return toRender;
2021-02-13 21:12:18 +03:30
}