From 819138fa88934d40a16b46a069703fb0879a1290 Mon Sep 17 00:00:00 2001 From: Renato Augusto Gama dos Santos Date: Tue, 30 Jul 2024 17:56:39 -0300 Subject: [PATCH] AI Proofread: Fix suggestions invalidation from different features (#38633) * AI Proofread: Add ignore button * AI Proofread: Mark suggestion ignored on store * AI Proofread: Use block id as high level of the reducer structure * changelog --- .../changelog/fix-invalidate-suggestion | 4 ++ .../components/breve/highlight/index.tsx | 12 ++--- .../components/breve/store/actions.ts | 9 ++-- .../components/breve/store/reducer.ts | 45 ++++++++----------- .../components/breve/store/selectors.ts | 15 +++---- .../components/breve/types.ts | 16 +++---- 6 files changed, 42 insertions(+), 59 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/fix-invalidate-suggestion diff --git a/projects/plugins/jetpack/changelog/fix-invalidate-suggestion b/projects/plugins/jetpack/changelog/fix-invalidate-suggestion new file mode 100644 index 0000000000000..dd93c71b86707 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-invalidate-suggestion @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Fix suggestion invalidation on different features diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/highlight/index.tsx b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/highlight/index.tsx index 9cf54fc0339b1..1046882320737 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/highlight/index.tsx +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/highlight/index.tsx @@ -186,7 +186,7 @@ export default function Highlight() { } const [ newBlock ] = rawHandler( { HTML: render } ); - invalidateSuggestions( feature, blockId ); + invalidateSuggestions( blockId ); updateBlockAttributes( blockId, newBlock.attributes ); setPopoverHover( false ); @@ -198,7 +198,7 @@ export default function Highlight() { }; const handleIgnoreSuggestion = () => { - ignoreSuggestion( feature, blockId, id ); + ignoreSuggestion( blockId, id ); setPopoverHover( false ); tracks.recordEvent( 'jetpack_ai_breve_ignore', { feature: BREVE_FEATURE_NAME, @@ -288,7 +288,7 @@ export function registerBreveHighlights() { return { isProofreadEnabled: isProofreadEnabled(), isFeatureEnabled: isFeatureEnabled( config.name ), - ignored: getIgnoredSuggestions( { feature: config.name, blockId: blockClientId } ), + ignored: getIgnoredSuggestions( { blockId: blockClientId } ), }; }, __experimentalCreatePrepareEditableTree( @@ -310,7 +310,7 @@ export function registerBreveHighlights() { // Has to be defined here, as adding it to __experimentalGetPropsForEditableTreePreparation // causes an issue with the block inserter. ref p1721746774569699-slack-C054LN8RNVA - const currentMd5 = getBlockMd5( formatName, blockClientId ); + const currentMd5 = getBlockMd5( blockClientId ); if ( text && isProofreadEnabled && isFeatureEnabled ) { const block = getBlock( blockClientId ); @@ -320,8 +320,8 @@ export function registerBreveHighlights() { if ( currentMd5 !== textMd5 ) { ignoredList = []; - invalidateSuggestions( type, blockClientId ); - setBlockMd5( type, blockClientId, textMd5 ); + invalidateSuggestions( blockClientId ); + setBlockMd5( blockClientId, textMd5 ); } const highlights = featureHighlight( text ); diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/actions.ts b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/actions.ts index bbe40db68d434..96e4414be0666 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/actions.ts +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/actions.ts @@ -49,27 +49,24 @@ export function toggleFeature( feature: string, force?: boolean ) { }; } -export function setBlockMd5( feature: string, blockId: string, md5: string ) { +export function setBlockMd5( blockId: string, md5: string ) { return { type: 'SET_BLOCK_MD5', - feature, blockId, md5, }; } -export function invalidateSuggestions( feature: string, blockId: string ) { +export function invalidateSuggestions( blockId: string ) { return { type: 'INVALIDATE_SUGGESTIONS', - feature, blockId, }; } -export function ignoreSuggestion( feature: string, blockId: string, id: string ) { +export function ignoreSuggestion( blockId: string, id: string ) { return { type: 'IGNORE_SUGGESTION', - feature, blockId, id, }; diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/reducer.ts b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/reducer.ts index 922b91e9a9ae7..ccb47dab95e46 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/reducer.ts +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/reducer.ts @@ -107,7 +107,7 @@ export function suggestions( action: { type: string; id: string; - feature: string; + feature?: string; blockId: string; loading: boolean; md5?: string; @@ -119,17 +119,17 @@ export function suggestions( ) { const { id, feature, blockId } = action ?? {}; const current = { ...state }; - const currentBlock = current?.[ feature ]?.[ blockId ] ?? {}; - const currentItem = current?.[ feature ]?.[ blockId ]?.[ id ] || {}; + const currentBlock = current?.[ blockId ] ?? {}; + const currentItem = current?.[ blockId ]?.[ feature ]?.[ id ] || {}; switch ( action.type ) { case 'SET_SUGGESTIONS_LOADING': { return { ...current, - [ feature ]: { - ...( current[ feature ] ?? {} ), - [ blockId ]: { - ...currentBlock, + [ blockId ]: { + ...currentBlock, + [ feature ]: { + ...( currentBlock[ feature ] ?? {} ), [ id ]: { ...currentItem, loading: action.loading, @@ -142,10 +142,10 @@ export function suggestions( case 'SET_SUGGESTIONS': { return { ...current, - [ feature ]: { - ...( current[ feature ] ?? {} ), - [ blockId ]: { - ...currentBlock, + [ blockId ]: { + ...currentBlock, + [ feature ]: { + ...( currentBlock[ feature ] ?? {} ), [ id ]: { ...currentItem, loading: false, @@ -159,12 +159,9 @@ export function suggestions( case 'SET_BLOCK_MD5': { return { ...current, - [ feature ]: { - ...( current[ feature ] ?? {} ), - [ blockId ]: { - ...currentBlock, - md5: action.md5, - }, + [ blockId ]: { + md5: action.md5, + ...currentBlock, }, }; } @@ -172,22 +169,16 @@ export function suggestions( case 'INVALIDATE_SUGGESTIONS': { return { ...current, - [ feature ]: { - ...( current[ feature ] ?? {} ), - [ blockId ]: {}, - }, + [ blockId ]: {}, }; } case 'IGNORE_SUGGESTION': { return { ...current, - [ feature ]: { - ...( current[ feature ] ?? {} ), - [ blockId ]: { - ...currentBlock, - ignored: [ ...( currentBlock.ignored ?? [] ), id ], - }, + [ blockId ]: { + ...currentBlock, + ignored: [ ...( currentBlock.ignored ?? [] ), id ], }, }; } diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/selectors.ts b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/selectors.ts index 3753f1cd89733..b3aa92fb4b2c3 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/selectors.ts +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/store/selectors.ts @@ -37,27 +37,24 @@ export function getDisabledFeatures( state: BreveState ) { // Suggestions -export function getBlockMd5( state: BreveState, feature: string, blockId: string ) { - return state.suggestions?.[ feature ]?.[ blockId ]?.md5 ?? ''; +export function getBlockMd5( state: BreveState, blockId: string ) { + return state.suggestions?.[ blockId ]?.md5 ?? ''; } export function getSuggestionsLoading( state: BreveState, { feature, id, blockId }: { feature: string; id: string; blockId: string } ) { - return state.suggestions?.[ feature ]?.[ blockId ]?.[ id ]?.loading; + return state.suggestions?.[ blockId ]?.[ feature ]?.[ id ]?.loading; } export function getSuggestions( state: BreveState, { feature, id, blockId }: { feature: string; id: string; blockId: string } ) { - return state.suggestions?.[ feature ]?.[ blockId ]?.[ id ]?.suggestions; + return state.suggestions?.[ blockId ]?.[ feature ]?.[ id ]?.suggestions; } -export function getIgnoredSuggestions( - state: BreveState, - { feature, blockId }: { feature: string; blockId: string } -) { - return state.suggestions?.[ feature ]?.[ blockId ]?.ignored; +export function getIgnoredSuggestions( state: BreveState, { blockId }: { blockId: string } ) { + return state.suggestions?.[ blockId ]?.ignored; } diff --git a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/types.ts b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/types.ts index 922b1d4807a80..4f2ec5d580bcb 100644 --- a/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/types.ts +++ b/projects/plugins/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/types.ts @@ -42,7 +42,7 @@ export type BreveSelect = { isProofreadEnabled: () => boolean; isFeatureEnabled: ( feature: string ) => boolean; getDisabledFeatures: () => Array< string >; - getBlockMd5: ( feature: string, blockId: string ) => string; + getBlockMd5: ( blockId: string ) => string; getSuggestionsLoading: ( { feature, id, @@ -64,13 +64,7 @@ export type BreveSelect = { html: string; suggestion: string; }; - getIgnoredSuggestions: ( { - feature, - blockId, - }: { - feature: string; - blockId: string; - } ) => Array< string >; + getIgnoredSuggestions: ( { blockId }: { blockId: string } ) => Array< string >; }; export type BreveDispatch = { @@ -79,9 +73,9 @@ export type BreveDispatch = { setPopoverAnchor: ( anchor: Anchor ) => void; toggleProofread: ( force?: boolean ) => void; toggleFeature: ( feature: string, force?: boolean ) => void; - invalidateSuggestions: ( feature: string, blockId: string ) => void; - ignoreSuggestion: ( feature: string, blockId: string, id: string ) => void; - setBlockMd5: ( feature: string, blockId: string, md5: string ) => void; + invalidateSuggestions: ( blockId: string ) => void; + ignoreSuggestion: ( blockId: string, id: string ) => void; + setBlockMd5: ( blockId: string, md5: string ) => void; setSuggestions: ( suggestions: { anchor: Anchor[ 'target' ]; id: string;