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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-02-13 21:12:18 +03:30
|
|
|
|
2023-06-17 16:55:24 +02:00
|
|
|
import { Chip, Tab, Tabs, styled, Box } from '@mui/material';
|
2023-12-25 21:37:45 +01:00
|
|
|
import React, { useContext, useEffect, useMemo, useState } from 'react';
|
2021-11-19 16:36:02 +03:30
|
|
|
import { useQueryParam, NumberParam } from 'use-query-params';
|
2023-02-13 20:20:08 +03:30
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
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';
|
2023-06-05 01:42:39 +02:00
|
|
|
import { useLibraryOptionsContext } from '@/components/context/LibraryOptionsContext';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
|
2023-12-25 21:37:45 +01:00
|
|
|
import { useSelectableCollection } from '@/components/collection/useSelectableCollection.ts';
|
|
|
|
|
import { TManga } from '@/typings.ts';
|
|
|
|
|
import { SelectableCollectionSelectMode } from '@/components/collection/SelectableCollectionSelectMode.tsx';
|
|
|
|
|
import { useGetVisibleLibraryMangas } from '@/components/library/useGetVisibleLibraryMangas.ts';
|
|
|
|
|
import { SelectionFAB } from '@/components/manga/SelectionFAB.tsx';
|
|
|
|
|
import { MangasSelectionFABActionItems } from '@/components/manga/MangasSelectionFABActionItems.tsx';
|
|
|
|
|
import { PARTIAL_MANGA_FIELDS } from '@/lib/graphql/Fragments.ts';
|
2023-04-22 11:39:59 +02:00
|
|
|
|
2023-06-17 16:55:24 +02:00
|
|
|
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,
|
|
|
|
|
[theme.breakpoints.down('sm')]: {
|
|
|
|
|
top: '56px', // header height
|
|
|
|
|
width: '100%',
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2023-04-22 11:39:59 +02:00
|
|
|
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
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export 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-09-06 21:47:13 +02:00
|
|
|
const {
|
|
|
|
|
data: categoriesResponse,
|
|
|
|
|
error: tabsError,
|
|
|
|
|
loading: areCategoriesLoading,
|
|
|
|
|
} = requestManager.useGetCategories();
|
|
|
|
|
const tabsData = categoriesResponse?.categories.nodes.filter(
|
|
|
|
|
(category) => category.id !== 0 || (category.id === 0 && category.mangas.totalCount),
|
|
|
|
|
);
|
2022-11-02 10:42:02 +01:00
|
|
|
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],
|
|
|
|
|
);
|
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 {
|
2023-09-06 21:47:13 +02:00
|
|
|
data: categoryMangaResponse,
|
2022-11-02 21:48:22 +01:00
|
|
|
error: mangaError,
|
2023-09-06 21:47:13 +02:00
|
|
|
loading: mangaLoading,
|
2023-12-25 21:37:45 +01:00
|
|
|
} = requestManager.useGetCategoryMangas(activeTab?.id, { skip: !activeTab });
|
|
|
|
|
const categoryMangas = categoryMangaResponse?.mangas.nodes ?? [];
|
|
|
|
|
const { visibleMangas: mangas, showFilteredOutMessage } = useGetVisibleLibraryMangas(categoryMangas);
|
|
|
|
|
|
|
|
|
|
const [isSelectModeActive, setIsSelectModeActive] = useState(false);
|
|
|
|
|
const {
|
|
|
|
|
areNoItemsForKeySelected: areNoItemsSelected,
|
|
|
|
|
areAllItemsForKeySelected: areAllItemsSelected,
|
|
|
|
|
selectedItemIds,
|
|
|
|
|
handleSelectAll,
|
|
|
|
|
handleSelection,
|
|
|
|
|
} = useSelectableCollection<TManga['id'], string>(mangas.length, { currentKey: activeTab?.id.toString() });
|
|
|
|
|
|
|
|
|
|
const handleSelect = (id: number, selected: boolean) => {
|
|
|
|
|
setIsSelectModeActive(!!(selectedItemIds.length + (selected ? 1 : -1)));
|
|
|
|
|
handleSelection(id, selected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const selectedMangas = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
selectedItemIds.map(
|
|
|
|
|
(id) =>
|
|
|
|
|
requestManager.graphQLClient.client.cache.readFragment<TManga>({
|
|
|
|
|
id: requestManager.graphQLClient.client.cache.identify({ __typename: 'MangaType', id }),
|
|
|
|
|
fragment: PARTIAL_MANGA_FIELDS,
|
|
|
|
|
fragmentName: 'PARTIAL_MANGA_FIELDS',
|
|
|
|
|
})!,
|
|
|
|
|
),
|
2023-12-30 04:04:47 +01:00
|
|
|
[selectedItemIds.length, mangas],
|
2023-12-25 21:37:45 +01:00
|
|
|
);
|
2022-11-02 10:42:02 +01:00
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
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}
|
2023-12-25 21:37:45 +01:00
|
|
|
{options.showTabSize && <TitleSizeTag label={librarySize} />}
|
2023-04-22 11:39:59 +02:00
|
|
|
</TitleWithSizeTag>
|
|
|
|
|
);
|
|
|
|
|
setTitle(navBarTitle, title);
|
2022-11-02 10:42:02 +01:00
|
|
|
setAction(
|
2021-10-29 20:11:23 +02:00
|
|
|
<>
|
2023-12-25 21:37:45 +01:00
|
|
|
{!isSelectModeActive && (
|
|
|
|
|
<>
|
|
|
|
|
<AppbarSearch />
|
|
|
|
|
<LibraryToolbarMenu />
|
|
|
|
|
<UpdateChecker />
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
<SelectableCollectionSelectMode
|
|
|
|
|
isActive={isSelectModeActive}
|
|
|
|
|
areAllItemsSelected={areAllItemsSelected}
|
|
|
|
|
areNoItemsSelected={areNoItemsSelected}
|
|
|
|
|
onSelectAll={(selectAll) =>
|
|
|
|
|
handleSelectAll(selectAll, [...new Set(mangas.map((manga) => manga.id))])
|
|
|
|
|
}
|
|
|
|
|
onModeChange={(checked) => {
|
|
|
|
|
setIsSelectModeActive(checked);
|
|
|
|
|
|
|
|
|
|
if (checked) {
|
|
|
|
|
handleSelectAll(true, [...new Set(mangas.map((manga) => manga.id))]);
|
|
|
|
|
} else {
|
|
|
|
|
tabs.forEach((tab) => handleSelectAll(false, [], tab.id.toString()));
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
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-12-25 21:37:45 +01:00
|
|
|
}, [
|
|
|
|
|
t,
|
|
|
|
|
librarySize,
|
|
|
|
|
areCategoriesLoading,
|
|
|
|
|
options,
|
|
|
|
|
isSelectModeActive,
|
|
|
|
|
areNoItemsSelected,
|
|
|
|
|
areAllItemsSelected,
|
|
|
|
|
selectedItemIds.length,
|
|
|
|
|
mangas.length,
|
|
|
|
|
]);
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2021-02-21 04:27:41 +03:30
|
|
|
const handleTabChange = (newTab: number) => {
|
2023-09-06 21:47:13 +02:00
|
|
|
setTabSearchParam(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-09-06 21:47:13 +02:00
|
|
|
messageExtra={tabsError.message ?? tabsError}
|
2023-02-14 16:28:37 +03:30
|
|
|
/>
|
|
|
|
|
);
|
2022-11-02 10:42:02 +01:00
|
|
|
}
|
2021-08-29 20:47:57 +04:30
|
|
|
|
2023-06-05 01:24:56 +02:00
|
|
|
if (areCategoriesLoading) {
|
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-07-04 21:53:25 +02:00
|
|
|
message={t('library.error.label.empty')}
|
2022-11-02 21:48:22 +01:00
|
|
|
isLoading={activeTab != null && mangaLoading}
|
2023-12-25 21:37:45 +01:00
|
|
|
selectedMangaIds={selectedItemIds}
|
|
|
|
|
isSelectModeActive={isSelectModeActive}
|
|
|
|
|
handleSelection={handleSelect}
|
|
|
|
|
showFilteredOutMessage={showFilteredOutMessage}
|
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 (
|
2023-12-25 21:37:45 +01:00
|
|
|
<>
|
|
|
|
|
<StyledGridWrapper>
|
|
|
|
|
<TabsMenu
|
|
|
|
|
sx={{ borderBottom: 2, borderColor: 'divider' }}
|
|
|
|
|
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}
|
|
|
|
|
{options.showTabSize ? <TitleSizeTag label={tab.mangas.totalCount} /> : null}
|
|
|
|
|
</TitleWithSizeTag>
|
|
|
|
|
}
|
|
|
|
|
value={tab.order}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</TabsMenu>
|
2022-11-02 10:42:02 +01:00
|
|
|
{tabs.map((tab) => (
|
2023-12-25 21:37:45 +01:00
|
|
|
<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}
|
|
|
|
|
message={t('library.error.label.empty')}
|
|
|
|
|
isLoading={mangaLoading}
|
|
|
|
|
selectedMangaIds={selectedItemIds}
|
|
|
|
|
isSelectModeActive={isSelectModeActive}
|
|
|
|
|
handleSelection={handleSelect}
|
|
|
|
|
showFilteredOutMessage={showFilteredOutMessage}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</TabPanel>
|
2022-11-02 10:42:02 +01:00
|
|
|
))}
|
2023-12-25 21:37:45 +01:00
|
|
|
</StyledGridWrapper>
|
|
|
|
|
{isSelectModeActive && (
|
|
|
|
|
<SelectionFAB selectedItemsCount={selectedItemIds.length} title="manga.title">
|
|
|
|
|
{(handleClose) => (
|
|
|
|
|
<MangasSelectionFABActionItems
|
|
|
|
|
selectedMangas={selectedMangas}
|
|
|
|
|
handleClose={(selectionModeState) => {
|
|
|
|
|
handleClose();
|
|
|
|
|
setIsSelectModeActive(selectionModeState);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</SelectionFAB>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2022-11-02 10:42:02 +01:00
|
|
|
);
|
2021-02-13 21:12:18 +03:30
|
|
|
}
|