mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
* decentralizing page url-params management -- PoC / tsc 4.1 random fixes Signed-off-by: Roman <ixrock@gmail.com> * fixes, tweak example-extension for demo Signed-off-by: Roman <ixrock@gmail.com> * lint fixes, revert tests Signed-off-by: Roman <ixrock@gmail.com> * removed occasional changes related to typescript 4.1 Signed-off-by: Roman <ixrock@gmail.com> * updated example with 2 menu-items targeting same page with different params Signed-off-by: Roman <ixrock@gmail.com> * fix: merge page url chunks with native URL()-api, simplified default page-params registration Signed-off-by: Roman <ixrock@gmail.com> * fix: make lint happy Signed-off-by: Roman <ixrock@gmail.com> * fix: unit-tests Signed-off-by: Roman <ixrock@gmail.com> * renaming by jim's request: UrlParam => PageParam (type), createUrlParam => createPageParam (helper) Signed-off-by: Roman <ixrock@gmail.com> * fix: reverting NamespaceStore public-api breaking changes Signed-off-by: Roman <ixrock@gmail.com> * lint fix Signed-off-by: Roman <ixrock@gmail.com> * fine-tuning Signed-off-by: Roman <ixrock@gmail.com> * yes, lint always unhappy Signed-off-by: Roman <ixrock@gmail.com> * fix build Signed-off-by: Roman <ixrock@gmail.com> * small fixes Signed-off-by: Roman <ixrock@gmail.com> * fix merge-conflicts Signed-off-by: Roman <ixrock@gmail.com> * removed `isSystem` page-param's init field exposed to extensions-api Signed-off-by: Roman <ixrock@gmail.com>
140 lines
3.7 KiB
TypeScript
140 lines
3.7 KiB
TypeScript
import "./kube-object-details.scss";
|
|
|
|
import React from "react";
|
|
import { disposeOnUnmount, observer } from "mobx-react";
|
|
import { computed, observable, reaction } from "mobx";
|
|
import { Trans } from "@lingui/macro";
|
|
import { createPageParam, navigation } from "../../navigation";
|
|
import { Drawer } from "../drawer";
|
|
import { KubeObject } from "../../api/kube-object";
|
|
import { Spinner } from "../spinner";
|
|
import { apiManager } from "../../api/api-manager";
|
|
import { crdStore } from "../+custom-resources/crd.store";
|
|
import { CrdResourceDetails } from "../+custom-resources";
|
|
import { KubeObjectMenu } from "./kube-object-menu";
|
|
import { kubeObjectDetailRegistry } from "../../api/kube-object-detail-registry";
|
|
|
|
export const kubeDetailsUrlParam = createPageParam({
|
|
name: "kube-details",
|
|
isSystem: true,
|
|
});
|
|
|
|
export const kubeSelectedUrlParam = createPageParam({
|
|
name: "kube-selected",
|
|
isSystem: true,
|
|
get defaultValue() {
|
|
return kubeDetailsUrlParam.get();
|
|
}
|
|
});
|
|
|
|
export function showDetails(details = "", resetSelected = true) {
|
|
const detailsUrl = getDetailsUrl(details, resetSelected);
|
|
|
|
navigation.merge({ search: detailsUrl });
|
|
}
|
|
|
|
export function hideDetails() {
|
|
showDetails();
|
|
}
|
|
|
|
export function getDetailsUrl(details: string, resetSelected = false) {
|
|
const detailsUrl = kubeDetailsUrlParam.toSearchString({ value: details });
|
|
|
|
if (resetSelected) {
|
|
const params = new URLSearchParams(detailsUrl);
|
|
|
|
params.delete(kubeSelectedUrlParam.name);
|
|
|
|
return `?${params.toString()}`;
|
|
}
|
|
|
|
return detailsUrl;
|
|
}
|
|
|
|
export interface KubeObjectDetailsProps<T = KubeObject> {
|
|
className?: string;
|
|
object: T;
|
|
}
|
|
|
|
@observer
|
|
export class KubeObjectDetails extends React.Component {
|
|
@observable isLoading = false;
|
|
@observable.ref loadingError: React.ReactNode;
|
|
|
|
@computed get path() {
|
|
return kubeDetailsUrlParam.get();
|
|
}
|
|
|
|
@computed get object() {
|
|
const store = apiManager.getStore(this.path);
|
|
|
|
if (store) {
|
|
return store.getByPath(this.path);
|
|
}
|
|
}
|
|
|
|
@computed get isCrdInstance() {
|
|
return !!crdStore.getByObject(this.object);
|
|
}
|
|
|
|
@disposeOnUnmount
|
|
loader = reaction(() => [
|
|
this.path,
|
|
this.object, // resource might be updated via watch-event or from already opened details
|
|
crdStore.items.length, // crd stores initialized after loading
|
|
], async () => {
|
|
this.loadingError = "";
|
|
const { path, object } = this;
|
|
|
|
if (!object) {
|
|
const store = apiManager.getStore(path);
|
|
|
|
if (store) {
|
|
this.isLoading = true;
|
|
|
|
try {
|
|
await store.loadFromPath(path);
|
|
} catch (err) {
|
|
this.loadingError = <Trans>Resource loading has failed: <b>{err.toString()}</b></Trans>;
|
|
} finally {
|
|
this.isLoading = false;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
render() {
|
|
const { object, isLoading, loadingError, isCrdInstance } = this;
|
|
const isOpen = !!(object || isLoading || loadingError);
|
|
let title = "";
|
|
let details: React.ReactNode[];
|
|
|
|
if (object) {
|
|
const { kind, getName } = object;
|
|
|
|
title = `${kind}: ${getName()}`;
|
|
details = kubeObjectDetailRegistry.getItemsForKind(object.kind, object.apiVersion).map((item, index) => {
|
|
return <item.components.Details object={object} key={`object-details-${index}`}/>;
|
|
});
|
|
|
|
if (isCrdInstance && details.length === 0) {
|
|
details.push(<CrdResourceDetails object={object}/>);
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Drawer
|
|
className="KubeObjectDetails flex column"
|
|
open={isOpen}
|
|
title={title}
|
|
toolbar={<KubeObjectMenu object={object} toolbar={true}/>}
|
|
onClose={hideDetails}
|
|
>
|
|
{isLoading && <Spinner center/>}
|
|
{loadingError && <div className="box center">{loadingError}</div>}
|
|
{details}
|
|
</Drawer>
|
|
);
|
|
}
|
|
}
|