mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-12 02:35:42 +00:00
fix(Map): Add front-end migrations for local store settings
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { MapUserSettingsStructure, MigrationStructure } from '@/hooks/Mapper/mapRootProvider/types.ts';
|
||||
import { getDefaultSettingsByType } from '@/hooks/Mapper/mapRootProvider/helpers/createDefaultWidgetSettings.ts';
|
||||
|
||||
const extractData = (localStoreKey = 'map-user-settings'): MapUserSettingsStructure | null => {
|
||||
const val = localStorage.getItem(localStoreKey);
|
||||
if (!val) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(val);
|
||||
};
|
||||
|
||||
export const applyMigrations = (
|
||||
mapId: string,
|
||||
migrations: MigrationStructure[],
|
||||
localStoreKey = 'map-user-settings',
|
||||
) => {
|
||||
const currentLSData = extractData(localStoreKey);
|
||||
|
||||
// INFO if we have NO any data in store expected that we will use default
|
||||
if (!currentLSData) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentMapSettings = currentLSData[mapId];
|
||||
|
||||
for (const migration of migrations) {
|
||||
const { to, run, type } = migration;
|
||||
const currentValue = currentMapSettings[type];
|
||||
|
||||
if (!currentValue) {
|
||||
currentMapSettings[type] = getDefaultSettingsByType(type);
|
||||
continue;
|
||||
}
|
||||
|
||||
// we skip if current version is older
|
||||
if (currentValue.version > to) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const next = run(currentValue.settings);
|
||||
currentMapSettings[type].version = to;
|
||||
currentMapSettings[type].settings = next;
|
||||
}
|
||||
|
||||
return currentLSData;
|
||||
};
|
||||
Reference in New Issue
Block a user