fine-tuning

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman committed 2020-12-15 22:08:04 +02:00
1 parent 3a4a07ca7f
commit daf373168f
14 files changed
+259 -188

No files matched your search

+31
View File
@@ -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();
});
}
+36
View File
@@ -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;
}
+6
View File
@@ -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);
+5 -67
View File
@@ -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();
+40 -15
View File
@@ -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);
}