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

41 lines (36 loc) 1.07 kB
import {type SanityDocument} from '@sanity/types' import {useMemoObservable} from 'react-rx' import {map, startWith} from 'rxjs/operators' import {useDocumentStore} from '../store' interface ReferringDocumentsState { isLoading: boolean referringDocuments: SanityDocument[] } const INITIAL_STATE: ReferringDocumentsState = {referringDocuments: [], isLoading: true} /** * @internal * @param id - the id to search for referring documents for */ export function useReferringDocuments(id: string): ReferringDocumentsState { const documentStore = useDocumentStore() return useMemoObservable( () => documentStore .listenQuery( '*[references($docId)] [0...101]', {docId: id}, {tag: 'use-referring-documents'}, ) .pipe( map( (docs: SanityDocument[]): ReferringDocumentsState => ({ referringDocuments: docs, isLoading: false, }), ), startWith(INITIAL_STATE), ), [documentStore, id], INITIAL_STATE, ) }