1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { Dispatch } from "redux";
7 import { connect } from "react-redux";
8 import { RootState } from "store/store";
9 import { Button, IconButton, StyleRulesCallback, WithStyles, withStyles, SnackbarContent } from '@material-ui/core';
10 import MaterialSnackbar, { SnackbarOrigin } from "@material-ui/core/Snackbar";
11 import { snackbarActions, SnackbarKind, SnackbarMessage } from "store/snackbar/snackbar-actions";
12 import { navigateTo } from 'store/navigation/navigation-action';
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 { ArvadosTheme } from "common/custom-theme";
19 import { amber, green } from "@material-ui/core/colors";
20 import classNames from 'classnames';
22 interface SnackbarDataProps {
23 anchorOrigin?: SnackbarOrigin;
24 autoHideDuration?: number;
26 messages: SnackbarMessage[];
29 interface SnackbarEventProps {
30 onClose?: (event: React.SyntheticEvent<any>, reason: string, message?: string) => void;
32 onClick: (uuid: string) => void;
35 const mapStateToProps = (state: RootState): SnackbarDataProps => {
36 const messages = state.snackbar.messages;
38 anchorOrigin: { vertical: "bottom", horizontal: "right" },
39 open: state.snackbar.open,
41 autoHideDuration: messages.length > 0 ? messages[0].hideDuration : 0
45 const mapDispatchToProps = (dispatch: Dispatch): SnackbarEventProps => ({
46 onClose: (event: any, reason: string, id: undefined) => {
47 if (reason !== "clickaway") {
48 dispatch(snackbarActions.CLOSE_SNACKBAR(id));
52 dispatch(snackbarActions.SHIFT_MESSAGES());
54 onClick: (uuid: string) => {
55 dispatch<any>(navigateTo(uuid));
59 type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message" | "linkButton" | "snackbarContent";
61 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
63 backgroundColor: green[600]
66 backgroundColor: theme.palette.error.dark
69 backgroundColor: theme.palette.primary.main
72 backgroundColor: amber[700]
79 marginRight: theme.spacing.unit
93 type SnackbarProps = SnackbarDataProps & SnackbarEventProps & WithStyles<CssRules>;
95 export const Snackbar = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
96 (props: SnackbarProps) => {
97 const { classes } = props;
100 [SnackbarKind.INFO]: [InfoIcon, classes.info],
101 [SnackbarKind.WARNING]: [WarningIcon, classes.warning],
102 [SnackbarKind.SUCCESS]: [CheckCircleIcon, classes.success],
103 [SnackbarKind.ERROR]: [ErrorIcon, classes.error]
109 onClose={props.onClose}
110 onExited={props.onExited}
111 anchorOrigin={props.anchorOrigin}
112 autoHideDuration={props.autoHideDuration}>
113 <div data-cy="snackbar">
115 props.messages.map((message, index) => {
116 const [Icon, cssClass] = variants[message.kind];
118 return <SnackbarContent
119 key={`${index}-${message.message}`}
120 className={classNames(cssClass, classes.snackbarContent)}
121 aria-describedby="client-snackbar"
123 <span id="client-snackbar" className={classes.message}>
124 <Icon className={classNames(classes.icon, classes.iconVariant)} />
128 action={actions(message, props.onClick, props.onClose, classes, index)}
138 const actions = (props: SnackbarMessage, onClick, onClose, classes, index) => {
144 onClick={e => onClose && onClose(e, '', index)}>
145 <CloseIcon className={classes.icon} />
154 className={classes.linkButton}
155 onClick={() => onClick(props.link)}>
156 <span data-cy='snackbar-goto-action'>Go To</span>