Skip to content

Commit

Permalink
refactor: active release docs also use a getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanl17 committed Dec 13, 2024
1 parent 59bae9f commit 0553366
Showing 1 changed file with 102 additions and 83 deletions.
185 changes: 102 additions & 83 deletions packages/sanity/src/core/releases/tool/detail/useBundleDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {useObservable} from 'react-rx'
import {combineLatest, from, type Observable, of} from 'rxjs'
import {filter, map, mergeMap, startWith, switchAll, switchMap, take, toArray} from 'rxjs/operators'
import {mergeMapArray} from 'rxjs-mergemap-array'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from 'sanity'
import {DEFAULT_STUDIO_CLIENT_OPTIONS, type LocaleSource} from 'sanity'

import {useClient, useSchema} from '../../../hooks'
import {
Expand Down Expand Up @@ -36,6 +36,95 @@ export interface DocumentInRelease {
previewValues: {isLoading: boolean; values: ReturnType<typeof prepareForPreview>}
}

const getActiveReleaseDocumentsObservable: (
documentPreviewStore: DocumentPreviewStore,
groqFilter: string,
i18n: LocaleSource,
getClient: ReturnType<typeof useSource>['getClient'],
schema: Schema,
observableClient: ObservableSanityClient,
releaseId: string,
) => Observable<{
loading: boolean
results: DocumentInRelease[]
// eslint-disable-next-line max-params
}> = (documentPreviewStore, groqFilter, i18n, getClient, schema, observableClient, releaseId) => {
return documentPreviewStore.unstable_observeDocumentIdSet(groqFilter).pipe(
map((state) => (state.documentIds || []) as string[]),
mergeMapArray((id) => {
const ctx = {
observeDocument: documentPreviewStore.unstable_observeDocument,
observeDocumentPairAvailability:
documentPreviewStore.unstable_observeDocumentPairAvailability,
i18n,
getClient,
schema,
}

const document$ = documentPreviewStore.unstable_observeDocument(id).pipe(
filter(Boolean),
switchMap((doc) =>
observableClient
.fetch(
`*[_id in path("${getPublishedId(doc._id)}")]{_id}`,
{},
{tag: 'release-documents.check-existing'},
)
.pipe(
switchMap((publishedDocumentExists) =>
of({
...doc,
publishedDocumentExists: !!publishedDocumentExists.length,
}),
),
),
),
)
const validation$ = validateDocumentWithReferences(ctx, document$).pipe(
map((validationStatus) => ({
...validationStatus,
hasError: validationStatus.validation.some((marker) => isValidationErrorMarker(marker)),
})),
)

const previewValues$ = document$.pipe(
map((document) => {
const schemaType = schema.get(document._type)
if (!schemaType) {
throw new Error(`Schema type not found for document type ${document._type}`)
}

return documentPreviewStore.observeForPreview(document, schemaType).pipe(
map((version) => ({
isLoading: false,
values: prepareForPreview(
getPreviewValueWithFallback({
value: document,
version: version.snapshot,
perspective: releaseId,
}),
schemaType,
),
})),
startWith({isLoading: true, values: {}}),
)
}),
switchAll(),
)

return combineLatest([document$, validation$, previewValues$]).pipe(
map(([document, validation, previewValues]) => ({
document,
validation,
previewValues,
memoKey: uuid(),
})),
)
}),
map((results) => ({loading: false, results})),
)
}

const getPublishedArchivedReleaseDocumentsObservable: (
release: ReleaseDocument,
observableClient: ObservableSanityClient,
Expand Down Expand Up @@ -115,86 +204,6 @@ export function useBundleDocuments(releaseId: string): {
const observableClient = client.observable
const {dataset} = client.config()

const activeReleaseDocumentsObservable = useMemo(() => {
return documentPreviewStore.unstable_observeDocumentIdSet(groqFilter).pipe(
map((state) => (state.documentIds || []) as string[]),
mergeMapArray((id) => {
const ctx = {
observeDocument: documentPreviewStore.unstable_observeDocument,
observeDocumentPairAvailability:
documentPreviewStore.unstable_observeDocumentPairAvailability,
i18n,
getClient,
schema,
}

const document$ = documentPreviewStore.unstable_observeDocument(id).pipe(
filter(Boolean),
switchMap((doc) =>
observableClient
.fetch(
`*[_id in path("${getPublishedId(doc._id)}")]{_id}`,
{},
{tag: 'release-documents.check-existing'},
)
.pipe(
// eslint-disable-next-line max-nested-callbacks
switchMap((publishedDocumentExists) =>
of({
...doc,
publishedDocumentExists: !!publishedDocumentExists.length,
}),
),
),
),
)
const validation$ = validateDocumentWithReferences(ctx, document$).pipe(
map((validationStatus) => ({
...validationStatus,
// eslint-disable-next-line max-nested-callbacks
hasError: validationStatus.validation.some((marker) => isValidationErrorMarker(marker)),
})),
)

const previewValues$ = document$.pipe(
map((document) => {
const schemaType = schema.get(document._type)
if (!schemaType) {
throw new Error(`Schema type not found for document type ${document._type}`)
}

return documentPreviewStore.observeForPreview(document, schemaType).pipe(
// eslint-disable-next-line max-nested-callbacks
map((version) => ({
isLoading: false,
values: prepareForPreview(
getPreviewValueWithFallback({
value: document,
version: version.snapshot,
perspective: releaseId,
}),
schemaType,
),
})),
startWith({isLoading: true, values: {}}),
)
}),
switchAll(),
)

return combineLatest([document$, validation$, previewValues$]).pipe(
map(([document, validation, previewValues]) => ({
document,
validation,
previewValues,
memoKey: uuid(),
})),
)
}),
map((results) => ({loading: false, results})),
)
}, [documentPreviewStore, groqFilter, i18n, getClient, schema, observableClient, releaseId])

const observable = useMemo(
() =>
releasesState$.pipe(
Expand All @@ -217,13 +226,23 @@ export function useBundleDocuments(releaseId: string): {
)
}

return activeReleaseDocumentsObservable
return getActiveReleaseDocumentsObservable(
documentPreviewStore,
groqFilter,
i18n,
getClient,
schema,
observableClient,
releaseId,
)
}),
),
[
activeReleaseDocumentsObservable,
dataset,
documentPreviewStore,
getClient,
groqFilter,
i18n,
observableClient,
releaseId,
releasesState$,
Expand Down

0 comments on commit 0553366

Please sign in to comment.