- Notifications
You must be signed in to change notification settings - Fork928
feat: New static error summary component#3107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a1cbeeb
935d4bf
3f8c631
793c689
8334a6c
66fd954
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,125 @@ | ||
import Button from "@material-ui/core/Button" | ||
import Collapse from "@material-ui/core/Collapse" | ||
import IconButton from "@material-ui/core/IconButton" | ||
import Link from "@material-ui/core/Link" | ||
import { darken, makeStyles, Theme } from "@material-ui/core/styles" | ||
import CloseIcon from "@material-ui/icons/Close" | ||
import RefreshIcon from "@material-ui/icons/Refresh" | ||
import { ApiError, getErrorDetail, getErrorMessage } from "api/errors" | ||
import { Stack } from "components/Stack/Stack" | ||
import { FC, useState } from "react" | ||
const Language = { | ||
retryMessage: "Retry", | ||
unknownErrorMessage: "An unknown error has occurred", | ||
moreDetails: "More", | ||
lessDetails: "Less", | ||
} | ||
export interface ErrorSummaryProps { | ||
error:ApiError |Error | unknown | ||
retry?: () => void | ||
dismissible?: boolean | ||
defaultMessage?: string | ||
} | ||
export const ErrorSummary: FC<ErrorSummaryProps> = ({ | ||
error, | ||
retry, | ||
dismissible, | ||
defaultMessage, | ||
}) => { | ||
const message = getErrorMessage(error, defaultMessage || Language.unknownErrorMessage) | ||
const detail = getErrorDetail(error) | ||
const [showDetails, setShowDetails] = useState(false) | ||
const [isOpen, setOpen] = useState(true) | ||
const styles = useStyles({ showDetails }) | ||
const toggleShowDetails = () => { | ||
setShowDetails(!showDetails) | ||
} | ||
const closeError = () => { | ||
setOpen(false) | ||
} | ||
if (!isOpen) { | ||
return null | ||
} | ||
return ( | ||
<Stack className={styles.root}> | ||
<Stack direction="row" alignItems="center" className={styles.messageBox}> | ||
<div> | ||
<span className={styles.errorMessage}>{message}</span> | ||
{!!detail && ( | ||
<Link | ||
aria-expanded={showDetails} | ||
onClick={toggleShowDetails} | ||
className={styles.detailsLink} | ||
tabIndex={0} | ||
> | ||
{showDetails ? Language.lessDetails : Language.moreDetails} | ||
</Link> | ||
)} | ||
</div> | ||
{dismissible && ( | ||
<IconButton onClick={closeError} className={styles.iconButton}> | ||
<CloseIcon className={styles.closeIcon} /> | ||
</IconButton> | ||
)} | ||
</Stack> | ||
<Collapse in={showDetails}> | ||
<div className={styles.details}>{detail}</div> | ||
</Collapse> | ||
{retry && ( | ||
<div className={styles.retry}> | ||
<Button size="small" onClick={retry} startIcon={<RefreshIcon />} variant="outlined"> | ||
{Language.retryMessage} | ||
</Button> | ||
</div> | ||
)} | ||
</Stack> | ||
) | ||
} | ||
interface StyleProps { | ||
showDetails?: boolean | ||
} | ||
const useStyles = makeStyles<Theme, StyleProps>((theme) => ({ | ||
root: { | ||
background: darken(theme.palette.error.main, 0.6), | ||
margin: `${theme.spacing(2)}px`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Just a suggestion: this component might be a little more flexible if we leave off the margin and instead let the parent decide what the margin should be (it will probably vary). If you feel like it, you could even pass in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think we're going to use this component a lot and I'd like it to look decent without additional styling. Maybe we can give it margins we think are typical and then override when necessary. | ||
padding: `${theme.spacing(2)}px`, | ||
borderRadius: theme.shape.borderRadius, | ||
gap: 0, | ||
}, | ||
messageBox: { | ||
justifyContent: "space-between", | ||
}, | ||
errorMessage: { | ||
marginRight: `${theme.spacing(1)}px`, | ||
}, | ||
detailsLink: { | ||
cursor: "pointer", | ||
}, | ||
details: { | ||
marginTop: `${theme.spacing(2)}px`, | ||
padding: `${theme.spacing(2)}px`, | ||
background: darken(theme.palette.error.main, 0.7), | ||
borderRadius: theme.shape.borderRadius, | ||
}, | ||
iconButton: { | ||
padding: 0, | ||
}, | ||
closeIcon: { | ||
width: 25, | ||
height: 25, | ||
color: theme.palette.primary.contrastText, | ||
}, | ||
retry: { | ||
marginTop: `${theme.spacing(2)}px`, | ||
}, | ||
})) |