@@ -7,17 +7,26 @@ import {
77} from 'react' ;
88
99interface NetworkErrorContextValue {
10- showError : ( message ?: string ) => void ;
10+ /**
11+ * Show the banner. Pass `retry` when the failed work can be re-issued; the
12+ * button then reads "Retry" and runs it. Without one it reads "Dismiss".
13+ */
14+ showError : ( message ?: string , retry ?: ( ) => void ) => void ;
1115 clearError : ( ) => void ;
1216}
1317
18+ interface NetworkErrorState {
19+ message : string ;
20+ retry ?: ( ) => void ;
21+ }
22+
1423const NetworkErrorContext = createContext < NetworkErrorContextValue | null > ( null ) ;
1524
1625export function NetworkErrorProvider ( { children } : { children : ReactNode } ) {
17- const [ error , setError ] = useState < string | null > ( null ) ;
26+ const [ error , setError ] = useState < NetworkErrorState | null > ( null ) ;
1827
19- const showError = useCallback ( ( message ?: string ) => {
20- setError ( message ?? 'Something went wrong. We are looking at it.' ) ;
28+ const showError = useCallback ( ( message ?: string , retry ?: ( ) => void ) => {
29+ setError ( { message : message ?? 'Something went wrong. We are looking at it.' , retry } ) ;
2130 } , [ ] ) ;
2231
2332 const clearError = useCallback ( ( ) => {
@@ -32,13 +41,15 @@ export function NetworkErrorProvider({ children }: { children: ReactNode }) {
3241 className = "bg-destructive text-destructive-foreground px-4 py-2 text-sm flex items-center justify-between"
3342 data-testid = "network-error-banner"
3443 >
35- < span > { error } </ span >
44+ < span > { error . message } </ span >
3645 < button
37- onClick = { clearError }
46+ onClick = { ( ) => {
47+ error . retry ?.( ) ;
48+ clearError ( ) ;
49+ } }
3850 className = "ml-4 underline hover:no-underline"
39- aria-label = "Dismiss error"
4051 >
41- Retry
52+ { error . retry ? ' Retry' : 'Dismiss' }
4253 </ button >
4354 </ div >
4455 ) }
0 commit comments