-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathHeader.tsx
317 lines (294 loc) · 10 KB
/
Header.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { JobSerializers } from "@dmm-com/airone-apiclient-typescript-fetch";
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
import PersonIcon from "@mui/icons-material/Person";
import TaskIcon from "@mui/icons-material/Task";
import {
AppBar,
Badge,
Box,
Button,
Divider,
IconButton,
Menu,
MenuItem,
Toolbar,
Typography,
TypographyTypeMap,
} from "@mui/material";
import { OverridableComponent } from "@mui/material/OverridableComponent";
import { styled } from "@mui/material/styles";
import PopupState, { bindHover, bindMenu } from "material-ui-popup-state";
import HoverMenu from "material-ui-popup-state/HoverMenu";
import React, { FC, MouseEvent, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import { useInterval } from "react-use";
import { useTranslation } from "../hooks/useTranslation";
import {
jobsPath,
userPath,
usersPath,
groupsPath,
entitiesPath,
advancedSearchPath,
loginPath,
rolesPath,
topPath,
} from "Routes";
import { SearchBox } from "components/common/SearchBox";
import { useSimpleSearch } from "hooks/useSimpleSearch";
import { aironeApiClient } from "repository/AironeApiClient";
import {
JobOperations,
JobRefreshIntervalMilliSec,
JobStatuses,
} from "services/Constants";
import {
getLatestCheckDate,
jobTargetLabel,
updateLatestCheckDate,
} from "services/JobUtil";
import { ServerContext } from "services/ServerContext";
const Frame = styled(Box)(({}) => ({
width: "100%",
height: "56px",
}));
const Fixed = styled(Box)(({ theme }) => ({
position: "fixed",
zIndex: 2,
width: "100%",
backgroundColor: theme.palette.primary.main,
display: "flex",
justifyContent: "center",
}));
const StyledAppBar = styled(AppBar)(({ theme }) => ({
maxWidth: theme.breakpoints.values.lg,
}));
const StyledToolbar = styled(Toolbar)(({}) => ({
height: "56px",
}));
const TitleBox = styled(Box)(({}) => ({
display: "flex",
alignItems: "center",
}));
const Title = styled(Typography)(({}) => ({
color: "white",
textDecoration: "none",
})) as OverridableComponent<TypographyTypeMap>;
const Version = styled(Typography)(({}) => ({
color: "#FFFFFF8A",
paddingLeft: "20px",
maxWidth: "64px",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
}));
const MenuBox = styled(Box)(({}) => ({
flexGrow: 1,
display: "flex",
color: "white",
marginLeft: "16px",
"& a": {
color: "inherit",
margin: "0px 4px",
},
"& button": {
color: "inherit",
margin: "0px 4px",
},
}));
const SearchBoxWrapper = styled(Box)(({}) => ({
display: "flex",
alignItems: "center",
width: "240px",
}));
export const Header: FC = () => {
const serverContext = ServerContext.getInstance();
const { t } = useTranslation();
const [query, submitQuery] = useSimpleSearch();
const [userAnchorEl, setUserAnchorEl] = useState<HTMLButtonElement | null>();
const [jobAnchorEl, setJobAnchorEl] = useState<HTMLButtonElement | null>();
const [latestCheckDate, setLatestCheckDate] = useState<Date | null>(
getLatestCheckDate()
);
const [recentJobs, setRecentJobs] = useState<Array<JobSerializers>>([]);
useInterval(async () => {
try {
setRecentJobs(await aironeApiClient.getRecentJobs());
} catch (e) {
console.warn("failed to get recent jobs. will auto retried ...");
}
}, JobRefreshIntervalMilliSec);
const uncheckedJobsCount = useMemo(() => {
return latestCheckDate != null
? recentJobs.filter((job) => job.createdAt > latestCheckDate).length ?? 0
: recentJobs.length;
}, [latestCheckDate, recentJobs]);
const handleLogout = async () => {
await aironeApiClient.postLogout();
window.location.href = `${loginPath()}?next=${window.location.pathname}`;
};
const handleOpenMenu = async (e: MouseEvent<HTMLButtonElement>) => {
setJobAnchorEl(e.currentTarget);
try {
setRecentJobs(await aironeApiClient.getRecentJobs());
} catch (e) {
console.warn("failed to get recent jobs. will auto retried ...");
}
updateLatestCheckDate(new Date());
setLatestCheckDate(getLatestCheckDate());
};
return (
<Frame>
<Fixed>
<StyledAppBar position="static" elevation={0}>
<StyledToolbar variant="dense">
<TitleBox>
<Title fontSize="24px" component={Link} to={topPath()}>
AirOne
</Title>
<Version fontSize="12px" title={serverContext?.version}>
{serverContext?.version}
</Version>
</TitleBox>
<MenuBox>
<Button component={Link} to={entitiesPath()}>
{t("entities")}
</Button>
<Button component={Link} to={advancedSearchPath()}>
{t("advancedSearch")}
</Button>
<PopupState variant="popover" popupId="basic">
{(popupState) => (
<React.Fragment>
<Button {...bindHover(popupState)}>
{t("management")}
<KeyboardArrowDownIcon fontSize="small" />
</Button>
<HoverMenu {...bindMenu(popupState)}>
<MenuItem component={Link} to={usersPath()}>
{t("manageUsers")}
</MenuItem>
<MenuItem component={Link} to={groupsPath()}>
{t("manageGroups")}
</MenuItem>
<MenuItem component={Link} to={rolesPath()}>
{t("manageRoles")}
</MenuItem>
</HoverMenu>
</React.Fragment>
)}
</PopupState>
{/* If there is another menu settings are passed from Server,
this represent another menu*/}
{serverContext?.extendedHeaderMenus.map((menu, index) => (
<PopupState variant="popover" popupId={menu.name} key={index}>
{(popupState) => (
<React.Fragment>
<Button {...bindHover(popupState)}>
{menu.name}
<KeyboardArrowDownIcon fontSize="small" />
</Button>
<HoverMenu {...bindMenu(popupState)}>
{menu.children.map((child, index) => (
<MenuItem key={index} component="a" href={child.url}>
{child.name}
</MenuItem>
))}
</HoverMenu>
</React.Fragment>
)}
</PopupState>
))}
</MenuBox>
<MenuBox justifyContent="flex-end">
{serverContext?.legacyUiDisabled === false && (
<Button href="/dashboard/">{t("previousVersion")}</Button>
)}
<IconButton
aria-controls="user-menu"
aria-haspopup="true"
onClick={(e) => setUserAnchorEl(e.currentTarget)}
>
<PersonIcon />
</IconButton>
<Menu
id="user-menu"
anchorEl={userAnchorEl}
open={Boolean(userAnchorEl)}
onClose={() => setUserAnchorEl(null)}
keepMounted
>
<MenuItem>
{serverContext?.user?.username ?? "不明なユーザ"}{" "}
{t("currentUser")}
</MenuItem>
<Divider light />
<MenuItem
component={Link}
to={userPath(serverContext?.user?.id ?? 0)}
>
{t("userSetting")}
</MenuItem>
<MenuItem onClick={() => handleLogout()}>
{t("logout")}
</MenuItem>
</Menu>
<IconButton
aria-controls="job-menu"
aria-haspopup="true"
onClick={handleOpenMenu}
>
<Badge badgeContent={uncheckedJobsCount} color="secondary">
<TaskIcon />
</Badge>
</IconButton>
<Menu
anchorEl={jobAnchorEl}
open={Boolean(jobAnchorEl)}
onClose={() => setJobAnchorEl(null)}
keepMounted
>
{recentJobs.length > 0 ? (
recentJobs.map((job) => (
<MenuItem key={job.id}>
{(job.operation == JobOperations.EXPORT_ENTRY ||
job.operation == JobOperations.EXPORT_SEARCH_RESULT ||
job.operation == JobOperations.EXPORT_ENTRY_V2 ||
job.operation ==
JobOperations.EXPORT_SEARCH_RESULT_V2) &&
job.status == JobStatuses.DONE ? (
<a href={`/job/api/v2/${job.id}/download?encode=utf-8`}>
{jobTargetLabel(job)}
</a>
) : (
<Typography>{jobTargetLabel(job)}</Typography>
)}
</MenuItem>
))
) : (
<MenuItem>
<Typography>{t("noRunningJobs")}</Typography>
</MenuItem>
)}
<Divider light />
<MenuItem component={Link} to={jobsPath()}>
{t("jobs")}
</MenuItem>
</Menu>
<SearchBoxWrapper>
<SearchBox
placeholder="Search"
defaultValue={query}
onKeyPress={(e) => {
e.key === "Enter" && submitQuery(e.target.value);
}}
inputSx={{ height: "42px", "& input": { py: "9px" } }}
/>
</SearchBoxWrapper>
</MenuBox>
</StyledToolbar>
</StyledAppBar>
</Fixed>
</Frame>
);
};