Skip to content

Commit

Permalink
feat(sanity): add "Compare versions" document action to releases plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
juice49 committed Jan 9, 2025
1 parent 1d9fd43 commit 413c6f4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<DocumentActionProps> = ({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],
)
}
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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,
]
}

0 comments on commit 413c6f4

Please sign in to comment.