|
1 | 1 | import Button from "@material-ui/core/Button" |
| 2 | +import Collapse from "@material-ui/core/Collapse" |
| 3 | +import IconButton from "@material-ui/core/IconButton" |
| 4 | +import Link from "@material-ui/core/Link" |
| 5 | +import { darken, makeStyles, Theme } from "@material-ui/core/styles" |
| 6 | +import CloseIcon from "@material-ui/icons/Close" |
2 | 7 | import RefreshIcon from "@material-ui/icons/Refresh" |
| 8 | +import { ApiError, getErrorDetail, getErrorMessage } from "api/errors" |
3 | 9 | import { Stack } from "components/Stack/Stack" |
4 | | -import { FC } from "react" |
| 10 | +import { FC, useState } from "react" |
5 | 11 |
|
6 | 12 | const Language = { |
7 | 13 | retryMessage: "Retry", |
8 | 14 | unknownErrorMessage: "An unknown error has occurred", |
| 15 | + moreDetails: "More", |
| 16 | + lessDetails: "Less", |
9 | 17 | } |
10 | 18 |
|
11 | 19 | export interface ErrorSummaryProps { |
12 | | - error: Error | unknown |
| 20 | + error: ApiError | Error | unknown |
13 | 21 | retry?: () => void |
| 22 | + dismissible?: boolean |
| 23 | + defaultMessage?: string |
14 | 24 | } |
15 | 25 |
|
16 | | -export const ErrorSummary: FC<ErrorSummaryProps> = ({ error, retry }) => ( |
17 | | - <Stack> |
18 | | - {!(error instanceof Error) ? ( |
19 | | - <div>{Language.unknownErrorMessage}</div> |
20 | | - ) : ( |
21 | | - <div>{error.toString()}</div> |
22 | | - )} |
23 | | - |
24 | | - {retry && ( |
25 | | - <div> |
26 | | - <Button onClick={retry} startIcon={<RefreshIcon />} variant="outlined"> |
27 | | - {Language.retryMessage} |
28 | | - </Button> |
29 | | - </div> |
30 | | - )} |
31 | | - </Stack> |
32 | | -) |
| 26 | +export const ErrorSummary: FC<ErrorSummaryProps> = ({ |
| 27 | + error, |
| 28 | + retry, |
| 29 | + dismissible, |
| 30 | + defaultMessage, |
| 31 | +}) => { |
| 32 | + const message = getErrorMessage(error, defaultMessage || Language.unknownErrorMessage) |
| 33 | + const detail = getErrorDetail(error) |
| 34 | + const [showDetails, setShowDetails] = useState(false) |
| 35 | + const [isOpen, setOpen] = useState(true) |
| 36 | + |
| 37 | + const styles = useStyles({ showDetails }) |
| 38 | + |
| 39 | + const toggleShowDetails = () => { |
| 40 | + setShowDetails(!showDetails) |
| 41 | + } |
| 42 | + |
| 43 | + const closeError = () => { |
| 44 | + setOpen(false) |
| 45 | + } |
| 46 | + |
| 47 | + if (!isOpen) { |
| 48 | + return null |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <Stack className={styles.root}> |
| 53 | + <Stack direction="row" alignItems="center" className={styles.messageBox}> |
| 54 | + <div> |
| 55 | + <span className={styles.errorMessage}>{message}</span> |
| 56 | + {!!detail && ( |
| 57 | + <Link |
| 58 | + aria-expanded={showDetails} |
| 59 | + onClick={toggleShowDetails} |
| 60 | + className={styles.detailsLink} |
| 61 | + tabIndex={0} |
| 62 | + > |
| 63 | + {showDetails ? Language.lessDetails : Language.moreDetails} |
| 64 | + </Link> |
| 65 | + )} |
| 66 | + </div> |
| 67 | + {dismissible && ( |
| 68 | + <IconButton onClick={closeError} className={styles.iconButton}> |
| 69 | + <CloseIcon className={styles.closeIcon} /> |
| 70 | + </IconButton> |
| 71 | + )} |
| 72 | + </Stack> |
| 73 | + <Collapse in={showDetails}> |
| 74 | + <div className={styles.details}>{detail}</div> |
| 75 | + </Collapse> |
| 76 | + {retry && ( |
| 77 | + <div className={styles.retry}> |
| 78 | + <Button size="small" onClick={retry} startIcon={<RefreshIcon />} variant="outlined"> |
| 79 | + {Language.retryMessage} |
| 80 | + </Button> |
| 81 | + </div> |
| 82 | + )} |
| 83 | + </Stack> |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +interface StyleProps { |
| 88 | + showDetails?: boolean |
| 89 | +} |
| 90 | + |
| 91 | +const useStyles = makeStyles<Theme, StyleProps>((theme) => ({ |
| 92 | + root: { |
| 93 | + background: darken(theme.palette.error.main, 0.6), |
| 94 | + margin: `${theme.spacing(2)}px`, |
| 95 | + padding: `${theme.spacing(2)}px`, |
| 96 | + borderRadius: theme.shape.borderRadius, |
| 97 | + gap: 0, |
| 98 | + }, |
| 99 | + messageBox: { |
| 100 | + justifyContent: "space-between", |
| 101 | + }, |
| 102 | + errorMessage: { |
| 103 | + marginRight: `${theme.spacing(1)}px`, |
| 104 | + }, |
| 105 | + detailsLink: { |
| 106 | + cursor: "pointer", |
| 107 | + }, |
| 108 | + details: { |
| 109 | + marginTop: `${theme.spacing(2)}px`, |
| 110 | + padding: `${theme.spacing(2)}px`, |
| 111 | + background: darken(theme.palette.error.main, 0.7), |
| 112 | + borderRadius: theme.shape.borderRadius, |
| 113 | + }, |
| 114 | + iconButton: { |
| 115 | + padding: 0, |
| 116 | + }, |
| 117 | + closeIcon: { |
| 118 | + width: 25, |
| 119 | + height: 25, |
| 120 | + color: theme.palette.primary.contrastText, |
| 121 | + }, |
| 122 | + retry: { |
| 123 | + marginTop: `${theme.spacing(2)}px`, |
| 124 | + }, |
| 125 | +})) |
0 commit comments