1
1
import { type ReleaseId } from '@sanity/client'
2
+ import { type ObjectSchemaType } from '@sanity/types'
2
3
import { useMemo } from 'react'
3
4
import {
4
5
EMPTY_ARRAY ,
@@ -11,8 +12,10 @@ import {
11
12
isDeleteDocumentVersionEvent ,
12
13
PerspectiveProvider ,
13
14
useArchivedReleases ,
15
+ useEditState ,
14
16
useEventsStore ,
15
17
usePerspective ,
18
+ useSchema ,
16
19
} from 'sanity'
17
20
18
21
import { usePaneRouter } from '../../components'
@@ -24,14 +27,28 @@ import {type DocumentPaneProviderProps} from './types'
24
27
export const DocumentEventsPane = ( props : DocumentPaneProviderProps ) => {
25
28
const { params = EMPTY_PARAMS } = usePaneRouter ( )
26
29
const options = usePaneOptions ( props . pane . options , params )
30
+ const schema = useSchema ( )
31
+ const documentType = options . type
32
+ const schemaType = schema . get ( options . type ) as ObjectSchemaType | undefined
33
+ const liveEdit = Boolean ( schemaType ?. liveEdit )
27
34
28
- const { selectedPerspectiveName} = usePerspective ( )
35
+ const { selectedPerspectiveName, selectedReleaseId , selectedPerspective } = usePerspective ( )
29
36
const { data : archivedReleases } = useArchivedReleases ( )
37
+ const editState = useEditState (
38
+ getPublishedId ( options . id ) ,
39
+ documentType ,
40
+ 'default' ,
41
+ selectedReleaseId ,
42
+ )
30
43
44
+ const showingPublishedOnDraft = liveEdit && selectedPerspective === 'drafts' && ! editState ?. draft
31
45
const { rev, since} = params
32
46
const historyVersion = params . historyVersion as ReleaseId | undefined
33
47
34
48
const documentId = useMemo ( ( ) => {
49
+ if ( showingPublishedOnDraft ) {
50
+ return getPublishedId ( options . id )
51
+ }
35
52
if (
36
53
historyVersion &&
37
54
archivedReleases . some (
@@ -47,11 +64,18 @@ export const DocumentEventsPane = (props: DocumentPaneProviderProps) => {
47
64
if ( selectedPerspectiveName === 'published' ) {
48
65
return getPublishedId ( options . id )
49
66
}
50
- if ( selectedPerspectiveName . startsWith ( 'r' ) ) {
67
+ if ( selectedReleaseId ) {
51
68
return getVersionId ( options . id , selectedPerspectiveName )
52
69
}
53
70
return options . id
54
- } , [ archivedReleases , historyVersion , selectedPerspectiveName , options . id ] )
71
+ } , [
72
+ archivedReleases ,
73
+ historyVersion ,
74
+ selectedPerspectiveName ,
75
+ options . id ,
76
+ showingPublishedOnDraft ,
77
+ selectedReleaseId ,
78
+ ] )
55
79
56
80
const eventsStore = useEventsStore ( { documentId, documentType : options . type , rev, since} )
57
81
0 commit comments