Skip to content

Commit 50a65ba

Browse files
authoredMar 4, 2025··
fix(core): add isReleaseLocked or willBeUnpublished to useDocumentForm (#8822)
1 parent 82489b9 commit 50a65ba

File tree

4 files changed

+48
-31
lines changed

4 files changed

+48
-31
lines changed
 

‎packages/sanity/src/core/form/useDocumentForm.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-statements */
12
import {type SanityDocument} from '@sanity/client'
23
import {isActionEnabled} from '@sanity/schema/_internal'
34
import {useTelemetry} from '@sanity/telemetry/react'
@@ -28,8 +29,12 @@ import {useEditState} from '../hooks/useEditState'
2829
import {useSchema} from '../hooks/useSchema'
2930
import {useValidationStatus} from '../hooks/useValidationStatus'
3031
import {useTranslation} from '../i18n/hooks/useTranslation'
32+
import {getSelectedPerspective} from '../perspective/getSelectedPerspective'
3133
import {type ReleaseId} from '../perspective/types'
32-
import {isPublishedPerspective} from '../releases/util/util'
34+
import {isReleaseDocument} from '../releases/store/types'
35+
import {useActiveReleases} from '../releases/store/useActiveReleases'
36+
import {isGoingToUnpublish} from '../releases/util/isGoingToUnpublish'
37+
import {isPublishedPerspective, isReleaseScheduledOrScheduling} from '../releases/util/util'
3338
import {
3439
type DocumentPresence,
3540
type EditStateFor,
@@ -123,6 +128,7 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
123128
} = options
124129
const schema = useSchema()
125130
const presenceStore = usePresenceStore()
131+
const {data: releases} = useActiveReleases()
126132

127133
const schemaType = schema.get(documentType) as ObjectSchemaType | undefined
128134
if (!schemaType) {
@@ -223,12 +229,25 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
223229

224230
const ready = connectionState === 'connected' && editState.ready
225231

232+
const selectedPerspective = useMemo(() => {
233+
return getSelectedPerspective(selectedPerspectiveName, releases)
234+
}, [selectedPerspectiveName, releases])
235+
236+
const isReleaseLocked = useMemo(
237+
() =>
238+
isReleaseDocument(selectedPerspective)
239+
? isReleaseScheduledOrScheduling(selectedPerspective)
240+
: false,
241+
[selectedPerspective],
242+
)
243+
226244
const readOnly = useMemo(() => {
227245
const hasNoPermission = !isPermissionsLoading && !permissions?.granted
228246
const updateActionDisabled = !isActionEnabled(schemaType!, 'update')
229247
const createActionDisabled = isNonExistent && !isActionEnabled(schemaType!, 'create')
230248
const reconnecting = connectionState === 'reconnecting'
231249
const isLocked = editState.transactionSyncLock?.enabled
250+
const willBeUnpublished = value ? isGoingToUnpublish(value) : false
232251

233252
// in cases where the document has drafts but the schema is live edit, there is a risk of data loss, so we disable editing in this case
234253
if (liveEdit && editState.draft?._id) {
@@ -250,7 +269,9 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
250269
createActionDisabled ||
251270
reconnecting ||
252271
isLocked ||
253-
isCreateLinked
272+
isCreateLinked ||
273+
willBeUnpublished ||
274+
isReleaseLocked
254275

255276
if (isReadOnly) return true
256277
if (typeof readOnlyProp === 'function') return readOnlyProp(editState)
@@ -269,6 +290,7 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
269290
ready,
270291
isCreateLinked,
271292
readOnlyProp,
293+
isReleaseLocked,
272294
])
273295

274296
const {patch} = useDocumentOperation(documentId, documentType, releaseId)

‎packages/sanity/src/core/perspective/PerspectiveProvider.tsx

+5-9
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {PerspectiveContext} from 'sanity/_singletons'
33

44
import {getReleasesPerspectiveStack} from '../releases/hooks/utils'
55
import {useActiveReleases} from '../releases/store/useActiveReleases'
6-
import {getReleaseIdFromReleaseDocumentId} from '../releases/util/getReleaseIdFromReleaseDocumentId'
76
import {EMPTY_ARRAY} from '../util/empty'
7+
import {getSelectedPerspective} from './getSelectedPerspective'
88
import {type PerspectiveContextValue, type ReleaseId, type SelectedPerspective} from './types'
99

1010
/**
@@ -21,14 +21,10 @@ export function PerspectiveProvider({
2121
}) {
2222
const {data: releases} = useActiveReleases()
2323

24-
const selectedPerspective: SelectedPerspective = useMemo(() => {
25-
if (!selectedPerspectiveName) return 'drafts'
26-
if (selectedPerspectiveName === 'published') return 'published'
27-
const selectedRelease = releases.find(
28-
(release) => getReleaseIdFromReleaseDocumentId(release._id) === selectedPerspectiveName,
29-
)
30-
return selectedRelease || 'drafts'
31-
}, [selectedPerspectiveName, releases])
24+
const selectedPerspective: SelectedPerspective = useMemo(
25+
() => getSelectedPerspective(selectedPerspectiveName, releases),
26+
[selectedPerspectiveName, releases],
27+
)
3228

3329
const perspectiveStack = useMemo(
3430
() =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {type ReleaseDocument} from '../releases/store/types'
2+
import {getReleaseIdFromReleaseDocumentId} from '../releases/util/getReleaseIdFromReleaseDocumentId'
3+
import {type ReleaseId, type SelectedPerspective} from './types'
4+
5+
export function getSelectedPerspective(
6+
selectedPerspectiveName: 'published' | ReleaseId | undefined,
7+
releases: ReleaseDocument[],
8+
): SelectedPerspective {
9+
if (!selectedPerspectiveName) return 'drafts'
10+
if (selectedPerspectiveName === 'published') return 'published'
11+
const selectedRelease = releases.find(
12+
(release) => getReleaseIdFromReleaseDocumentId(release._id) === selectedPerspectiveName,
13+
)
14+
return selectedRelease || 'drafts'
15+
}

‎packages/sanity/src/structure/panes/document/DocumentPaneProvider.tsx

+4-20
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import {
1111
type EditStateFor,
1212
EMPTY_ARRAY,
1313
getPublishedId,
14-
isGoingToUnpublish,
15-
isReleaseDocument,
16-
isReleaseScheduledOrScheduling,
1714
isVersionId,
1815
type PartialContext,
1916
useCopyPaste,
@@ -90,24 +87,16 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
9087

9188
const perspective = usePerspective()
9289

93-
const {isReleaseLocked, selectedReleaseId, selectedPerspectiveName} = useMemo(() => {
90+
const {selectedReleaseId, selectedPerspectiveName} = useMemo(() => {
9491
// TODO: COREL - Remove this after updating sanity-assist to use <PerspectiveProvider>
9592
if (forcedVersion) {
9693
return forcedVersion
9794
}
9895
return {
9996
selectedPerspectiveName: perspective.selectedPerspectiveName,
10097
selectedReleaseId: perspective.selectedReleaseId,
101-
isReleaseLocked: isReleaseDocument(perspective.selectedPerspective)
102-
? isReleaseScheduledOrScheduling(perspective.selectedPerspective)
103-
: false,
10498
}
105-
}, [
106-
forcedVersion,
107-
perspective.selectedPerspectiveName,
108-
perspective.selectedReleaseId,
109-
perspective.selectedPerspective,
110-
])
99+
}, [forcedVersion, perspective.selectedPerspectiveName, perspective.selectedReleaseId])
111100

112101
const initialValue = useDocumentPaneInitialValue({
113102
paneOptions,
@@ -165,16 +154,11 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
165154

166155
const getIsReadOnly = useCallback(
167156
(editState: EditStateFor): boolean => {
168-
const value = editState?.version || editState?.draft || editState?.published
169-
const willBeUnpublished = value ? isGoingToUnpublish(value) : false
170-
171157
const isDeleted = getIsDeleted(editState)
172158
const seeingHistoryDocument = revisionId !== null
173-
return (
174-
seeingHistoryDocument || isDeleting || isDeleted || isReleaseLocked || willBeUnpublished
175-
)
159+
return seeingHistoryDocument || isDeleting || isDeleted
176160
},
177-
[getIsDeleted, isDeleting, isReleaseLocked, revisionId],
161+
[getIsDeleted, isDeleting, revisionId],
178162
)
179163

180164
const getDisplayed = useCallback(

0 commit comments

Comments
 (0)
Please sign in to comment.