Skip to content

Commit 2a155d9

Browse files
authoredMar 14, 2025··
fix: version operations error toast (#8920)
1 parent 24ee87c commit 2a155d9

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed
 

‎packages/sanity/src/core/releases/components/dialog/DiscardVersionDialog.tsx

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Box, Stack, Text} from '@sanity/ui'
1+
import {Box, Stack, Text, useToast} from '@sanity/ui'
22
import {useCallback, useState} from 'react'
33

44
import {Dialog} from '../../../../ui-components'
@@ -23,10 +23,12 @@ export function DiscardVersionDialog(props: {
2323
}): React.JSX.Element {
2424
const {onClose, documentId, documentType} = props
2525
const {t} = useTranslation(releasesLocaleNamespace)
26+
const {t: coreT} = useTranslation()
2627
const {discardChanges} = useDocumentOperation(getPublishedId(documentId), documentType)
2728
const {selectedPerspective} = usePerspective()
2829
const {discardVersion} = useVersionOperations()
2930
const schema = useSchema()
31+
const toast = useToast()
3032
const [isDiscarding, setIsDiscarding] = useState(false)
3133

3234
const schemaType = schema.get(documentType)
@@ -35,11 +37,20 @@ export function DiscardVersionDialog(props: {
3537
setIsDiscarding(true)
3638

3739
if (isVersionId(documentId)) {
38-
await discardVersion(
39-
getVersionFromId(documentId) ||
40-
getReleaseIdFromReleaseDocumentId((selectedPerspective as ReleaseDocument)._id),
41-
documentId,
42-
)
40+
try {
41+
await discardVersion(
42+
getVersionFromId(documentId) ||
43+
getReleaseIdFromReleaseDocumentId((selectedPerspective as ReleaseDocument)._id),
44+
documentId,
45+
)
46+
} catch (err) {
47+
toast.push({
48+
closable: true,
49+
status: 'error',
50+
title: coreT('release.action.discard-version.failure'),
51+
description: err.message,
52+
})
53+
}
4354
} else {
4455
// on the document header you can also discard the draft
4556
discardChanges.execute()
@@ -48,7 +59,7 @@ export function DiscardVersionDialog(props: {
4859
setIsDiscarding(false)
4960

5061
onClose()
51-
}, [documentId, onClose, discardVersion, selectedPerspective, discardChanges])
62+
}, [documentId, onClose, discardVersion, selectedPerspective, toast, coreT, discardChanges])
5263

5364
return (
5465
<Dialog

‎packages/sanity/src/core/releases/components/dialog/UnpublishVersionDialog.tsx

+22-12
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,29 @@ export function UnpublishVersionDialog(props: {
4848
const handleUnpublish = useCallback(async () => {
4949
setIsUnpublishing(true)
5050

51-
await unpublishVersion(documentVersionId)
51+
try {
52+
await unpublishVersion(documentVersionId)
53+
toast.push({
54+
closable: true,
55+
status: 'success',
56+
description: (
57+
<Translate
58+
t={coreT}
59+
i18nKey={'release.action.unpublish-version.success'}
60+
values={{title: preview?.value?.title || documentVersionId}}
61+
/>
62+
),
63+
})
64+
} catch (err) {
65+
toast.push({
66+
closable: true,
67+
status: 'error',
68+
title: coreT('release.action.unpublish-version.failure'),
69+
description: err.message,
70+
})
71+
}
72+
5273
setIsUnpublishing(false)
53-
toast.push({
54-
closable: true,
55-
status: 'success',
56-
description: (
57-
<Translate
58-
t={coreT}
59-
i18nKey={'release.action.unpublish-version.success'}
60-
values={{title: preview?.value?.title || documentVersionId}}
61-
/>
62-
),
63-
})
6474

6575
onClose()
6676
}, [coreT, documentVersionId, onClose, preview?.value?.title, toast, unpublishVersion])

‎packages/sanity/src/core/releases/components/documentHeader/VersionChip.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Text,
88
useClickOutsideEvent,
99
useGlobalKeyDown,
10+
useToast,
1011
} from '@sanity/ui'
1112
// eslint-disable-next-line camelcase
1213
import {
@@ -22,6 +23,7 @@ import {
2223
import {css, styled} from 'styled-components'
2324

2425
import {Popover, Tooltip} from '../../../../ui-components'
26+
import {useTranslation} from '../../../i18n/hooks/useTranslation'
2527
import {getVersionId} from '../../../util/draftUtils'
2628
import {useReleasesUpsell} from '../../contexts/upsell/useReleasesUpsell'
2729
import {useVersionOperations} from '../../hooks/useVersionOperations'
@@ -121,6 +123,8 @@ export const VersionChip = memo(function VersionChip(props: {
121123
const docId = isVersion ? getVersionId(documentId, fromRelease) : documentId // operations recognises publish and draft as empty
122124

123125
const {createVersion} = useVersionOperations()
126+
const toast = useToast()
127+
const {t} = useTranslation()
124128

125129
const close = useCallback(() => setContextMenuPoint(undefined), [])
126130

@@ -161,10 +165,20 @@ export const VersionChip = memo(function VersionChip(props: {
161165

162166
const handleAddVersion = useCallback(
163167
async (targetRelease: string) => {
164-
await createVersion(getReleaseIdFromReleaseDocumentId(targetRelease), docId)
168+
try {
169+
await createVersion(getReleaseIdFromReleaseDocumentId(targetRelease), docId)
170+
} catch (err) {
171+
toast.push({
172+
closable: true,
173+
status: 'error',
174+
title: t('release.action.create-version.failure'),
175+
description: err.message,
176+
})
177+
}
178+
165179
close()
166180
},
167-
[createVersion, docId, close],
181+
[close, createVersion, docId, t, toast],
168182
)
169183

170184
const referenceElement = useMemo(() => {

‎packages/sanity/src/core/releases/hooks/useVersionOperations.tsx

+9-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {useTelemetry} from '@sanity/telemetry/react'
2-
import {useToast} from '@sanity/ui'
32

4-
import {useTranslation} from '../../i18n'
53
import {type ReleaseId} from '../../perspective/types'
64
import {useSetPerspective} from '../../perspective/useSetPerspective'
75
import {getDocumentVariantType} from '../../util/getDocumentVariantType'
@@ -25,56 +23,24 @@ export function useVersionOperations(): VersionOperationsValue {
2523

2624
const setPerspective = useSetPerspective()
2725

28-
const toast = useToast()
29-
const {t} = useTranslation()
30-
3126
const handleCreateVersion = async (
3227
releaseId: ReleaseId,
3328
documentId: string,
3429
initialValue?: Record<string, unknown>,
3530
) => {
3631
const origin = getDocumentVariantType(documentId)
37-
try {
38-
await createVersion(releaseId, documentId, initialValue)
39-
setPerspective(releaseId)
40-
telemetry.log(AddedVersion, {
41-
documentOrigin: origin,
42-
})
43-
} catch (err) {
44-
toast.push({
45-
closable: true,
46-
status: 'error',
47-
title: t('release.action.create-version.failure'),
48-
description: err.message,
49-
})
50-
}
32+
await createVersion(releaseId, documentId, initialValue)
33+
setPerspective(releaseId)
34+
telemetry.log(AddedVersion, {
35+
documentOrigin: origin,
36+
})
5137
}
5238

53-
const handleDiscardVersion = async (releaseId: string, documentId: string) => {
54-
try {
55-
await discardVersion(releaseId, documentId)
56-
} catch (err) {
57-
toast.push({
58-
closable: true,
59-
status: 'error',
60-
title: t('release.action.discard-version.failure'),
61-
description: err.message,
62-
})
63-
}
64-
}
39+
const handleDiscardVersion = async (releaseId: string, documentId: string) =>
40+
discardVersion(releaseId, documentId)
41+
42+
const handleUnpublishVersion = async (documentId: string) => unpublishVersion(documentId)
6543

66-
const handleUnpublishVersion = async (documentId: string) => {
67-
try {
68-
await unpublishVersion(documentId)
69-
} catch (err) {
70-
toast.push({
71-
closable: true,
72-
status: 'error',
73-
title: t('release.action.unpublish-version.failure'),
74-
description: err.message,
75-
})
76-
}
77-
}
7844
return {
7945
createVersion: handleCreateVersion,
8046
discardVersion: handleDiscardVersion,

0 commit comments

Comments
 (0)
Please sign in to comment.