Fix hotbar migration of workspaces (#3178)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-06-25 09:55:25 -04:00
1 parent b9dfd20a93
commit 51655884c7
39 files changed
+333 -446

No files matched your search

+10 -20
View File
@@ -20,34 +20,24 @@
*/
// Cleans up a store that had the state related data stored
import type { Hotbar } from "../../common/hotbar-store";
import { ClusterStore } from "../../common/cluster-store";
import { Hotbar, HotbarStore } from "../../common/hotbar-store";
import * as uuid from "uuid";
import type { MigrationDeclaration } from "../helpers";
import { catalogEntity } from "../../main/catalog-sources/general";
export default {
version: "5.0.0-alpha.0",
run(store) {
const hotbars: Hotbar[] = [];
const hotbar: Hotbar = {
id: uuid.v4(),
name: "default",
items: HotbarStore.getInitialItems(),
};
ClusterStore.getInstance().clustersList.forEach((cluster: any) => {
const name = cluster.workspace;
const { metadata: { uid, name, source } } = catalogEntity;
if (!name) return;
hotbar.items[0] = { entity: { uid, name, source } };
let hotbar = hotbars.find((h) => h.name === name);
if (!hotbar) {
hotbar = { id: uuid.v4(), name, items: [] };
hotbars.push(hotbar);
}
hotbar.items.push({
entity: { uid: cluster.id },
params: {}
});
});
store.set("hotbars", hotbars);
store.set("hotbars", [hotbar]);
}
} as MigrationDeclaration;
+57 -2
View File
@@ -19,12 +19,15 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { createHash } from "crypto";
import { app } from "electron";
import fse from "fs-extra";
import { isNull } from "lodash";
import path from "path";
import * as uuid from "uuid";
import type { ClusterStoreModel } from "../../common/cluster-store";
import { defaultHotbarCells, Hotbar } from "../../common/hotbar-store";
import { defaultHotbarCells, Hotbar, HotbarStore } from "../../common/hotbar-store";
import { catalogEntity } from "../../main/catalog-sources/general";
import type { MigrationDeclaration } from "../helpers";
interface Pre500WorkspaceStoreModel {
@@ -49,7 +52,19 @@ export default {
workspaceHotbars.set(id, {
id: uuid.v4(), // don't use the old IDs as they aren't necessarily UUIDs
items: [],
name: `Workspace: ${name}`,
});
}
{
// grab the default named hotbar or the first.
const defaultHotbarIndex = Math.max(0, hotbars.findIndex(hotbar => hotbar.name === "default"));
const [{ name, id, items }] = hotbars.splice(defaultHotbarIndex, 1);
workspaceHotbars.set("default", {
name,
id,
items: items.filter(Boolean),
});
}
@@ -59,7 +74,7 @@ export default {
if (workspaceHotbar?.items.length < defaultHotbarCells) {
workspaceHotbar.items.push({
entity: {
uid: cluster.id,
uid: createHash("md5").update(`${cluster.kubeConfigPath}:${cluster.contextName}`).digest("hex"),
name: cluster.preferences.clusterName || cluster.contextName,
}
});
@@ -74,6 +89,46 @@ export default {
hotbars.push(hotbar);
}
/**
* Finally, make sure that the catalog entity hotbar item is in place.
* Just in case something else removed it.
*
* if every hotbar has elements that all not the `catalog-entity` item
*/
if (hotbars.every(hotbar => hotbar.items.every(item => item?.entity?.uid !== "catalog-entity"))) {
// note, we will add a new whole hotbar here called "default" if that was previously removed
const defaultHotbar = hotbars.find(hotbar => hotbar.name === "default");
const { metadata: { uid, name, source } } = catalogEntity;
if (defaultHotbar) {
const freeIndex = defaultHotbar.items.findIndex(isNull);
if (freeIndex === -1) {
// making a new hotbar is less destructive if the first hotbar
// called "default" is full than overriding a hotbar item
const hotbar = {
id: uuid.v4(),
name: "initial",
items: HotbarStore.getInitialItems(),
};
hotbar.items[0] = { entity: { uid, name, source } };
hotbars.unshift(hotbar);
} else {
defaultHotbar.items[freeIndex] = { entity: { uid, name, source } };
}
} else {
const hotbar = {
id: uuid.v4(),
name: "default",
items: HotbarStore.getInitialItems(),
};
hotbar.items[0] = { entity: { uid, name, source } };
hotbars.unshift(hotbar);
}
}
store.set("hotbars", hotbars);
} catch (error) {
if (!(error.code === "ENOENT" && error.path.endsWith("lens-workspace-store.json"))) {
+1 -1
View File
@@ -20,7 +20,7 @@
*/
import type { Hotbar } from "../../common/hotbar-store";
import { catalogEntityRegistry } from "../../renderer/api/catalog-entity-registry";
import { catalogEntityRegistry } from "../../main/catalog";
import type { MigrationDeclaration } from "../helpers";
export default {