-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCatalogWithSearch.tsx
288 lines (275 loc) · 8.98 KB
/
CatalogWithSearch.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright ish group pty ltd 2022.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*/
import { Delete } from '@mui/icons-material';
import AddIcon from '@mui/icons-material/Add';
import { Fade, Typography } from '@mui/material';
import Fab from '@mui/material/Fab';
import IconButton from '@mui/material/IconButton';
import $t from '@t';
import clsx from 'clsx';
import { AddButton, AnyArgFunction, DynamicSizeList, makeAppStyles } from 'ish-ui';
import React, { memo, useMemo, useState } from 'react';
import AutoSizer from 'react-virtualized-auto-sizer';
import { areEqual } from 'react-window';
import { CatalogData, CatalogItemType } from '../../../../model/common/Catalog';
import NewsRender from '../../news/NewsRender';
import ExpandableContainer from '../expandable/ExpandableContainer';
import UserSearch from '../swipeable-sidebar/components/UserSearch';
import CatalogItem from './CatalogItem';
const Row = memo<any>(
({
style, item, onOpen, onRemove, forwardedRef
}) => (
<div style={style} ref={forwardedRef}>
<CatalogItem
item={item}
onOpen={onOpen}
secondaryAction={item.keyCode?.startsWith("ish.") ? (
<IconButton
onMouseDown={e => e.stopPropagation()}
onClick={e => {
e.stopPropagation();
onRemove(item);
}}
className="lightGrayIconButton"
size="small"
>
<Delete fontSize="inherit"/>
</IconButton>
) : null}
grayOut={!item.enabled}
showDot={!item.hideDot}
hoverSecondary
/>
</div>
),
areEqual
);
const RowRenderer = React.forwardRef<any, any>(({ data, index, style }, ref) => {
const { items, ...rest } = data;
return (
<Row
key={items[index]?.id}
item={items[index]}
style={style}
forwardedRef={ref}
{...rest}
/>
);
});
interface Props {
items: CatalogItemType[];
title: string;
itemsListTitle: string;
onOpen: AnyArgFunction;
toggleInstall?: (item: CatalogItemType) => void;
addNewItem?: Partial<CatalogItemType>;
onClickNew?: AnyArgFunction;
customAddNew?: AnyArgFunction;
description?: string;
}
const useStyles = makeAppStyles<void, 'fabOpened' | 'fabTip' | 'fab'>()((theme, p, classes) => ({
root: {
padding: theme.spacing(4),
marginTop: theme.spacing(1),
display: "flex",
flexDirection: "column",
height: "100vh"
},
fabContainer: {
zIndex: 1,
position: "relative",
[`&.${classes.fabOpened}`]: {
[`& .${classes.fabTip}`]: {
paddingLeft: theme.spacing(2),
transform: "translateX(0)",
visibility: "visible"
},
[`& .${classes.fab}`]: {
backgroundColor: theme.palette.primary.main,
transform: "rotate(45deg)"
}
},
},
fabTip: {
zIndex: -1,
top: 12,
visibility: "hidden",
position: "absolute",
transform: "translateX(-30px)",
willChange: "transform,visibility",
transition: "transform 0.2s ease-in-out,visibility 0.2s ease-in-out",
},
fab: {
willChange: "transform,backgroundColor",
backgroundColor: "black",
transition: "backgroundColor 0.2s ease-in-out, transform 0.2s ease-in-out",
},
fabOpened: {},
}));
const CatalogWithSearch = React.memo<Props>((
{
title,
items,
onOpen,
toggleInstall,
addNewItem,
description,
itemsListTitle,
customAddNew,
onClickNew
}
) => {
const [search, setSearch] = useState("");
const [opened, setOpened] = useState(false);
const [expanded, setExpanded] = useState([0]);
const { classes } = useStyles();
const open = () => (customAddNew ? customAddNew() : setOpened(prev => !prev));
const filteredItems = useMemo<CatalogData>(() => {
const result = {
installed: [],
categories: {},
other: []
};
items
.filter(i => i.title.toLowerCase().includes(search.toLowerCase()))
.forEach(i => {
if (i.installed) {
result.installed.push(i);
}
if (i.category) {
if (!result.categories[i.category]) {
result.categories[i.category] = [];
}
result.categories[i.category].push(i);
} else if (i.keyCode?.startsWith("ish.")) {
result.other.push(i);
}
});
return result;
}, [items, search]);
const categoryKeys = Object.keys(filteredItems.categories);
return (
<div className={classes.root}>
<div className="centeredFlex">
<div className={clsx(classes.fabContainer, opened && classes.fabOpened)}>
<Fab
type="button"
size="large"
color="primary"
className={classes.fab}
onClick={open}
>
<AddIcon/>
</Fab>
<Typography className={classes.fabTip} variant="overline" color="primary" fontWeight="bold">{$t('close')}</Typography>
</div>
<div className="flex-fill"/>
<UserSearch getSearchResults={setSearch} placeholder={$t('filter_items')}/>
</div>
<Typography variant="h4" className="mt-5 mb-3">{title}</Typography>
<NewsRender page className="mb-3"/>
<div className="relative flex-fill flex-column">
<Fade in={opened} className="flex-fill flex-column absolute w-100 h-100 overflow-auto">
<div>
{description && (
<div className="mb-3">
{description}
</div>
)}
{addNewItem && (
<div className="mb-2">
<div className="heading">
{addNewItem.category}
</div>
<CatalogItem item={{ ...addNewItem, installed: true, enabled: true }} onOpen={onClickNew}/>
</div>
)}
<div>
{categoryKeys.map((c, index) => (
<ExpandableContainer
key={c + index}
index={index}
header={c}
expanded={search ? [index] : expanded}
setExpanded={setExpanded}
noDivider
>
{filteredItems.categories[c].map(i => (
<CatalogItem
item={i}
onOpen={() => toggleInstall(i)}
key={i.id}
secondaryAction={i.installed && <Typography variant="caption">{$t('Added')}</Typography>}
disabled={i.installed}
/>
))}
</ExpandableContainer>
))}
{Boolean(filteredItems.other.length) && (
<ExpandableContainer
index={categoryKeys.length}
header="Other"
expanded={search ? [categoryKeys.length] : expanded}
setExpanded={setExpanded}
noDivider
>
{filteredItems.other.map(i => (
<CatalogItem
item={i}
onOpen={() => toggleInstall(i)}
key={i.id}
secondaryAction={i.installed && <Typography variant="caption">{$t('Added')}</Typography>}
disabled={i.installed}
/>
))}
</ExpandableContainer>
)}
</div>
</div>
</Fade>
<Fade in={!opened} className="flex-fill flex-column absolute w-100 h-100">
<div>
<div className="centeredFlex">
<div className="heading">
{itemsListTitle}
</div>
<div className="flex-fill"/>
<div className="centeredFlex primaryColor">
<Typography variant="button">
{$t('add_new')}
</Typography>
<AddButton className="p-1" onClick={open}/>
</div>
</div>
<div className="flex-fill">
<AutoSizer>
{({ width, height }) => (
<DynamicSizeList
height={height}
width={width}
itemCount={filteredItems.installed.length}
estimatedItemSize={54}
itemData={{
items: filteredItems.installed,
onOpen,
onRemove: toggleInstall
}}
>
{RowRenderer as any}
</DynamicSizeList>
)}
</AutoSizer>
</div>
</div>
</Fade>
</div>
</div>
);
});
export default CatalogWithSearch;