mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cascade different refactorings to eliminate global shared state from app paths
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
1 parent
908a40975e
commit
9d33fff906
336 files changed
+9105
-3307
No files matched your search
@@ -31,25 +31,37 @@ import { KubeObjectMeta } from "../kube-object-meta";
|
||||
import { ServicePortComponent } from "./service-port-component";
|
||||
import { endpointStore } from "../+network-endpoints/endpoints.store";
|
||||
import { ServiceDetailsEndpoint } from "./service-details-endpoint";
|
||||
import { kubeWatchApi } from "../../../common/k8s-api/kube-watch-api";
|
||||
import { portForwardStore } from "../../port-forward";
|
||||
import type { PortForwardStore } from "../../port-forward";
|
||||
import logger from "../../../common/logger";
|
||||
import type { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import type { Disposer } from "../../../common/utils";
|
||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||
import kubeWatchApiInjectable
|
||||
from "../../kube-watch-api/kube-watch-api.injectable";
|
||||
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
||||
import type { KubeWatchSubscribeStoreOptions } from "../../kube-watch-api/kube-watch-api";
|
||||
|
||||
interface Props extends KubeObjectDetailsProps<Service> {
|
||||
}
|
||||
|
||||
interface Dependencies {
|
||||
subscribeStores: (stores: KubeObjectStore<KubeObject>[], options: KubeWatchSubscribeStoreOptions) => Disposer
|
||||
portForwardStore: PortForwardStore
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ServiceDetails extends React.Component<Props> {
|
||||
class NonInjectedServiceDetails extends React.Component<Props & Dependencies> {
|
||||
componentDidMount() {
|
||||
const { object: service } = this.props;
|
||||
|
||||
disposeOnUnmount(this, [
|
||||
kubeWatchApi.subscribeStores([
|
||||
this.props.subscribeStores([
|
||||
endpointStore,
|
||||
], {
|
||||
namespaces: [service.getNs()],
|
||||
}),
|
||||
portForwardStore.watch(),
|
||||
this.props.portForwardStore.watch(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -140,3 +152,15 @@ export class ServiceDetails extends React.Component<Props> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const ServiceDetails = withInjectables<Dependencies, Props>(
|
||||
NonInjectedServiceDetails,
|
||||
|
||||
{
|
||||
getProps: (di, props) => ({
|
||||
subscribeStores: di.inject(kubeWatchApiInjectable).subscribeStores,
|
||||
portForwardStore: di.inject(portForwardStoreInjectable),
|
||||
...props,
|
||||
}),
|
||||
},
|
||||
);
|
||||
@@ -28,22 +28,36 @@ import { observable, makeObservable, reaction } from "mobx";
|
||||
import { cssNames } from "../../utils";
|
||||
import { Notifications } from "../notifications";
|
||||
import { Button } from "../button";
|
||||
import { aboutPortForwarding, addPortForward, getPortForward, getPortForwards, openPortForward, PortForwardDialog, portForwardStore, predictProtocol, removePortForward } from "../../port-forward";
|
||||
import { aboutPortForwarding, getPortForward, getPortForwards, openPortForward, PortForwardStore, predictProtocol } from "../../port-forward";
|
||||
import type { ForwardedPort } from "../../port-forward";
|
||||
import { Spinner } from "../spinner";
|
||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
||||
import removePortForwardInjectable from "../../port-forward/port-forward-store/remove-port-forward/remove-port-forward.injectable";
|
||||
import portForwardDialogModelInjectable
|
||||
from "../../port-forward/port-forward-dialog-model/port-forward-dialog-model.injectable";
|
||||
import addPortForwardInjectable
|
||||
from "../../port-forward/port-forward-store/add-port-forward/add-port-forward.injectable";
|
||||
|
||||
interface Props {
|
||||
service: Service;
|
||||
port: ServicePort;
|
||||
}
|
||||
|
||||
interface Dependencies {
|
||||
portForwardStore: PortForwardStore
|
||||
removePortForward: (item: ForwardedPort) => Promise<void>
|
||||
addPortForward: (item: ForwardedPort) => Promise<number>
|
||||
openPortForwardDialog: (item: ForwardedPort, options: { openInBrowser: boolean }) => void
|
||||
}
|
||||
|
||||
@observer
|
||||
export class ServicePortComponent extends React.Component<Props> {
|
||||
class NonInjectedServicePortComponent extends React.Component<Props & Dependencies> {
|
||||
@observable waiting = false;
|
||||
@observable forwardPort = 0;
|
||||
@observable isPortForwarded = false;
|
||||
|
||||
constructor(props: Props) {
|
||||
constructor(props: Props & Dependencies) {
|
||||
super(props);
|
||||
makeObservable(this);
|
||||
this.checkExistingPortForwarding();
|
||||
@@ -51,7 +65,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
|
||||
componentDidMount() {
|
||||
disposeOnUnmount(this, [
|
||||
reaction(() => [portForwardStore.portForwards, this.props.service], () => this.checkExistingPortForwarding()),
|
||||
reaction(() => [this.props.portForwardStore.portForwards, this.props.service], () => this.checkExistingPortForwarding()),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -96,7 +110,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
// determine how many port-forwards are already active
|
||||
const { length } = await getPortForwards();
|
||||
|
||||
this.forwardPort = await addPortForward(portForward);
|
||||
this.forwardPort = await this.props.addPortForward(portForward);
|
||||
|
||||
if (this.forwardPort) {
|
||||
portForward.forwardPort = this.forwardPort;
|
||||
@@ -129,7 +143,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
this.waiting = true;
|
||||
|
||||
try {
|
||||
await removePortForward(portForward);
|
||||
await this.props.removePortForward(portForward);
|
||||
this.isPortForwarded = false;
|
||||
} catch (error) {
|
||||
Notifications.error(`Error occurred stopping the port-forward from port ${portForward.forwardPort}.`);
|
||||
@@ -155,7 +169,7 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
protocol: predictProtocol(port.name),
|
||||
};
|
||||
|
||||
PortForwardDialog.open(portForward, { openInBrowser: true });
|
||||
this.props.openPortForwardDialog(portForward, { openInBrowser: true });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -172,3 +186,18 @@ export class ServicePortComponent extends React.Component<Props> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export const ServicePortComponent = withInjectables<Dependencies, Props>(
|
||||
NonInjectedServicePortComponent,
|
||||
|
||||
{
|
||||
getProps: (di, props) => ({
|
||||
portForwardStore: di.inject(portForwardStoreInjectable),
|
||||
removePortForward: di.inject(removePortForwardInjectable),
|
||||
addPortForward: di.inject(addPortForwardInjectable),
|
||||
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
||||
...props,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user