Skip to content

Commit

Permalink
fix(core): references to documents on same release are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 18, 2025
1 parent b47e306 commit c6e33e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/sanity/src/core/preview/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {combineLatest, defer, from, type Observable, of} from 'rxjs'
import {distinctUntilChanged, map, mergeMap, reduce, switchMap} from 'rxjs/operators'
import shallowEquals from 'shallow-equals'

import {createSWR, getDraftId, getPublishedId, getVersionId, isRecord, isVersionId} from '../util'
import {createSWR, getDraftId, getPublishedId, getVersionId, isRecord} from '../util'
import {
AVAILABILITY_NOT_FOUND,
AVAILABILITY_PERMISSION_DENIED,
Expand Down Expand Up @@ -150,7 +150,7 @@ export function createPreviewAvailabilityObserver(
): Observable<DraftsModelDocumentAvailability> {
const draftId = getDraftId(id)
const publishedId = getPublishedId(id)
const versionId = isVersionId(id) && version ? getVersionId(id, version) : undefined
const versionId = version ? getVersionId(id, version) : undefined
return combineLatest([
observeDocumentAvailability(draftId),
observeDocumentAvailability(publishedId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ import {
shareReplay,
skip,
throttleTime,
withLatestFrom,
} from 'rxjs/operators'
import {exhaustMapWithTrailing} from 'rxjs-exhaustmap-with-trailing'
import shallowEquals from 'shallow-equals'

import {type DocumentPreviewStore, type LocaleSource, type SourceClientOptions} from '..'
import {type SourceClientOptions} from '../config/types'
import {type LocaleSource} from '../i18n/types'
import {type DocumentPreviewStore} from '../preview/documentPreviewStore'
import {getVersionFromId} from '../util/draftUtils'
import {validateDocumentObservable} from './validateDocument'

/**
Expand Down Expand Up @@ -70,8 +74,11 @@ type GetDocumentExists = NonNullable<ValidationContext['getDocumentExists']>
const listenDocumentExists = (
observeDocumentAvailability: DocumentPreviewStore['unstable_observeDocumentPairAvailability'],
id: string,
versionId: string | undefined,
): Observable<boolean> =>
observeDocumentAvailability(id).pipe(map(({published}) => published.available))
observeDocumentAvailability(id, {version: versionId}).pipe(
map(({published, version}) => published.available || version?.available || false),
)

// throttle delay for referenced document updates (i.e. time between responding to changes in referenced documents)
const REF_UPDATE_DELAY = 1000
Expand All @@ -98,14 +105,19 @@ export function validateDocumentWithReferences(
mergeMap((ids) => from(ids)),
)

const versionId$ = document$.pipe(
map((doc) => getVersionFromId(doc?._id || '')),
distinctUntilChanged(),
)
// Note: we only use this to trigger a re-run of validation when a referenced document is published/unpublished
const referenceExistence$ = referenceIds$.pipe(
groupBy((id) => id, {duration: () => timer(1000 * 60 * 30)}),
mergeMap((id$) =>
id$.pipe(
distinct(),
mergeMap((id) =>
listenDocumentExists(ctx.observeDocumentPairAvailability, id).pipe(
withLatestFrom(versionId$),
mergeMap(([id, versionId]) =>
listenDocumentExists(ctx.observeDocumentPairAvailability, id, versionId).pipe(
map(
// eslint-disable-next-line max-nested-callbacks
(result) => [id, result] as const,
Expand Down

0 comments on commit c6e33e3

Please sign in to comment.