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

121 lines
4.3 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-09-09 17:51:22 +04:30
import { Tab, Tabs } from '@mui/material';
import React, {
useContext, useEffect, useState,
} from 'react';
2021-10-30 16:10:34 +03:30
import NavbarContext from 'components/context/NavbarContext';
import EmptyView from 'components/util/EmptyView';
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
2021-10-26 12:42:30 +03:30
import TabPanel from 'components/util/TabPanel';
2021-10-30 16:16:36 +03:30
import LibraryOptions from 'components/library/LibraryOptions';
import LibraryMangaGrid from 'components/library/LibraryMangaGrid';
import AppbarSearch from 'components/util/AppbarSearch';
import { useQueryParam, NumberParam } from 'use-query-params';
import useSWR from 'swr';
import UpdateChecker from '../components/library/UpdateChecker';
2021-02-13 21:12:18 +03:30
2021-02-20 01:23:52 +03:30
export default function Library() {
const { data: tabsData, error: tabsError } = useSWR<ICategory[]>('/api/v1/category');
const tabs = tabsData ?? [];
const [tabSearchParam, setTabSearchParam] = useQueryParam('tab', NumberParam);
const activeTab = tabs.find((t) => t.order === tabSearchParam) ?? tabs[0];
const { data: mangaData, error: mangaError } = useSWR<IManga[]>(`/api/v1/category/${activeTab?.id}`, {
isPaused: () => activeTab == null,
});
const mangas = mangaData ?? [];
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => {
setTitle('Library');
setAction(
<>
<AppbarSearch />
<LibraryOptions />
<UpdateChecker />
</>,
);
}, []);
2021-03-09 16:44:09 +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) => {
setTabSearchParam(newTab === 0 ? undefined : newTab);
2021-02-21 04:27:41 +03:30
};
if (tabsError != null) {
return <EmptyView message="Could not load categories" messageExtra={tabsError?.message ?? tabsError} />;
}
2021-08-29 20:47:57 +04:30
if (tabsData == null) {
return <LoadingPlaceholder />;
2021-08-29 20:47:57 +04:30
}
2021-08-21 03:46:01 +04:30
if (tabs.length === 0) {
return <EmptyView message="Your Library is empty" />;
2021-08-21 03:46:01 +04:30
}
2021-03-07 16:27:13 +03:30
if (tabs.length === 1) {
return (
<LibraryMangaGrid
2021-02-20 01:23:52 +03:30
mangas={mangas}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
message="Your Library is empty"
isLoading={activeTab != null && mangaData == null && mangaError == null}
2021-02-20 01:23:52 +03:30
/>
);
}
// Visual Hack: 160px is min-width for viewport width of >600
const scrollableTabs = window.innerWidth < tabs.length * 160;
return (
<>
<Tabs
key={activeTab.order}
value={activeTab.order}
onChange={(e, newTab) => handleTabChange(newTab)}
indicatorColor="primary"
textColor="primary"
centered={!scrollableTabs}
variant={scrollableTabs ? 'scrollable' : 'fullWidth'}
scrollButtons
allowScrollButtonsMobile
>
{tabs.map((tab) => (
<Tab key={tab.id} label={tab.name} value={tab.order} />
))}
</Tabs>
{tabs.map((tab) => (
<TabPanel key={tab.order} index={tab.order} currentIndex={activeTab.order}>
{tab === activeTab && (mangaError
? (
<EmptyView message="Could not load manga" messageExtra={mangaError?.message ?? mangaError} />
)
: (
<LibraryMangaGrid
mangas={mangas}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
message="Category is Empty"
isLoading={mangaData == null}
/>
))}
</TabPanel>
))}
</>
);
2021-02-13 21:12:18 +03:30
}