Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): only use vX client if strictly necessary #8466

Merged
merged 12 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ export {
isDraftPerspective,
isPublishedPerspective,
isReleaseDocument,
isReleasePerspective,
isReleaseScheduledOrScheduling,
LATEST,
type ReleaseDocument,
RELEASES_INTENT,
RELEASES_STUDIO_CLIENT_OPTIONS,
useActiveReleases,
useArchivedReleases,
useDocumentVersions,
Expand All @@ -51,7 +53,7 @@ export type {SearchFactoryOptions, SearchOptions, SearchSort, SearchTerms} from
export {createSearch, getSearchableTypes, isPerspectiveRaw} from './search'
export * from './store'
export * from './studio'
export * from './studioClient'
export {DEFAULT_STUDIO_CLIENT_OPTIONS} from './studioClient'
export {IsLastPaneProvider} from './tasks'
export * from './templates'
export * from './theme'
Expand Down
7 changes: 3 additions & 4 deletions packages/sanity/src/core/preview/documentPreviewStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface DocumentPreviewStore {
* @param filter - A groq filter to use for the document set
* @param params - Parameters to use with the groq filter
* @param options - Options for the observer
* @param apiVersion - Specify the API version to use for the query
*/
unstable_observeDocumentIdSet: (
filter: string,
Expand All @@ -87,6 +88,7 @@ export interface DocumentPreviewStore {
* Where to insert new items into the set. Defaults to 'sorted' which is based on the lexicographic order of the id
*/
insert?: 'sorted' | 'prepend' | 'append'
apiVersion?: string
},
) => Observable<DocumentIdSetObserverState>

Expand Down Expand Up @@ -139,10 +141,7 @@ export function createDocumentPreviewStore({
)
}

const observeDocumentIdSet = createDocumentIdSetObserver(
// TODO: COREL - Replace once releases API are stable.
versionedClient.withConfig({apiVersion: 'X'}),
)
const observeDocumentIdSet = createDocumentIdSetObserver(versionedClient)

const observeForPreview = createPreviewObserver({observeDocumentTypeFromId, observePaths})
const observeDocumentPairAvailability = createPreviewAvailabilityObserver(
Expand Down
14 changes: 9 additions & 5 deletions packages/sanity/src/core/preview/liveDocumentIdSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import {sortedIndex} from 'lodash'
import {of} from 'rxjs'
import {distinctUntilChanged, filter, map, mergeMap, scan, tap} from 'rxjs/operators'

import {type SourceClientOptions} from '../config/types'
import {versionedClient} from '../studioClient'

export type DocumentIdSetObserverState = {
status: 'reconnecting' | 'connected'
documentIds: string[]
}

interface LiveDocumentIdSetOptions {
insert?: 'sorted' | 'prepend' | 'append'
apiVersion?: SourceClientOptions['apiVersion']
}

export function createDocumentIdSetObserver(client: SanityClient) {
Expand All @@ -18,12 +22,12 @@ export function createDocumentIdSetObserver(client: SanityClient) {
params?: QueryParams,
options: LiveDocumentIdSetOptions = {},
) {
const {insert: insertOption = 'sorted'} = options
const {insert: insertOption = 'sorted', apiVersion} = options

const query = `*[${queryFilter}]._id`
function fetchFilter() {
return client.observable
.fetch(query, params, {
return versionedClient(client, apiVersion)
.observable.fetch(query, params, {
tag: 'preview.observe-document-set.fetch',
})
.pipe(
Expand All @@ -36,8 +40,8 @@ export function createDocumentIdSetObserver(client: SanityClient) {
}),
)
}
return client.observable
.listen(query, params, {
return versionedClient(client, apiVersion)
.observable.listen(query, params, {
visibility: 'transaction',
events: ['welcome', 'mutation', 'reconnect'],
includeResult: false,
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/preview/useLiveDocumentIdSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function useLiveDocumentIdSet(
options: {
// how to insert new document ids. Defaults to `sorted`
insert?: 'sorted' | 'prepend' | 'append'
apiVersion?: string
} = {},
) {
const documentPreviewStore = useDocumentPreviewStore()
Expand Down
9 changes: 7 additions & 2 deletions packages/sanity/src/core/preview/useLiveDocumentSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ const INITIAL_VALUE = {loading: true, documents: []}
export function useLiveDocumentSet(
groqFilter: string,
params?: QueryParams,
options: {
// how to insert new document ids. Defaults to `sorted`
insert?: 'sorted' | 'prepend' | 'append'
apiVersion?: string
} = {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on making apiVersion part of options here?

): {loading: boolean; documents: SanityDocument[]} {
const documentPreviewStore = useDocumentPreviewStore()
const observable = useMemo(() => {
return documentPreviewStore.unstable_observeDocumentIdSet(groqFilter, params).pipe(
return documentPreviewStore.unstable_observeDocumentIdSet(groqFilter, params, options).pipe(
map((state) => (state.documentIds || []) as string[]),
mergeMapArray((id) => documentPreviewStore.unstable_observeDocument(id)),
map((docs) => ({loading: false, documents: docs as SanityDocument[]})),
)
}, [documentPreviewStore, groqFilter, params])
}, [documentPreviewStore, groqFilter, params, options])
return useObservable(observable, INITIAL_VALUE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {catchError} from 'rxjs/operators'
import {useDocumentPreviewStore} from '../../store'
import {getPublishedId} from '../../util/draftUtils'
import {createSWR} from '../../util/rxSwr'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../util/releasesClient'

export interface DocumentPerspectiveProps {
documentId: string
Expand Down Expand Up @@ -35,7 +36,9 @@ export function useDocumentVersions(props: DocumentPerspectiveProps): DocumentPe

const observable = useMemo(() => {
return documentPreviewStore
.unstable_observeDocumentIdSet(`sanity::versionsOf("${publishedId}")`)
.unstable_observeDocumentIdSet(`sanity::versionsOf("${publishedId}")`, undefined, {
apiVersion: RELEASES_STUDIO_CLIENT_OPTIONS.apiVersion,
})
.pipe(
swr(`${publishedId}`),
map(({value}) => ({
Expand Down
3 changes: 1 addition & 2 deletions packages/sanity/src/core/releases/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from '../store/_legacy'
export * from '../store/user'
export * from './__telemetry__/releases.telemetry'
export * from './components'
export * from './hooks'
Expand All @@ -9,4 +7,5 @@ export * from './util/const'
export * from './util/createReleaseId'
export * from './util/getReleaseIdFromReleaseDocumentId'
export * from './util/getReleaseTone'
export {isReleasePerspective, RELEASES_STUDIO_CLIENT_OPTIONS} from './util/releasesClient'
export * from './util/util'
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {useMemo} from 'react'

import {useClient} from '../../hooks'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../util/releasesClient'
import {createReleaseOperationsStore} from './createReleaseOperationStore'

/**
* @internal
*/
export function useReleaseOperations() {
const studioClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const studioClient = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)
return useMemo(
() =>
createReleaseOperationsStore({
Expand Down
4 changes: 2 additions & 2 deletions packages/sanity/src/core/releases/store/useReleasesStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useMemo} from 'react'
import {useClient} from '../../hooks'
import {useDocumentPreviewStore, useResourceCache} from '../../store'
import {useWorkspace} from '../../studio'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../util/releasesClient'
import {createReleaseStore} from './createReleaseStore'
import {type ReleaseStore} from './types'

Expand All @@ -12,7 +12,7 @@ export function useReleasesStore(): ReleaseStore {
const resourceCache = useResourceCache()
const workspace = useWorkspace()
const previewStore = useDocumentPreviewStore()
const studioClient = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const studioClient = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)

return useMemo(() => {
const releaseStore =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {IntentLink} from 'sanity/router'
import {type PreviewLayoutKey} from '../../../components/previews/types'
import {DocumentPreviewPresence} from '../../../presence'
import {SanityDefaultPreview} from '../../../preview/components/SanityDefaultPreview'
import {useDocumentPresence} from '../../../store/_legacy/presence/useDocumentPresence'
import {getPublishedId} from '../../../util/draftUtils'
import {type ReleaseState, useDocumentPresence} from '../../index'
import {type ReleaseState} from '../../store/types'
import {getReleaseIdFromReleaseDocumentId} from '../../util/getReleaseIdFromReleaseDocumentId'

interface ReleaseDocumentPreviewProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {catchError, forkJoin, from, map, type Observable, of, switchMap} from 'r

import {useClient} from '../../../../../hooks/useClient'
import {getTransactionsLogs} from '../../../../../store/translog/getTransactionsLogs'
import {API_VERSION} from '../../../../../tasks/constants'
import {getPublishedId} from '../../../../../util/draftUtils'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../../../../util/releasesClient'
import {type DocumentInRelease} from '../../../detail/useBundleDocuments'

export type RevertDocument = SanityDocument & {
Expand All @@ -20,7 +20,7 @@ type RevertDocuments = RevertDocument[]
type DocumentRevertStates = RevertDocuments | null | undefined

export const useDocumentRevertStates = (releaseDocuments: DocumentInRelease[]) => {
const client = useClient({apiVersion: API_VERSION})
const client = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)
const observableClient = client.observable
const transactionId = releaseDocuments[0]?.document._rev
const {dataset} = client.config()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {catchError, from, map, of} from 'rxjs'

import {useClient} from '../../../../../hooks/useClient'
import {getTransactionsLogs} from '../../../../../store/translog/getTransactionsLogs'
import {API_VERSION} from '../../../../../tasks/constants'
import {getPublishedId} from '../../../../../util/draftUtils'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../../../../util/releasesClient'
import {type DocumentInRelease} from '../../../detail/useBundleDocuments'

export const usePostPublishTransactions = (documents: DocumentInRelease[]) => {
const client = useClient({apiVersion: API_VERSION})
const client = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)
const transactionId = documents[0]?.document._rev

const memoHasPostPublishTransactions = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {useCallback, useEffect, useMemo, useState} from 'react'

import {useClient} from '../../../../hooks'
import {getJsonStream} from '../../../../store/_legacy/history/history/getJsonStream'
import {API_VERSION} from '../../../../tasks/constants'
import {getVersionId} from '../../../../util'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../../../util/releasesClient'

export type DocumentHistory = {
history: TransactionLogEventWithEffects[]
Expand All @@ -22,7 +22,7 @@ export function useReleaseHistory(
collaborators: string[]
loading: boolean
} {
const client = useClient({apiVersion: API_VERSION})
const client = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)
const {dataset, token} = client.config()
const [history, setHistory] = useState<TransactionLogEventWithEffects[]>([])
const queryParams = `tag=sanity.studio.tasks.history&effectFormat=mendoza&excludeContent=true&includeIdentifiedDocumentsOnly=true`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {type DocumentPreviewStore} from '../../../../preview/documentPreviewStore'
import {type ReleasesReducerState} from '../../../store/reducer'
import {getReleaseIdFromReleaseDocumentId} from '../../../util/getReleaseIdFromReleaseDocumentId'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../../../util/releasesClient'
import {getReleaseActivityEvents} from './getReleaseActivityEvents'
import {getReleaseEditEvents} from './getReleaseEditEvents'
import {isCreateReleaseEvent, isEventsAPIEvent, isTranslogEvent, type ReleaseEvent} from './types'
Expand Down Expand Up @@ -70,13 +71,17 @@ export function getReleaseEvents({
)

const groqFilter = `_id in path("versions.${getReleaseIdFromReleaseDocumentId(releaseId)}.*")`
const documentsCount$ = documentPreviewStore.unstable_observeDocumentIdSet(groqFilter).pipe(
filter(({status}) => status === 'connected'),
map(({documentIds}) => documentIds.length),
distinctUntilChanged(),
// Emit only when count changes, after first non null value.
skip(1),
)
const documentsCount$ = documentPreviewStore
.unstable_observeDocumentIdSet(groqFilter, undefined, {
apiVersion: RELEASES_STUDIO_CLIENT_OPTIONS.apiVersion,
})
.pipe(
filter(({status}) => status === 'connected'),
map(({documentIds}) => documentIds.length),
distinctUntilChanged(),
// Emit only when count changes, after first non null value.
skip(1),
)

const sideEffects$ = merge(releaseRev$, documentsCount$).pipe(
tap(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {useDocumentPreviewStore} from '../../../../store/_legacy/datastores'
import {useSource} from '../../../../studio/source'
import {useReleasesStore} from '../../../store/useReleasesStore'
import {getReleaseDocumentIdFromReleaseId} from '../../../util/getReleaseDocumentIdFromReleaseId'
import {RELEASES_STUDIO_CLIENT_OPTIONS} from '../../../util/releasesClient'
import {EVENTS_INITIAL_VALUE, getReleaseEvents} from './getReleaseEvents'
import {type ReleaseEvent} from './types'

Expand All @@ -18,8 +19,7 @@ export interface ReleaseEvents {
}

export function useReleaseEvents(releaseId: string): ReleaseEvents {
// Needs vX version of the API
const client = useClient({apiVersion: 'X'})
const client = useClient(RELEASES_STUDIO_CLIENT_OPTIONS)
const documentPreviewStore = useDocumentPreviewStore()
const {state$: releasesState$} = useReleasesStore()
const source = useSource()
Expand Down
Loading
Loading