From 4053cca39937667da746f983d58ef9313c8bdd5b Mon Sep 17 00:00:00 2001 From: Mike Watson Date: Fri, 15 Nov 2024 12:36:19 -0500 Subject: [PATCH] Jetpack AI: Only display "turn list into table" menu item for top level lists (#40177) * Jetpack AI: Only display "turn list into table" menu item for top level lists * changelog * Adding clientId prop to AiAssistantToolbarDropdownContentProps * Last fix for the js tests thinking getBlockParents doesn't exist (even though the code works?) --- ...ge-jetpack-ai-list-to-table-top-level-only | 4 ++ .../dropdown-content.tsx | 50 ++++++++++++------- .../ai-assistant-toolbar-dropdown/index.tsx | 3 ++ .../ai-assistant-toolbar-dropdown/index.tsx | 3 ++ 4 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/change-jetpack-ai-list-to-table-top-level-only diff --git a/projects/plugins/jetpack/changelog/change-jetpack-ai-list-to-table-top-level-only b/projects/plugins/jetpack/changelog/change-jetpack-ai-list-to-table-top-level-only new file mode 100644 index 0000000000000..305409b4615a8 --- /dev/null +++ b/projects/plugins/jetpack/changelog/change-jetpack-ai-list-to-table-top-level-only @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Jetpack AI: Only display "turn list into table" menu item for top level lists diff --git a/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/dropdown-content.tsx b/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/dropdown-content.tsx index 51bd269c5f849..85559ab42c041 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/dropdown-content.tsx +++ b/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/dropdown-content.tsx @@ -3,6 +3,7 @@ */ import { aiAssistantIcon } from '@automattic/jetpack-ai-client'; import { MenuItem, MenuGroup, Notice } from '@wordpress/components'; +import { select } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { post, postContent, postExcerpt, termDescription, blockTable } from '@wordpress/icons'; import React from 'react'; @@ -160,6 +161,7 @@ if ( getFeatureAvailability( 'ai-list-to-table-transform' ) ) { options: { userPrompt: 'make a table from this list, do not enclose the response in a code block', alwaysTransformToAIAssistant: true, + rootParentOnly: true, }, } ); } @@ -169,6 +171,7 @@ export type AiAssistantDropdownOnChangeOptionsArgProps = { language?: string; userPrompt?: string; alwaysTransformToAIAssistant?: boolean; + rootParentOnly?: boolean; }; export type OnRequestSuggestion = ( @@ -182,6 +185,7 @@ type AiAssistantToolbarDropdownContentProps = { disabled?: boolean; onAskAiAssistant: () => void; onRequestSuggestion: OnRequestSuggestion; + clientId: string; }; /** @@ -191,12 +195,18 @@ type AiAssistantToolbarDropdownContentProps = { */ export default function AiAssistantToolbarDropdownContent( { blockType, + clientId, disabled = false, onAskAiAssistant, onRequestSuggestion, }: AiAssistantToolbarDropdownContentProps ): ReactElement { const blockQuickActions = quickActionsList[ blockType ] ?? []; + const { getBlockParents } = select( 'core/block-editor' ) as unknown as { + getBlockParents: ( blockId: string ) => string[]; + }; + const blockParents = getBlockParents( clientId ); + return ( <> { disabled && ( @@ -218,23 +228,29 @@ export default function AiAssistantToolbarDropdownContent( { - { [ ...quickActionsList.default, ...blockQuickActions ].map( quickAction => ( - { - onRequestSuggestion( - quickAction.aiSuggestion, - { ...( quickAction.options ?? {} ) }, - quickAction.name - ); - } } - disabled={ disabled } - > -
{ quickAction.name }
-
- ) ) } + { [ ...quickActionsList.default, ...blockQuickActions ] + .filter( + quickAction => ! ( quickAction.options?.rootParentOnly && blockParents.length > 0 ) + ) + .map( quickAction => { + return ( + { + onRequestSuggestion( + quickAction.aiSuggestion, + { ...( quickAction.options ?? {} ) }, + quickAction.name + ); + } } + disabled={ disabled } + > +
{ quickAction.name }
+
+ ); + } ) } { diff --git a/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/index.tsx b/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/index.tsx index b21e4768532e3..8f30ed0e5f9c4 100644 --- a/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/index.tsx +++ b/projects/plugins/jetpack/extensions/blocks/ai-assistant/components/ai-assistant-toolbar-dropdown/index.tsx @@ -144,9 +144,12 @@ function AiAssistantBlockToolbarDropdownContent( { tracks.recordEvent( 'jetpack_ai_assistant_prompt_show', { block_type: blockType } ); }; + const [ clientId ] = getSelectedBlockClientIds(); + return (