-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
navbar-menu.tsx
182 lines (167 loc) · 6.2 KB
/
navbar-menu.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
import AddLinkIcon from '@mui/icons-material/AddLink';
import ClearAllIcon from '@mui/icons-material/ClearAll';
import DeleteSweepIcon from '@mui/icons-material/DeleteSweep';
import InfoIcon from '@mui/icons-material/Info';
import LaunchIcon from '@mui/icons-material/Launch';
import PauseIcon from '@mui/icons-material/Pause';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import RefreshIcon from '@mui/icons-material/Refresh';
import SettingsIcon from '@mui/icons-material/Settings';
import TuneIcon from '@mui/icons-material/Tune';
import { DialogContentText, Divider, IconButton, Menu, Tooltip } from '@mui/material';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
import { ConfirmationDialog } from '@src/components';
import type { NavbarButton } from '@src/models';
import { AppLinks, AppRoute, ErrorType, LoginError, NavbarButtonType } from '@src/models';
import { NotificationService, QueryService } from '@src/services';
import { setNavbar } from '@src/store/actions';
import { getGlobalNavbarButton, getUrl } from '@src/store/selectors';
import { createTab, useI18n } from '@src/utils';
import NavbarMenuIcon from './navbar-menu-icon';
type NavbarMenuProps = { menuIcon: React.ReactNode };
export const NavbarMenu = ({ menuIcon }: NavbarMenuProps) => {
const i18n = useI18n('navbar', 'menu');
const dispatch = useDispatch();
const url = useSelector(getUrl) + AppLinks.DownloadStation;
const navbarButtons = useSelector(getGlobalNavbarButton);
const [anchorEl, setAnchorEl] = React.useState<null | Element>(null);
const open = Boolean(anchorEl);
const handleClick = <T extends Element>(event: React.MouseEvent<T>) => setAnchorEl(event.currentTarget);
const handleClose = () => setAnchorEl(null);
const handleClearTab = () => dispatch(setNavbar());
const handleUrl = () => createTab({ url });
const handleError = (button: string) => ({
error: (error: any) => {
if (error instanceof LoginError || ErrorType.Login === error?.type) {
NotificationService.loginRequired();
} else if (error) {
NotificationService.error({
title: i18n(`task_${button}_fail`, 'common', 'error'),
message: error?.message ?? error?.name ?? '',
});
}
},
});
// Dialog
const [prompt, setPrompt] = React.useState(false);
// Button list
const buttons: NavbarButton[] = [
{
type: NavbarButtonType.Add,
label: i18n('menu_add'),
icon: <AddLinkIcon />,
component: Link,
to: AppRoute.Add,
onClick: handleClearTab,
},
{
type: NavbarButtonType.Refresh,
label: i18n('menu_refresh'),
icon: <RefreshIcon />,
onClick: () => QueryService.listTasks().subscribe(handleError('refresh')),
},
{
type: NavbarButtonType.Resume,
label: i18n('menu_resume'),
icon: <PlayArrowIcon />,
onClick: () => QueryService.resumeAllTasks().subscribe(handleError('resume')),
},
{
type: NavbarButtonType.Pause,
label: i18n('menu_pause'),
icon: <PauseIcon />,
onClick: () => QueryService.pauseAllTasks().subscribe(handleError('pause')),
},
{ type: NavbarButtonType.Remove, label: i18n('menu_remove'), icon: <DeleteSweepIcon />, onClick: () => setPrompt(true) },
{
type: NavbarButtonType.Clear,
label: i18n('menu_clear'),
icon: <ClearAllIcon />,
onClick: () => QueryService.deleteFinishedTasks().subscribe(handleError('clear')),
},
{
type: NavbarButtonType.Configs,
label: i18n(`menu_configs`),
icon: <SettingsIcon />,
component: Link,
to: AppRoute.Config,
onClick: handleClearTab,
},
{
type: NavbarButtonType.Settings,
label: i18n('menu_settings'),
icon: <TuneIcon />,
component: Link,
to: AppRoute.Settings,
onClick: handleClearTab,
},
{ type: NavbarButtonType.Open, label: i18n('menu_open'), icon: <LaunchIcon />, onClick: handleUrl },
{ type: NavbarButtonType.About, label: i18n('menu_about'), icon: <InfoIcon />, component: Link, to: AppRoute.About, onClick: handleClearTab },
];
return (
<React.Fragment>
{buttons
?.filter(({ type }) => navbarButtons?.includes(type))
?.map(({ type, icon, label, ..._props }) => (
<Tooltip title={label} key={type}>
<IconButton id={`${type}-pinned`} aria-controls={`${type}-pinned-button`} {..._props}>
{icon}
</IconButton>
</Tooltip>
))}
<Tooltip title="Actions and Settings">
<IconButton
sx={{ m: '0 0.25rem' }}
id="dropdown-menu-button"
aria-controls="dropdown-menu-button"
aria-haspopup="true"
aria-expanded={open ? 'true' : undefined}
onClick={handleClick}
>
{menuIcon}
</IconButton>
</Tooltip>
<Menu
id="dropdown-menu"
anchorEl={anchorEl}
open={open}
onClose={handleClose}
onClick={handleClose}
MenuListProps={{ 'aria-labelledby': 'dropdown-menu' }}
>
<NavbarMenuIcon {...buttons[0]} />
<Divider />
<NavbarMenuIcon {...buttons[1]} />
<NavbarMenuIcon {...buttons[2]} />
<NavbarMenuIcon {...buttons[3]} />
<NavbarMenuIcon {...buttons[4]} />
<NavbarMenuIcon {...buttons[5]} />
<Divider />
<NavbarMenuIcon {...buttons[6]} />
<NavbarMenuIcon {...buttons[7]} />
<NavbarMenuIcon {...buttons[8]} />
<Divider />
<NavbarMenuIcon {...buttons[9]} />
</Menu>
<ConfirmationDialog
open={prompt}
title={i18n('confirmation_title')}
description={
<React.Fragment>
<DialogContentText id="alert-dialog-description">{i18n('confirmation_line_1')}</DialogContentText>
<br />
<DialogContentText id="alert-dialog-description">{i18n('confirmation_line_2')}</DialogContentText>
</React.Fragment>
}
onCancel={() => setPrompt(false)}
onConfirm={() => {
setPrompt(false);
QueryService.deleteAllTasks().subscribe(handleError('delete'));
}}
/>
</React.Fragment>
);
};
export default NavbarMenu;