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

feat(i18n): use i18n strings in discard changes action #4965

Closed
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions dev/test-studio/plugins/locale-no-nb/bundles/desk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ const deskResources: Record<DeskLocaleResourceKeys, string> = {

/** Label for the "Delete" document action while the document is being deleted */
'action.delete.running.label': 'Sletter…',

/** --- DISCARD CHANGES ACTION --- **/
/** Tooltip when discard changes button is disabled because the document has no unpublished changes */
'action.discardChanges.disabled.noChange':
'Dette dokumentet har ingen endringer siden sist publisert',
/** Tooltip when delete button is disabled because the document is not published */
'action.discardChanges.disabled.notPublished': 'Dette dokumentet er ikke publisert',
/** Tooltip when delete button is disabled because the operation is not ready */
'action.discardChanges.disabled.notReady': 'Operasjonen er ikke klar',
/** Label for the "Discard changes" document action */
'action.discardChanges.label': 'Forkast endringer',
/** Message prompting the user to confirm discarding changes */
'action.discardChanges.confirmDialog.confirmDiscardChanges':
'Er du sikker på at du vil forkaste alle endringer siden forrige gang dette dokumentet ble publisert?',
}

export default deskResources
26 changes: 14 additions & 12 deletions packages/sanity/src/desk/documentActions/DiscardChangesAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@

import {ResetIcon} from '@sanity/icons'
import React, {useCallback, useMemo, useState} from 'react'
import {deskLocaleNamespace} from '../i18n'
import {
DocumentActionComponent,
DocumentActionDialogProps,
InsufficientPermissionsMessage,
useCurrentUser,
useDocumentOperation,
useDocumentPairPermissions,
useTranslation,
} from 'sanity'

const DISABLED_REASON_TITLE = {
NO_CHANGES: 'This document has no unpublished changes',
NOT_PUBLISHED: 'This document is not published',
}
const DISABLED_REASON_KEY = {
NO_CHANGES: 'action.discardChanges.disabled.noChanges',
NOT_PUBLISHED: 'action.discardChanges.disabled.notPublished',
NOT_READY: 'action.discardChanges.disabled.notReady',
} as const

/** @internal */
export const DiscardChangesAction: DocumentActionComponent = ({
Expand All @@ -33,6 +36,8 @@ export const DiscardChangesAction: DocumentActionComponent = ({
})
const currentUser = useCurrentUser()

const {t} = useTranslation(deskLocaleNamespace)

const handleConfirm = useCallback(() => {
discardChanges.execute()
onComplete()
Expand All @@ -49,9 +54,9 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
onCancel: onComplete,
onConfirm: handleConfirm,
message: <>Are you sure you want to discard all changes since last published?</>,
message: t('action.discardChanges.confirmDialog.confirmDiscardChanges'),
},
[handleConfirm, isConfirmDialogOpen, onComplete],
[handleConfirm, isConfirmDialogOpen, onComplete, t],
)

if (!published || liveEdit) {
Expand All @@ -63,7 +68,7 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
icon: ResetIcon,
disabled: true,
label: 'Discard changes',
label: t('action.discardChanges.label'),
title: (
<InsufficientPermissionsMessage
operationLabel="discard changes in this document"
Expand All @@ -77,11 +82,8 @@ export const DiscardChangesAction: DocumentActionComponent = ({
tone: 'critical',
icon: ResetIcon,
disabled: Boolean(discardChanges.disabled) || isPermissionsLoading,
title:
(discardChanges.disabled &&
DISABLED_REASON_TITLE[discardChanges.disabled as keyof typeof DISABLED_REASON_TITLE]) ||
'',
label: 'Discard changes',
title: (discardChanges.disabled && DISABLED_REASON_KEY[discardChanges.disabled]) || '',
label: t('action.discardChanges.label'),
onHandle: handle,
dialog,
}
Expand Down
13 changes: 13 additions & 0 deletions packages/sanity/src/desk/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ const deskLocaleStrings = {

/** Label for the "Delete" document action while the document is being deleted */
'action.delete.running.label': 'Deleting…',

/** --- DISCARD CHANGES ACTION --- **/
/** Tooltip when discard changes button is disabled because the document has no unpublished changes */
'action.discardChanges.disabled.noChange': 'This document has no unpublished changes',
/** Tooltip when delete button is disabled because the document is not published */
'action.discardChanges.disabled.notPublished': 'This document is not published',
/** Tooltip when delete button is disabled because the operation is not ready */
'action.discardChanges.disabled.notReady': 'Operation not ready',
/** Label for the "Discard changes" document action */
'action.discardChanges.label': 'Discard changes',
/** Message prompting the user to confirm discarding changes */
'action.discardChanges.confirmDialog.confirmDiscardChanges':
'Are you sure you want to discard all changes since last published?',
}

/**
Expand Down