1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { connect } from "react-redux";
7 import { RootState } from "~/store/store";
8 import MaterialSnackbar, { SnackbarOrigin } from "@material-ui/core/Snackbar";
9 import { Dispatch } from "redux";
10 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
11 import IconButton from '@material-ui/core/IconButton';
12 import SnackbarContent from '@material-ui/core/SnackbarContent';
13 import WarningIcon from '@material-ui/icons/Warning';
14 import CheckCircleIcon from '@material-ui/icons/CheckCircle';
15 import ErrorIcon from '@material-ui/icons/Error';
16 import InfoIcon from '@material-ui/icons/Info';
17 import CloseIcon from '@material-ui/icons/Close';
18 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
19 import { ArvadosTheme } from "~/common/custom-theme";
20 import { amber, green } from "@material-ui/core/colors";
21 import * as classNames from 'classnames';
23 interface SnackbarDataProps {
24 anchorOrigin?: SnackbarOrigin;
25 autoHideDuration?: number;
27 message?: React.ReactElement<any>;
31 interface SnackbarEventProps {
32 onClose?: (event: React.SyntheticEvent<any>, reason: string) => void;
36 const mapStateToProps = (state: RootState): SnackbarDataProps => {
37 const messages = state.snackbar.messages;
39 anchorOrigin: { vertical: "bottom", horizontal: "right" },
40 open: state.snackbar.open,
41 message: <span>{messages.length > 0 ? messages[0].message : ""}</span>,
42 autoHideDuration: messages.length > 0 ? messages[0].hideDuration : 0,
43 kind: messages.length > 0 ? messages[0].kind : SnackbarKind.INFO
47 const mapDispatchToProps = (dispatch: Dispatch): SnackbarEventProps => ({
48 onClose: (event: any, reason: string) => {
49 if (reason !== "clickaway") {
50 dispatch(snackbarActions.CLOSE_SNACKBAR());
54 dispatch(snackbarActions.SHIFT_MESSAGES());
58 type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message";
60 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
62 backgroundColor: green[600]
65 backgroundColor: theme.palette.error.dark
68 backgroundColor: theme.palette.primary.dark
71 backgroundColor: amber[700]
78 marginRight: theme.spacing.unit
86 export const Snackbar = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
87 (props: SnackbarDataProps & SnackbarEventProps & WithStyles<CssRules>) => {
88 const { classes } = props;
91 [SnackbarKind.INFO]: [InfoIcon, classes.info],
92 [SnackbarKind.WARNING]: [WarningIcon, classes.warning],
93 [SnackbarKind.SUCCESS]: [CheckCircleIcon, classes.success],
94 [SnackbarKind.ERROR]: [ErrorIcon, classes.error]
97 const [Icon, cssClass] = variants[props.kind];
102 message={props.message}
103 onClose={props.onClose}
104 onExited={props.onExited}
105 anchorOrigin={props.anchorOrigin}
106 autoHideDuration={props.autoHideDuration}>
108 className={classNames(cssClass)}
109 aria-describedby="client-snackbar"
111 <span id="client-snackbar" className={classes.message}>
112 <Icon className={classNames(classes.icon, classes.iconVariant)}/>
121 onClick={e => props.onClose && props.onClose(e, '')}>
122 <CloseIcon className={classes.icon}/>