From 96e69b344f75aa922865b82602f335569c2325aa Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 8 Dec 2020 15:00:08 +0200 Subject: [PATCH] renaming by jim's request: UrlParam => PageParam (type), createUrlParam => createPageParam (helper) Signed-off-by: Roman --- extensions/example-extension/page.tsx | 2 +- src/extensions/registries/page-registry.ts | 6 +-- src/extensions/renderer-api/navigation.ts | 3 +- .../components/+custom-resources/crd-list.tsx | 4 +- .../components/+namespaces/namespace.store.ts | 4 +- .../components/input/search-input-url.tsx | 4 +- .../kube-object/kube-object-details.tsx | 6 +-- src/renderer/components/table/table.tsx | 6 +-- src/renderer/navigation/helpers.ts | 33 -------------- src/renderer/navigation/index.ts | 45 ++++++++++++++----- .../{url-param.ts => page-param.ts} | 10 ++--- 11 files changed, 57 insertions(+), 66 deletions(-) delete mode 100644 src/renderer/navigation/helpers.ts rename src/renderer/navigation/{url-param.ts => page-param.ts} (91%) diff --git a/extensions/example-extension/page.tsx b/extensions/example-extension/page.tsx index a74482e990..a49faa4c39 100644 --- a/extensions/example-extension/page.tsx +++ b/extensions/example-extension/page.tsx @@ -4,7 +4,7 @@ import path from "path"; import { observer } from "mobx-react"; import { CoffeeDoodle } from "react-open-doodles"; -export const exampleId = Navigation.createUrlParam({ +export const exampleId = Navigation.createPageParam({ name: "exampleId", defaultValue: "demo", }); diff --git a/src/extensions/registries/page-registry.ts b/src/extensions/registries/page-registry.ts index 52e95db882..f3ae87f92b 100644 --- a/src/extensions/registries/page-registry.ts +++ b/src/extensions/registries/page-registry.ts @@ -3,7 +3,7 @@ import type React from "react"; import { action } from "mobx"; import { BaseRegistry } from "./base-registry"; import { LensExtension, sanitizeExtensionName } from "../lens-extension"; -import { UrlParam } from "../../renderer/navigation/url-param"; +import { PageParam } from "../../renderer/navigation/page-param"; import logger from "../../main/logger"; export interface PageRegistration { @@ -18,7 +18,7 @@ export interface PageRegistration { * Used to generate final page url when provided in getExtensionPageUrl()-helper. * Advanced usage: provide `UrlParam` as values to customize parsing/stringification from/to URL. */ - params?: PageTargetParams; + params?: PageTargetParams; } export interface PageComponents { @@ -56,7 +56,7 @@ export function getExtensionPageUrl(target: PageTarget): string { Object.entries(registeredPage.params).forEach(([name, param]) => { const targetParamValue = targetParams[name]; - if (param instanceof UrlParam) { + if (param instanceof PageParam) { pageUrl.searchParams.set(name, param.stringify(targetParamValue)); } else { const value = String(targetParamValue ?? param); diff --git a/src/extensions/renderer-api/navigation.ts b/src/extensions/renderer-api/navigation.ts index 4e281bc3de..2e08eb48c1 100644 --- a/src/extensions/renderer-api/navigation.ts +++ b/src/extensions/renderer-api/navigation.ts @@ -1,3 +1,4 @@ -export { createUrlParam, navigate, isActiveRoute, UrlParamInit, UrlParam } from "../../renderer/navigation"; +export { PageParam, PageParamInit } from "../../renderer/navigation/page-param"; +export { navigate, isActiveRoute, createPageParam } from "../../renderer/navigation"; export { hideDetails, showDetails, getDetailsUrl } from "../../renderer/components/kube-object/kube-object-details"; export { IURLParams } from "../../common/utils/buildUrl"; diff --git a/src/renderer/components/+custom-resources/crd-list.tsx b/src/renderer/components/+custom-resources/crd-list.tsx index 6fcd74c848..0737001e8c 100644 --- a/src/renderer/components/+custom-resources/crd-list.tsx +++ b/src/renderer/components/+custom-resources/crd-list.tsx @@ -10,10 +10,10 @@ import { KubeObjectListLayout } from "../kube-object"; import { crdStore } from "./crd.store"; import { CustomResourceDefinition } from "../../api/endpoints/crd.api"; import { Select, SelectOption } from "../select"; -import { createUrlParam } from "../../navigation"; +import { createPageParam } from "../../navigation"; import { Icon } from "../icon"; -export const crdGroupsUrlParam = createUrlParam({ +export const crdGroupsUrlParam = createPageParam({ name: "groups", multiValues: true, isSystem: true, diff --git a/src/renderer/components/+namespaces/namespace.store.ts b/src/renderer/components/+namespaces/namespace.store.ts index 7f3eb7ed75..c5a415abe0 100644 --- a/src/renderer/components/+namespaces/namespace.store.ts +++ b/src/renderer/components/+namespaces/namespace.store.ts @@ -2,14 +2,14 @@ import { action, comparer, observable, reaction } from "mobx"; import { autobind, createStorage } from "../../utils"; import { KubeObjectStore } from "../../kube-object.store"; import { Namespace, namespacesApi } from "../../api/endpoints"; -import { createUrlParam } from "../../navigation"; +import { createPageParam } from "../../navigation"; import { apiManager } from "../../api/api-manager"; import { isAllowedResource } from "../../../common/rbac"; import { getHostedCluster } from "../../../common/cluster-store"; const storage = createStorage("context_namespaces", []); -export const namespaceUrlParam = createUrlParam({ +export const namespaceUrlParam = createPageParam({ name: "namespaces", isSystem: true, multiValues: true, diff --git a/src/renderer/components/input/search-input-url.tsx b/src/renderer/components/input/search-input-url.tsx index bed3d54345..2b1045ede2 100644 --- a/src/renderer/components/input/search-input-url.tsx +++ b/src/renderer/components/input/search-input-url.tsx @@ -4,9 +4,9 @@ import { autorun, observable } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { InputProps } from "./input"; import { SearchInput } from "./search-input"; -import { createUrlParam } from "../../navigation"; +import { createPageParam } from "../../navigation"; -export const searchUrlParam = createUrlParam({ +export const searchUrlParam = createPageParam({ name: "search", isSystem: true, defaultValue: "", diff --git a/src/renderer/components/kube-object/kube-object-details.tsx b/src/renderer/components/kube-object/kube-object-details.tsx index f24e7dcf09..988282e238 100644 --- a/src/renderer/components/kube-object/kube-object-details.tsx +++ b/src/renderer/components/kube-object/kube-object-details.tsx @@ -4,7 +4,7 @@ import React from "react"; import { disposeOnUnmount, observer } from "mobx-react"; import { computed, observable, reaction } from "mobx"; import { Trans } from "@lingui/macro"; -import { createUrlParam, navigation } from "../../navigation"; +import { createPageParam, navigation } from "../../navigation"; import { Drawer } from "../drawer"; import { KubeObject } from "../../api/kube-object"; import { Spinner } from "../spinner"; @@ -14,12 +14,12 @@ import { CrdResourceDetails } from "../+custom-resources"; import { KubeObjectMenu } from "./kube-object-menu"; import { kubeObjectDetailRegistry } from "../../api/kube-object-detail-registry"; -export const kubeDetailsUrlParam = createUrlParam({ +export const kubeDetailsUrlParam = createPageParam({ name: "kube-details", isSystem: true, }); -export const kubeSelectedUrlParam = createUrlParam({ +export const kubeSelectedUrlParam = createPageParam({ name: "kube-selected", isSystem: true, get defaultValue() { diff --git a/src/renderer/components/table/table.tsx b/src/renderer/components/table/table.tsx index 92bb142060..9b5d396e85 100644 --- a/src/renderer/components/table/table.tsx +++ b/src/renderer/components/table/table.tsx @@ -9,7 +9,7 @@ import { TableRow, TableRowElem, TableRowProps } from "./table-row"; import { TableHead, TableHeadElem, TableHeadProps } from "./table-head"; import { TableCellElem } from "./table-cell"; import { VirtualList } from "../virtual-list"; -import { createUrlParam } from "../../navigation"; +import { createPageParam } from "../../navigation"; import { ItemObject } from "../../item.store"; export type TableSortBy = string; @@ -41,12 +41,12 @@ export interface TableProps extends React.DOMAttributes { getTableRow?: (uid: string) => React.ReactElement; } -export const sortByUrlParam = createUrlParam({ +export const sortByUrlParam = createPageParam({ name: "sort", isSystem: true, }); -export const orderByUrlParam = createUrlParam({ +export const orderByUrlParam = createPageParam({ name: "order", isSystem: true, }); diff --git a/src/renderer/navigation/helpers.ts b/src/renderer/navigation/helpers.ts deleted file mode 100644 index 71b7735589..0000000000 --- a/src/renderer/navigation/helpers.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Navigation helpers - -import { matchPath, RouteProps } from "react-router"; -import { LocationDescriptor } from "history"; -import { clusterViewRoute, IClusterViewRouteParams } from "../components/cluster-manager/cluster-view.route"; -import { navigation } from "./index"; - -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

(route: string | string[] | RouteProps) { - return matchPath

(navigation.location.pathname, route); -} - -export function isActiveRoute(route: string | string[] | RouteProps): boolean { - return !!matchParams(route); -} - -export function getMatchedClusterId(): string { - const matched = matchPath(navigation.location.pathname, { - exact: true, - path: clusterViewRoute.path - }); - - return matched?.params.clusterId; -} diff --git a/src/renderer/navigation/index.ts b/src/renderer/navigation/index.ts index 76863b2cc7..8e4bee117d 100644 --- a/src/renderer/navigation/index.ts +++ b/src/renderer/navigation/index.ts @@ -1,19 +1,47 @@ // Navigation helpers import { ipcRenderer } from "electron"; -import logger from "../../main/logger"; import { reaction } from "mobx"; +import { matchPath, RouteProps } from "react-router"; import { createObservableHistory } from "mobx-observable-history"; -import { createBrowserHistory, createMemoryHistory } from "history"; +import { createBrowserHistory, createMemoryHistory, LocationDescriptor } from "history"; import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc"; -import { getMatchedClusterId, navigate } from "./helpers"; -import { UrlParam, UrlParamInit } from "./url-param"; +import { PageParam, PageParamInit } from "./page-param"; +import { clusterViewRoute, IClusterViewRouteParams } from "../components/cluster-manager/cluster-view.route"; +import logger from "../../main/logger"; export let history = ipcRenderer ? createBrowserHistory() : createMemoryHistory(); export let navigation = createObservableHistory(history); -export function createUrlParam(init: UrlParamInit) { - return new UrlParam(init, navigation); +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

(route: string | string[] | RouteProps) { + return matchPath

(navigation.location.pathname, route); +} + +export function isActiveRoute(route: string | string[] | RouteProps): boolean { + return !!matchParams(route); +} + +export function getMatchedClusterId(): string { + const matched = matchPath(navigation.location.pathname, { + exact: true, + path: clusterViewRoute.path + }); + + return matched?.params.clusterId; +} + +export function createPageParam(init: PageParamInit) { + return new PageParam(init, navigation); } if (ipcRenderer) { @@ -40,8 +68,3 @@ if (ipcRenderer) { location.reload(); }); } - -// Re-exports from sub-modules -export * from "./helpers"; -export * from "./url-param"; - diff --git a/src/renderer/navigation/url-param.ts b/src/renderer/navigation/page-param.ts similarity index 91% rename from src/renderer/navigation/url-param.ts rename to src/renderer/navigation/page-param.ts index 9cdaa98502..0577116294 100644 --- a/src/renderer/navigation/url-param.ts +++ b/src/renderer/navigation/page-param.ts @@ -1,7 +1,7 @@ // Manage observable URL-param via location.search import { IObservableHistory } from "mobx-observable-history"; -export interface UrlParamInit { +export interface PageParamInit { name: string; isSystem?: boolean; defaultValue?: V; @@ -12,20 +12,20 @@ export interface UrlParamInit { stringify?(values: V): string | string[]; // serialize params to URL } -export class UrlParam { +export class PageParam { static SYSTEM_PREFIX = "lens-"; readonly name: string; - readonly urlName: string; + protected urlName: string; - constructor(private init: UrlParamInit, private history: IObservableHistory) { + constructor(private init: PageParamInit, private history: IObservableHistory) { const { isSystem, name, skipEmpty = true } = init; this.name = name; this.init.skipEmpty = skipEmpty; // prefixing to avoid collisions with extensions - this.urlName = `${isSystem ? UrlParam.SYSTEM_PREFIX : ""}${name}`; + this.urlName = `${isSystem ? PageParam.SYSTEM_PREFIX : ""}${name}`; } isEmpty(value: V) {