mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Merge branch 'master' into sync-open-twice
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
commit
cc19c44e38
@ -19,26 +19,36 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { action, computed, observable, toJS } from "mobx";
|
import { action, computed, observable } from "mobx";
|
||||||
|
import { Disposer, ExtendedMap } from "../utils";
|
||||||
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity";
|
import { CatalogCategory, CatalogEntityData, CatalogEntityKindData } from "./catalog-entity";
|
||||||
|
|
||||||
export class CatalogCategoryRegistry {
|
export class CatalogCategoryRegistry {
|
||||||
@observable protected categories: CatalogCategory[] = [];
|
protected categories = observable.set<CatalogCategory>();
|
||||||
|
protected groupKinds = new ExtendedMap<string, ExtendedMap<string, CatalogCategory>>();
|
||||||
|
|
||||||
@action add(category: CatalogCategory) {
|
@action add(category: CatalogCategory): Disposer {
|
||||||
this.categories.push(category);
|
this.categories.add(category);
|
||||||
|
this.updateGroupKinds(category);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
this.categories.delete(category);
|
||||||
|
this.groupKinds.clear();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@action remove(category: CatalogCategory) {
|
private updateGroupKinds(category: CatalogCategory) {
|
||||||
this.categories = this.categories.filter((cat) => cat.apiVersion !== category.apiVersion && cat.kind !== category.kind);
|
this.groupKinds
|
||||||
|
.getOrInsert(category.spec.group, ExtendedMap.new)
|
||||||
|
.strictSet(category.spec.names.kind, category);
|
||||||
}
|
}
|
||||||
|
|
||||||
@computed get items() {
|
@computed get items() {
|
||||||
return toJS(this.categories);
|
return Array.from(this.categories);
|
||||||
}
|
}
|
||||||
|
|
||||||
getForGroupKind<T extends CatalogCategory>(group: string, kind: string) {
|
getForGroupKind<T extends CatalogCategory>(group: string, kind: string): T | undefined {
|
||||||
return this.categories.find((c) => c.spec.group === group && c.spec.names.kind === kind) as T;
|
return this.groupKinds.get(group)?.get(kind) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
getEntityForData(data: CatalogEntityData & CatalogEntityKindData) {
|
getEntityForData(data: CatalogEntityData & CatalogEntityKindData) {
|
||||||
@ -60,17 +70,11 @@ export class CatalogCategoryRegistry {
|
|||||||
return new specVersion.entityClass(data);
|
return new specVersion.entityClass(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCategoryForEntity<T extends CatalogCategory>(data: CatalogEntityData & CatalogEntityKindData) {
|
getCategoryForEntity<T extends CatalogCategory>(data: CatalogEntityData & CatalogEntityKindData): T | undefined {
|
||||||
const splitApiVersion = data.apiVersion.split("/");
|
const splitApiVersion = data.apiVersion.split("/");
|
||||||
const group = splitApiVersion[0];
|
const group = splitApiVersion[0];
|
||||||
|
|
||||||
const category = this.categories.find((category) => {
|
return this.getForGroupKind(group, data.kind);
|
||||||
return category.spec.group === group && category.spec.names.kind === data.kind;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!category) return null;
|
|
||||||
|
|
||||||
return category as T;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,4 +21,3 @@
|
|||||||
|
|
||||||
export * from "./catalog-category-registry";
|
export * from "./catalog-category-registry";
|
||||||
export * from "./catalog-entity";
|
export * from "./catalog-entity";
|
||||||
export * from "./catalog-entity-registry";
|
|
||||||
|
|||||||
@ -22,19 +22,17 @@
|
|||||||
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
|
import { action, IEnhancer, IObservableMapInitialValues, ObservableMap } from "mobx";
|
||||||
|
|
||||||
export class ExtendedMap<K, V> extends Map<K, V> {
|
export class ExtendedMap<K, V> extends Map<K, V> {
|
||||||
constructor(protected getDefault: () => V, entries?: readonly (readonly [K, V])[] | null) {
|
static new<K, V>(entries?: readonly (readonly [K, V])[] | null): ExtendedMap<K, V> {
|
||||||
super(entries);
|
return new ExtendedMap<K, V>(entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
getOrInsert(key: K, val: V): V {
|
/**
|
||||||
if (this.has(key)) {
|
* Get the value behind `key`. If it was not pressent, first insert the value returned by `getVal`
|
||||||
return this.get(key);
|
* @param key The key to insert into the map with
|
||||||
}
|
* @param getVal A function that returns a new instance of `V`.
|
||||||
|
* @returns The value in the map
|
||||||
return this.set(key, val).get(key);
|
*/
|
||||||
}
|
getOrInsert(key: K, getVal: () => V): V {
|
||||||
|
|
||||||
getOrInsertWith(key: K, getVal: () => V): V {
|
|
||||||
if (this.has(key)) {
|
if (this.has(key)) {
|
||||||
return this.get(key);
|
return this.get(key);
|
||||||
}
|
}
|
||||||
@ -42,12 +40,29 @@ export class ExtendedMap<K, V> extends Map<K, V> {
|
|||||||
return this.set(key, getVal()).get(key);
|
return this.set(key, getVal()).get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
getOrDefault(key: K): V {
|
/**
|
||||||
|
* Set the value associated with `key` iff there was not a previous value
|
||||||
|
* @throws if `key` already in map
|
||||||
|
* @returns `this` so that `strictSet` can be chained
|
||||||
|
*/
|
||||||
|
strictSet(key: K, val: V): this {
|
||||||
if (this.has(key)) {
|
if (this.has(key)) {
|
||||||
return this.get(key);
|
throw new TypeError("Duplicate key in map");
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.set(key, this.getDefault()).get(key);
|
return this.set(key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value associated with `key`
|
||||||
|
* @throws if `key` did not a value associated with it
|
||||||
|
*/
|
||||||
|
strictGet(key: K): V {
|
||||||
|
if (!this.has(key)) {
|
||||||
|
throw new TypeError("key not in map");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.get(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import { CatalogEntity, catalogEntityRegistry as registry } from "../../common/catalog";
|
import type { CatalogEntity } from "../../common/catalog";
|
||||||
|
import { catalogEntityRegistry as registry } from "../../main/catalog";
|
||||||
|
|
||||||
export { catalogCategoryRegistry as catalogCategories } from "../../common/catalog/catalog-category-registry";
|
export { catalogCategoryRegistry as catalogCategories } from "../../common/catalog/catalog-category-registry";
|
||||||
export * from "../../common/catalog-entities";
|
export * from "../../common/catalog-entities";
|
||||||
|
|||||||
@ -22,7 +22,8 @@
|
|||||||
import { LensExtension } from "./lens-extension";
|
import { LensExtension } from "./lens-extension";
|
||||||
import { WindowManager } from "../main/window-manager";
|
import { WindowManager } from "../main/window-manager";
|
||||||
import { getExtensionPageUrl } from "./registries/page-registry";
|
import { getExtensionPageUrl } from "./registries/page-registry";
|
||||||
import { CatalogEntity, catalogEntityRegistry } from "../common/catalog";
|
import { catalogEntityRegistry } from "../main/catalog";
|
||||||
|
import type { CatalogEntity } from "../common/catalog";
|
||||||
import type { IObservableArray } from "mobx";
|
import type { IObservableArray } from "mobx";
|
||||||
import type { MenuRegistration } from "./registries";
|
import type { MenuRegistration } from "./registries";
|
||||||
|
|
||||||
|
|||||||
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
import { reaction, toJS } from "mobx";
|
import { reaction, toJS } from "mobx";
|
||||||
import { broadcastMessage } from "../common/ipc";
|
import { broadcastMessage } from "../common/ipc";
|
||||||
import type { CatalogEntityRegistry } from "../common/catalog";
|
import type { CatalogEntityRegistry} from "./catalog";
|
||||||
import "../common/catalog-entities/kubernetes-cluster";
|
import "../common/catalog-entities/kubernetes-cluster";
|
||||||
|
|
||||||
export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) {
|
export function pushCatalogToRenderer(catalog: CatalogEntityRegistry) {
|
||||||
|
|||||||
@ -20,7 +20,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { action, observable, IComputedValue, computed, ObservableMap, runInAction } from "mobx";
|
import { action, observable, IComputedValue, computed, ObservableMap, runInAction } from "mobx";
|
||||||
import { CatalogEntity, catalogEntityRegistry } from "../../common/catalog";
|
import type { CatalogEntity } from "../../common/catalog";
|
||||||
|
import { catalogEntityRegistry } from "../../main/catalog";
|
||||||
import { watch } from "chokidar";
|
import { watch } from "chokidar";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
|
|||||||
@ -20,8 +20,30 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { observable, reaction } from "mobx";
|
import { observable, reaction } from "mobx";
|
||||||
import { WebLink } from "../catalog-entities";
|
import { WebLink, WebLinkSpec, WebLinkStatus } from "../../../common/catalog-entities";
|
||||||
import { CatalogEntityRegistry } from "../catalog";
|
import { catalogCategoryRegistry, CatalogEntity, CatalogEntityMetadata } from "../../../common/catalog";
|
||||||
|
import { CatalogEntityRegistry } from "../catalog-entity-registry";
|
||||||
|
|
||||||
|
class InvalidEntity extends CatalogEntity<CatalogEntityMetadata, WebLinkStatus, WebLinkSpec> {
|
||||||
|
public readonly apiVersion = "entity.k8slens.dev/v1alpha1";
|
||||||
|
public readonly kind = "Invalid";
|
||||||
|
|
||||||
|
async onRun() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public onSettingsOpen(): void {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public onDetailsOpen(): void {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public onContextMenuOpen(): void {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("CatalogEntityRegistry", () => {
|
describe("CatalogEntityRegistry", () => {
|
||||||
let registry: CatalogEntityRegistry;
|
let registry: CatalogEntityRegistry;
|
||||||
@ -39,9 +61,23 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
phase: "valid"
|
phase: "valid"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const invalidEntity = new InvalidEntity({
|
||||||
|
metadata: {
|
||||||
|
uid: "invalid",
|
||||||
|
name: "test-link",
|
||||||
|
source: "test",
|
||||||
|
labels: {}
|
||||||
|
},
|
||||||
|
spec: {
|
||||||
|
url: "https://k8slens.dev"
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
phase: "valid"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
registry = new CatalogEntityRegistry();
|
registry = new CatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("addSource", () => {
|
describe("addSource", () => {
|
||||||
@ -79,4 +115,22 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
expect(registry.items.length).toEqual(0);
|
expect(registry.items.length).toEqual(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("items", () => {
|
||||||
|
it("returns added items", () => {
|
||||||
|
expect(registry.items.length).toBe(0);
|
||||||
|
|
||||||
|
const source = observable.array([entity]);
|
||||||
|
|
||||||
|
registry.addObservableSource("test", source);
|
||||||
|
expect(registry.items.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not return items without matching category", () => {
|
||||||
|
const source = observable.array([invalidEntity]);
|
||||||
|
|
||||||
|
registry.addObservableSource("test", source);
|
||||||
|
expect(registry.items.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@ -20,12 +20,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { action, computed, observable, IComputedValue, IObservableArray } from "mobx";
|
import { action, computed, observable, IComputedValue, IObservableArray } from "mobx";
|
||||||
import type { CatalogEntity } from "./catalog-entity";
|
import { CatalogCategoryRegistry, catalogCategoryRegistry, CatalogEntity } from "../../common/catalog";
|
||||||
import { iter } from "../utils";
|
import { iter } from "../../common/utils";
|
||||||
|
|
||||||
export class CatalogEntityRegistry {
|
export class CatalogEntityRegistry {
|
||||||
protected sources = observable.map<string, IComputedValue<CatalogEntity[]>>([], { deep: true });
|
protected sources = observable.map<string, IComputedValue<CatalogEntity[]>>([], { deep: true });
|
||||||
|
|
||||||
|
constructor(private categoryRegistry: CatalogCategoryRegistry) {}
|
||||||
|
|
||||||
@action addObservableSource(id: string, source: IObservableArray<CatalogEntity>) {
|
@action addObservableSource(id: string, source: IObservableArray<CatalogEntity>) {
|
||||||
this.sources.set(id, computed(() => source));
|
this.sources.set(id, computed(() => source));
|
||||||
}
|
}
|
||||||
@ -39,7 +41,9 @@ export class CatalogEntityRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@computed get items(): CatalogEntity[] {
|
@computed get items(): CatalogEntity[] {
|
||||||
return Array.from(iter.flatMap(this.sources.values(), source => source.get()));
|
const allItems = Array.from(iter.flatMap(this.sources.values(), source => source.get()));
|
||||||
|
|
||||||
|
return allItems.filter((entity) => this.categoryRegistry.getCategoryForEntity(entity) !== undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
||||||
@ -49,4 +53,4 @@ export class CatalogEntityRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const catalogEntityRegistry = new CatalogEntityRegistry();
|
export const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
22
src/main/catalog/index.ts
Normal file
22
src/main/catalog/index.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2021 OpenLens Authors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to
|
||||||
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export * from "./catalog-entity-registry";
|
||||||
@ -28,7 +28,7 @@ import type { Cluster } from "./cluster";
|
|||||||
import logger from "./logger";
|
import logger from "./logger";
|
||||||
import { apiKubePrefix } from "../common/vars";
|
import { apiKubePrefix } from "../common/vars";
|
||||||
import { Singleton } from "../common/utils";
|
import { Singleton } from "../common/utils";
|
||||||
import { catalogEntityRegistry } from "../common/catalog";
|
import { catalogEntityRegistry } from "./catalog";
|
||||||
import { KubernetesCluster, KubernetesClusterPrometheusMetrics } from "../common/catalog-entities/kubernetes-cluster";
|
import { KubernetesCluster, KubernetesClusterPrometheusMetrics } from "../common/catalog-entities/kubernetes-cluster";
|
||||||
|
|
||||||
export class ClusterManager extends Singleton {
|
export class ClusterManager extends Singleton {
|
||||||
|
|||||||
@ -50,7 +50,7 @@ import { bindBroadcastHandlers } from "../common/ipc";
|
|||||||
import { startUpdateChecking } from "./app-updater";
|
import { startUpdateChecking } from "./app-updater";
|
||||||
import { IpcRendererNavigationEvents } from "../renderer/navigation/events";
|
import { IpcRendererNavigationEvents } from "../renderer/navigation/events";
|
||||||
import { pushCatalogToRenderer } from "./catalog-pusher";
|
import { pushCatalogToRenderer } from "./catalog-pusher";
|
||||||
import { catalogEntityRegistry } from "../common/catalog";
|
import { catalogEntityRegistry } from "./catalog";
|
||||||
import { HotbarStore } from "../common/hotbar-store";
|
import { HotbarStore } from "../common/hotbar-store";
|
||||||
import { HelmRepoManager } from "./helm/helm-repo-manager";
|
import { HelmRepoManager } from "./helm/helm-repo-manager";
|
||||||
import { KubeconfigSyncManager } from "./catalog-sources";
|
import { KubeconfigSyncManager } from "./catalog-sources";
|
||||||
|
|||||||
@ -22,11 +22,18 @@
|
|||||||
import { CatalogEntityRegistry } from "../catalog-entity-registry";
|
import { CatalogEntityRegistry } from "../catalog-entity-registry";
|
||||||
import "../../../common/catalog-entities";
|
import "../../../common/catalog-entities";
|
||||||
import { catalogCategoryRegistry } from "../../../common/catalog/catalog-category-registry";
|
import { catalogCategoryRegistry } from "../../../common/catalog/catalog-category-registry";
|
||||||
|
import type { CatalogEntityData, CatalogEntityKindData } from "../catalog-entity";
|
||||||
|
|
||||||
|
class TestCatalogEntityRegistry extends CatalogEntityRegistry {
|
||||||
|
replaceItems(items: Array<CatalogEntityData & CatalogEntityKindData>) {
|
||||||
|
this.rawItems.replace(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("CatalogEntityRegistry", () => {
|
describe("CatalogEntityRegistry", () => {
|
||||||
describe("updateItems", () => {
|
describe("updateItems", () => {
|
||||||
it("adds new catalog item", () => {
|
it("adds new catalog item", () => {
|
||||||
const catalog = new CatalogEntityRegistry(catalogCategoryRegistry);
|
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
const items = [{
|
const items = [{
|
||||||
apiVersion: "entity.k8slens.dev/v1alpha1",
|
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||||
kind: "KubernetesCluster",
|
kind: "KubernetesCluster",
|
||||||
@ -42,7 +49,7 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
spec: {}
|
spec: {}
|
||||||
}];
|
}];
|
||||||
|
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
expect(catalog.items.length).toEqual(1);
|
expect(catalog.items.length).toEqual(1);
|
||||||
|
|
||||||
items.push({
|
items.push({
|
||||||
@ -60,12 +67,12 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
spec: {}
|
spec: {}
|
||||||
});
|
});
|
||||||
|
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
expect(catalog.items.length).toEqual(2);
|
expect(catalog.items.length).toEqual(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("updates existing items", () => {
|
it("updates existing items", () => {
|
||||||
const catalog = new CatalogEntityRegistry(catalogCategoryRegistry);
|
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
const items = [{
|
const items = [{
|
||||||
apiVersion: "entity.k8slens.dev/v1alpha1",
|
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||||
kind: "KubernetesCluster",
|
kind: "KubernetesCluster",
|
||||||
@ -81,19 +88,19 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
spec: {}
|
spec: {}
|
||||||
}];
|
}];
|
||||||
|
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
expect(catalog.items.length).toEqual(1);
|
expect(catalog.items.length).toEqual(1);
|
||||||
expect(catalog.items[0].status.phase).toEqual("disconnected");
|
expect(catalog.items[0].status.phase).toEqual("disconnected");
|
||||||
|
|
||||||
items[0].status.phase = "connected";
|
items[0].status.phase = "connected";
|
||||||
|
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
expect(catalog.items.length).toEqual(1);
|
expect(catalog.items.length).toEqual(1);
|
||||||
expect(catalog.items[0].status.phase).toEqual("connected");
|
expect(catalog.items[0].status.phase).toEqual("connected");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("removes deleted items", () => {
|
it("removes deleted items", () => {
|
||||||
const catalog = new CatalogEntityRegistry(catalogCategoryRegistry);
|
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
const items = [
|
const items = [
|
||||||
{
|
{
|
||||||
apiVersion: "entity.k8slens.dev/v1alpha1",
|
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||||
@ -125,11 +132,51 @@ describe("CatalogEntityRegistry", () => {
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
items.splice(0, 1);
|
items.splice(0, 1);
|
||||||
catalog.updateItems(items);
|
catalog.replaceItems(items);
|
||||||
expect(catalog.items.length).toEqual(1);
|
expect(catalog.items.length).toEqual(1);
|
||||||
expect(catalog.items[0].metadata.uid).toEqual("456");
|
expect(catalog.items[0].metadata.uid).toEqual("456");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("items", () => {
|
||||||
|
it("does not return items without matching category", () => {
|
||||||
|
const catalog = new TestCatalogEntityRegistry(catalogCategoryRegistry);
|
||||||
|
const items = [
|
||||||
|
{
|
||||||
|
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||||
|
kind: "KubernetesCluster",
|
||||||
|
metadata: {
|
||||||
|
uid: "123",
|
||||||
|
name: "foobar",
|
||||||
|
source: "test",
|
||||||
|
labels: {}
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
phase: "disconnected"
|
||||||
|
},
|
||||||
|
spec: {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
apiVersion: "entity.k8slens.dev/v1alpha1",
|
||||||
|
kind: "FooBar",
|
||||||
|
metadata: {
|
||||||
|
uid: "456",
|
||||||
|
name: "barbaz",
|
||||||
|
source: "test",
|
||||||
|
labels: {}
|
||||||
|
},
|
||||||
|
status: {
|
||||||
|
phase: "disconnected"
|
||||||
|
},
|
||||||
|
spec: {}
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
catalog.replaceItems(items);
|
||||||
|
|
||||||
|
expect(catalog.items.length).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -19,27 +19,24 @@
|
|||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { action, observable } from "mobx";
|
import { computed, observable } from "mobx";
|
||||||
import { subscribeToBroadcast } from "../../common/ipc";
|
import { subscribeToBroadcast } from "../../common/ipc";
|
||||||
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog";
|
import { CatalogCategory, CatalogEntity, CatalogEntityData, catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntityKindData } from "../../common/catalog";
|
||||||
import "../../common/catalog-entities";
|
import "../../common/catalog-entities";
|
||||||
|
import { iter } from "../utils";
|
||||||
|
|
||||||
export class CatalogEntityRegistry {
|
export class CatalogEntityRegistry {
|
||||||
@observable protected _items: CatalogEntity[] = observable.array([], { deep: true });
|
protected rawItems = observable.array<CatalogEntityData & CatalogEntityKindData>([], { deep: true });
|
||||||
@observable protected _activeEntity: CatalogEntity;
|
@observable protected _activeEntity: CatalogEntity;
|
||||||
|
|
||||||
constructor(private categoryRegistry: CatalogCategoryRegistry) {}
|
constructor(private categoryRegistry: CatalogCategoryRegistry) {}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
|
subscribeToBroadcast("catalog:items", (ev, items: (CatalogEntityData & CatalogEntityKindData)[]) => {
|
||||||
this.updateItems(items);
|
this.rawItems.replace(items);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@action updateItems(items: (CatalogEntityData & CatalogEntityKindData)[]) {
|
|
||||||
this._items = items.map(data => this.categoryRegistry.getEntityForData(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
set activeEntity(entity: CatalogEntity) {
|
set activeEntity(entity: CatalogEntity) {
|
||||||
this._activeEntity = entity;
|
this._activeEntity = entity;
|
||||||
}
|
}
|
||||||
@ -48,23 +45,27 @@ export class CatalogEntityRegistry {
|
|||||||
return this._activeEntity;
|
return this._activeEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
get items() {
|
@computed get items() {
|
||||||
return this._items;
|
return Array.from(iter.filterMap(this.rawItems, rawItem => this.categoryRegistry.getEntityForData(rawItem)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@computed get entities(): Map<string, CatalogEntity> {
|
||||||
|
return new Map(this.items.map(item => [item.metadata.uid, item]));
|
||||||
}
|
}
|
||||||
|
|
||||||
getById(id: string) {
|
getById(id: string) {
|
||||||
return this._items.find((entity) => entity.metadata.uid === id);
|
return this.entities.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
getItemsForApiKind<T extends CatalogEntity>(apiVersion: string, kind: string): T[] {
|
||||||
const items = this._items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
|
const items = this.items.filter((item) => item.apiVersion === apiVersion && item.kind === kind);
|
||||||
|
|
||||||
return items as T[];
|
return items as T[];
|
||||||
}
|
}
|
||||||
|
|
||||||
getItemsForCategory<T extends CatalogEntity>(category: CatalogCategory): T[] {
|
getItemsForCategory<T extends CatalogEntity>(category: CatalogCategory): T[] {
|
||||||
const supportedVersions = category.spec.versions.map((v) => `${category.spec.group}/${v.name}`);
|
const supportedVersions = category.spec.versions.map((v) => `${category.spec.group}/${v.name}`);
|
||||||
const items = this._items.filter((item) => supportedVersions.includes(item.apiVersion) && item.kind === category.spec.names.kind);
|
const items = this.items.filter((item) => supportedVersions.includes(item.apiVersion) && item.kind === category.spec.names.kind);
|
||||||
|
|
||||||
return items as T[];
|
return items as T[];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,8 +22,12 @@
|
|||||||
import type { RouteProps } from "react-router";
|
import type { RouteProps } from "react-router";
|
||||||
import { buildURL } from "../../../common/utils/buildUrl";
|
import { buildURL } from "../../../common/utils/buildUrl";
|
||||||
|
|
||||||
|
export interface ICatalogViewRouteParam {
|
||||||
|
group?: string;
|
||||||
|
kind?: string;
|
||||||
|
}
|
||||||
export const catalogRoute: RouteProps = {
|
export const catalogRoute: RouteProps = {
|
||||||
path: "/catalog"
|
path: "/catalog/:group?/:kind?"
|
||||||
};
|
};
|
||||||
|
|
||||||
export const catalogURL = buildURL(catalogRoute.path);
|
export const catalogURL = buildURL<ICatalogViewRouteParam>(catalogRoute.path);
|
||||||
|
|||||||
@ -23,7 +23,7 @@ import "./catalog.scss";
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { disposeOnUnmount, observer } from "mobx-react";
|
import { disposeOnUnmount, observer } from "mobx-react";
|
||||||
import { ItemListLayout } from "../item-object-list";
|
import { ItemListLayout } from "../item-object-list";
|
||||||
import { action, observable, reaction } from "mobx";
|
import { action, observable, reaction, when } from "mobx";
|
||||||
import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store";
|
import { CatalogEntityItem, CatalogEntityStore } from "./catalog-entity.store";
|
||||||
import { navigate } from "../../navigation";
|
import { navigate } from "../../navigation";
|
||||||
import { kebabCase } from "lodash";
|
import { kebabCase } from "lodash";
|
||||||
@ -37,18 +37,33 @@ import { ConfirmDialog } from "../confirm-dialog";
|
|||||||
import { Tab, Tabs } from "../tabs";
|
import { Tab, Tabs } from "../tabs";
|
||||||
import { catalogCategoryRegistry } from "../../../common/catalog";
|
import { catalogCategoryRegistry } from "../../../common/catalog";
|
||||||
import { CatalogAddButton } from "./catalog-add-button";
|
import { CatalogAddButton } from "./catalog-add-button";
|
||||||
|
import type { RouteComponentProps } from "react-router";
|
||||||
|
import type { ICatalogViewRouteParam } from "./catalog.route";
|
||||||
|
import { Notifications } from "../notifications";
|
||||||
|
|
||||||
enum sortBy {
|
enum sortBy {
|
||||||
name = "name",
|
name = "name",
|
||||||
source = "source",
|
source = "source",
|
||||||
status = "status"
|
status = "status"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Props extends RouteComponentProps<ICatalogViewRouteParam> {}
|
||||||
@observer
|
@observer
|
||||||
export class Catalog extends React.Component {
|
export class Catalog extends React.Component<Props> {
|
||||||
@observable private catalogEntityStore?: CatalogEntityStore;
|
@observable private catalogEntityStore?: CatalogEntityStore;
|
||||||
@observable.deep private contextMenu: CatalogEntityContextMenuContext;
|
@observable.deep private contextMenu: CatalogEntityContextMenuContext;
|
||||||
@observable activeTab?: string;
|
@observable activeTab?: string;
|
||||||
|
|
||||||
|
get routeActiveTab(): string | undefined {
|
||||||
|
const { group, kind } = this.props.match.params ?? {};
|
||||||
|
|
||||||
|
if (group && kind) {
|
||||||
|
return `${group}/${kind}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
this.contextMenu = {
|
this.contextMenu = {
|
||||||
menuItems: [],
|
menuItems: [],
|
||||||
@ -57,12 +72,22 @@ export class Catalog extends React.Component {
|
|||||||
this.catalogEntityStore = new CatalogEntityStore();
|
this.catalogEntityStore = new CatalogEntityStore();
|
||||||
disposeOnUnmount(this, [
|
disposeOnUnmount(this, [
|
||||||
this.catalogEntityStore.watch(),
|
this.catalogEntityStore.watch(),
|
||||||
|
when(() => catalogCategoryRegistry.items.length > 0, () => {
|
||||||
|
const item = catalogCategoryRegistry.items.find(i => i.getId() === this.routeActiveTab);
|
||||||
|
|
||||||
|
if (item || this.routeActiveTab === undefined) {
|
||||||
|
this.activeTab = this.routeActiveTab;
|
||||||
|
this.catalogEntityStore.activeCategory = item;
|
||||||
|
} else {
|
||||||
|
Notifications.error(<p>Unknown category: {this.routeActiveTab}</p>);
|
||||||
|
}
|
||||||
|
}),
|
||||||
reaction(() => catalogCategoryRegistry.items, (items) => {
|
reaction(() => catalogCategoryRegistry.items, (items) => {
|
||||||
if (!this.activeTab && items.length > 0) {
|
if (!this.activeTab && items.length > 0) {
|
||||||
this.activeTab = items[0].getId();
|
this.activeTab = items[0].getId();
|
||||||
this.catalogEntityStore.activeCategory = items[0];
|
this.catalogEntityStore.activeCategory = items[0];
|
||||||
}
|
}
|
||||||
}, { fireImmediately: true })
|
}),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -93,6 +93,10 @@ export const HotbarIcon = observer(({menuItems = [], ...props}: Props) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const getIconString = () => {
|
const getIconString = () => {
|
||||||
|
if (!title) {
|
||||||
|
return "??";
|
||||||
|
}
|
||||||
|
|
||||||
const [rawFirst, rawSecond, rawThird] = getNameParts(title);
|
const [rawFirst, rawSecond, rawThird] = getNameParts(title);
|
||||||
const splitter = new GraphemeSplitter();
|
const splitter = new GraphemeSplitter();
|
||||||
const first = splitter.iterateGraphemes(rawFirst);
|
const first = splitter.iterateGraphemes(rawFirst);
|
||||||
@ -108,7 +112,7 @@ export const HotbarIcon = observer(({menuItems = [], ...props}: Props) => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={cssNames("HotbarIcon flex inline", className, { disabled })}>
|
<div className={cssNames("HotbarIcon flex inline", className, { disabled })}>
|
||||||
<MaterialTooltip title={`${title} (${source})`} placement="right">
|
<MaterialTooltip title={`${title || "unknown"} (${source || "unknown"})`} placement="right">
|
||||||
<div id={id}>
|
<div id={id}>
|
||||||
<Avatar
|
<Avatar
|
||||||
{...rest}
|
{...rest}
|
||||||
|
|||||||
@ -43,6 +43,13 @@ export function bindProtocolAddRouteHandlers() {
|
|||||||
.addInternalHandler("/landing", () => {
|
.addInternalHandler("/landing", () => {
|
||||||
navigate(catalogURL());
|
navigate(catalogURL());
|
||||||
})
|
})
|
||||||
|
.addInternalHandler("/landing/view/:group/:kind", ({ pathname: { group, kind } }) => {
|
||||||
|
navigate(catalogURL({
|
||||||
|
params: {
|
||||||
|
group, kind
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
})
|
||||||
.addInternalHandler("/cluster", () => {
|
.addInternalHandler("/cluster", () => {
|
||||||
navigate(addClusterURL());
|
navigate(addClusterURL());
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user