Skip to content

Commit

Permalink
feat(sanity): add call to config from Structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Sep 30, 2024
1 parent 92b6071 commit bbc4331
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Box, ErrorBoundary, Text} from '@sanity/ui'
import {type ComponentProps, useCallback, useId, useState} from 'react'
import {useTranslation} from 'sanity'
import {useSource, useTranslation} from 'sanity'

import {Dialog} from '../../../ui-components'
import {structureLocaleNamespace} from '../../i18n'
Expand All @@ -17,6 +17,18 @@ function ConfirmDeleteDialogContainer(props: ConfirmDeleteDialogProps) {
const id = useId()
const [error, setError] = useState<ErrorInfo | null>(null)
const handleRetry = useCallback(() => setError(null), [])
const {onStudioError} = useSource()

const handleCatchError = useCallback(
({error: caughtError, info: caughtInfo}: {error: Error; info: React.ErrorInfo}) => {
setError({error: caughtError, info: caughtInfo})

if (onStudioError) {
onStudioError(caughtError, caughtInfo)
}
},
[onStudioError],
)

return error ? (
<Dialog
Expand All @@ -37,7 +49,7 @@ function ConfirmDeleteDialogContainer(props: ConfirmDeleteDialogProps) {
</Box>
</Dialog>
) : (
<ErrorBoundary onCatch={setError}>
<ErrorBoundary onCatch={handleCatchError}>
<ConfirmDeleteDialog {...props} />
</ErrorBoundary>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const DocumentTitle = (props: {documentId: string; documentType: string}) => {
document.title = newTitle
}, [documentTitle, settled, newTitle])

throw new Error('This is an error from the structure tool')
return null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ErrorBoundary} from '@sanity/ui'
import {useEffect, useState} from 'react'
import {useCallback, useEffect, useState} from 'react'
import {SourceProvider, type Tool, useWorkspace} from 'sanity'

import {setActivePanes} from '../../getIntentState'
Expand All @@ -25,6 +25,7 @@ export function StructureToolBoundary({tool: {options}}: StructureToolBoundaryPr
}, [])

const [{error}, setError] = useState<{error: unknown}>({error: null})

// this re-throws if the error it catches is not a PaneResolutionError
if (error) return <StructureError error={error} />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@sanity/types'
import {Box, Card, type CardTone, ErrorBoundary, Flex, Stack, Text} from '@sanity/ui'
import {createElement, type ErrorInfo, Fragment, useCallback, useMemo, useState} from 'react'
import {type DocumentInspectorProps, useTranslation} from 'sanity'
import {type DocumentInspectorProps, useSource, useTranslation} from 'sanity'

import {DocumentInspectorHeader} from '../../documentInspector'
import {useDocumentPane} from '../../useDocumentPane'
Expand Down Expand Up @@ -44,6 +44,7 @@ export function ValidationInspector(props: DocumentInspectorProps) {
[onFocus, onPathOpen],
)

throw new Error('This is an error from the validation inspector')
return (
<Flex direction="column" height="fill" overflow="hidden">
<DocumentInspectorHeader
Expand Down

0 comments on commit bbc4331

Please sign in to comment.