From 413c6f473b0c4801a5d9f5fb9368d53dbed089a6 Mon Sep 17 00:00:00 2001 From: Ash Date: Thu, 28 Nov 2024 16:05:48 +0000 Subject: [PATCH] feat(sanity): add "Compare versions" document action to releases plugin --- .../documentActions/CompareVersionsAction.tsx | 37 +++++++++++++++++++ .../releases/plugin/documentActions/index.ts | 11 ++++-- 2 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 packages/sanity/src/core/releases/plugin/documentActions/CompareVersionsAction.tsx diff --git a/packages/sanity/src/core/releases/plugin/documentActions/CompareVersionsAction.tsx b/packages/sanity/src/core/releases/plugin/documentActions/CompareVersionsAction.tsx new file mode 100644 index 00000000000..31e2a6d46e0 --- /dev/null +++ b/packages/sanity/src/core/releases/plugin/documentActions/CompareVersionsAction.tsx @@ -0,0 +1,37 @@ +import {TransferIcon} from '@sanity/icons' +import {useMemo} from 'react' + +import {type ActionComponent, type DocumentActionProps} from '../../../config' +import {useDiffViewRouter} from '../../../diffView/hooks/useDiffViewRouter' +import {useTranslation} from '../../../i18n' +import {usePerspective} from '../../hooks/usePerspective' +import {releasesLocaleNamespace} from '../../i18n' + +export const CompareVersionsAction: ActionComponent = ({type, version}) => { + const {t} = useTranslation(releasesLocaleNamespace) + const {navigateDiffView} = useDiffViewRouter() + const {perspectiveStack} = usePerspective() + const isEnabled = version !== null && perspectiveStack.length > 1 + + return useMemo( + () => ({ + icon: TransferIcon, + label: t('action.compare-versions'), + group: ['paneActions'], + disabled: !isEnabled, + onHandle: () => { + if (!isEnabled) { + return + } + navigateDiffView({ + mode: 'version', + nextDocument: { + type, + id: version._id, + }, + }) + }, + }), + [isEnabled, navigateDiffView, t, type, version?._id], + ) +} diff --git a/packages/sanity/src/core/releases/plugin/documentActions/index.ts b/packages/sanity/src/core/releases/plugin/documentActions/index.ts index fb981ed5008..68c0cdf508b 100644 --- a/packages/sanity/src/core/releases/plugin/documentActions/index.ts +++ b/packages/sanity/src/core/releases/plugin/documentActions/index.ts @@ -1,5 +1,6 @@ import {type DocumentActionComponent} from '../../../config/document/actions' import {type DocumentActionsContext} from '../../../config/types' +import {CompareVersionsAction} from './CompareVersionsAction' import {DiscardVersionAction} from './DiscardVersionAction' import {UnpublishVersionAction} from './UnpublishVersionAction' @@ -10,8 +11,10 @@ export default function resolveDocumentActions( context: DocumentActionsContext, ): Action[] { const duplicateAction = existingActions.filter(({name}) => name === 'DuplicateAction') - - return context.versionType === 'version' - ? duplicateAction.concat(DiscardVersionAction).concat(UnpublishVersionAction) - : existingActions + return [ + ...(context.versionType === 'version' + ? duplicateAction.concat(DiscardVersionAction).concat(UnpublishVersionAction) + : existingActions), + CompareVersionsAction, + ] }