Skip to content

Commit

Permalink
Revert "feat(sanity): #TEMPORARY #DELETE handle versions in `Document…
Browse files Browse the repository at this point in the history
…PaneProvider`"

This reverts commit 4e9be02.
  • Loading branch information
juice49 committed Aug 8, 2024
1 parent 0ef90df commit bc2aad2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {type Schema} from '@sanity/types'

export const isLiveEditEnabled = (schema: Schema, typeName: string): boolean =>
(globalThis as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_LIVE_EDIT_OVERRIDE ??
schema.get(typeName)?.liveEdit === true
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {fromString as pathFromString, resolveKeyedPath} from '@sanity/util/paths
import {omit, throttle} from 'lodash'
import {memo, useCallback, useEffect, useMemo, useRef, useState} from 'react'
import deepEquals from 'react-fast-compare'
import {useObservable} from 'react-rx'
import {map} from 'rxjs'
import {
type DocumentFieldAction,
type DocumentInspector,
Expand All @@ -29,7 +27,6 @@ import {
useConnectionState,
useCopyPaste,
useDocumentOperation,
useDocumentStore,
useDocumentValuePermissions,
useDocumentVersions,
useEditState,
Expand All @@ -48,8 +45,6 @@ import {
import {DocumentPaneContext} from 'sanity/_singletons'
import {useRouter} from 'sanity/router'

// eslint-disable-next-line
import {type DraftsModelDocumentAvailability} from '../../../core/preview'
import {usePaneRouter} from '../../components'
import {structureLocaleNamespace} from '../../i18n'
import {type PaneMenuItem} from '../../types'
Expand Down Expand Up @@ -90,41 +85,18 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
const setPaneParams = paneRouter.setParams
const {features} = useStructureTool()
const {push: pushToast} = useToast()
const documentStore = useDocumentStore()
const {
options,
menuItemGroups = DEFAULT_MENU_ITEM_GROUPS,
title = null,
views: viewsProp = [],
} = pane
const perspective = stickyParams.perspective
const bundle = perspective?.startsWith('bundle.') ? perspective.split('bundle.').at(1) : undefined
const paneOptions = useUnique(options)
const documentIdRaw = paneOptions.id
const [documentId, setDocumentId] = useState<string>(
bundle ? [bundle, getPublishedId(documentIdRaw)].join('.') : getPublishedId(documentIdRaw),
)

const documentId = getPublishedId(documentIdRaw)
const documentType = options.type
const params = useUnique(paneRouter.params) || EMPTY_PARAMS
const panePayload = useUnique(paneRouter.payload)

const versionExists$ = useMemo(
() =>
documentStore
.listenQuery(
'*[_id == $versionId][0]._id',
{
versionId: [bundle, getPublishedId(documentIdRaw)].join('.'),
},
{},
)
.pipe(map((id) => id !== null)),
[documentStore, documentIdRaw, bundle],
)

const versionExists = useObservable(versionExists$)

const {templateName, templateParams} = useMemo(
() =>
getInitialValueTemplateOpts(templates, {
Expand All @@ -143,42 +115,18 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
templateParams,
})

const [liveEdit, setLiveEdit] = useState<boolean>(false)
const initialValue = useUnique(initialValueRaw)
const {patch} = useDocumentOperation(documentId, documentType)
const schemaType = schema.get(documentType) as ObjectSchemaType | undefined
const editState = useEditState(documentId, documentType, 'default')
const editState = useEditState(documentId, documentType)
const {validation: validationRaw} = useValidationStatus(documentId, documentType)
const connectionState = useConnectionState(documentId, documentType)
const {data: documentVersions} = useDocumentVersions({documentId})

// When a bundle is checked out and the document being viewed either comes into existence or is
// removed from the bundle, switch to the version or the default document accordingly.
useEffect(() => {
setDocumentId(
bundle && versionExists
? [bundle, getPublishedId(documentIdRaw)].join('.')
: getPublishedId(documentIdRaw),
)
}, [bundle, versionExists, documentIdRaw])

// When a bundle is checked out and the document being viewed exists in that bundle, enable
// live editing.
useEffect(() => {
const isEnabled = Boolean(schemaType?.liveEdit || versionExists)

setLiveEdit(isEnabled)
;(globalThis as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_LIVE_EDIT_OVERRIDE =
isEnabled

return () => {
;(globalThis as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED_LIVE_EDIT_OVERRIDE =
undefined
}
}, [schemaType?.liveEdit, versionExists])
const perspective = stickyParams.perspective

const value: SanityDocumentLike =
(perspective === 'published' && !bundle
(perspective === 'published'
? editState.published || editState.draft
: editState?.draft || editState?.published) || initialValue.value
const [isDeleting, setIsDeleting] = useState(false)
Expand Down Expand Up @@ -313,10 +261,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
* a timeline revision in this instance will display an error localized to the popover itself.
*/
const ready =
connectionState === 'connected' &&
editState.ready &&
(timelineReady || !!timelineError) &&
typeof versionExists === 'boolean'
connectionState === 'connected' && editState.ready && (timelineReady || !!timelineError)

const displayed: Partial<SanityDocument> | undefined = useMemo(
() => (onOlderRevision ? timelineDisplayed || {_id: value._id, _type: value._type} : value),
Expand Down Expand Up @@ -535,6 +480,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
)

const requiredPermission = value._createdAt ? 'update' : 'create'
const liveEdit = Boolean(schemaType?.liveEdit)
const docId = value._id ? value._id : 'dummy-id'
const docPermissionsInput = useMemo(() => {
return {
Expand All @@ -558,7 +504,7 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
const isLocked = editState.transactionSyncLock?.enabled

return (
(!!perspective && !bundle) ||
!!perspective ||
!ready ||
revTime !== null ||
hasNoPermission ||
Expand All @@ -581,7 +527,6 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
revTime,
isDeleting,
isDeleted,
bundle,
])

const formState = useFormState(schemaType!, {
Expand Down

0 comments on commit bc2aad2

Please sign in to comment.