-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathLibrary.tsx
156 lines (141 loc) · 5.73 KB
/
Library.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
*/
import { Chip, Tab, Tabs, styled } from '@mui/material';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useQueryParam, NumberParam } from 'use-query-params';
import { useTranslation } from 'react-i18next';
import requestManager from '@/lib/RequestManager';
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 TitleWithSizeTag = styled('span')({
display: 'flex',
alignItems: 'center',
});
const TitleSizeTag = (props: React.ComponentProps<typeof Chip>) => (
<Chip {...props} size="small" sx={{ marginLeft: '5px' }} />
);
export default function Library() {
const { t } = useTranslation();
const { options } = useLibraryOptionsContext();
const [lastLibraryUpdate, setLastLibraryUpdate] = useState(Date.now());
const { data: tabsData, error: tabsError, isLoading: areCategoriesLoading } = requestManager.useGetCategories();
const tabs = tabsData ?? [];
const librarySize = useMemo(() => tabs.map((tab) => tab.size).reduce((prev, curr) => prev + curr, 0), [tabs]);
const [tabSearchParam, setTabSearchParam] = useQueryParam('tab', NumberParam);
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 ?? [];
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]);
const handleTabChange = (newTab: number) => {
setTabSearchParam(newTab === 0 ? undefined : newTab);
};
if (tabsError != null) {
return (
<EmptyView
message={t('category.error.label.request_failure')}
messageExtra={tabsError?.message ?? tabsError}
/>
);
}
if (areCategoriesLoading) {
return <LoadingPlaceholder />;
}
if (tabs.length === 0) {
return <EmptyView message={t('library.error.label.empty')} />;
}
if (tabs.length === 1) {
return (
<LibraryMangaGrid
mangas={mangas}
lastLibraryUpdate={lastLibraryUpdate}
message={t('library.error.label.empty') as string}
isLoading={activeTab != null && mangaLoading}
/>
);
}
// Visual Hack: 160px is min-width for viewport width of >600
const scrollableTabs = window.innerWidth < tabs.length * 160;
return (
<>
<Tabs
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.size} /> : null}
</TitleWithSizeTag>
}
value={tab.order}
/>
))}
</Tabs>
{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') as string}
isLoading={mangaLoading}
/>
))}
</TabPanel>
))}
</>
);
}