asynqmon/ui/src/App.tsx

305 lines
9.7 KiB
TypeScript
Raw Normal View History

2021-01-23 14:26:04 +08:00
import React from "react";
2020-12-10 23:06:54 +08:00
import { connect, ConnectedProps } from "react-redux";
2020-11-24 22:54:00 +08:00
import clsx from "clsx";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
2021-01-13 08:41:14 +08:00
import { makeStyles, Theme, ThemeProvider } from "@material-ui/core/styles";
2020-11-24 22:54:00 +08:00
import AppBar from "@material-ui/core/AppBar";
import Drawer from "@material-ui/core/Drawer";
import Toolbar from "@material-ui/core/Toolbar";
import List from "@material-ui/core/List";
2020-12-27 10:45:46 +08:00
import ListItem from "@material-ui/core/ListItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemText from "@material-ui/core/ListItemText";
2020-11-24 22:54:00 +08:00
import Typography from "@material-ui/core/Typography";
2020-12-10 23:06:54 +08:00
import Snackbar from "@material-ui/core/Snackbar";
import SnackbarContent from "@material-ui/core/SnackbarContent";
2020-11-24 22:54:00 +08:00
import IconButton from "@material-ui/core/IconButton";
2020-12-10 23:06:54 +08:00
import Slide from "@material-ui/core/Slide";
import { TransitionProps } from "@material-ui/core/transitions";
2020-11-24 22:54:00 +08:00
import MenuIcon from "@material-ui/icons/Menu";
import BarChartIcon from "@material-ui/icons/BarChart";
import LayersIcon from "@material-ui/icons/Layers";
import SettingsIcon from "@material-ui/icons/Settings";
2020-12-29 22:40:11 +08:00
import ScheduleIcon from "@material-ui/icons/Schedule";
2020-12-27 10:45:46 +08:00
import FeedbackIcon from "@material-ui/icons/Feedback";
2021-01-03 23:32:14 +08:00
import DoubleArrowIcon from "@material-ui/icons/DoubleArrow";
2020-12-10 23:06:54 +08:00
import CloseIcon from "@material-ui/icons/Close";
import { AppState } from "./store";
2020-11-24 22:54:00 +08:00
import { paths } from "./paths";
import { useTheme } from "./theme";
2020-12-10 23:06:54 +08:00
import { closeSnackbar } from "./actions/snackbarActions";
2021-01-23 14:26:04 +08:00
import { toggleDrawer } from "./actions/settingsActions";
2020-11-24 22:54:00 +08:00
import ListItemLink from "./components/ListItemLink";
import SchedulersView from "./views/SchedulersView";
2020-11-24 22:54:00 +08:00
import DashboardView from "./views/DashboardView";
import TasksView from "./views/TasksView";
import SettingsView from "./views/SettingsView";
2020-12-29 22:40:11 +08:00
import ServersView from "./views/ServersView";
2021-01-03 23:32:14 +08:00
import RedisInfoView from "./views/RedisInfoView";
2021-01-07 22:41:33 +08:00
import PageNotFoundView from "./views/PageNotFoundView";
2020-11-24 22:54:00 +08:00
const drawerWidth = 220;
2021-01-13 08:41:14 +08:00
// FIXME: For some reason, the following code does not work:
// makeStyles(theme => ({ /* use theme here */}));
// Using closure to work around this problem.
const useStyles = (theme: Theme) =>
makeStyles({
root: {
display: "flex",
},
toolbar: {
paddingRight: 24, // keep right padding when drawer closed
},
toolbarIcon: {
display: "flex",
alignItems: "center",
justifyContent: "flex-end",
padding: "0 8px",
...theme.mixins.toolbar,
},
appBar: {
backgroundColor: theme.palette.background.paper,
zIndex: theme.zIndex.drawer + 1,
},
menuButton: {
marginRight: theme.spacing(2),
2021-01-16 04:09:55 +08:00
color:
theme.palette.type === "dark"
? theme.palette.grey[100]
: theme.palette.grey[700],
2021-01-13 08:41:14 +08:00
},
menuButtonHidden: {
display: "none",
},
title: {
flexGrow: 1,
},
drawerPaper: {
position: "relative",
whiteSpace: "nowrap",
width: drawerWidth,
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
border: "none",
},
drawerPaperClose: {
overflowX: "hidden",
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
width: theme.spacing(7),
[theme.breakpoints.up("sm")]: {
width: theme.spacing(9),
},
},
snackbar: {
background: theme.palette.grey["A400"],
color: "#ffffff",
},
snackbarCloseIcon: {
color: theme.palette.grey[400],
},
appBarSpacer: theme.mixins.toolbar,
mainContainer: {
display: "flex",
width: "100vw",
},
content: {
flex: 1,
height: "100vh",
overflow: "hidden",
background: theme.palette.background.paper,
},
contentWrapper: {
height: "100%",
display: "flex",
paddingTop: "64px", // app-bar height
overflow: "scroll",
},
sidebarContainer: {
display: "flex",
justifyContent: "space-between",
height: "100%",
flexDirection: "column",
},
listItem: {
borderTopRightRadius: "24px",
borderBottomRightRadius: "24px",
},
});
2020-11-24 22:54:00 +08:00
2020-12-10 23:06:54 +08:00
function mapStateToProps(state: AppState) {
2021-01-13 07:55:56 +08:00
return {
snackbar: state.snackbar,
themePreference: state.settings.themePreference,
2021-01-23 14:26:04 +08:00
isDrawerOpen: state.settings.isDrawerOpen,
2021-01-13 07:55:56 +08:00
};
2020-12-10 23:06:54 +08:00
}
const mapDispatchToProps = {
closeSnackbar,
2021-01-23 14:26:04 +08:00
toggleDrawer,
2020-12-10 23:06:54 +08:00
};
const connector = connect(mapStateToProps, mapDispatchToProps);
function SlideUpTransition(props: TransitionProps) {
return <Slide {...props} direction="up" />;
}
function App(props: ConnectedProps<typeof connector>) {
const theme = useTheme(props.themePreference);
2021-01-13 08:41:14 +08:00
const classes = useStyles(theme)();
2020-11-24 22:54:00 +08:00
return (
2021-01-13 07:55:56 +08:00
<ThemeProvider theme={theme}>
<Router>
<div className={classes.root}>
<AppBar
position="absolute"
className={classes.appBar}
variant="outlined"
2020-11-24 22:54:00 +08:00
>
2021-01-13 07:55:56 +08:00
<Toolbar className={classes.toolbar}>
<IconButton
edge="start"
color="inherit"
aria-label="open drawer"
2021-01-23 14:26:04 +08:00
onClick={props.toggleDrawer}
2021-01-13 07:55:56 +08:00
className={classes.menuButton}
>
<MenuIcon />
</IconButton>
<Typography
component="h1"
variant="h6"
noWrap
className={classes.title}
2021-01-13 08:41:14 +08:00
color="textPrimary"
2021-01-13 07:55:56 +08:00
>
Asynq Monitoring
</Typography>
</Toolbar>
</AppBar>
<div className={classes.mainContainer}>
<Drawer
variant="permanent"
classes={{
paper: clsx(
classes.drawerPaper,
2021-01-23 14:26:04 +08:00
!props.isDrawerOpen && classes.drawerPaperClose
2021-01-13 07:55:56 +08:00
),
}}
2021-01-23 14:26:04 +08:00
open={props.isDrawerOpen}
2020-12-10 23:06:54 +08:00
>
2021-01-13 07:55:56 +08:00
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
open={props.snackbar.isOpen}
autoHideDuration={6000}
onClose={props.closeSnackbar}
TransitionComponent={SlideUpTransition}
>
<SnackbarContent
message={props.snackbar.message}
className={classes.snackbar}
action={
<IconButton
size="small"
aria-label="close"
color="inherit"
onClick={props.closeSnackbar}
>
<CloseIcon
className={classes.snackbarCloseIcon}
fontSize="small"
/>
</IconButton>
}
/>
</Snackbar>
<div className={classes.appBarSpacer} />
<div className={classes.sidebarContainer}>
<List>
<div>
<ListItemLink
to={paths.HOME}
primary="Queues"
icon={<BarChartIcon />}
2020-12-10 23:06:54 +08:00
/>
2021-01-13 07:55:56 +08:00
<ListItemLink
to={paths.SERVERS}
primary="Servers"
icon={<DoubleArrowIcon />}
/>
<ListItemLink
to={paths.SCHEDULERS}
primary="Schedulers"
icon={<ScheduleIcon />}
/>
<ListItemLink
to={paths.REDIS}
primary="Redis"
icon={<LayersIcon />}
/>
</div>
</List>
<List>
2021-01-03 23:32:14 +08:00
<ListItemLink
2021-01-13 07:55:56 +08:00
to={paths.SETTINGS}
primary="Settings"
icon={<SettingsIcon />}
2021-01-03 23:32:14 +08:00
/>
2021-01-13 07:55:56 +08:00
<ListItem
button
component="a"
className={classes.listItem}
href="https://github.com/hibiken/asynqmon/issues"
target="_blank"
>
<ListItemIcon>
<FeedbackIcon />
</ListItemIcon>
<ListItemText primary="Send Feedback" />
</ListItem>
</List>
</div>
</Drawer>
<main className={classes.content}>
<div className={classes.contentWrapper}>
<Switch>
<Route exact path={paths.QUEUE_DETAILS}>
<TasksView />
</Route>
<Route exact path={paths.SCHEDULERS}>
<SchedulersView />
</Route>
<Route exact path={paths.SERVERS}>
<ServersView />
</Route>
<Route exact path={paths.REDIS}>
<RedisInfoView />
</Route>
<Route exact path={paths.SETTINGS}>
<SettingsView />
</Route>
<Route exact path={paths.HOME}>
<DashboardView />
</Route>
<Route path="*">
<PageNotFoundView />
</Route>
</Switch>
</div>
</main>
</div>
2020-11-24 22:54:00 +08:00
</div>
2021-01-13 07:55:56 +08:00
</Router>
</ThemeProvider>
2020-11-24 22:54:00 +08:00
);
}
2020-12-10 23:06:54 +08:00
export default connector(App);