Skip to content

Commit

Permalink
feat(sanity): add always present document action to copy url to clipb…
Browse files Browse the repository at this point in the history
…oard (#7416)

* feat: add always present document action to copy url to clipboard

* chore: add comment to menuitems

* feat: add check for navigator presence

* feat: add tracking for copy document URL

* fix: update to use intent URL instead of just current URL

* feat: resolve intent link on demand

* revert: go back to just copying current URL

* chore: add comment explanation
  • Loading branch information
drewlyton authored Aug 26, 2024
1 parent 17a7b1d commit 7afcdb4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/sanity/src/structure/i18n/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {defineLocalesResources} from 'sanity'
* @internal
*/
const structureLocaleStrings = defineLocalesResources('structure', {
/** Label for the "Copy Document URL" document action */
'action.copy-document-url.label': 'Copy Document URL',
/** Tooltip when action button is disabled because the operation is not ready */
'action.delete.disabled.not-ready': 'Operation not ready',
/** Tooltip when action button is disabled because the document does not exist */
Expand Down Expand Up @@ -349,6 +351,8 @@ const structureLocaleStrings = defineLocalesResources('structure', {
/** The text when a generic operation succeeded (fallback, generally not shown) */
'panes.document-operation-results.operation-success':
'Successfully performed {{context}} on document',
/** The text when copy URL operation succeeded */
'panes.document-operation-results.operation-success_copy-url': 'Document URL copied to clipboard',
/** The text when a delete operation succeeded */
'panes.document-operation-results.operation-success_delete':
'The document was successfully deleted',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable camelcase */
import {isActionEnabled} from '@sanity/schema/_internal'
import {useTelemetry} from '@sanity/telemetry/react'
import {
type ObjectSchemaType,
type Path,
Expand Down Expand Up @@ -47,6 +48,7 @@ import {usePaneRouter} from '../../components'
import {structureLocaleNamespace} from '../../i18n'
import {type PaneMenuItem} from '../../types'
import {useStructureTool} from '../../useStructureTool'
import {DocumentURLCopied} from './__telemetry__'
import {
DEFAULT_MENU_ITEM_GROUPS,
EMPTY_PARAMS,
Expand Down Expand Up @@ -400,13 +402,30 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
[inspectOpen, params, setPaneParams],
)

const telemetry = useTelemetry()

const handleMenuAction = useCallback(
(item: PaneMenuItem) => {
if (item.action === 'production-preview' && previewUrl) {
window.open(previewUrl)
return true
}

if (item.action === 'copy-document-url' && navigator) {
telemetry.log(DocumentURLCopied)
// Chose to copy the user's current URL instead of
// the document's edit intent link because
// of bugs when resolving a document that has
// multiple access paths within Structure
navigator.clipboard.writeText(window.location.toString())
pushToast({
id: 'copy-document-url',
status: 'info',
title: t('panes.document-operation-results.operation-success_copy-url'),
})
return true
}

if (item.action === 'inspect') {
toggleLegacyInspect(true)
return true
Expand Down Expand Up @@ -434,13 +453,16 @@ export const DocumentPaneProvider = memo((props: DocumentPaneProviderProps) => {
return false
},
[
t,
closeInspector,
handleHistoryOpen,
inspectorName,
inspectors,
openInspector,
previewUrl,
toggleLegacyInspect,
pushToast,
telemetry,
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {defineEvent} from '@sanity/telemetry'

/**
* @internal
*/
export const DocumentURLCopied = defineEvent({
name: 'DocumentURLCopied',
version: 1,
description: 'User copied document URL to clipboard',
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './documentPanes.telemetry'
9 changes: 8 additions & 1 deletion packages/sanity/src/structure/panes/document/menuItems.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {EarthAmericasIcon, JsonIcon} from '@sanity/icons'
import {EarthAmericasIcon, JsonIcon, LinkIcon} from '@sanity/icons'
import {type DocumentInspector, type DocumentInspectorMenuItem, type TFunction} from 'sanity'

import {type PaneMenuItem, type StructureToolFeatures} from '../../types'
Expand Down Expand Up @@ -72,6 +72,13 @@ export function getMenuItems(params: GetMenuItemsParams): PaneMenuItem[] {
].filter(Boolean) as PaneMenuItem[]

return [
// Always present document menu item to copy current url to clipboard
{
action: 'copy-document-url',
showAsAction: true,
title: params.t('action.copy-document-url.label'),
icon: LinkIcon,
},
...inspectorItems,

// TODO: convert to inspector or document view?
Expand Down

0 comments on commit 7afcdb4

Please sign in to comment.