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, { SnackbarProps } 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 const mapStateToProps = (state: RootState): SnackbarProps & ArvadosSnackbarProps => {
24 const messages = state.snackbar.messages;
26 anchorOrigin: { vertical: "bottom", horizontal: "right" },
27 open: state.snackbar.open,
28 message: <span>{messages.length > 0 ? messages[0].message : ""}</span>,
29 autoHideDuration: messages.length > 0 ? messages[0].hideDuration : 0,
30 kind: messages.length > 0 ? messages[0].kind : SnackbarKind.INFO
34 const mapDispatchToProps = (dispatch: Dispatch) => ({
35 onClose: (event: any, reason: string) => {
36 if (reason !== "clickaway") {
37 dispatch(snackbarActions.CLOSE_SNACKBAR());
41 dispatch(snackbarActions.SHIFT_MESSAGES());
45 const ArvadosSnackbar = (props: any) => <MaterialSnackbar {...props}>
46 <ArvadosSnackbarContent {...props}/>
49 type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message";
51 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
53 backgroundColor: green[600]
56 backgroundColor: theme.palette.error.dark
59 backgroundColor: theme.palette.primary.dark
62 backgroundColor: amber[700]
69 marginRight: theme.spacing.unit
77 interface ArvadosSnackbarProps {
81 const ArvadosSnackbarContent = (props: SnackbarProps & ArvadosSnackbarProps & WithStyles<CssRules>) => {
82 const { classes, className, message, onClose, kind } = props;
85 let cssClass = classes.info;
88 case SnackbarKind.INFO:
90 cssClass = classes.info;
92 case SnackbarKind.WARNING:
94 cssClass = classes.warning;
96 case SnackbarKind.SUCCESS:
97 Icon = CheckCircleIcon;
98 cssClass = classes.success;
100 case SnackbarKind.ERROR:
102 cssClass = classes.error;
108 className={classNames(cssClass, className)}
109 aria-describedby="client-snackbar"
111 <span id="client-snackbar" className={classes.message}>
112 <Icon className={classNames(classes.icon, classes.iconVariant)}/>
126 <CloseIcon className={classes.icon}/>
133 export const Snackbar = connect(mapStateToProps, mapDispatchToProps)(
134 withStyles(styles)(ArvadosSnackbar)