2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* 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/. */
|
|
|
|
|
|
2023-04-22 11:39:59 +02:00
|
|
|
import { Chip, Tab, Tabs } from '@mui/material';
|
|
|
|
|
import React, { useContext, useEffect, useMemo, 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';
|
2022-11-24 09:41:03 +01:00
|
|
|
import LibraryToolbarMenu from 'components/library/LibraryToolbarMenu';
|
2021-10-30 16:16:36 +03:30
|
|
|
import LibraryMangaGrid from 'components/library/LibraryMangaGrid';
|
2021-12-19 18:06:27 +03:30
|
|
|
import AppbarSearch from 'components/util/AppbarSearch';
|
2021-11-19 16:36:02 +03:30
|
|
|
import { useQueryParam, NumberParam } from 'use-query-params';
|
2022-11-02 21:48:22 +01:00
|
|
|
import { useQuery } from 'util/client';
|
2023-02-05 16:15:20 +01:00
|
|
|
import UpdateChecker from 'components/library/UpdateChecker';
|
2023-02-13 20:20:08 +03:30
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-02-24 16:40:37 +01:00
|
|
|
import { ICategory, IManga } from 'typings';
|
2023-04-22 11:39:59 +02:00
|
|
|
import { styled } from '@mui/system';
|
|
|
|
|
import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext';
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-04-22 11:39:59 +02:00
|
|
|
const { options } = useLibraryOptionsContext();
|
2023-01-06 17:35:26 +01:00
|
|
|
const [lastLibraryUpdate, setLastLibraryUpdate] = useState(Date.now());
|
2023-05-15 11:56:55 +02:00
|
|
|
const { data: tabsData, error: tabsError, isLoading } = useQuery<ICategory[]>('/api/v1/category');
|
2022-11-02 10:42:02 +01:00
|
|
|
const tabs = tabsData ?? [];
|
2023-04-22 11:39:59 +02:00
|
|
|
const librarySize = useMemo(() => tabs.map((tab) => tab.size).reduce((prev, curr) => prev + curr, 0), [tabs]);
|
2022-11-02 10:42:02 +01:00
|
|
|
|
|
|
|
|
const [tabSearchParam, setTabSearchParam] = useQueryParam('tab', NumberParam);
|
|
|
|
|
|
2023-02-13 20:20:08 +03:30
|
|
|
const activeTab = tabs.find((tab) => tab.order === tabSearchParam) ?? tabs[0];
|
2022-11-02 21:48:22 +01:00
|
|
|
const {
|
|
|
|
|
data: mangaData,
|
|
|
|
|
error: mangaError,
|
2023-05-15 11:56:55 +02:00
|
|
|
isLoading: mangaLoading,
|
|
|
|
|
} = useQuery<IManga[]>(activeTab ? `/api/v1/category/${activeTab?.id}` : null);
|
2022-11-02 10:42:02 +01:00
|
|
|
const mangas = mangaData ?? [];
|
|
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
2021-10-29 20:11:23 +02:00
|
|
|
useEffect(() => {
|
2023-04-22 11:39:59 +02:00
|
|
|
const title = t('library.title');
|
|
|
|
|
const navBarTitle = (
|
|
|
|
|
<TitleWithSizeTag>
|
|
|
|
|
{title}
|
|
|
|
|
{mangaLoading || !options.showTabSize ? null : <TitleSizeTag label={librarySize} />}
|
|
|
|
|
</TitleWithSizeTag>
|
|
|
|
|
);
|
|
|
|
|
setTitle(navBarTitle, title);
|
2022-11-02 10:42:02 +01:00
|
|
|
setAction(
|
2021-10-29 20:11:23 +02:00
|
|
|
<>
|
2021-12-19 18:06:27 +03:30
|
|
|
<AppbarSearch />
|
2022-11-24 09:41:03 +01:00
|
|
|
<LibraryToolbarMenu />
|
2023-01-06 17:35:26 +01:00
|
|
|
<UpdateChecker handleFinishedUpdate={setLastLibraryUpdate} />
|
2021-10-29 20:11:23 +02:00
|
|
|
</>,
|
|
|
|
|
);
|
2022-11-09 18:09:15 +01:00
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
2023-03-11 18:05:58 +01:00
|
|
|
setAction(null);
|
2022-11-09 18:09:15 +01:00
|
|
|
};
|
2023-04-22 11:39:59 +02:00
|
|
|
}, [t, librarySize, mangaLoading, options]);
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2021-02-21 04:27:41 +03:30
|
|
|
const handleTabChange = (newTab: number) => {
|
2022-11-02 10:42:02 +01:00
|
|
|
setTabSearchParam(newTab === 0 ? undefined : newTab);
|
2021-02-21 04:27:41 +03:30
|
|
|
};
|
|
|
|
|
|
2022-11-02 10:42:02 +01:00
|
|
|
if (tabsError != null) {
|
2023-02-14 16:28:37 +03:30
|
|
|
return (
|
|
|
|
|
<EmptyView
|
2023-03-23 13:59:32 +01:00
|
|
|
message={t('category.error.label.request_failure')}
|
2023-02-14 16:28:37 +03:30
|
|
|
messageExtra={tabsError?.message ?? tabsError}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-11-02 10:42:02 +01:00
|
|
|
}
|
2021-08-29 20:47:57 +04:30
|
|
|
|
2023-05-15 11:56:55 +02:00
|
|
|
if (isLoading) {
|
2021-10-24 22:56:10 +03:30
|
|
|
return <LoadingPlaceholder />;
|
2021-08-29 20:47:57 +04:30
|
|
|
}
|
2021-08-21 03:46:01 +04:30
|
|
|
|
|
|
|
|
if (tabs.length === 0) {
|
2023-03-23 13:59:32 +01:00
|
|
|
return <EmptyView message={t('library.error.label.empty')} />;
|
2021-08-21 03:46:01 +04:30
|
|
|
}
|
2021-03-07 16:27:13 +03:30
|
|
|
|
2022-11-02 10:42:02 +01:00
|
|
|
if (tabs.length === 1) {
|
|
|
|
|
return (
|
2021-10-29 20:11:23 +02:00
|
|
|
<LibraryMangaGrid
|
2021-02-20 01:23:52 +03:30
|
|
|
mangas={mangas}
|
2023-01-06 17:35:26 +01:00
|
|
|
lastLibraryUpdate={lastLibraryUpdate}
|
2023-03-23 13:59:32 +01:00
|
|
|
message={t('library.error.label.empty') as string}
|
2022-11-02 21:48:22 +01:00
|
|
|
isLoading={activeTab != null && mangaLoading}
|
2021-02-20 01:23:52 +03:30
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 10:42:02 +01:00
|
|
|
// 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) => (
|
2023-04-22 11:39:59 +02:00
|
|
|
<Tab
|
|
|
|
|
sx={{ display: 'flex' }}
|
|
|
|
|
key={tab.id}
|
|
|
|
|
label={
|
|
|
|
|
<TitleWithSizeTag>
|
|
|
|
|
{tab.name}
|
|
|
|
|
{options.showTabSize ? <TitleSizeTag label={tab.size} /> : null}
|
|
|
|
|
</TitleWithSizeTag>
|
|
|
|
|
}
|
|
|
|
|
value={tab.order}
|
|
|
|
|
/>
|
2022-11-02 10:42:02 +01:00
|
|
|
))}
|
|
|
|
|
</Tabs>
|
|
|
|
|
{tabs.map((tab) => (
|
|
|
|
|
<TabPanel key={tab.order} index={tab.order} currentIndex={activeTab.order}>
|
2023-02-06 10:06:33 +01:00
|
|
|
{tab === activeTab &&
|
|
|
|
|
(mangaError ? (
|
|
|
|
|
<EmptyView
|
2023-03-23 13:59:32 +01:00
|
|
|
message={t('manga.error.label.request_failure')}
|
2023-02-06 10:06:33 +01:00
|
|
|
messageExtra={mangaError?.message ?? mangaError}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
2022-11-02 10:42:02 +01:00
|
|
|
<LibraryMangaGrid
|
|
|
|
|
mangas={mangas}
|
2023-01-06 17:35:26 +01:00
|
|
|
lastLibraryUpdate={lastLibraryUpdate}
|
2023-03-23 13:59:32 +01:00
|
|
|
message={t('library.error.label.empty') as string}
|
2022-11-02 21:48:22 +01:00
|
|
|
isLoading={mangaLoading}
|
2022-11-02 10:42:02 +01:00
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</TabPanel>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2021-02-13 21:12:18 +03:30
|
|
|
}
|