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

Allow injection of history

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2022-01-20 11:35:17 +02:00
parent 9e823535d4
commit 6670dd4c3d
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
6 changed files with 63 additions and 14 deletions

View File

@ -11,12 +11,13 @@ import { createPortal } from "react-dom";
import { cssNames, noop, StorageHelper } from "../../utils"; import { cssNames, noop, StorageHelper } from "../../utils";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { Animate, AnimateName } from "../animate"; import { Animate, AnimateName } from "../animate";
import { history } from "../../navigation";
import { ResizeDirection, ResizeGrowthDirection, ResizeSide, ResizingAnchor } from "../resizing-anchor"; import { ResizeDirection, ResizeGrowthDirection, ResizeSide, ResizingAnchor } from "../resizing-anchor";
import drawerStorageInjectable, { import drawerStorageInjectable, {
defaultDrawerWidth, defaultDrawerWidth,
} from "./drawer-storage/drawer-storage.injectable"; } from "./drawer-storage/drawer-storage.injectable";
import { withInjectables } from "@ogre-tools/injectable-react"; import { withInjectables } from "@ogre-tools/injectable-react";
import historyInjectable from "../../navigation/history.injectable";
import type { History } from "history";
export type DrawerPosition = "top" | "left" | "right" | "bottom"; export type DrawerPosition = "top" | "left" | "right" | "bottom";
@ -59,6 +60,7 @@ resizingAnchorProps.set("top", [ResizeDirection.VERTICAL, ResizeSide.TRAILING, R
resizingAnchorProps.set("bottom", [ResizeDirection.VERTICAL, ResizeSide.LEADING, ResizeGrowthDirection.BOTTOM_TO_TOP]); resizingAnchorProps.set("bottom", [ResizeDirection.VERTICAL, ResizeSide.LEADING, ResizeGrowthDirection.BOTTOM_TO_TOP]);
interface Dependencies { interface Dependencies {
history: History
drawerStorage: StorageHelper<{ width: number }>; drawerStorage: StorageHelper<{ width: number }>;
} }
@ -70,7 +72,7 @@ class NonInjectedDrawer extends React.Component<DrawerProps & Dependencies, Stat
private scrollElem: HTMLElement; private scrollElem: HTMLElement;
private scrollPos = new Map<string, number>(); private scrollPos = new Map<string, number>();
private stopListenLocation = history.listen(() => { private stopListenLocation = this.props.history.listen(() => {
this.restoreScrollPos(); this.restoreScrollPos();
}); });
@ -111,14 +113,14 @@ class NonInjectedDrawer extends React.Component<DrawerProps & Dependencies, Stat
saveScrollPos = () => { saveScrollPos = () => {
if (!this.scrollElem) return; if (!this.scrollElem) return;
const key = history.location.key; const key = this.props.history.location.key;
this.scrollPos.set(key, this.scrollElem.scrollTop); this.scrollPos.set(key, this.scrollElem.scrollTop);
}; };
restoreScrollPos = () => { restoreScrollPos = () => {
if (!this.scrollElem) return; if (!this.scrollElem) return;
const key = history.location.key; const key = this.props.history.location.key;
this.scrollElem.scrollTop = this.scrollPos.get(key) || 0; this.scrollElem.scrollTop = this.scrollPos.get(key) || 0;
}; };
@ -232,6 +234,7 @@ export const Drawer = withInjectables<Dependencies, DrawerProps>(
{ {
getProps: (di, props) => ({ getProps: (di, props) => ({
history: di.inject(historyInjectable),
drawerStorage: di.inject(drawerStorageInjectable), drawerStorage: di.inject(drawerStorageInjectable),
...props, ...props,
}), }),

View File

@ -6,7 +6,6 @@ import React from "react";
import { observable, makeObservable } from "mobx"; import { observable, makeObservable } from "mobx";
import { disposeOnUnmount, observer } from "mobx-react"; import { disposeOnUnmount, observer } from "mobx-react";
import { Redirect, Route, Router, Switch } from "react-router"; import { Redirect, Route, Router, Switch } from "react-router";
import { history } from "../../navigation";
import { UserManagement } from "../../components/+user-management/user-management"; import { UserManagement } from "../../components/+user-management/user-management";
import { ConfirmDialog } from "../../components/confirm-dialog"; import { ConfirmDialog } from "../../components/confirm-dialog";
import { ClusterOverview } from "../../components/+cluster/cluster-overview"; import { ClusterOverview } from "../../components/+cluster/cluster-overview";
@ -41,17 +40,18 @@ import { PortForwardDialog } from "../../port-forward";
import { DeleteClusterDialog } from "../../components/delete-cluster-dialog"; import { DeleteClusterDialog } from "../../components/delete-cluster-dialog";
import type { NamespaceStore } from "../../components/+namespaces/namespace-store/namespace.store"; import type { NamespaceStore } from "../../components/+namespaces/namespace-store/namespace.store";
import { withInjectables } from "@ogre-tools/injectable-react"; import { withInjectables } from "@ogre-tools/injectable-react";
import namespaceStoreInjectable import namespaceStoreInjectable from "../../components/+namespaces/namespace-store/namespace-store.injectable";
from "../../components/+namespaces/namespace-store/namespace-store.injectable";
import type { ClusterId } from "../../../common/cluster-types"; import type { ClusterId } from "../../../common/cluster-types";
import hostedClusterInjectable import hostedClusterInjectable from "../../../common/cluster-store/hosted-cluster/hosted-cluster.injectable";
from "../../../common/cluster-store/hosted-cluster/hosted-cluster.injectable";
import type { KubeObjectStore } from "../../../common/k8s-api/kube-object.store"; import type { KubeObjectStore } from "../../../common/k8s-api/kube-object.store";
import type { KubeObject } from "../../../common/k8s-api/kube-object"; import type { KubeObject } from "../../../common/k8s-api/kube-object";
import type { Disposer } from "../../../common/utils"; import type { Disposer } from "../../../common/utils";
import kubeWatchApiInjectable from "../../kube-watch-api/kube-watch-api.injectable"; import kubeWatchApiInjectable from "../../kube-watch-api/kube-watch-api.injectable";
import historyInjectable from "../../navigation/history.injectable";
import type { History } from "history";
interface Dependencies { interface Dependencies {
history: History,
namespaceStore: NamespaceStore namespaceStore: NamespaceStore
hostedClusterId: ClusterId hostedClusterId: ClusterId
subscribeStores: (stores: KubeObjectStore<KubeObject>[]) => Disposer subscribeStores: (stores: KubeObjectStore<KubeObject>[]) => Disposer
@ -135,10 +135,11 @@ class NonInjectedClusterFrame extends React.Component<Dependencies> {
render() { render() {
return ( return (
<Router history={history}> <Router history={this.props.history}>
<ErrorBoundary> <ErrorBoundary>
<MainLayout sidebar={<Sidebar />} footer={<Dock />}> <MainLayout sidebar={<Sidebar />} footer={<Dock />}>
<Switch> <Switch>
<Route component={ClusterOverview} {...routes.clusterRoute}/> <Route component={ClusterOverview} {...routes.clusterRoute}/>
<Route component={Nodes} {...routes.nodesRoute}/> <Route component={Nodes} {...routes.nodesRoute}/>
<Route component={Workloads} {...routes.workloadsRoute}/> <Route component={Workloads} {...routes.workloadsRoute}/>
@ -181,6 +182,7 @@ class NonInjectedClusterFrame extends React.Component<Dependencies> {
export const ClusterFrame = withInjectables<Dependencies>(NonInjectedClusterFrame, { export const ClusterFrame = withInjectables<Dependencies>(NonInjectedClusterFrame, {
getProps: di => ({ getProps: di => ({
history: di.inject(historyInjectable),
namespaceStore: di.inject(namespaceStoreInjectable), namespaceStore: di.inject(namespaceStoreInjectable),
hostedClusterId: di.inject(hostedClusterInjectable).id, hostedClusterId: di.inject(hostedClusterInjectable).id,
subscribeStores: di.inject(kubeWatchApiInjectable).subscribeStores, subscribeStores: di.inject(kubeWatchApiInjectable).subscribeStores,

View File

@ -7,7 +7,6 @@ import { injectSystemCAs } from "../../../common/system-ca";
import React from "react"; import React from "react";
import { Route, Router, Switch } from "react-router"; import { Route, Router, Switch } from "react-router";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { history } from "../../navigation";
import { ClusterManager } from "../../components/cluster-manager"; import { ClusterManager } from "../../components/cluster-manager";
import { ErrorBoundary } from "../../components/error-boundary"; import { ErrorBoundary } from "../../components/error-boundary";
import { Notifications } from "../../components/notifications"; import { Notifications } from "../../components/notifications";
@ -16,14 +15,21 @@ import { CommandContainer } from "../../components/command-palette/command-conta
import { ipcRenderer } from "electron"; import { ipcRenderer } from "electron";
import { IpcRendererNavigationEvents } from "../../navigation/events"; import { IpcRendererNavigationEvents } from "../../navigation/events";
import { ClusterFrameHandler } from "../../components/cluster-manager/lens-views"; import { ClusterFrameHandler } from "../../components/cluster-manager/lens-views";
import historyInjectable from "../../navigation/history.injectable";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { History } from "history";
injectSystemCAs(); injectSystemCAs();
interface Dependencies {
history: History
}
@observer @observer
export class RootFrame extends React.Component { class NonInjectedRootFrame extends React.Component<Dependencies> {
static displayName = "RootFrame"; static displayName = "RootFrame";
constructor(props: {}) { constructor(props: Dependencies) {
super(props); super(props);
ClusterFrameHandler.createInstance(); ClusterFrameHandler.createInstance();
@ -35,7 +41,7 @@ export class RootFrame extends React.Component {
render() { render() {
return ( return (
<Router history={history}> <Router history={this.props.history}>
<ErrorBoundary> <ErrorBoundary>
<Switch> <Switch>
<Route component={ClusterManager} /> <Route component={ClusterManager} />
@ -48,3 +54,7 @@ export class RootFrame extends React.Component {
); );
} }
} }
export const RootFrame = withInjectables(NonInjectedRootFrame, {
getProps: (di) => ({ history: di.inject(historyInjectable) }),
});

View File

@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { history } from "./history";
const historyInjectable = getInjectable({
instantiate: () => history,
lifecycle: lifecycleEnum.singleton,
});
export default historyInjectable;

View File

@ -14,8 +14,14 @@ export const searchParamsOptions: ObservableSearchParamsOptions = {
joinArraysWith: ",", // param values splitter, applicable only with {joinArrays:true} joinArraysWith: ",", // param values splitter, applicable only with {joinArrays:true}
}; };
/**
* @deprecated: Switch to using di.inject(historyInjectable)
*/
export const history = ipcRenderer ? createBrowserHistory() : createMemoryHistory(); export const history = ipcRenderer ? createBrowserHistory() : createMemoryHistory();
/**
* @deprecated: Switch to using di.inject(observableHistoryInjectable)
*/
export const navigation = createObservableHistory(history, { export const navigation = createObservableHistory(history, {
searchParams: searchParamsOptions, searchParams: searchParamsOptions,
}); });

View File

@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { navigation as observableHistory } from "./history";
const observableHistoryInjectable = getInjectable({
instantiate: () => observableHistory,
lifecycle: lifecycleEnum.singleton,
});
export default observableHistoryInjectable;