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
32 lines (24 loc) • 962 B
text/typescript
import {useMemo} from 'react'
import {getPublishedId, useSearchState} from 'sanity'
import {useDocumentSheetListStore} from './useDocumentSheetListStore'
interface DocumentSheetListOptions {
/** The schemaType.name */
typeName: string
}
export function useDocumentSheetList({typeName}: DocumentSheetListOptions) {
const {state} = useSearchState()
const items = useMemo(() => {
const map = new Map()
state.result.hits.forEach((h) => map.set(getPublishedId(h.hit._id), h.hit))
return map
}, [state.result.hits])
// The store is listening to all the documents that match with the _type filter.
const {data, isLoading} = useDocumentSheetListStore({
filter: `_type == "${typeName}"`,
})
// Only return the documents that match with the serverSide filter items.
const documents = useMemo(() => {
return data.filter((doc) => items.has(getPublishedId(doc._id)))
}, [data, items])
return {data: documents, isLoading}
}