|
| 1 | +// Copyright © 2021 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +import React, { useEffect, useRef, useState } from 'react'; |
| 18 | +import { |
| 19 | + IconButton, |
| 20 | + makeStyles, |
| 21 | + Slide, |
| 22 | + Snackbar, |
| 23 | + Theme, |
| 24 | +} from '@material-ui/core'; |
| 25 | +import { TransitionProps } from '@material-ui/core/transitions'; |
| 26 | +import AlertCircleIcon from 'mdi-react/AlertCircleIcon'; |
| 27 | +import CloseIcon from 'mdi-react/CloseIcon'; |
| 28 | +import CheckIcon from 'mdi-react/CheckIcon'; |
| 29 | + |
| 30 | +const TRANSITION_TIMEOUT = 400; |
| 31 | +export type SnackbarMessageType = 'error' | 'success'; |
| 32 | + |
| 33 | +interface MessageSnackbarProps { |
| 34 | + message: string; |
| 35 | + setMessage: React.Dispatch<React.SetStateAction<string>>; |
| 36 | + messageType?: SnackbarMessageType; |
| 37 | +} |
| 38 | + |
| 39 | +function SlideTransition(props: TransitionProps) { |
| 40 | + return <Slide {...props} direction="up" timeout={TRANSITION_TIMEOUT} />; |
| 41 | +} |
| 42 | + |
| 43 | +export const MessageSnackbar: React.FC<MessageSnackbarProps> = ({ |
| 44 | + message, |
| 45 | + setMessage, |
| 46 | + messageType = 'error', |
| 47 | +}) => { |
| 48 | + const classes = useStyles(); |
| 49 | + const [open, setOpen] = useState(message ? true : false); |
| 50 | + const timeoutRef = useRef<number>(0); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + return () => window.clearTimeout(timeoutRef.current); |
| 54 | + }, []); |
| 55 | + |
| 56 | + const handleClose = () => { |
| 57 | + window.clearTimeout(timeoutRef.current); |
| 58 | + setOpen(false); |
| 59 | + timeoutRef.current = window.setTimeout( |
| 60 | + () => setMessage(''), |
| 61 | + TRANSITION_TIMEOUT |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + setOpen(message ? true : false); |
| 67 | + }, [message]); |
| 68 | + |
| 69 | + return ( |
| 70 | + <Snackbar |
| 71 | + open={open} |
| 72 | + onClose={handleClose} |
| 73 | + anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }} |
| 74 | + autoHideDuration={messageType === 'error' ? 60000 : 6000} |
| 75 | + ContentProps={{ |
| 76 | + className: messageType === 'error' ? classes.error : classes.success, |
| 77 | + }} |
| 78 | + TransitionComponent={SlideTransition} |
| 79 | + message={ |
| 80 | + <div className={classes.message}> |
| 81 | + {messageType === 'error' && ( |
| 82 | + <AlertCircleIcon className={classes.icon} /> |
| 83 | + )} |
| 84 | + {messageType === 'success' && <CheckIcon className={classes.icon} />} |
| 85 | + {message} |
| 86 | + </div> |
| 87 | + } |
| 88 | + action={[ |
| 89 | + <IconButton |
| 90 | + key="close" |
| 91 | + aria-label="close" |
| 92 | + color="inherit" |
| 93 | + onClick={handleClose} |
| 94 | + > |
| 95 | + <CloseIcon /> |
| 96 | + </IconButton>, |
| 97 | + ]} |
| 98 | + /> |
| 99 | + ); |
| 100 | +}; |
| 101 | + |
| 102 | +const useStyles = makeStyles<Theme>((theme) => ({ |
| 103 | + error: { |
| 104 | + backgroundColor: theme.palette.error.main, |
| 105 | + color: theme.palette.text.primary, |
| 106 | + }, |
| 107 | + success: { |
| 108 | + backgroundColor: theme.palette.success.main, |
| 109 | + color: theme.palette.text.primary, |
| 110 | + }, |
| 111 | + message: { |
| 112 | + display: 'flex', |
| 113 | + alignItems: 'center', |
| 114 | + }, |
| 115 | + icon: { |
| 116 | + marginRight: theme.spacing(1), |
| 117 | + }, |
| 118 | +})); |
0 commit comments