sanity
Version:
Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches
34 lines (28 loc) • 900 B
text/typescript
import {type SchemaType} from '@sanity/types'
import {useEffect, useState} from 'react'
import {useDocumentPreviewStore} from '../../store'
import {getPreviewStateObservable, type PaneItemPreviewState} from '../utils/paneItemHelpers'
export default function usePreviewState(
documentId: string,
schemaType?: SchemaType,
): PaneItemPreviewState {
const documentPreviewStore = useDocumentPreviewStore()
const [paneItemPreview, setPaneItemPreview] = useState<PaneItemPreviewState>({})
useEffect(() => {
if (!schemaType) {
return undefined
}
const subscription = getPreviewStateObservable(
documentPreviewStore,
schemaType,
documentId,
'',
).subscribe((state) => {
setPaneItemPreview(state)
})
return () => {
subscription?.unsubscribe()
}
}, [documentPreviewStore, schemaType, documentId])
return paneItemPreview
}