Skip to content

Commit

Permalink
TELESTION-442 Migration with deleted widget type
Browse files Browse the repository at this point in the history
Replaces widget instances of no-longer existing widgets with `"."` (i.e., empty spaces) in dashboard layouts during migration
  • Loading branch information
pklaschka committed Dec 31, 2023
1 parent 7424500 commit 4d9a5aa
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions frontend-react/src/lib/application/routes/migration/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,22 @@ export function migrationAction({
(entry): entry is [id: string, instance: WidgetInstance] => !!entry
);

const availableWidgetInstances = new Set(
newInstances.map(([widgetInstanceId]) => widgetInstanceId)
);

// filter dashboard layouts for no longer existing widgets
const newDashboards = Object.entries(oldUserData.dashboards).map(
dashboardTuple =>
migrateDashboardConfig(dashboardTuple, availableWidgetInstances)
);

setUserData({
...oldUserData,
version: version,
dashboards: {
...Object.fromEntries(newDashboards)
},
widgetInstances: {
...Object.fromEntries(newInstances)
}
Expand Down Expand Up @@ -131,6 +144,44 @@ async function parseUserData(
return userDataSchema.parse(JSON.parse(rawUserData));
}

/**
* Accepts a tuple of a dashboard id and the associated dashboard and performs a
* dashboard configuration migration by replacing all widget ids that are no
* longer registered with a dot (i.e., empty space).
* @param id - the id of the dashboard
* @param dashboard - the dashboard with the outdated widget ids
* @param availableWidgetInstances - a set of all available widget instance ids
* @returns a tuple of a dashboard with an up-to-date widget configuration
*/
function migrateDashboardConfig(
[id, dashboard]: [string, UserData['dashboards'][string]],
availableWidgetInstances: Set<string>
): [string, UserData['dashboards'][string]] {
return [
id,
{
...dashboard,
layout: dashboard.layout.map(row =>
row.map(widgetInstanceId => {
if (widgetInstanceId === '.') {
return '.';
}

if (!availableWidgetInstances.has(widgetInstanceId)) {
console.warn(
`Widget with id "${widgetInstanceId}" not found. Replacing with "."`
);

return '.';
}

return widgetInstanceId;
})
)
}
];
}

/**
* Accepts a tuple of a widget instance and the associated id and performs a
* widget configuration migration via the widget's {@link createConfig} function.
Expand Down

0 comments on commit 4d9a5aa

Please sign in to comment.