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

refactor: use branded ids for releases #7828

Draft
wants to merge 2 commits into
base: corel
Choose a base branch
from
Draft
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
17 changes: 12 additions & 5 deletions packages/@sanity/types/src/schema/preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {type ElementType, type ReactNode} from 'react'

import {type UploadState} from '../upload'
import {type SortOrdering} from './types'

/** @public */
Expand All @@ -9,22 +10,28 @@ export interface PrepareViewOptions {
}

/** @public */
export interface PreviewValue {
_id?: string
_createdAt?: string
_updatedAt?: string
export interface UserPreparedPreviewValue {
title?: string
subtitle?: string
description?: string
media?: ReactNode | ElementType
imageUrl?: string
}

/** @public */
export interface PreviewValue extends UserPreparedPreviewValue {
_id?: string
_type?: string
_upload?: UploadState
_createdAt?: string
_updatedAt?: string
}

/** @public */
export interface PreviewConfig<
Select extends Record<string, string> = Record<string, string>,
PrepareValue extends Record<keyof Select, any> = Record<keyof Select, any>,
> {
select?: Select
prepare?: (value: PrepareValue, viewOptions?: PrepareViewOptions) => PreviewValue
prepare?: (value: PrepareValue, viewOptions?: PrepareViewOptions) => UserPreparedPreviewValue
}
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/upload/uploadState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @internal */
/** @public */
export interface UploadState {
progress: number
/** @deprecated use createdAt instead */
Expand Down
3 changes: 3 additions & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"@sanity/eventsource": "^5.0.0",
"@sanity/export": "^3.41.0",
"@sanity/icons": "^3.4.0",
"@sanity/id-utils": "^1.0.0",
"@sanity/image-url": "^1.0.2",
"@sanity/import": "^3.37.3",
"@sanity/insert-menu": "1.0.11",
Expand Down Expand Up @@ -260,7 +261,9 @@
"speakingurl": "^14.0.1",
"tar-fs": "^2.1.1",
"tar-stream": "^3.1.7",
"ts-brand": "^0.2.0",
"use-device-pixel-ratio": "^1.1.0",
"use-effect-event": "^1.0.2",
"use-hot-module-reload": "^2.0.0",
"use-sync-external-store": "^1.2.0",
"vite": "^4.5.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function useNotificationTarget(

const previewStateObservable = useMemo(() => {
if (!documentId || !schemaType) return of(null)
return getPreviewStateObservable(documentPreviewStore, schemaType, documentId, '')
return getPreviewStateObservable(documentPreviewStore, schemaType, documentId)
}, [documentId, documentPreviewStore, schemaType])
const previewState = useObservable(previewStateObservable)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
useMemo,
useRef,
} from 'react'
import {usePerspective, useReleases} from 'sanity'
import {useReleases} from 'sanity'

import {type FIXME} from '../../../FIXME'
import {useSchema} from '../../../hooks'
import {useReleasesStack} from '../../../releases/store/useReleasesStack'
import {useDocumentPreviewStore} from '../../../store'
import {isNonNullable} from '../../../util'
import {useFormValue} from '../../contexts/FormValue'
Expand All @@ -35,7 +36,7 @@ interface Options {
export function useReferenceInput(options: Options) {
const {path, schemaType, version} = options
const schema = useSchema()
const perspective = usePerspective()

const releases = useReleases()
const documentPreviewStore = useDocumentPreviewStore()
const {EditReferenceLinkComponent, onEditReference, activePath, initialValueTemplateItems} =
Expand Down Expand Up @@ -116,6 +117,7 @@ export function useReferenceInput(options: Options) {
)
}, [disableNew, initialValueTemplateItems, schemaType.to])

const releasesStack = useReleasesStack()
const getReferenceInfo = useCallback(
(id: string) =>
adapter.getReferenceInfo(
Expand All @@ -124,17 +126,11 @@ export function useReferenceInput(options: Options) {
schemaType,
{version},
{
bundleIds: releases.releasesIds,
bundleStack: perspective.bundlesPerspective,
releaseIds: releases.releasesIds,
bundleStack: releasesStack,
},
),
[
documentPreviewStore,
schemaType,
version,
releases.releasesIds,
perspective.bundlesPerspective,
],
[documentPreviewStore, schemaType, version, releases.releasesIds, releasesStack],
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type VersionsRecord,
type VersionTuple,
} from '../../../../preview/utils/getPreviewStateObservable'
import {type ReleaseId} from '../../../../releases'
import {createSearch} from '../../../../search'
import {
collate,
Expand Down Expand Up @@ -48,7 +49,7 @@ export function getReferenceInfo(
id: string,
referenceType: ReferenceSchemaType,
{version}: {version?: string} = {},
perspective: {bundleIds: string[]; bundleStack: string[]} = {bundleIds: [], bundleStack: []},
perspective: {releaseIds: ReleaseId[]; bundleStack: string[]} = {releaseIds: [], bundleStack: []},
): Observable<ReferenceInfo> {
const {publishedId, draftId, versionId} = getIdPair(id, {version})

Expand Down Expand Up @@ -144,28 +145,30 @@ export function getReferenceInfo(
refSchemaType,
)

const versions$ = from(perspective.bundleIds).pipe(
mergeMap<string, Observable<VersionTuple>>((bundleId) =>
const versions$ = from(perspective.releaseIds).pipe(
mergeMap((bundleId) =>
documentPreviewStore
.observePaths({_id: getVersionId(id, bundleId)}, previewPaths)
.pipe(
// eslint-disable-next-line max-nested-callbacks
map((result) =>
result
? [
bundleId,
{
snapshot: {
_id: versionId,
...prepareForPreview(result, refSchemaType),
map(
// eslint-disable-next-line max-nested-callbacks
(result): VersionTuple =>
result
? [
bundleId,
{
snapshot: {
_id: versionId,
...prepareForPreview(result, refSchemaType),
},
},
},
]
: [bundleId, {snapshot: null}],
]
: [bundleId, {snapshot: undefined}],
),
),
),
scan((byBundleId, [bundleId, value]) => {

scan((byBundleId: VersionsRecord, [bundleId, value]) => {
if (value.snapshot === null) {
return omit({...byBundleId}, [bundleId])
}
Expand Down
7 changes: 6 additions & 1 deletion packages/sanity/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@ export {
AddedVersion,
DiscardVersionDialog,
getBundleIdFromReleaseDocumentId,
getPerspectiveTone,
getPublishDateFromRelease,
getReleaseIdFromReleaseDocumentId,
getReleaseTone,
isDraftPerspective,
isPublishedPerspective,
isReleaseDocument,
isReleaseScheduledOrScheduling,
LATEST,
PUBLISHED_PERSPECTIVE,
type ReleaseDocument,
useCurrentRelease,
useDocumentVersions,
usePerspective,
useReleases,
useReleasesStack,
useStudioPerspectiveState,
useVersionOperations,
VersionChip,
versionDocumentExists,
Expand Down
11 changes: 3 additions & 8 deletions packages/sanity/src/core/preview/createPathObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ function observePaths(
paths: PreviewPath[],
observeFields: ObserveFieldsFn,
apiConfig?: ApiConfig,
): Observable<Record<string, unknown> | null> {
if (!value || typeof value !== 'object') {
// Reached a leaf. Return as is
return observableOf(value as null) // @todo
}

): Observable<Record<string, unknown> | undefined> {
const id = getDocumentId(value)

const currentValue: Record<string, unknown> = id ? {...value, _id: id} : {...value}
Expand Down Expand Up @@ -70,7 +65,7 @@ function observePaths(
return observeFields(id, nextHeads, refApiConfig).pipe(
switchMap((snapshot) => {
if (snapshot === null) {
return observableOf(null)
return observableOf(undefined)
}

return observePaths(
Expand Down Expand Up @@ -132,7 +127,7 @@ export function createPathObserver(options: {observeFields: ObserveFieldsFn}) {
value: Previewable,
paths: (FieldName | PreviewPath)[],
apiConfig?: ApiConfig,
): Observable<Record<string, unknown> | null> => {
): Observable<Record<string, unknown> | undefined> => {
return observePaths(value, normalizePaths(paths), observeFields, apiConfig)
}
}
7 changes: 3 additions & 4 deletions packages/sanity/src/core/preview/createPreviewObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type PreviewableType,
} from './types'
import {getPreviewPaths} from './utils/getPreviewPaths'
import {invokePrepare, prepareForPreview} from './utils/prepareForPreview'
import {prepareForPreview} from './utils/prepareForPreview'

function isRecord(value: unknown): value is Record<string, unknown> {
return isPlainObject(value)
Expand Down Expand Up @@ -99,7 +99,7 @@ export function createPreviewObserver(context: {
return observePaths(value, paths, apiConfig).pipe(
map((snapshot) => ({
type: type,
snapshot: snapshot ? prepareForPreview(snapshot, type, viewOptions) : null,
snapshot: snapshot ? prepareForPreview(snapshot, type, viewOptions) : undefined,
})),
)
}
Expand All @@ -110,8 +110,7 @@ export function createPreviewObserver(context: {
// `file`s, and `document`s
return of({
type,
snapshot:
value && isRecord(value) ? invokePrepare(type, value, viewOptions).returnValue : null,
snapshot: value && isRecord(value) ? prepareForPreview(value, type, viewOptions) : undefined,
})
}
}
6 changes: 3 additions & 3 deletions packages/sanity/src/core/preview/documentPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function createObservePathsDocumentPair(options: {
// short circuit, neither draft nor published is available so no point in trying to get a snapshot
return of({
id: publishedId,
type: null,
type: undefined,
draft: {
availability: availability.draft,
snapshot: undefined,
Expand Down Expand Up @@ -74,11 +74,11 @@ export function createObservePathsDocumentPair(options: {
(isRecord(publishedSnapshot) &&
'_type' in publishedSnapshot &&
publishedSnapshot._type) ||
null
undefined

return {
id: publishedId,
type: typeof type === 'string' ? type : null,
type: typeof type === 'string' ? type : undefined,
draft: {
availability: availability.draft,
snapshot: draftSnapshot as T,
Expand Down
12 changes: 8 additions & 4 deletions packages/sanity/src/core/preview/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
type CrossDatasetType,
type PreviewValue,
type Reference,
type SanityDocumentLike,
type SchemaType,
} from '@sanity/types'
Expand Down Expand Up @@ -39,6 +38,11 @@ export type PreviewPath = FieldName[]
/** @internal */
export type Selection = [Id, FieldName[]]

/** @internal */
export interface PartialPreviewDocument {
_id: string
_type: string
}
/**
* @hidden
* @beta */
Expand Down Expand Up @@ -104,7 +108,7 @@ export interface DraftsModelDocumentAvailability {
* @beta */
export interface DraftsModelDocument<T extends SanityDocumentLike = SanityDocumentLike> {
id: string
type: string | null
type: string | undefined
draft: {
availability: DocumentAvailability
snapshot: T | undefined
Expand Down Expand Up @@ -135,7 +139,7 @@ export type InvalidationChannelEvent =
* @beta */
export interface PreparedSnapshot {
type?: PreviewableType
snapshot: PreviewValue | null | undefined
snapshot: PreviewValue | undefined
}

/** @internal */
Expand All @@ -152,7 +156,7 @@ export interface ObservePathsFn {
value: Previewable,
paths: (string | PreviewPath)[],
apiConfig?: ApiConfig,
): Observable<PreviewValue | SanityDocumentLike | Reference | string | null>
): Observable<Record<string, unknown> | undefined>
}

/**
Expand Down
Loading
Loading