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(releases): use actions API for (most of) release and versions #7720

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const defaultWorkspace = {
// eslint-disable-next-line camelcase
__internal_serverDocumentActions: {
// TODO: Switched off because Actions API doesn't support versions (yet).
enabled: false,
enabled: true,
},
scheduledPublishing: {
enabled: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {defineEvent} from '@sanity/telemetry'

import {type VersionOriginTypes} from '../../store'

interface VersionInfo {
/**
* document type that was added
*/
schemaType: string

/**
* the origin of the version created (from a draft or from a version)
*/
documentOrigin: 'draft' | 'version'
documentOrigin: VersionOriginTypes
}

export interface OriginInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import {LoadingBlock} from '../../../components'
import {useSchema} from '../../../hooks'
import {useTranslation} from '../../../i18n'
import {Preview} from '../../../preview'
import {useVersionOperations} from '../../hooks'
import {type ReleaseDocument} from '../../../store'
import {usePerspective, useVersionOperations} from '../../hooks'
import {releasesLocaleNamespace} from '../../i18n'
import {getBundleIdFromReleaseDocumentId} from '../../util/getBundleIdFromReleaseDocumentId'

/**
* @internal
Expand All @@ -20,7 +22,8 @@ export function DiscardVersionDialog(props: {
const {onClose, documentId, documentType} = props
const {t} = useTranslation(releasesLocaleNamespace)

const {discardVersion} = useVersionOperations(documentId, documentType)
const {currentGlobalBundle} = usePerspective()
const {discardVersion} = useVersionOperations()
const schema = useSchema()
const [isDiscarding, setIsDiscarding] = useState(false)

Expand All @@ -29,11 +32,14 @@ export function DiscardVersionDialog(props: {
const handleDiscardVersion = useCallback(async () => {
setIsDiscarding(true)

discardVersion()
await discardVersion(
getBundleIdFromReleaseDocumentId((currentGlobalBundle as ReleaseDocument)._id),
documentId,
)
setIsDiscarding(false)

onClose()
}, [discardVersion, onClose])
}, [currentGlobalBundle, discardVersion, documentId, onClose])

return (
<Dialog
Expand Down
24 changes: 16 additions & 8 deletions packages/sanity/src/core/releases/hooks/usePerspective.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export interface PerspectiveValue {
/* Change the perspective in the studio based on the perspective name */
setPerspective: (perspectiveId: string) => void
/* change the perspective in the studio based on a release ID */
setPerspectiveFromRelease: (releaseId: string) => void
setPerspectiveFromReleaseDocumentId: (releaseDocumentId: string) => void
setPerspectiveFromReleaseId: (releaseId: string) => void
/* Add/remove excluded perspectives */
toggleExcludedPerspective: (perspectiveId: string) => void
/* Check if a perspective is excluded */
Expand Down Expand Up @@ -94,11 +95,16 @@ export function usePerspective(): PerspectiveValue {
[perspective, selectedBundle],
)

const setPerspectiveFromRelease = useCallback(
(releaseId: string) => setPerspective(getBundleIdFromReleaseDocumentId(releaseId)),
const setPerspectiveFromReleaseId = useCallback(
(releaseId: string) => setPerspective(releaseId),
[setPerspective],
)

const setPerspectiveFromReleaseDocumentId = useCallback(
(releaseId: string) => setPerspectiveFromReleaseId(getBundleIdFromReleaseDocumentId(releaseId)),
[setPerspectiveFromReleaseId],
)

const bundlesPerspective = useMemo(
() =>
getReleasesPerspective({
Expand Down Expand Up @@ -142,7 +148,8 @@ export function usePerspective(): PerspectiveValue {
perspective,
excludedPerspectives,
setPerspective,
setPerspectiveFromRelease,
setPerspectiveFromReleaseDocumentId: setPerspectiveFromReleaseDocumentId,
setPerspectiveFromReleaseId: setPerspectiveFromReleaseId,
toggleExcludedPerspective,
currentGlobalBundle,
currentGlobalBundleId: isPublishedPerspective(currentGlobalBundle)
Expand All @@ -154,12 +161,13 @@ export function usePerspective(): PerspectiveValue {
[
perspective,
excludedPerspectives,
bundlesPerspective,
currentGlobalBundle,
isPerspectiveExcluded,
setPerspective,
setPerspectiveFromRelease,
setPerspectiveFromReleaseDocumentId,
setPerspectiveFromReleaseId,
toggleExcludedPerspective,
currentGlobalBundle,
bundlesPerspective,
isPerspectiveExcluded,
],
)
}
73 changes: 26 additions & 47 deletions packages/sanity/src/core/releases/hooks/useVersionOperations.tsx
Original file line number Diff line number Diff line change
@@ -1,67 +1,45 @@
import {useTelemetry} from '@sanity/telemetry/react'
import {useToast} from '@sanity/ui'
import {filter, firstValueFrom} from 'rxjs'

import {useClient, useDocumentOperation} from '../../hooks'
import {Translate, useTranslation} from '../../i18n'
import {useDocumentStore} from '../../store'
import {DEFAULT_STUDIO_CLIENT_OPTIONS} from '../../studioClient'
import {getPublishedId, getVersionFromId, getVersionId} from '../../util'
import {useReleaseOperations} from '../../store'
import {AddedVersion} from '../__telemetry__/releases.telemetry'
import {getBundleIdFromReleaseDocumentId} from '../util/getBundleIdFromReleaseDocumentId'
import {getCreateVersionOrigin} from '../util/util'
import {usePerspective} from './usePerspective'

/** @internal */
export function useVersionOperations(
documentId: string,
documentType: string,
): {
createVersion: (releaseId: string) => void
discardVersion: () => void
export function useVersionOperations(): {
createVersion: (releaseId: string, documentId: string) => Promise<void>
discardVersion: (releaseId: string, documentId: string) => Promise<void>
} {
const publishedId = getPublishedId(documentId)

const documentStore = useDocumentStore()
const telemetry = useTelemetry()
const {createVersion} = useDocumentOperation(
publishedId,
documentType,
getVersionFromId(documentId),
)
const {setPerspectiveFromRelease, setPerspective} = usePerspective()
const {createVersion, discardVersion} = useReleaseOperations()

const {setPerspectiveFromReleaseId} = usePerspective()
const toast = useToast()
const client = useClient(DEFAULT_STUDIO_CLIENT_OPTIONS)
const {t} = useTranslation()
const {currentGlobalBundle} = usePerspective()

const handleCreateVersion = async (releaseId: string) => {
// set up the listener before executing to make sure it's successful
const createVersionSuccess = firstValueFrom(
documentStore.pair
.operationEvents(getPublishedId(documentId), documentType)
.pipe(filter((e) => e.op === 'createVersion' && e.type === 'success')),
)

const docId = getVersionId(publishedId, getBundleIdFromReleaseDocumentId(releaseId))

const handleCreateVersion = async (releaseId: string, documentId: string) => {
const origin = getCreateVersionOrigin(documentId)
createVersion.execute(docId, origin)

// only change if the version was created successfully
await createVersionSuccess
setPerspectiveFromRelease(releaseId)

telemetry.log(AddedVersion, {
schemaType: documentType,
documentOrigin: origin,
})
try {
await createVersion(releaseId, documentId)
setPerspectiveFromReleaseId(releaseId)
telemetry.log(AddedVersion, {
documentOrigin: origin,
})
} catch (err) {
toast.push({
closable: true,
status: 'error',
title: t('release.action.create-version.failure'),
description: err.message,
})
}
}

const handleDiscardVersion = async () => {
const handleDiscardVersion = async (releaseId: string, documentId: string) => {
try {
/** @todo eventually change this from using document operations */
await client.delete(documentId)
await discardVersion(releaseId, documentId)

toast.push({
closable: true,
Expand All @@ -74,11 +52,12 @@ export function useVersionOperations(
/>
),
})
} catch (e) {
} catch (err) {
toast.push({
closable: true,
status: 'error',
title: t('release.action.discard-version.failure'),
description: err.message,
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const GlobalPerspectiveMenuItem = forwardRef<
const {release, rangePosition} = props
const {
currentGlobalBundleId,
setPerspectiveFromRelease,
setPerspectiveFromReleaseDocumentId,
setPerspective,
toggleExcludedPerspective,
isPerspectiveExcluded,
Expand Down Expand Up @@ -124,8 +124,8 @@ export const GlobalPerspectiveMenuItem = forwardRef<
() =>
isReleasePublishedPerspective
? setPerspective(releaseId)
: setPerspectiveFromRelease(releaseId),
[releaseId, isReleasePublishedPerspective, setPerspective, setPerspectiveFromRelease],
: setPerspectiveFromReleaseDocumentId(releaseId),
[releaseId, isReleasePublishedPerspective, setPerspective, setPerspectiveFromReleaseDocumentId],
)

const canReleaseBeExcluded = !isPublishedPerspective(release) && inRange && !last
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export function AddDocumentSearch({
const telemetry = useTelemetry()

const addDocument = useCallback(
(item: Pick<SanityDocumentLike, '_id' | '_type'>) => {
async (item: Pick<SanityDocumentLike, '_id' | '_type'>) => {
try {
createVersion(item._id, getBundleIdFromReleaseDocumentId(releaseId))
await createVersion(getBundleIdFromReleaseDocumentId(releaseId), item._id)

toast.push({
closable: true,
Expand All @@ -37,7 +37,6 @@ export function AddDocumentSearch({
const origin = getCreateVersionOrigin(item._id)

telemetry.log(AddedVersion, {
schemaType: item._type,
documentOrigin: origin,
})
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ export function ReleaseDashboardDetails({release}: {release: ReleaseDocument}) {

const {t: tRelease} = useTranslation(releasesLocaleNamespace)

const {currentGlobalBundleId, setPerspective, setPerspectiveFromRelease} = usePerspective()
const {currentGlobalBundleId, setPerspective, setPerspectiveFromReleaseDocumentId} =
usePerspective()

const handlePinRelease = useCallback(() => {
if (_id === currentGlobalBundleId) {
setPerspective('drafts')
} else {
setPerspectiveFromRelease(_id)
setPerspectiveFromReleaseDocumentId(_id)
}
}, [_id, currentGlobalBundleId, setPerspective, setPerspectiveFromRelease])
}, [_id, currentGlobalBundleId, setPerspective, setPerspectiveFromReleaseDocumentId])

return (
<Container width={3}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@ const ReleaseNameCell: Column<TableRelease>['cell'] = ({cellProps, datum: releas
const router = useRouter()
const {t} = useTranslation(releasesLocaleNamespace)
const {t: tCore} = useTranslation()
const {currentGlobalBundleId, setPerspective, setPerspectiveFromRelease} = usePerspective()
const {currentGlobalBundleId, setPerspective, setPerspectiveFromReleaseDocumentId} =
usePerspective()
const {state, _id} = release
const isArchived = state === 'archived'

const handlePinRelease = useCallback(() => {
if (_id === currentGlobalBundleId) {
setPerspective('drafts')
} else {
setPerspectiveFromRelease(_id)
setPerspectiveFromReleaseDocumentId(_id)
}
}, [_id, currentGlobalBundleId, setPerspective, setPerspectiveFromRelease])
}, [_id, currentGlobalBundleId, setPerspective, setPerspectiveFromReleaseDocumentId])

const cardProps: TableRowProps = release.isDeleted
? {tone: 'transparent'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {consistencyStatus} from './consistencyStatus'
import {operationArgs} from './operationArgs'
import {type OperationArgs, type OperationsAPI} from './operations'
import {commit} from './operations/commit'
import {createVersion} from './operations/createVersion'
import {del} from './operations/delete'
import {discardChanges} from './operations/discardChanges'
import {duplicate} from './operations/duplicate'
Expand Down Expand Up @@ -62,7 +61,6 @@ const operationImpls = {
unpublish,
duplicate,
restore,
createVersion,
} as const

//as we add server operations one by one, we can add them here
Expand Down

This file was deleted.

Loading
Loading