-
Notifications
You must be signed in to change notification settings - Fork 937
refactor(site): Refactor alerts #7587
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
Changes from all commits
26d7edd
550959e
4099477
4831c6d
d56172c
8fe9719
98b97e3
709b1ed
94ba2ea
21aa9a2
669527d
112aa12
00b5e4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Alert } from "./Alert" | ||
import Button from "@mui/material/Button" | ||
import Link from "@mui/material/Link" | ||
import type { Meta, StoryObj } from "@storybook/react" | ||
|
||
const meta: Meta<typeof Alert> = { | ||
title: "components/Alert", | ||
component: Alert, | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof Alert> | ||
|
||
const ExampleAction = ( | ||
<Button onClick={() => null} size="small" variant="text"> | ||
Button | ||
</Button> | ||
) | ||
|
||
export const Warning: Story = { | ||
args: { | ||
children: "This is a warning", | ||
severity: "warning", | ||
}, | ||
} | ||
|
||
export const WarningWithDismiss: Story = { | ||
args: { | ||
children: "This is a warning", | ||
dismissible: true, | ||
severity: "warning", | ||
}, | ||
} | ||
|
||
export const WarningWithAction: Story = { | ||
args: { | ||
children: "This is a warning", | ||
actions: [ExampleAction], | ||
severity: "warning", | ||
}, | ||
} | ||
|
||
export const WarningWithActionAndDismiss: Story = { | ||
args: { | ||
children: "This is a warning", | ||
actions: [ExampleAction], | ||
dismissible: true, | ||
severity: "warning", | ||
}, | ||
} | ||
|
||
export const WithChildren: Story = { | ||
args: { | ||
severity: "warning", | ||
children: ( | ||
<div> | ||
This is a message with a <Link href="#">link</Link> | ||
</div> | ||
), | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useState, FC, ReactNode, PropsWithChildren } from "react" | ||
import Collapse from "@mui/material/Collapse" | ||
// eslint-disable-next-line no-restricted-imports -- It is the base component | ||
import MuiAlert, { AlertProps as MuiAlertProps } from "@mui/material/Alert" | ||
import Button from "@mui/material/Button" | ||
|
||
export interface AlertProps extends PropsWithChildren { | ||
severity: MuiAlertProps["severity"] | ||
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. If 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.
|
||
actions?: ReactNode[] | ||
dismissible?: boolean | ||
onRetry?: () => void | ||
onDismiss?: () => void | ||
} | ||
|
||
export const Alert: FC<AlertProps> = ({ | ||
children, | ||
actions = [], | ||
onRetry, | ||
dismissible, | ||
severity, | ||
onDismiss, | ||
}) => { | ||
const [open, setOpen] = useState(true) | ||
|
||
return ( | ||
<Collapse in={open}> | ||
<MuiAlert | ||
severity={severity} | ||
action={ | ||
<> | ||
{/* CTAs passed in by the consumer */} | ||
{actions.length > 0 && | ||
actions.map((action) => <div key={String(action)}>{action}</div>)} | ||
|
||
{/* retry CTA */} | ||
{onRetry && ( | ||
<Button variant="text" size="small" onClick={onRetry}> | ||
Retry | ||
</Button> | ||
)} | ||
|
||
{/* close CTA */} | ||
{dismissible && ( | ||
<Button | ||
variant="text" | ||
size="small" | ||
onClick={() => { | ||
setOpen(false) | ||
onDismiss && onDismiss() | ||
}} | ||
data-testid="dismiss-banner-btn" | ||
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. Should we stick an 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. Since it has a text, I think we don't need it 🤔 but I'm not sure if I understood your point 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. No, you're right! I had to google it but I think since we have the text we can forget the label. |
||
> | ||
Dismiss | ||
</Button> | ||
)} | ||
</> | ||
} | ||
> | ||
{children} | ||
</MuiAlert> | ||
</Collapse> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import Button from "@mui/material/Button" | ||
import { mockApiError } from "testHelpers/entities" | ||
import type { Meta, StoryObj } from "@storybook/react" | ||
import { action } from "@storybook/addon-actions" | ||
import { ErrorAlert } from "./ErrorAlert" | ||
|
||
const mockError = mockApiError({ | ||
message: "Email or password was invalid", | ||
detail: "Password is invalid", | ||
}) | ||
|
||
const meta: Meta<typeof ErrorAlert> = { | ||
title: "components/ErrorAlert", | ||
component: ErrorAlert, | ||
args: { | ||
error: mockError, | ||
dismissible: false, | ||
onRetry: undefined, | ||
}, | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof ErrorAlert> | ||
|
||
const ExampleAction = ( | ||
<Button onClick={() => null} size="small" variant="text"> | ||
Button | ||
</Button> | ||
) | ||
|
||
export const WithOnlyMessage: Story = { | ||
args: { | ||
error: mockApiError({ | ||
message: "Email or password was invalid", | ||
}), | ||
}, | ||
} | ||
|
||
export const WithDismiss: Story = { | ||
args: { | ||
dismissible: true, | ||
}, | ||
} | ||
|
||
export const WithAction: Story = { | ||
args: { | ||
actions: [ExampleAction], | ||
}, | ||
} | ||
|
||
export const WithActionAndDismiss: Story = { | ||
args: { | ||
actions: [ExampleAction], | ||
dismissible: true, | ||
}, | ||
} | ||
|
||
export const WithRetry: Story = { | ||
args: { | ||
onRetry: action("retry"), | ||
dismissible: true, | ||
}, | ||
} | ||
|
||
export const WithActionRetryAndDismiss: Story = { | ||
args: { | ||
actions: [ExampleAction], | ||
onRetry: action("retry"), | ||
dismissible: true, | ||
}, | ||
} | ||
|
||
export const WithNonApiError: Story = { | ||
args: { | ||
error: new Error("Non API error here"), | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { AlertProps, Alert } from "./Alert" | ||
import AlertTitle from "@mui/material/AlertTitle" | ||
import Box from "@mui/material/Box" | ||
import { getErrorMessage, getErrorDetail } from "api/errors" | ||
import { FC } from "react" | ||
|
||
export const ErrorAlert: FC< | ||
Omit<AlertProps, "severity" | "children"> & { error: unknown } | ||
> = ({ error, ...alertProps }) => { | ||
const message = getErrorMessage(error, "Something went wrong.") | ||
const detail = getErrorDetail(error) | ||
|
||
return ( | ||
<Alert severity="error" {...alertProps}> | ||
{detail ? ( | ||
<> | ||
<AlertTitle>{message}</AlertTitle> | ||
<Box | ||
component="span" | ||
color={(theme) => theme.palette.text.secondary} | ||
fontSize={13} | ||
data-chromatic="ignore" | ||
> | ||
{detail} | ||
</Box> | ||
</> | ||
) : ( | ||
message | ||
)} | ||
</Alert> | ||
) | ||
} |
This file was deleted.
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.
Nice!