/** * 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. */ import { EventEmitter } from "events"; import { observable } from "mobx"; type ExtractEntityMetadataType = Entity extends CatalogEntity ? Metadata : never; type ExtractEntityStatusType = Entity extends CatalogEntity ? Status : never; type ExtractEntitySpecType = Entity extends CatalogEntity ? Spec : never; export type CatalogEntityConstructor = ( (new (data: CatalogEntityData< ExtractEntityMetadataType, ExtractEntityStatusType, ExtractEntitySpecType >) => Entity) ); export interface CatalogCategoryVersion { name: string; entityClass: CatalogEntityConstructor; } export interface CatalogCategorySpec { group: string; versions: CatalogCategoryVersion[]; names: { kind: string; }; } export abstract class CatalogCategory extends EventEmitter { abstract readonly apiVersion: string; abstract readonly kind: string; abstract metadata: { name: string; icon: string; }; abstract spec: CatalogCategorySpec; public getId(): string { return `${this.spec.group}/${this.spec.names.kind}`; } } export interface CatalogEntityMetadata { uid: string; name: string; description?: string; source?: string; labels: Record; [key: string]: string | object; } export interface CatalogEntityStatus { phase: string; reason?: string; message?: string; active?: boolean; } export interface CatalogEntityActionContext { navigate: (url: string) => void; setCommandPaletteContext: (context?: CatalogEntity) => void; } export interface CatalogEntityContextMenu { title: string; onlyVisibleForSource?: string; // show only if empty or if matches with entity source onClick: () => void | Promise; confirm?: { message: string; } } export interface CatalogEntityAddMenu extends CatalogEntityContextMenu { icon: string; } export interface CatalogEntitySettingsMenu { group?: string; title: string; components: { View: React.ComponentType }; } export interface CatalogEntityContextMenuContext { navigate: (url: string) => void; menuItems: CatalogEntityContextMenu[]; } export interface CatalogEntitySettingsContext { menuItems: CatalogEntityContextMenu[]; } export interface CatalogEntityAddMenuContext { navigate: (url: string) => void; menuItems: CatalogEntityAddMenu[]; } export type CatalogEntitySpec = Record; export interface CatalogEntityData< Metadata extends CatalogEntityMetadata = CatalogEntityMetadata, Status extends CatalogEntityStatus = CatalogEntityStatus, Spec extends CatalogEntitySpec = CatalogEntitySpec, > { metadata: Metadata; status: Status; spec: Spec; } export interface CatalogEntityKindData { readonly apiVersion: string; readonly kind: string; } export abstract class CatalogEntity< Metadata extends CatalogEntityMetadata = CatalogEntityMetadata, Status extends CatalogEntityStatus = CatalogEntityStatus, Spec extends CatalogEntitySpec = CatalogEntitySpec, > implements CatalogEntityKindData { public abstract readonly apiVersion: string; public abstract readonly kind: string; @observable metadata: Metadata; @observable status: Status; @observable spec: Spec; constructor(data: CatalogEntityData) { this.metadata = data.metadata; this.status = data.status; this.spec = data.spec; } public getId(): string { return this.metadata.uid; } public getName(): string { return this.metadata.name; } public abstract onRun?(context: CatalogEntityActionContext): void | Promise; public abstract onDetailsOpen(context: CatalogEntityActionContext): void | Promise; public abstract onContextMenuOpen(context: CatalogEntityContextMenuContext): void | Promise; public abstract onSettingsOpen(context: CatalogEntitySettingsContext): void | Promise; }