From 5deba9112042ced0fe9b39401c7f9361a2bdbb3d Mon Sep 17 00:00:00 2001 From: Pedro Bonamin <46196328+pedrobonamin@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:05:25 +0100 Subject: [PATCH] fix(core): `VersionOriginType` to `DocumentVariantType` (#7924) --- .../__telemetry__/releases.telemetry.ts | 4 ++-- .../releases/hooks/useVersionOperations.tsx | 4 ++-- .../tool/detail/AddDocumentSearch.tsx | 4 ++-- .../sanity/src/core/releases/util/util.ts | 14 ----------- .../document/document-pair/checkoutPair.ts | 4 ++-- .../document-pair/operations/types.ts | 3 --- .../src/core/util/getDocumentVariantType.ts | 23 +++++++++++++++++++ test/e2e/tests/navbar/appearanceMenu.spec.ts | 2 +- 8 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 packages/sanity/src/core/util/getDocumentVariantType.ts diff --git a/packages/sanity/src/core/releases/__telemetry__/releases.telemetry.ts b/packages/sanity/src/core/releases/__telemetry__/releases.telemetry.ts index 5abdf62f2e0..2888a123dff 100644 --- a/packages/sanity/src/core/releases/__telemetry__/releases.telemetry.ts +++ b/packages/sanity/src/core/releases/__telemetry__/releases.telemetry.ts @@ -1,6 +1,6 @@ import {defineEvent} from '@sanity/telemetry' -import {type VersionOriginTypes} from '../index' +import {type DocumentVariantType} from '../../util/getDocumentVariantType' interface VersionInfo { /** @@ -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 { diff --git a/packages/sanity/src/core/releases/hooks/useVersionOperations.tsx b/packages/sanity/src/core/releases/hooks/useVersionOperations.tsx index 24b843c1591..a21c388c9ee 100644 --- a/packages/sanity/src/core/releases/hooks/useVersionOperations.tsx +++ b/packages/sanity/src/core/releases/hooks/useVersionOperations.tsx @@ -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 { @@ -30,7 +30,7 @@ export function useVersionOperations(): VersionOperationsValue { documentId: string, initialValue?: Record, ) => { - const origin = getCreateVersionOrigin(documentId) + const origin = getDocumentVariantType(documentId) try { await createVersion(releaseId, documentId, initialValue) setPerspectiveFromReleaseId(releaseId) diff --git a/packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx b/packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx index 933d2c74bcd..f554836b407 100644 --- a/packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx +++ b/packages/sanity/src/core/releases/tool/detail/AddDocumentSearch.tsx @@ -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({ @@ -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, diff --git a/packages/sanity/src/core/releases/util/util.ts b/packages/sanity/src/core/releases/util/util.ts index 11bca4577da..aac4a1342b0 100644 --- a/packages/sanity/src/core/releases/util/util.ts +++ b/packages/sanity/src/core/releases/util/util.ts @@ -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' @@ -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 diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts index 7cf4fb32efc..47333f273a6 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/checkoutPair.ts @@ -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, @@ -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 = @@ -28,7 +28,7 @@ const isMutationEventForDocId = /** * @hidden * @beta */ -export type WithVersion = T & {version: VersionOriginTypes} +export type WithVersion = T & {version: DocumentVariantType} /** * @hidden diff --git a/packages/sanity/src/core/store/_legacy/document/document-pair/operations/types.ts b/packages/sanity/src/core/store/_legacy/document/document-pair/operations/types.ts index f230502cf81..1d173a9d890 100644 --- a/packages/sanity/src/core/store/_legacy/document/document-pair/operations/types.ts +++ b/packages/sanity/src/core/store/_legacy/document/document-pair/operations/types.ts @@ -23,9 +23,6 @@ export interface Operation 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 { diff --git a/packages/sanity/src/core/util/getDocumentVariantType.ts b/packages/sanity/src/core/util/getDocumentVariantType.ts new file mode 100644 index 00000000000..26f4e656c89 --- /dev/null +++ b/packages/sanity/src/core/util/getDocumentVariantType.ts @@ -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.` + * 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' +} diff --git a/test/e2e/tests/navbar/appearanceMenu.spec.ts b/test/e2e/tests/navbar/appearanceMenu.spec.ts index 8a4aa2b9540..e9407fcf30f 100644 --- a/test/e2e/tests/navbar/appearanceMenu.spec.ts +++ b/test/e2e/tests/navbar/appearanceMenu.spec.ts @@ -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()