Skip to content

Commit

Permalink
Migrate local storage data from interface to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Mar 16, 2022
1 parent da9898c commit da10f5c
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 1 deletion.
119 changes: 118 additions & 1 deletion packages/data/src/plugins/persistence/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { merge, isPlainObject } from 'lodash';
import { merge, isPlainObject, reduce } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -402,6 +402,122 @@ export function migrateThirdPartyFeaturePreferencesToPreferencesStore(
}
}

/**
* Migrates interface 'enableItems' data to the preferences store.
*
* The interface package stores this data in this format:
* ```js
* {
* enableItems: {
* singleEnableItems: {
* complementaryArea: {
* 'core/edit-post': 'edit-post/document',
* 'core/edit-site': 'edit-site/global-styles',
* }
* },
* multipleEnableItems: {
* pinnedItems: {
* 'core/edit-post': {
* 'plugin-1': true,
* },
* 'core/edit-site': {
* 'plugin-2': true,
* },
* },
* }
* }
* }
* ```
* and it should be migrated it to:
* ```js
* {
* 'core/edit-post': {
* complementaryArea: 'edit-post/document',
* pinnedItems: {
* 'plugin-1': true,
* },
* },
* 'core/edit-site': {
* complementaryArea: 'edit-site/global-styles',
* pinnedItems: {
* 'plugin-2': true,
* },
* },
* }
* ```
*
* @param {Object} persistence The persistence interface.
*/
export function migrateInterfaceEnableItemsToPreferencesStore( persistence ) {
const interfaceStoreName = 'core/interface';
const preferencesStoreName = 'core/preferences';
const state = persistence.get();
const sourceEnableItems = state[ interfaceStoreName ]?.enableItems;

// There's nothing to migrate, exit early.
if ( ! sourceEnableItems ) {
return;
}

const allPreferences = state[ preferencesStoreName ]?.preferences ?? {};

// First convert complementaryAreas into the right format.
// Use the existing preferences as the accumulator so that the data is
// merged.
const sourceComplementaryAreas =
sourceEnableItems?.singleEnableItems?.complementaryArea;
const convertedComplementaryAreas = reduce(
sourceComplementaryAreas,
( accumulator, data, scope ) => {
// Don't overwrite any existing data in the preferences store.
if ( accumulator[ scope ]?.complementaryArea ) {
return accumulator;
}

return {
...accumulator,
[ scope ]: {
...accumulator[ scope ],
complementaryArea: data,
},
};
},
allPreferences
);

// Next feed the converted complementary areas back into a reducer that
// converts the pinned items, resulting in the fully migrated data.
const sourcePinnedItems =
sourceEnableItems?.multipleEnableItems?.pinnedItems;
const allConvertedData = reduce(
sourcePinnedItems,
( accumulator, data, scope ) => {
// Don't overwrite any existing data in the preferences store.
if ( accumulator[ scope ]?.pinnedItems ) {
return accumulator;
}

return {
...accumulator,
[ scope ]: {
...accumulator[ scope ],
pinnedItems: data,
},
};
},
convertedComplementaryAreas
);

persistence.set( preferencesStoreName, {
preferences: allConvertedData,
} );

// Remove migrated preferences.
persistence.set( interfaceStoreName, {
enableItems: undefined,
} );
}

persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
const persistence = createPersistenceInterface( pluginOptions );

Expand Down Expand Up @@ -445,6 +561,7 @@ persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
'core/edit-site',
'editorMode'
);
migrateInterfaceEnableItemsToPreferencesStore( persistence );
};

export default persistencePlugin;
120 changes: 120 additions & 0 deletions packages/data/src/plugins/persistence/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import plugin, {
migrateFeaturePreferencesToPreferencesStore,
migrateThirdPartyFeaturePreferencesToPreferencesStore,
migrateIndividualPreferenceToPreferencesStore,
migrateInterfaceEnableItemsToPreferencesStore,
} from '../';
import objectStorage from '../storage/object';
import { createRegistry } from '../../../';
Expand Down Expand Up @@ -923,3 +924,122 @@ describe( 'migrateThirdPartyFeaturePreferencesToPreferencesStore', () => {
} );
} );
} );

describe( 'migrateInterfaceEnableItemsToPreferencesStore', () => {
it( 'migrates enableItems to the preferences store', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

persistenceInterface.set( 'core/interface', {
enableItems: {
singleEnableItems: {
complementaryArea: {
'core/edit-post': 'edit-post/document',
'core/edit-site': 'edit-site/global-styles',
},
},
multipleEnableItems: {
pinnedItems: {
'core/edit-post': {
'plugin-1': true,
},
'core/edit-site': {
'plugin-2': true,
},
},
},
},
} );

migrateInterfaceEnableItemsToPreferencesStore( persistenceInterface );

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/edit-post': {
complementaryArea: 'edit-post/document',
pinnedItems: {
'plugin-1': true,
},
},
'core/edit-site': {
complementaryArea: 'edit-site/global-styles',
pinnedItems: {
'plugin-2': true,
},
},
},
},
'core/interface': {
enableItems: undefined,
},
} );
} );

it( 'merges pinnedItems and complementaryAreas with existing preferences store data', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

persistenceInterface.set( 'core/interface', {
enableItems: {
singleEnableItems: {
complementaryArea: {
'core/edit-post': 'edit-post/document',
'core/edit-site': 'edit-site/global-styles',
},
},
multipleEnableItems: {
pinnedItems: {
'core/edit-post': {
'plugin-1': true,
},
'core/edit-site': {
'plugin-2': true,
},
},
},
},
} );

persistenceInterface.set( 'core/preferences', {
preferences: {
'core/edit-post': {
preferenceA: 1,
preferenceB: 2,
},
'core/edit-site': {
preferenceC: true,
},
},
} );

migrateInterfaceEnableItemsToPreferencesStore( persistenceInterface );

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/edit-post': {
preferenceA: 1,
preferenceB: 2,
complementaryArea: 'edit-post/document',
pinnedItems: {
'plugin-1': true,
},
},
'core/edit-site': {
preferenceC: true,
complementaryArea: 'edit-site/global-styles',
pinnedItems: {
'plugin-2': true,
},
},
},
},
'core/interface': {
enableItems: undefined,
},
} );
} );
} );

0 comments on commit da10f5c

Please sign in to comment.