-
Notifications
You must be signed in to change notification settings - Fork 891
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1cbeeb
create new static error summary component
AbhineetJain 935d4bf
fix import path
AbhineetJain 3f8c631
Merge branch 'main' into abhineetjain/error-summary-component
AbhineetJain 793c689
add aria-expanded
AbhineetJain 8334a6c
add detail background, redesign retry button
AbhineetJain 66fd954
Merge branch 'main' into abhineetjain/error-summary-component
AbhineetJain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } from "react" | ||
import { FC, useState } from "react" | ||
|
||
const Language = { | ||
retryMessage: "Retry", | ||
unknownErrorMessage: "An unknown error has occurred", | ||
moreDetails: "More", | ||
lessDetails: "Less", | ||
} | ||
|
||
export interface ErrorSummaryProps { | ||
error: Error | unknown | ||
error: ApiError | Error | unknown | ||
retry?: () => void | ||
dismissible?: boolean | ||
defaultMessage?: string | ||
} | ||
|
||
export const ErrorSummary: FC<ErrorSummaryProps> = ({ error, retry }) => ( | ||
<Stack> | ||
{!(error instanceof Error) ? ( | ||
<div>{Language.unknownErrorMessage}</div> | ||
) : ( | ||
<div>{error.toString()}</div> | ||
)} | ||
|
||
{retry && ( | ||
<div> | ||
<Button onClick={retry} startIcon={<RefreshIcon />} variant="outlined"> | ||
{Language.retryMessage} | ||
</Button> | ||
</div> | ||
)} | ||
</Stack> | ||
) | ||
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`, | ||
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`, | ||
}, | ||
})) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
style
prop so that the parent can pass through its own styles.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.