diff --git a/editor/store/reducer.js b/editor/store/reducer.js index ebfd05aaf27385..5f0776bbe65f38 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -12,6 +12,7 @@ import { first, last, omit, + omitBy, without, mapValues, findIndex, @@ -541,6 +542,16 @@ export function preferences( state = PREFERENCES_DEFAULTS, action ) { }, }; }, state ); + case 'REMOVE_REUSABLE_BLOCK': + const { id } = action; + return { + ...state, + recentInserts: reject( state.recentInserts, ( { ref } ) => ref === id ), + insertFrequency: omitBy( state.insertFrequency, ( frequency, key ) => { + const { ref } = JSON.parse( key ); + return ref === id; + } ), + }; case 'TOGGLE_FEATURE': return { ...state, diff --git a/editor/store/test/reducer.js b/editor/store/test/reducer.js index 07ccb650031682..f60011d37b12dd 100644 --- a/editor/store/test/reducer.js +++ b/editor/store/test/reducer.js @@ -1055,6 +1055,37 @@ describe( 'state', () => { } ); } ); + it( 'should remove usage stats for reusable blocks that are removed', () => { + const initialState = { + recentInserts: [ + { name: 'core/paragraph' }, + { name: 'core/block', ref: 123 }, + { name: 'core/block', ref: 456 }, + ], + insertFrequency: { + [ JSON.stringify( { name: 'core/paragraph' } ) ]: 4, + [ JSON.stringify( { name: 'core/block', ref: 123 } ) ]: 2, + [ JSON.stringify( { name: 'core/block', ref: 456 } ) ]: 2, + }, + }; + + const state = preferences( deepFreeze( initialState ), { + type: 'REMOVE_REUSABLE_BLOCK', + id: 123, + } ); + + expect( state ).toEqual( { + recentInserts: [ + { name: 'core/paragraph' }, + { name: 'core/block', ref: 456 }, + ], + insertFrequency: { + [ JSON.stringify( { name: 'core/paragraph' } ) ]: 4, + [ JSON.stringify( { name: 'core/block', ref: 456 } ) ]: 2, + }, + } ); + } ); + it( 'should toggle a feature flag', () => { const state = preferences( deepFreeze( { features: { chicken: true } } ), { type: 'TOGGLE_FEATURE',