Skip to content

Commit

Permalink
refactor(sanity): re-add sanity::versionOf
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Aug 23, 2024
1 parent e9a86a5 commit 516625a
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions packages/sanity/src/core/bundles/hooks/useDocumentVersions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,24 @@ export function useDocumentVersions(props: DocumentPerspectiveProps): DocumentPe
const documentPreviewStore = useDocumentPreviewStore()

const observable = useMemo(() => {
return (
documentPreviewStore
// TODO - re-add versionOf once CL has it ready
.unstable_observeDocumentIdSet(`_id in path("versions.**") && _id match '*.${publishedId}'`)
.pipe(
map(({documentIds}) => {
return documentIds.flatMap((id) => {
// eslint-disable-next-line max-nested-callbacks
const matchingBundle = bundles?.find((bundle) => getBundleSlug(id) === bundle.slug)
return matchingBundle || []
})
}),
map((data) => ({data})),
catchError((error) => {
return of({error})
}),
scan((state, result) => {
return {...state, ...result}
}, INITIAL_STATE),
)
)
return documentPreviewStore
.unstable_observeDocumentIdSet(`sanity::versionOf("${publishedId}")`)
.pipe(
map(({documentIds}) => {
return documentIds.flatMap((id) => {
// eslint-disable-next-line max-nested-callbacks
const matchingBundle = bundles?.find((bundle) => getBundleSlug(id) === bundle.slug)
return matchingBundle || []
})
}),
map((data) => ({data})),
catchError((error) => {
return of({error})
}),
scan((state, result) => {
return {...state, ...result}
}, INITIAL_STATE),
)
}, [bundles, documentPreviewStore, publishedId])

return useObservable(observable, INITIAL_STATE)
Expand Down

0 comments on commit 516625a

Please sign in to comment.