1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

cluster-view reworks

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-08-08 12:12:36 +03:00
parent c3689369fc
commit cf70fb5582
3 changed files with 75 additions and 45 deletions

View File

@ -1,9 +1,11 @@
.ClusterStatus { .ClusterStatus {
--flex-gap: #{$padding * 2}; --flex-gap: #{$padding * 2};
position: relative;
min-width: 350px; min-width: 350px;
margin: auto; margin: auto;
text-align: center; text-align: center;
z-index: 1;
pre { pre {
@include hidden-scrollbar; @include hidden-scrollbar;

View File

@ -1,18 +1,24 @@
.ClusterView { .ClusterView {
position: relative;
width: 100%; width: 100%;
height: 100%; height: 100%;
display: none; display: flex;
flex: 1;
&.loaded {
display: flex;
}
} }
//#lens-views { #lens-views {
// position: absolute; position: absolute;
// left: 0; left: 0;
// top: 0; top: 0;
// width: 0; right: 0;
// height: 0; bottom: 0;
// overflow: hidden; display: flex;
//}
> * {
flex: 1;
}
body > & {
display: none;
}
}

View File

@ -1,66 +1,87 @@
import "./cluster-view.scss" import "./cluster-view.scss"
import React from "react"; import React from "react";
import { WebviewTag } from "electron" import { WebviewTag } from "electron";
import { action, autorun, computed, observable } from "mobx"; import { observable, reaction } from "mobx";
import { disposeOnUnmount, observer } from "mobx-react"; import { disposeOnUnmount, observer } from "mobx-react";
import { ClusterId, clusterStore } from "../../../common/cluster-store"; import { ClusterId, clusterStore } from "../../../common/cluster-store";
import { getMatchedClusterId } from "./cluster-view.route"; import { getMatchedClusterId } from "./cluster-view.route";
import { ClusterStatus } from "./cluster-status"; import { ClusterStatus } from "./cluster-status";
import logger from "../../../main/logger";
import { clusterIpc } from "../../../common/cluster-ipc"; import { clusterIpc } from "../../../common/cluster-ipc";
import logger from "../../../main/logger";
const lensViews = observable.map<ClusterId, WebviewTag>() // todo: figure out how to replace webview-tag to iframe
const isLoaded = observable.map<ClusterId, boolean>() // fixme: webview reloading/blinking when switching common <-> cluster views
interface LensView {
clusterId: ClusterId;
webview: WebviewTag
isLoaded?: boolean
}
const lensViews = observable.map<ClusterId, LensView>()
const lensViewsHolder = document.createElement("div")
lensViewsHolder.id = "lens-views"
document.body.appendChild(lensViewsHolder);
@observer @observer
export class ClusterView extends React.Component { export class ClusterView extends React.Component {
protected placeholder: HTMLElement; protected placeholder: HTMLElement;
@computed get cluster() { get cluster() {
return clusterStore.getById(getMatchedClusterId()) return clusterStore.getById(getMatchedClusterId())
} }
// fixme: attach/detach doesn't work properly
componentDidMount() { componentDidMount() {
// disposeOnUnmount(this, [ this.attachViews();
// autorun(() => { disposeOnUnmount(this, [
// const activeClusterId = this.cluster?.id; reaction(() => this.cluster, selectedCluster => {
// if (activeClusterId) { this.initView(selectedCluster?.id)
// this.initView(activeClusterId); this.refreshViews()
// Array.from(lensViews).forEach(([clusterId, view]) => { }, {
// if (activeClusterId === clusterId && isLoaded.has(clusterId)) { fireImmediately: true
// this.placeholder.appendChild(view) })
// } else { ])
// view.parentElement.removeChild(view);
// }
// })
// }
// }),
// ])
} }
@action componentWillUnmount() {
initView(clusterId: ClusterId) { this.detachViews();
if (lensViews.has(clusterId)) { }
initView = (clusterId: ClusterId) => {
if (!clusterId || lensViews.has(clusterId)) {
return; return;
} }
logger.info(`[WEBVIEW]: init view for clusterId=${clusterId}`) logger.info(`[WEBVIEW]: init view for clusterId=${clusterId}`)
const webview = document.createElement("webview"); const webview = document.createElement("webview");
webview.className = "ClusterView"
webview.setAttribute("src", `//${clusterId}.${location.host}`) webview.setAttribute("src", `//${clusterId}.${location.host}`)
webview.setAttribute("nodeintegration", "true") webview.setAttribute("nodeintegration", "true")
webview.setAttribute("enableremotemodule", "true") webview.setAttribute("enableremotemodule", "true")
webview.addEventListener("did-finish-load", () => { webview.addEventListener("did-finish-load", () => {
logger.info(`[WEBVIEW]: loaded, clusterId=${clusterId}`) logger.info(`[WEBVIEW]: loaded, clusterId=${clusterId}`)
isLoaded.set(clusterId, true);
webview.classList.add("loaded")
clusterIpc.init.invokeFromRenderer(clusterId); // push cluster-state to webview clusterIpc.init.invokeFromRenderer(clusterId); // push cluster-state to webview
lensViews.get(clusterId).isLoaded = true;
this.refreshViews();
}); });
webview.addEventListener("did-fail-load", (event) => { webview.addEventListener("did-fail-load", (event) => {
logger.error(`[WEBVIEW]: failed to load, clusterId=${clusterId}`, event) logger.error(`[WEBVIEW]: failed to load, clusterId=${clusterId}`, event)
}); });
lensViews.set(clusterId, webview); lensViews.set(clusterId, { clusterId, webview });
document.body.appendChild(webview); lensViewsHolder.appendChild(webview); // add to dom and start loading frame
}
attachViews = () => {
this.placeholder.appendChild(lensViewsHolder)
}
detachViews = () => {
document.body.appendChild(lensViewsHolder);
}
refreshViews = () => {
lensViews.forEach(({ clusterId, webview, isLoaded }) => {
const isActive = clusterId === this.cluster?.id;
webview.style.display = isLoaded && isActive ? "flex" : "none"
})
} }
bindRef = (elem: HTMLElement) => { bindRef = (elem: HTMLElement) => {
@ -69,10 +90,11 @@ export class ClusterView extends React.Component {
render() { render() {
const { cluster } = this; const { cluster } = this;
const showStatus = cluster && !cluster.accessible; const view = lensViews.get(cluster?.id);
const showStatusPage = cluster && (!cluster.accessible || !view?.isLoaded);
return ( return (
<div className="ClusterView" ref={this.bindRef}> <div className="ClusterView" ref={this.bindRef}>
{showStatus && <ClusterStatus clusterId={cluster.id}/>} {showStatusPage && <ClusterStatus clusterId={cluster.id}/>}
</div> </div>
) )
} }