|
1 | 1 | /* eslint-disable i18next/no-literal-string */
|
2 | 2 | /* eslint-disable @sanity/i18n/no-attribute-string-literals */
|
3 | 3 | import {
|
| 4 | + Box, |
4 | 5 | Card,
|
5 | 6 | Code,
|
6 | 7 | Container,
|
7 | 8 | ErrorBoundary,
|
8 | 9 | type ErrorBoundaryProps,
|
9 | 10 | Heading,
|
10 | 11 | Stack,
|
| 12 | + Text, |
11 | 13 | } from '@sanity/ui'
|
12 | 14 | import {type ComponentType, type ErrorInfo, type ReactNode, useCallback, useState} from 'react'
|
| 15 | +import {ErrorActions, isDev, isProd} from 'sanity' |
| 16 | +import {styled} from 'styled-components' |
13 | 17 | import {useHotModuleReload} from 'use-hot-module-reload'
|
14 | 18 |
|
15 |
| -import {Button} from '../../ui-components' |
16 | 19 | import {SchemaError} from '../config'
|
17 | 20 | import {errorReporter} from '../error/errorReporter'
|
18 | 21 | import {CorsOriginError} from '../store'
|
@@ -42,15 +45,17 @@ const INITIAL_STATE = {
|
42 | 45 | eventId: null,
|
43 | 46 | } satisfies ErrorBoundaryState
|
44 | 47 |
|
| 48 | +const View = styled(Box)` |
| 49 | + align-items: center; |
| 50 | +` |
| 51 | + |
45 | 52 | export const StudioErrorBoundary: ComponentType<StudioErrorBoundaryProps> = ({
|
46 | 53 | children,
|
47 | 54 | heading = 'An error occurred',
|
48 | 55 | }) => {
|
49 | 56 | const [{error, eventId}, setError] = useState<ErrorBoundaryState>(INITIAL_STATE)
|
50 |
| - |
51 | 57 | const message = isRecord(error) && typeof error.message === 'string' && error.message
|
52 | 58 | const stack = isRecord(error) && typeof error.stack === 'string' && error.stack
|
53 |
| - |
54 | 59 | const handleResetError = useCallback(() => setError(INITIAL_STATE), [])
|
55 | 60 | const handleCatchError: ErrorBoundaryProps['onCatch'] = useCallback((params) => {
|
56 | 61 | const report = errorReporter.reportError(params.error, {
|
@@ -80,37 +85,37 @@ export const StudioErrorBoundary: ComponentType<StudioErrorBoundaryProps> = ({
|
80 | 85 | }
|
81 | 86 |
|
82 | 87 | return (
|
83 |
| - <Card |
84 |
| - height="fill" |
85 |
| - overflow="auto" |
86 |
| - paddingY={[4, 5, 6, 7]} |
87 |
| - paddingX={4} |
88 |
| - sizing="border" |
89 |
| - tone="critical" |
90 |
| - > |
91 |
| - <Container width={3}> |
92 |
| - <Stack space={4}> |
93 |
| - {/* TODO: better error boundary */} |
94 |
| - |
95 |
| - <Heading>{heading}</Heading> |
96 |
| - |
97 |
| - <div> |
98 |
| - <Button onClick={handleResetError} text="Retry" tone="default" /> |
99 |
| - </div> |
100 |
| - |
101 |
| - <Card border radius={2} overflow="auto" padding={4} tone="inherit"> |
| 88 | + <Card height="fill" overflow="auto" paddingY={[4, 5, 6, 7]} paddingX={4} sizing="border"> |
| 89 | + <View display="flex" height="fill"> |
| 90 | + <Container width={3}> |
| 91 | + <Stack space={6}> |
102 | 92 | <Stack space={4}>
|
103 |
| - {message && ( |
104 |
| - <Code size={1}> |
105 |
| - <strong>Error: {message}</strong> |
106 |
| - </Code> |
| 93 | + <Heading>{heading}</Heading> |
| 94 | + <Text>An error occurred that Sanity Studio was unable to recover from.</Text> |
| 95 | + {isProd && ( |
| 96 | + <Text> |
| 97 | + <strong>To report this error,</strong> copy the error details and share them with |
| 98 | + your development team or Sanity Support. |
| 99 | + </Text> |
| 100 | + )} |
| 101 | + {isDev && ( |
| 102 | + <Card border radius={2} overflow="auto" padding={4} tone="critical"> |
| 103 | + <Stack space={4}> |
| 104 | + {message && ( |
| 105 | + <Code size={1}> |
| 106 | + <strong>Error: {message}</strong> |
| 107 | + </Code> |
| 108 | + )} |
| 109 | + {stack && <Code size={1}>{stack}</Code>} |
| 110 | + {eventId && <Code size={1}>Event ID: {eventId}</Code>} |
| 111 | + </Stack> |
| 112 | + </Card> |
107 | 113 | )}
|
108 |
| - {stack && <Code size={1}>{stack}</Code>} |
109 |
| - {eventId && <Code size={1}>Event ID: {eventId}</Code>} |
110 | 114 | </Stack>
|
111 |
| - </Card> |
112 |
| - </Stack> |
113 |
| - </Container> |
| 115 | + <ErrorActions error={error} eventId={eventId} onRetry={handleResetError} size="large" /> |
| 116 | + </Stack> |
| 117 | + </Container> |
| 118 | + </View> |
114 | 119 | </Card>
|
115 | 120 | )
|
116 | 121 | }
|
0 commit comments