1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as 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 } from "~/store/snackbar/snackbar-actions";
12 import { navigateToProject } 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 * as classNames from 'classnames';
22 interface SnackbarDataProps {
23 anchorOrigin?: SnackbarOrigin;
24 autoHideDuration?: number;
26 message?: React.ReactElement<any>;
31 interface SnackbarEventProps {
32 onClose?: (event: React.SyntheticEvent<any>, reason: string) => void;
34 onClick: (uuid: string) => void;
37 const mapStateToProps = (state: RootState): SnackbarDataProps => {
38 const messages = state.snackbar.messages;
40 anchorOrigin: { vertical: "bottom", horizontal: "right" },
41 open: state.snackbar.open,
42 message: <span>{messages.length > 0 ? messages[0].message : ""}</span>,
43 autoHideDuration: messages.length > 0 ? messages[0].hideDuration : 0,
44 kind: messages.length > 0 ? messages[0].kind : SnackbarKind.INFO,
45 link: messages.length > 0 ? messages[0].link : ''
49 const mapDispatchToProps = (dispatch: Dispatch): SnackbarEventProps => ({
50 onClose: (event: any, reason: string) => {
51 if (reason !== "clickaway") {
52 dispatch(snackbarActions.CLOSE_SNACKBAR());
56 dispatch(snackbarActions.SHIFT_MESSAGES());
58 onClick: (uuid: string) => {
59 dispatch(navigateToProject(uuid));
63 type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message" | "linkButton";
65 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
67 backgroundColor: green[600]
70 backgroundColor: theme.palette.error.dark
73 backgroundColor: theme.palette.primary.main
76 backgroundColor: amber[700]
83 marginRight: theme.spacing.unit
94 type SnackbarProps = SnackbarDataProps & SnackbarEventProps & WithStyles<CssRules>;
96 export const Snackbar = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
97 (props: SnackbarProps) => {
98 const { classes } = props;
101 [SnackbarKind.INFO]: [InfoIcon, classes.info],
102 [SnackbarKind.WARNING]: [WarningIcon, classes.warning],
103 [SnackbarKind.SUCCESS]: [CheckCircleIcon, classes.success],
104 [SnackbarKind.ERROR]: [ErrorIcon, classes.error]
107 const [Icon, cssClass] = variants[props.kind];
114 message={props.message}
115 onClose={props.onClose}
116 onExited={props.onExited}
117 anchorOrigin={props.anchorOrigin}
118 autoHideDuration={props.autoHideDuration}>
120 className={classNames(cssClass)}
121 aria-describedby="client-snackbar"
123 <span id="client-snackbar" className={classes.message}>
124 <Icon className={classNames(classes.icon, classes.iconVariant)}/>
128 action={actions(props)}
135 const actions = (props: SnackbarProps) => {
136 const { link, onClose, onClick, classes } = props;
142 onClick={e => onClose && onClose(e, '')}>
143 <CloseIcon className={classes.icon} />
152 className={classes.linkButton}
153 onClick={() => onClick(link) }>