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

189 lines
6.9 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-13 21:12:18 +03:30
import { Chip, Tab, Tabs, styled, Box } from '@mui/material';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useQueryParam, NumberParam } from 'use-query-params';
2023-02-13 20:20:08 +03:30
import { useTranslation } from 'react-i18next';
2023-08-15 13:54:29 +02:00
import requestManager from '@/lib/requests/RequestManager.ts';
import NavbarContext from '@/components/context/NavbarContext';
import EmptyView from '@/components/util/EmptyView';
import LoadingPlaceholder from '@/components/util/LoadingPlaceholder';
import TabPanel from '@/components/util/TabPanel';
import LibraryToolbarMenu from '@/components/library/LibraryToolbarMenu';
import LibraryMangaGrid from '@/components/library/LibraryMangaGrid';
import AppbarSearch from '@/components/util/AppbarSearch';
import UpdateChecker from '@/components/library/UpdateChecker';
import { useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext';
const StyledGridWrapper = styled(Box)(({ theme }) => ({
// TabsMenu height + TabsMenu bottom padding - grid item top padding
marginTop: `calc(48px + 13px - 8px)`,
// header height - TabsMenu height - TabsMenu bottom padding + grid item top padding
minHeight: 'calc(100vh - 64px - 48px - 13px + 8px)',
position: 'relative',
[theme.breakpoints.down('sm')]: {
// TabsMenu - 8px margin diff header height (56px) + TabsMenu bottom padding - grid item top padding
marginTop: `calc(48px - 8px + 13px - 8px)`,
// header height (+ 8px margin) - footer height - TabsMenu height
minHeight: 'calc(100vh - 64px - 64px - 48px)',
},
}));
const TabsMenu = styled(Tabs)(({ theme }) => ({
display: 'flex',
position: 'fixed',
top: '64px',
width: 'calc(100% - 64px)',
zIndex: 1,
backgroundColor: theme.palette.background.default,
paddingBottom: '13px',
[theme.breakpoints.down('sm')]: {
top: '56px', // header height
width: '100%',
},
}));
const TitleWithSizeTag = styled('span')({
display: 'flex',
alignItems: 'center',
});
const TitleSizeTag = (props: React.ComponentProps<typeof Chip>) => (
<Chip {...props} size="small" sx={{ marginLeft: '5px' }} />
);
2021-02-13 21:12:18 +03:30
2021-02-20 01:23:52 +03:30
export default function Library() {
2023-02-13 20:20:08 +03:30
const { t } = useTranslation();
2023-02-14 16:47:38 +03:30
const { options } = useLibraryOptionsContext();
const [lastLibraryUpdate, setLastLibraryUpdate] = useState(Date.now());
2023-09-01 20:39:30 +02:00
const { data, error: tabsError, loading: areCategoriesLoading } = requestManager.useGetCategories();
const tabsData = data?.categories.nodes.filter((category) => category.id !== 0);
const tabs = tabsData ?? [];
2023-09-01 20:39:30 +02:00
const librarySize = useMemo(
() => tabs.map((tab) => tab.mangas.totalCount).reduce((prev, curr) => prev + curr, 0),
[tabs],
);
const [tabSearchParam, setTabSearchParam] = useQueryParam('tab', NumberParam);
2023-02-13 20:20:08 +03:30
const activeTab = tabs.find((tab) => tab.order === tabSearchParam) ?? tabs[0];
const {
data: mangaData,
error: mangaError,
isLoading: mangaLoading,
} = requestManager.useGetCategoryMangas(activeTab?.id, { skipRequest: !activeTab });
const mangas = mangaData ?? [];
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
useEffect(() => {
const title = t('library.title');
const navBarTitle = (
<TitleWithSizeTag>
{title}
{areCategoriesLoading || !options.showTabSize ? null : <TitleSizeTag label={librarySize} />}
</TitleWithSizeTag>
);
setTitle(navBarTitle, title);
setAction(
<>
<AppbarSearch />
<LibraryToolbarMenu />
<UpdateChecker handleFinishedUpdate={setLastLibraryUpdate} />
</>,
);
return () => {
setTitle('');
setAction(null);
};
}, [t, librarySize, areCategoriesLoading, options]);
2021-03-09 16:44:09 +03:30
2021-02-21 04:27:41 +03:30
const handleTabChange = (newTab: number) => {
setTabSearchParam(newTab === 0 ? undefined : newTab);
2021-02-21 04:27:41 +03:30
};
if (tabsError != null) {
2023-02-14 16:28:37 +03:30
return (
<EmptyView
message={t('category.error.label.request_failure')}
2023-09-01 20:39:30 +02:00
messageExtra={(tabsError?.message as any) ?? tabsError}
2023-02-14 16:28:37 +03:30
/>
);
}
2021-08-29 20:47:57 +04:30
if (areCategoriesLoading) {
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={t('library.error.label.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}
lastLibraryUpdate={lastLibraryUpdate}
message={t('library.error.label.empty')}
isLoading={activeTab != null && mangaLoading}
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 (
<StyledGridWrapper>
<TabsMenu
value={activeTab.order}
onChange={(e, newTab) => handleTabChange(newTab)}
indicatorColor="primary"
textColor="primary"
centered={!scrollableTabs}
variant={scrollableTabs ? 'scrollable' : 'fullWidth'}
scrollButtons
allowScrollButtonsMobile
>
{tabs.map((tab) => (
<Tab
sx={{ display: 'flex' }}
key={tab.id}
label={
<TitleWithSizeTag>
{tab.name}
2023-09-01 20:39:30 +02:00
{options.showTabSize ? <TitleSizeTag label={tab.mangas.totalCount} /> : null}
</TitleWithSizeTag>
}
value={tab.order}
/>
))}
</TabsMenu>
{tabs.map((tab) => (
<TabPanel key={tab.order} index={tab.order} currentIndex={activeTab.order}>
{tab === activeTab &&
(mangaError ? (
<EmptyView
message={t('manga.error.label.request_failure')}
messageExtra={mangaError?.message ?? mangaError}
/>
) : (
<LibraryMangaGrid
mangas={mangas}
lastLibraryUpdate={lastLibraryUpdate}
message={t('library.error.label.empty')}
isLoading={mangaLoading}
/>
))}
</TabPanel>
))}
</StyledGridWrapper>
);
2021-02-13 21:12:18 +03:30
}