mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
1 parent
3a4a07ca7f
commit
daf373168f
14 files changed
+259
-188
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { ipcRenderer } from "electron";
|
||||
import { reaction } from "mobx";
|
||||
import { getMatchedClusterId, navigate } from "./helpers";
|
||||
import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc";
|
||||
import logger from "../../main/logger";
|
||||
|
||||
export function bindEvents() {
|
||||
if (!ipcRenderer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (process.isMainFrame) {
|
||||
// Keep track of active cluster-id for handling IPC/menus/etc.
|
||||
reaction(() => getMatchedClusterId(), clusterId => {
|
||||
broadcastMessage("cluster-view:current-id", clusterId);
|
||||
}, {
|
||||
fireImmediately: true
|
||||
});
|
||||
}
|
||||
|
||||
// Handle navigation via IPC (e.g. from top menu)
|
||||
subscribeToBroadcast("renderer:navigate", (event, url: string) => {
|
||||
logger.info(`[IPC]: ${event.type} ${JSON.stringify(url)}`, event);
|
||||
navigate(url);
|
||||
});
|
||||
|
||||
// Reload dashboard window
|
||||
subscribeToBroadcast("renderer:reload", () => {
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { LocationDescriptor } from "history";
|
||||
import { matchPath, RouteProps } from "react-router";
|
||||
import { PageParam, PageParamInit } from "./page-param";
|
||||
import { clusterViewRoute, IClusterViewRouteParams } from "../components/cluster-manager/cluster-view.route";
|
||||
import { navigation } from "./history";
|
||||
|
||||
export function navigate(location: LocationDescriptor) {
|
||||
const currentLocation = navigation.getPath();
|
||||
|
||||
navigation.push(location);
|
||||
|
||||
if (currentLocation === navigation.getPath()) {
|
||||
navigation.goBack(); // prevent sequences of same url in history
|
||||
}
|
||||
}
|
||||
|
||||
export function createPageParam<V = string>(init: PageParamInit<V>) {
|
||||
return new PageParam<V>(init, navigation);
|
||||
}
|
||||
|
||||
export function matchRoute<P>(route: string | string[] | RouteProps) {
|
||||
return matchPath<P>(navigation.location.pathname, route);
|
||||
}
|
||||
|
||||
export function isActiveRoute(route: string | string[] | RouteProps): boolean {
|
||||
return !!matchRoute(route);
|
||||
}
|
||||
|
||||
export function getMatchedClusterId(): string {
|
||||
const matched = matchPath<IClusterViewRouteParams>(navigation.location.pathname, {
|
||||
exact: true,
|
||||
path: clusterViewRoute.path
|
||||
});
|
||||
|
||||
return matched?.params.clusterId;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { ipcRenderer } from "electron";
|
||||
import { createBrowserHistory, createMemoryHistory } from "history";
|
||||
import { createObservableHistory } from "mobx-observable-history";
|
||||
|
||||
export const history = ipcRenderer ? createBrowserHistory() : createMemoryHistory();
|
||||
export const navigation = createObservableHistory(history);
|
||||
@@ -1,70 +1,8 @@
|
||||
// Navigation helpers
|
||||
// Navigation (renderer)
|
||||
|
||||
import { ipcRenderer } from "electron";
|
||||
import { reaction } from "mobx";
|
||||
import { matchPath, RouteProps } from "react-router";
|
||||
import { createObservableHistory } from "mobx-observable-history";
|
||||
import { createBrowserHistory, createMemoryHistory, LocationDescriptor } from "history";
|
||||
import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc";
|
||||
import { PageParam, PageParamInit } from "./page-param";
|
||||
import { clusterViewRoute, IClusterViewRouteParams } from "../components/cluster-manager/cluster-view.route";
|
||||
import logger from "../../main/logger";
|
||||
import { bindEvents } from "./events";
|
||||
|
||||
export let history = ipcRenderer ? createBrowserHistory() : createMemoryHistory();
|
||||
export let navigation = createObservableHistory(history);
|
||||
export * from "./history";
|
||||
export * from "./helpers";
|
||||
|
||||
export function navigate(location: LocationDescriptor) {
|
||||
const currentLocation = navigation.getPath();
|
||||
|
||||
navigation.push(location);
|
||||
|
||||
if (currentLocation === navigation.getPath()) {
|
||||
navigation.goBack(); // prevent sequences of same url in history
|
||||
}
|
||||
}
|
||||
|
||||
export function matchParams<P>(route: string | string[] | RouteProps) {
|
||||
return matchPath<P>(navigation.location.pathname, route);
|
||||
}
|
||||
|
||||
export function isActiveRoute(route: string | string[] | RouteProps): boolean {
|
||||
return !!matchParams(route);
|
||||
}
|
||||
|
||||
export function getMatchedClusterId(): string {
|
||||
const matched = matchPath<IClusterViewRouteParams>(navigation.location.pathname, {
|
||||
exact: true,
|
||||
path: clusterViewRoute.path
|
||||
});
|
||||
|
||||
return matched?.params.clusterId;
|
||||
}
|
||||
|
||||
export function createPageParam<V = string>(init: PageParamInit<V>) {
|
||||
return new PageParam<V>(init, navigation);
|
||||
}
|
||||
|
||||
if (ipcRenderer) {
|
||||
history = createBrowserHistory();
|
||||
navigation = createObservableHistory(history);
|
||||
|
||||
if (process.isMainFrame) {
|
||||
// Keep track of active cluster-id for handling IPC/menus/etc.
|
||||
reaction(() => getMatchedClusterId(), clusterId => {
|
||||
broadcastMessage("cluster-view:current-id", clusterId);
|
||||
}, {
|
||||
fireImmediately: true
|
||||
});
|
||||
}
|
||||
|
||||
// Handle navigation via IPC (e.g. from top menu)
|
||||
subscribeToBroadcast("renderer:navigate", (event, url: string) => {
|
||||
logger.info(`[IPC]: ${event.type} ${JSON.stringify(url)}`, event);
|
||||
navigate(url);
|
||||
});
|
||||
|
||||
// Reload dashboard window
|
||||
subscribeToBroadcast("renderer:reload", () => {
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
bindEvents();
|
||||
@@ -1,24 +1,25 @@
|
||||
// Manage observable URL-param via location.search
|
||||
// Manage observable URL-param from document.location.search
|
||||
import { IObservableHistory } from "mobx-observable-history";
|
||||
|
||||
export interface PageParamInit<V = any> {
|
||||
name: string;
|
||||
isSystem?: boolean;
|
||||
defaultValue?: V;
|
||||
defaultValueStringified?: string | string[]; // serialized version of "defaultValue"
|
||||
multiValues?: boolean; // false == by default
|
||||
multiValueSep?: string; // joining multiple values with separator, default: ","
|
||||
skipEmpty?: boolean; // skip empty value(s), e.g. "?param=", default: true
|
||||
parse?(values: string[]): V; // deserialize from URL
|
||||
stringify?(values: V): string | string[]; // serialize params to URL
|
||||
parse?(value: string[]): V; // deserialize from URL
|
||||
stringify?(value: V): string | string[]; // serialize params to URL
|
||||
}
|
||||
|
||||
export class PageParam<V = any | any[]> {
|
||||
export class PageParam<V = any> {
|
||||
static SYSTEM_PREFIX = "lens-";
|
||||
|
||||
readonly name: string;
|
||||
protected urlName: string;
|
||||
|
||||
constructor(private init: PageParamInit<V>, private history: IObservableHistory) {
|
||||
constructor(readonly init: PageParamInit<V>, protected history: IObservableHistory) {
|
||||
const { isSystem, name, skipEmpty = true } = init;
|
||||
|
||||
this.name = name;
|
||||
@@ -28,7 +29,7 @@ export class PageParam<V = any | any[]> {
|
||||
this.urlName = `${isSystem ? PageParam.SYSTEM_PREFIX : ""}${name}`;
|
||||
}
|
||||
|
||||
isEmpty(value: V) {
|
||||
isEmpty(value: V | any) {
|
||||
return [value].flat().every(value => value == "" || value == null);
|
||||
}
|
||||
|
||||
@@ -59,12 +60,10 @@ export class PageParam<V = any | any[]> {
|
||||
}
|
||||
|
||||
get(): V {
|
||||
const { history, urlName } = this;
|
||||
const { multiValueSep, defaultValue, skipEmpty } = this.init;
|
||||
const value = this.parse(history.searchParams.getAsArray(urlName, multiValueSep));
|
||||
const value = this.parse(this.getRaw());
|
||||
|
||||
if (skipEmpty && this.isEmpty(value)) {
|
||||
return defaultValue;
|
||||
if (this.init.skipEmpty && this.isEmpty(value)) {
|
||||
return this.getDefaultValue();
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -76,12 +75,29 @@ export class PageParam<V = any | any[]> {
|
||||
this.history.merge({ search }, replaceHistory);
|
||||
}
|
||||
|
||||
getDefaultValue(){
|
||||
return this.init.defaultValue;
|
||||
setRaw(value: string | string[]) {
|
||||
const { history, urlName } = this;
|
||||
const { multiValues, multiValueSep, skipEmpty } = this.init;
|
||||
const paramValue = multiValues ? [value].flat().join(multiValueSep) : String(value);
|
||||
|
||||
if (skipEmpty && this.isEmpty(paramValue)) {
|
||||
history.searchParams.delete(urlName);
|
||||
} else {
|
||||
history.searchParams.set(urlName, paramValue);
|
||||
}
|
||||
}
|
||||
|
||||
isDefault() {
|
||||
return this.get() === this.getDefaultValue();
|
||||
getRaw(): string[] {
|
||||
const { history, urlName } = this;
|
||||
const { multiValueSep } = this.init;
|
||||
|
||||
return history.searchParams.getAsArray(urlName, multiValueSep);
|
||||
}
|
||||
|
||||
getDefaultValue() {
|
||||
const { defaultValue, defaultValueStringified } = this.init;
|
||||
|
||||
return defaultValueStringified ? this.parse([defaultValueStringified].flat()) : defaultValue;
|
||||
}
|
||||
|
||||
clear() {
|
||||
@@ -113,3 +129,12 @@ export class PageParam<V = any | any[]> {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function isPageParamInit(paramInit: PageParamInit | any = {}): paramInit is PageParamInit {
|
||||
const init: PageParamInit = paramInit;
|
||||
|
||||
return [
|
||||
init.defaultValue !== undefined || init.defaultValueStringified !== undefined,
|
||||
typeof init.parse === "function" && typeof init.stringify === "function",
|
||||
].some(Boolean);
|
||||
}
|
||||
Reference in new issue
Block a user