X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/ea54fb82c3a59ca8a959643f8bec4776635433e0..f9dafeec8d45f0cb19d71326b7fa877891a7eb9e:/src/views-components/snackbar/snackbar.tsx

diff --git a/src/views-components/snackbar/snackbar.tsx b/src/views-components/snackbar/snackbar.tsx
index 7449e1e2..4250dcdf 100644
--- a/src/views-components/snackbar/snackbar.tsx
+++ b/src/views-components/snackbar/snackbar.tsx
@@ -3,35 +3,50 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from "react";
+import { Dispatch } from "redux";
 import { connect } from "react-redux";
 import { RootState } from "~/store/store";
-import MaterialSnackbar, { SnackbarProps } from "@material-ui/core/Snackbar";
-import { Dispatch } from "redux";
+import { Button, IconButton, StyleRulesCallback, WithStyles, withStyles, SnackbarContent } from '@material-ui/core';
+import MaterialSnackbar, { SnackbarOrigin } from "@material-ui/core/Snackbar";
 import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions";
-import IconButton from '@material-ui/core/IconButton';
-import SnackbarContent from '@material-ui/core/SnackbarContent';
+import { navigateToProject } from '~/store/navigation/navigation-action';
 import WarningIcon from '@material-ui/icons/Warning';
 import CheckCircleIcon from '@material-ui/icons/CheckCircle';
 import ErrorIcon from '@material-ui/icons/Error';
 import InfoIcon from '@material-ui/icons/Info';
 import CloseIcon from '@material-ui/icons/Close';
-import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
 import { ArvadosTheme } from "~/common/custom-theme";
 import { amber, green } from "@material-ui/core/colors";
 import * as classNames from 'classnames';
 
-const mapStateToProps = (state: RootState): SnackbarProps & ArvadosSnackbarProps => {
+interface SnackbarDataProps {
+    anchorOrigin?: SnackbarOrigin;
+    autoHideDuration?: number;
+    open: boolean;
+    message?: React.ReactElement<any>;
+    kind: SnackbarKind;
+    link?: string;
+}
+
+interface SnackbarEventProps {
+    onClose?: (event: React.SyntheticEvent<any>, reason: string) => void;
+    onExited: () => void;
+    onClick: (uuid: string) => void;
+}
+
+const mapStateToProps = (state: RootState): SnackbarDataProps => {
     const messages = state.snackbar.messages;
     return {
         anchorOrigin: { vertical: "bottom", horizontal: "right" },
         open: state.snackbar.open,
         message: <span>{messages.length > 0 ? messages[0].message : ""}</span>,
         autoHideDuration: messages.length > 0 ? messages[0].hideDuration : 0,
-        kind: messages.length > 0 ? messages[0].kind : SnackbarKind.INFO
+        kind: messages.length > 0 ? messages[0].kind : SnackbarKind.INFO,
+        link: messages.length > 0 ? messages[0].link : ''
     };
 };
 
-const mapDispatchToProps = (dispatch: Dispatch) => ({
+const mapDispatchToProps = (dispatch: Dispatch): SnackbarEventProps => ({
     onClose: (event: any, reason: string) => {
         if (reason !== "clickaway") {
             dispatch(snackbarActions.CLOSE_SNACKBAR());
@@ -39,14 +54,13 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({
     },
     onExited: () => {
         dispatch(snackbarActions.SHIFT_MESSAGES());
+    },
+    onClick: (uuid: string) => {
+        dispatch(navigateToProject(uuid));
     }
 });
 
-const ArvadosSnackbar = (props: any) => <MaterialSnackbar {...props}>
-    <ArvadosSnackbarContent {...props}/>
-</MaterialSnackbar>;
-
-type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message";
+type CssRules = "success" | "error" | "info" | "warning" | "icon" | "iconVariant" | "message" | "linkButton";
 
 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     success: {
@@ -56,7 +70,7 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
         backgroundColor: theme.palette.error.dark
     },
     info: {
-        backgroundColor: theme.palette.primary.dark
+        backgroundColor: theme.palette.primary.main
     },
     warning: {
         backgroundColor: amber[700]
@@ -72,64 +86,74 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
         display: 'flex',
         alignItems: 'center'
     },
+    linkButton: {
+        fontWeight: 'bolder'
+    }
 });
 
-interface ArvadosSnackbarProps {
-    kind: SnackbarKind;
-}
+type SnackbarProps = SnackbarDataProps & SnackbarEventProps & WithStyles<CssRules>;
+
+export const Snackbar = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
+    (props: SnackbarProps) => {
+        const { classes } = props;
 
-const ArvadosSnackbarContent = (props: SnackbarProps & ArvadosSnackbarProps & WithStyles<CssRules>) => {
-    const { classes, className, message, onClose, kind } = props;
+        const variants = {
+            [SnackbarKind.INFO]: [InfoIcon, classes.info],
+            [SnackbarKind.WARNING]: [WarningIcon, classes.warning],
+            [SnackbarKind.SUCCESS]: [CheckCircleIcon, classes.success],
+            [SnackbarKind.ERROR]: [ErrorIcon, classes.error]
+        };
 
-    let Icon = InfoIcon;
-    let cssClass = classes.info;
+        const [Icon, cssClass] = variants[props.kind];
 
-    switch (kind) {
-        case SnackbarKind.INFO:
-            Icon = InfoIcon;
-            cssClass = classes.info;
-            break;
-        case SnackbarKind.WARNING:
-            Icon = WarningIcon;
-            cssClass = classes.warning;
-            break;
-        case SnackbarKind.SUCCESS:
-            Icon = CheckCircleIcon;
-            cssClass = classes.success;
-            break;
-        case SnackbarKind.ERROR:
-            Icon = ErrorIcon;
-            cssClass = classes.error;
-            break;
+
+
+        return (
+            <MaterialSnackbar
+                open={props.open}
+                message={props.message}
+                onClose={props.onClose}
+                onExited={props.onExited}
+                anchorOrigin={props.anchorOrigin}
+                autoHideDuration={props.autoHideDuration}>
+                <SnackbarContent
+                    className={classNames(cssClass)}
+                    aria-describedby="client-snackbar"
+                    message={
+                        <span id="client-snackbar" className={classes.message}>
+                            <Icon className={classNames(classes.icon, classes.iconVariant)}/>
+                            {props.message}
+                        </span>
+                    }
+                    action={actions(props)}
+                />
+            </MaterialSnackbar>
+        );
     }
+));
 
-    return (
-        <SnackbarContent
-            className={classNames(cssClass, className)}
-            aria-describedby="client-snackbar"
-            message={
-                <span id="client-snackbar" className={classes.message}>
-                    <Icon className={classNames(classes.icon, classes.iconVariant)}/>
-                    {message}
-                </span>
-            }
-            action={
-                <IconButton
-                    key="close"
-                    aria-label="Close"
-                    color="inherit"
-                    onClick={e => {
-                        if (onClose) {
-                            onClose(e, '');
-                        }
-                    }}>
-                    <CloseIcon className={classes.icon}/>
-                </IconButton>
-            }
-        />
-    );
+const actions = (props: SnackbarProps) => {
+    const { link, onClose, onClick, classes } = props;
+    const actions = [
+        <IconButton
+            key="close"
+            aria-label="Close"
+            color="inherit"
+            onClick={e => onClose && onClose(e, '')}>
+            <CloseIcon className={classes.icon} />
+        </IconButton>
+    ];
+    if (link) {
+        actions.splice(0, 0,
+            <Button key="goTo"
+                aria-label="goTo"
+                size="small"
+                color="inherit"
+                className={classes.linkButton}
+                onClick={() => onClick(link) }>
+                Go To
+            </Button>
+        );
+    }
+    return actions;
 };
-
-export const Snackbar = connect(mapStateToProps, mapDispatchToProps)(
-    withStyles(styles)(ArvadosSnackbar)
-);
