Skip to content

Commit

Permalink
fix(core): VersionOriginType to DocumentVariantType (#7924)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin authored Dec 2, 2024
1 parent 7a4b7a3 commit 5deba91
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {defineEvent} from '@sanity/telemetry'

import {type VersionOriginTypes} from '../index'
import {type DocumentVariantType} from '../../util/getDocumentVariantType'

interface VersionInfo {
/**
Expand All @@ -10,7 +10,7 @@ interface VersionInfo {
/**
* the origin of the version created (from a draft or from a version)
*/
documentOrigin: VersionOriginTypes
documentOrigin: DocumentVariantType
}

export interface OriginInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {useTelemetry} from '@sanity/telemetry/react'
import {useToast} from '@sanity/ui'

import {Translate, useTranslation} from '../../i18n'
import {getDocumentVariantType} from '../../util/getDocumentVariantType'
import {AddedVersion} from '../__telemetry__/releases.telemetry'
import {useReleaseOperations} from '../store/useReleaseOperations'
import {getCreateVersionOrigin} from '../util/util'
import {usePerspective} from './usePerspective'

export interface VersionOperationsValue {
Expand All @@ -30,7 +30,7 @@ export function useVersionOperations(): VersionOperationsValue {
documentId: string,
initialValue?: Record<string, unknown>,
) => {
const origin = getCreateVersionOrigin(documentId)
const origin = getDocumentVariantType(documentId)
try {
await createVersion(releaseId, documentId, initialValue)
setPerspectiveFromReleaseId(releaseId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {AddedVersion} from 'sanity'

import {SearchPopover} from '../../../studio/components/navbar/search/components/SearchPopover'
import {SearchProvider} from '../../../studio/components/navbar/search/contexts/search/SearchProvider'
import {getDocumentVariantType} from '../../../util/getDocumentVariantType'
import {useReleaseOperations} from '../../store/useReleaseOperations'
import {getBundleIdFromReleaseDocumentId} from '../../util/getBundleIdFromReleaseDocumentId'
import {getCreateVersionOrigin} from '../../util/util'
import {useBundleDocuments} from './useBundleDocuments'

export function AddDocumentSearch({
Expand Down Expand Up @@ -38,7 +38,7 @@ export function AddDocumentSearch({
title: 'Document added to release',
})

const origin = getCreateVersionOrigin(item._id)
const origin = getDocumentVariantType(item._id)

telemetry.log(AddedVersion, {
documentOrigin: origin,
Expand Down
14 changes: 0 additions & 14 deletions packages/sanity/src/core/releases/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {
formatRelativeLocale,
getVersionFromId,
isDraftId,
isPublishedId,
isVersionId,
resolveBundlePerspective,
} from '../../util'
import {type CurrentPerspective} from '../hooks/usePerspective'
import {type VersionOriginTypes} from '../index'
import {type ReleaseDocument} from '../store/types'
import {LATEST} from './const'

Expand Down Expand Up @@ -53,17 +50,6 @@ export function isDraftOrPublished(versionName: string): boolean {
return versionName === 'drafts' || versionName === 'published'
}

/**
* @beta
* @param documentId - The document id, e.g. `my-document-id` or `drafts.my-document-id` or `summer.my-document-id`
* @returns VersionOriginTypes - the origin from which this version is being created from
*/
export function getCreateVersionOrigin(documentId: string): VersionOriginTypes {
if (isDraftId(documentId)) return 'draft'
if (isPublishedId(documentId)) return 'published'
return 'version'
}

/** @internal */
export function getPublishDateFromRelease(release: ReleaseDocument): Date | null {
if (release.metadata.releaseType !== 'scheduled') return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {omit} from 'lodash'
import {EMPTY, from, merge, type Observable, Subject} from 'rxjs'
import {filter, map, mergeMap, share, take, tap} from 'rxjs/operators'

import {type DocumentVariantType} from '../../../../util/getDocumentVariantType'
import {
type BufferedDocumentEvent,
type CommitRequest,
Expand All @@ -14,7 +15,6 @@ import {
} from '../buffered-doc'
import {getPairListener, type ListenerEvent, type PairListenerOptions} from '../getPairListener'
import {type IdPair, type PendingMutationsEvent, type ReconnectEvent} from '../types'
import {type VersionOriginTypes} from './operations'
import {actionsApiClient} from './utils/actionsApiClient'

const isMutationEventForDocId =
Expand All @@ -28,7 +28,7 @@ const isMutationEventForDocId =
/**
* @hidden
* @beta */
export type WithVersion<T> = T & {version: VersionOriginTypes}
export type WithVersion<T> = T & {version: DocumentVariantType}

/**
* @hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export interface Operation<ExtraArgs extends any[] = [], ErrorStrings extends st
type GuardedOperation = Operation<any[], 'NOT_READY'>
type Patch = any

/** @beta */
export type VersionOriginTypes = 'published' | 'draft' | 'version'

/** @internal */
// Note: Changing this interface in a backwards incompatible manner will be a breaking change
export interface OperationsAPI {
Expand Down
23 changes: 23 additions & 0 deletions packages/sanity/src/core/util/getDocumentVariantType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {isDraftId, isVersionId} from './draftUtils'

/**
* Indicates the type of document variant, either `draft`, `version` or `published`.
* Draft documents are prefixed with `drafts.`.
* Version documents are prefixed with `versions.<versionName>`
* The rest are considered published documents.
* @beta
*/
export type DocumentVariantType = 'draft' | 'version' | 'published'

/**
* Takes a document id and returns the variant type for that document
* If it's a document that starts with `version.` it's a `version` document.
* If it's a document that starts with `drafts.` it's a `draft` document.
* Otherwise, it's a `published` document.
* @beta
* */
export function getDocumentVariantType(documentId: string): DocumentVariantType {
if (isDraftId(documentId)) return 'draft'
if (isVersionId(documentId)) return 'version'
return 'published'
}
2 changes: 1 addition & 1 deletion test/e2e/tests/navbar/appearanceMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('color scheme changes and persists', async ({page, baseURL}) => {
await page.goto(baseURL ?? '/test/content')

await page.locator(`[id='user-menu']`).click()
await expect(await page.getByTestId('user-menu')).toBeVisible()
await expect(await page.getByTestId('user-menu')).toBeVisible({timeout: 4000})
await expect(await page.getByTestId('color-scheme-dark')).toBeVisible()
await page.getByTestId('color-scheme-dark').click()

Expand Down

0 comments on commit 5deba91

Please sign in to comment.