1
+ /* eslint-disable max-statements */
1
2
import { type SanityDocument } from '@sanity/client'
2
3
import { isActionEnabled } from '@sanity/schema/_internal'
3
4
import { useTelemetry } from '@sanity/telemetry/react'
@@ -28,8 +29,12 @@ import {useEditState} from '../hooks/useEditState'
28
29
import { useSchema } from '../hooks/useSchema'
29
30
import { useValidationStatus } from '../hooks/useValidationStatus'
30
31
import { useTranslation } from '../i18n/hooks/useTranslation'
32
+ import { getSelectedPerspective } from '../perspective/getSelectedPerspective'
31
33
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'
33
38
import {
34
39
type DocumentPresence ,
35
40
type EditStateFor ,
@@ -123,6 +128,7 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
123
128
} = options
124
129
const schema = useSchema ( )
125
130
const presenceStore = usePresenceStore ( )
131
+ const { data : releases } = useActiveReleases ( )
126
132
127
133
const schemaType = schema . get ( documentType ) as ObjectSchemaType | undefined
128
134
if ( ! schemaType ) {
@@ -223,12 +229,25 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
223
229
224
230
const ready = connectionState === 'connected' && editState . ready
225
231
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
+
226
244
const readOnly = useMemo ( ( ) => {
227
245
const hasNoPermission = ! isPermissionsLoading && ! permissions ?. granted
228
246
const updateActionDisabled = ! isActionEnabled ( schemaType ! , 'update' )
229
247
const createActionDisabled = isNonExistent && ! isActionEnabled ( schemaType ! , 'create' )
230
248
const reconnecting = connectionState === 'reconnecting'
231
249
const isLocked = editState . transactionSyncLock ?. enabled
250
+ const willBeUnpublished = value ? isGoingToUnpublish ( value ) : false
232
251
233
252
// 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
234
253
if ( liveEdit && editState . draft ?. _id ) {
@@ -250,7 +269,9 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
250
269
createActionDisabled ||
251
270
reconnecting ||
252
271
isLocked ||
253
- isCreateLinked
272
+ isCreateLinked ||
273
+ willBeUnpublished ||
274
+ isReleaseLocked
254
275
255
276
if ( isReadOnly ) return true
256
277
if ( typeof readOnlyProp === 'function' ) return readOnlyProp ( editState )
@@ -269,6 +290,7 @@ export function useDocumentForm(options: DocumentFormOptions): DocumentFormValue
269
290
ready ,
270
291
isCreateLinked ,
271
292
readOnlyProp ,
293
+ isReleaseLocked ,
272
294
] )
273
295
274
296
const { patch} = useDocumentOperation ( documentId , documentType , releaseId )
0 commit comments