UNPKG

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

52 lines (42 loc) 1.19 kB
import {unstable_useValuePreview as useValuePreview} from 'sanity' import {useDocumentPane} from './useDocumentPane' /** * useDocumentTitle hook return type. * * @beta * @hidden */ interface UseDocumentTitle { error?: string title?: string } /** * React hook that returns the document title for the current document in the document pane. * * @beta * @hidden * * @returns The document title or error. See {@link UseDocumentTitle} */ export function useDocumentTitle(): UseDocumentTitle { const {connectionState, schemaType, title, value: documentValue} = useDocumentPane() const subscribed = Boolean(documentValue) && connectionState !== 'connecting' const {error, value} = useValuePreview({ enabled: subscribed, schemaType, value: documentValue, }) if (connectionState === 'connecting') { return {error: undefined, title: undefined} } if (title) { return {error: undefined, title} } if (!documentValue) { return {error: undefined, title: `New ${schemaType?.title || schemaType?.name}`} } if (error) { return {error: `Error: ${error.message}`, title: undefined} } return {error: undefined, title: value?.title} }