|
1 | | -import { Alert, Button, Collapse, Group, Stack, Text } from "@mantine/core"; |
2 | | -import { useState } from "react"; |
| 1 | +import { |
| 2 | + Alert, |
| 3 | + Button, |
| 4 | + Collapse, |
| 5 | + Group, |
| 6 | + Stack, |
| 7 | + Text, |
| 8 | + Transition, |
| 9 | +} from "@mantine/core"; |
| 10 | +import { useEffect, useState } from "react"; |
3 | 11 |
|
4 | 12 | export interface InlineErrorProps { |
5 | 13 | error: { message: string; data?: unknown }; |
6 | 14 | retryCount?: number; |
7 | 15 | maxRetries?: number; |
8 | 16 | docLink?: string; |
| 17 | + /** |
| 18 | + * When set, the alert slides up + fades out after this many ms. |
| 19 | + * Resets whenever `error.message` changes so a fresh error always |
| 20 | + * starts a new countdown. |
| 21 | + */ |
| 22 | + autoDismissMs?: number; |
9 | 23 | } |
10 | 24 |
|
11 | 25 | const HeaderRow = Group.withProps({ |
@@ -58,39 +72,70 @@ export function InlineError({ |
58 | 72 | retryCount, |
59 | 73 | maxRetries, |
60 | 74 | docLink, |
| 75 | + autoDismissMs, |
61 | 76 | }: InlineErrorProps) { |
62 | 77 | const [expanded, setExpanded] = useState(false); |
| 78 | + // Derive visibility from "which message has been dismissed". A new |
| 79 | + // error message makes `dismissedMessage` stale and the alert renders; |
| 80 | + // when the timer fires we mark this message dismissed and the |
| 81 | + // Transition runs its exit animation. Doing it this way (vs. a |
| 82 | + // mounted boolean + setMounted(true) on message change) avoids the |
| 83 | + // forbidden setState-in-effect pattern. |
| 84 | + const [dismissedMessage, setDismissedMessage] = useState<string | undefined>( |
| 85 | + undefined, |
| 86 | + ); |
| 87 | + const mounted = error.message !== dismissedMessage; |
63 | 88 | const details = |
64 | 89 | error.data !== undefined ? formatDetails(error.data) : undefined; |
65 | 90 | const hasExpandable = details !== undefined || docLink !== undefined; |
66 | 91 |
|
| 92 | + useEffect(() => { |
| 93 | + if (!autoDismissMs) return; |
| 94 | + const timer = setTimeout( |
| 95 | + () => setDismissedMessage(error.message), |
| 96 | + autoDismissMs, |
| 97 | + ); |
| 98 | + return () => clearTimeout(timer); |
| 99 | + }, [autoDismissMs, error.message]); |
| 100 | + |
67 | 101 | return ( |
68 | | - <Alert color="red" variant="light"> |
69 | | - <Stack gap="xs"> |
70 | | - <HeaderRow> |
71 | | - <MessageGroup> |
72 | | - <ErrorMessage>{error.message}</ErrorMessage> |
73 | | - {retryCount !== undefined && ( |
74 | | - <RetryText>{formatRetryLabel(retryCount, maxRetries)}</RetryText> |
75 | | - )} |
76 | | - </MessageGroup> |
77 | | - {hasExpandable && ( |
78 | | - <ExpandButton onClick={() => setExpanded((v) => !v)}> |
79 | | - {expanded ? "Show less" : "Show more"} |
80 | | - </ExpandButton> |
81 | | - )} |
82 | | - </HeaderRow> |
83 | | - <Collapse in={expanded}> |
| 102 | + <Transition |
| 103 | + mounted={mounted} |
| 104 | + transition="slide-up" |
| 105 | + duration={350} |
| 106 | + timingFunction="ease" |
| 107 | + > |
| 108 | + {(transitionStyle) => ( |
| 109 | + <Alert color="red" variant="light" style={transitionStyle}> |
84 | 110 | <Stack gap="xs"> |
85 | | - {details && <Text size="sm">{details}</Text>} |
86 | | - {docLink && ( |
87 | | - <DocLinkButton href={docLink}> |
88 | | - View Troubleshooting Guide |
89 | | - </DocLinkButton> |
90 | | - )} |
| 111 | + <HeaderRow> |
| 112 | + <MessageGroup> |
| 113 | + <ErrorMessage>{error.message}</ErrorMessage> |
| 114 | + {retryCount !== undefined && ( |
| 115 | + <RetryText> |
| 116 | + {formatRetryLabel(retryCount, maxRetries)} |
| 117 | + </RetryText> |
| 118 | + )} |
| 119 | + </MessageGroup> |
| 120 | + {hasExpandable && ( |
| 121 | + <ExpandButton onClick={() => setExpanded((v) => !v)}> |
| 122 | + {expanded ? "Show less" : "Show more"} |
| 123 | + </ExpandButton> |
| 124 | + )} |
| 125 | + </HeaderRow> |
| 126 | + <Collapse in={expanded}> |
| 127 | + <Stack gap="xs"> |
| 128 | + {details && <Text size="sm">{details}</Text>} |
| 129 | + {docLink && ( |
| 130 | + <DocLinkButton href={docLink}> |
| 131 | + View Troubleshooting Guide |
| 132 | + </DocLinkButton> |
| 133 | + )} |
| 134 | + </Stack> |
| 135 | + </Collapse> |
91 | 136 | </Stack> |
92 | | - </Collapse> |
93 | | - </Stack> |
94 | | - </Alert> |
| 137 | + </Alert> |
| 138 | + )} |
| 139 | + </Transition> |
95 | 140 | ); |
96 | 141 | } |
0 commit comments