From d20e61b3e2a27dfd5481d49bff5f22ad36a5a011 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 15 Jul 2020 14:09:31 +0300 Subject: [PATCH] added more routes, refactoring Signed-off-by: Roman --- src/common/ipc-helpers.ts | 6 +-- src/common/workspace-store.ts | 20 +++++----- .../components/+add-cluster/add-cluster.tsx | 4 +- .../cluster-settings.route.ts | 8 ++++ .../+cluster-settings/cluster-settings.tsx | 5 +-- .../components/+cluster-settings/index.ts | 1 + .../components/+landing-page/index.tsx | 1 + .../components/+landing-page/landing-page.tsx | 5 +-- .../components/+preferences/index.tsx | 2 + .../+preferences/preferences.route.ts | 8 ++++ .../components/+preferences/preferences.tsx | 5 +-- src/renderer/components/+whats-new/index.tsx | 2 + .../components/+whats-new/whats-new.route.ts | 8 ++++ .../components/+whats-new/whats-new.tsx | 5 +-- src/renderer/components/+workspaces/index.ts | 1 + .../+workspaces/workspaces.route.ts | 8 ++++ .../components/+workspaces/workspaces.tsx | 2 + src/renderer/components/app.tsx | 10 +++++ .../cluster-manager/bottom-bar.scss | 7 +++- .../components/cluster-manager/bottom-bar.tsx | 37 +++++++++++-------- .../cluster-manager/cluster-context.tsx | 2 +- .../cluster-manager/clusters-menu.tsx | 30 +++++++++------ 22 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 src/renderer/components/+cluster-settings/cluster-settings.route.ts create mode 100644 src/renderer/components/+preferences/preferences.route.ts create mode 100644 src/renderer/components/+whats-new/whats-new.route.ts create mode 100644 src/renderer/components/+workspaces/workspaces.route.ts diff --git a/src/common/ipc-helpers.ts b/src/common/ipc-helpers.ts index 5ccc77e9eb..6bec3d577f 100644 --- a/src/common/ipc-helpers.ts +++ b/src/common/ipc-helpers.ts @@ -25,14 +25,14 @@ export function broadcastMessage({ channel, filter }: IpcBroadcastOpts, ...args: filter = webContent => webContent.getType() === "window" } webContents.getAllWebContents().filter(filter).forEach(webContent => { - logger.info(`[IPC]: broadcasting ${channel} to ${webContent.getType()}=${webContent.id}`); + logger.debug(`[IPC]: broadcasting ${channel} to ${webContent.getType()}=${webContent.id}`); webContent.send(channel, ...args); }) } -// fixme: support timeout +// todo: support timeout export async function invokeMessage(channel: IpcChannel, ...args: any[]): Promise { - logger.info(`[IPC]: invoke channel "${channel}"`, { args }); + logger.debug(`[IPC]: invoke channel "${channel}"`, { args }); return ipcRenderer.invoke(channel, ...args); } diff --git a/src/common/workspace-store.ts b/src/common/workspace-store.ts index bf8cdcf90c..393f14c0df 100644 --- a/src/common/workspace-store.ts +++ b/src/common/workspace-store.ts @@ -24,7 +24,7 @@ export class WorkspaceStore extends BaseStore { }); } - @observable currentWorkspace = WorkspaceStore.defaultId; + @observable currentWorkspaceId = WorkspaceStore.defaultId; @observable workspaces = observable.map({ [WorkspaceStore.defaultId]: { @@ -33,6 +33,10 @@ export class WorkspaceStore extends BaseStore { } }); + @computed get currentWorkspace(): Workspace { + return this.getById(this.currentWorkspaceId); + } + @computed get workspacesList() { return Array.from(this.workspaces.values()); } @@ -41,12 +45,6 @@ export class WorkspaceStore extends BaseStore { return this.workspaces.get(id); } - @action - setCurrent(id: WorkspaceId) { - if (!this.getById(id)) return; - this.currentWorkspace = id; - } - @action public saveWorkspace(workspace: Workspace) { const id = workspace.id; @@ -65,8 +63,8 @@ export class WorkspaceStore extends BaseStore { if (id === WorkspaceStore.defaultId) { throw new Error("Cannot remove default workspace"); } - if (id === this.currentWorkspace) { - this.currentWorkspace = WorkspaceStore.defaultId; + if (id === this.currentWorkspaceId) { + this.currentWorkspaceId = WorkspaceStore.defaultId; } this.workspaces.delete(id); clusterStore.removeByWorkspaceId(id) @@ -75,7 +73,7 @@ export class WorkspaceStore extends BaseStore { @action protected fromStore({ currentWorkspace, workspaces = [] }: WorkspaceStoreModel) { if (currentWorkspace) { - this.currentWorkspace = currentWorkspace + this.currentWorkspaceId = currentWorkspace } if (workspaces.length) { this.workspaces.clear(); @@ -87,7 +85,7 @@ export class WorkspaceStore extends BaseStore { toJSON(): WorkspaceStoreModel { return toJS({ - currentWorkspace: this.currentWorkspace, + currentWorkspace: this.currentWorkspaceId, workspaces: this.workspacesList, }, { recurseEverything: true diff --git a/src/renderer/components/+add-cluster/add-cluster.tsx b/src/renderer/components/+add-cluster/add-cluster.tsx index 899337f696..b4ff602145 100644 --- a/src/renderer/components/+add-cluster/add-cluster.tsx +++ b/src/renderer/components/+add-cluster/add-cluster.tsx @@ -1,11 +1,11 @@ import "./add-cluster.scss" import React from "react"; -import { observable } from "mobx"; +import { observer } from "mobx-react"; interface Props { } -@observable +@observer export class AddCluster extends React.Component { render() { return ( diff --git a/src/renderer/components/+cluster-settings/cluster-settings.route.ts b/src/renderer/components/+cluster-settings/cluster-settings.route.ts new file mode 100644 index 0000000000..7501ba60bd --- /dev/null +++ b/src/renderer/components/+cluster-settings/cluster-settings.route.ts @@ -0,0 +1,8 @@ +import { RouteProps } from "react-router"; +import { buildURL } from "../../navigation"; + +export const clusterSettingsRoute: RouteProps = { + path: "/cluster-settings" +} + +export const clusterSettingsURL = buildURL(clusterSettingsRoute.path) diff --git a/src/renderer/components/+cluster-settings/cluster-settings.tsx b/src/renderer/components/+cluster-settings/cluster-settings.tsx index c5aedb4017..8cb0c779ac 100644 --- a/src/renderer/components/+cluster-settings/cluster-settings.tsx +++ b/src/renderer/components/+cluster-settings/cluster-settings.tsx @@ -1,9 +1,8 @@ import "./cluster-settings.scss" import React from "react"; +import { observer } from "mobx-react"; -interface Props { -} - +@observer export class ClusterSettings extends React.Component { render() { return ( diff --git a/src/renderer/components/+cluster-settings/index.ts b/src/renderer/components/+cluster-settings/index.ts index 00a0fcf4a3..75dd6e60ea 100644 --- a/src/renderer/components/+cluster-settings/index.ts +++ b/src/renderer/components/+cluster-settings/index.ts @@ -1 +1,2 @@ +export * from "./cluster-settings.route" export * from "./cluster-settings" diff --git a/src/renderer/components/+landing-page/index.tsx b/src/renderer/components/+landing-page/index.tsx index e69de29bb2..e3632f1b0f 100644 --- a/src/renderer/components/+landing-page/index.tsx +++ b/src/renderer/components/+landing-page/index.tsx @@ -0,0 +1 @@ +export * from "./landing-page" \ No newline at end of file diff --git a/src/renderer/components/+landing-page/landing-page.tsx b/src/renderer/components/+landing-page/landing-page.tsx index 775f5cfcb9..f5bf542d24 100644 --- a/src/renderer/components/+landing-page/landing-page.tsx +++ b/src/renderer/components/+landing-page/landing-page.tsx @@ -1,9 +1,8 @@ import "./landing-page.scss" import React from "react"; +import { observer } from "mobx-react"; -interface Props { -} - +@observer export class LandingPage extends React.Component { render() { return ( diff --git a/src/renderer/components/+preferences/index.tsx b/src/renderer/components/+preferences/index.tsx index e69de29bb2..b4d669a029 100644 --- a/src/renderer/components/+preferences/index.tsx +++ b/src/renderer/components/+preferences/index.tsx @@ -0,0 +1,2 @@ +export * from "./preferences.route" +export * from "./preferences" diff --git a/src/renderer/components/+preferences/preferences.route.ts b/src/renderer/components/+preferences/preferences.route.ts new file mode 100644 index 0000000000..6e71a88963 --- /dev/null +++ b/src/renderer/components/+preferences/preferences.route.ts @@ -0,0 +1,8 @@ +import { RouteProps } from "react-router"; +import { buildURL } from "../../navigation"; + +export const preferencesRoute: RouteProps = { + path: "/preferences" +} + +export const preferencesURL = buildURL(preferencesRoute.path) diff --git a/src/renderer/components/+preferences/preferences.tsx b/src/renderer/components/+preferences/preferences.tsx index cac9241f1b..d8abe6076c 100644 --- a/src/renderer/components/+preferences/preferences.tsx +++ b/src/renderer/components/+preferences/preferences.tsx @@ -1,9 +1,8 @@ import "./preferences.scss" import React from "react"; +import { observer } from "mobx-react"; -interface Props { -} - +@observer export class Preferences extends React.Component { render() { return ( diff --git a/src/renderer/components/+whats-new/index.tsx b/src/renderer/components/+whats-new/index.tsx index e69de29bb2..9f46112328 100644 --- a/src/renderer/components/+whats-new/index.tsx +++ b/src/renderer/components/+whats-new/index.tsx @@ -0,0 +1,2 @@ +export * from "./whats-new.route" +export * from "./whats-new" diff --git a/src/renderer/components/+whats-new/whats-new.route.ts b/src/renderer/components/+whats-new/whats-new.route.ts new file mode 100644 index 0000000000..ee251ff81e --- /dev/null +++ b/src/renderer/components/+whats-new/whats-new.route.ts @@ -0,0 +1,8 @@ +import { RouteProps } from "react-router"; +import { buildURL } from "../../navigation"; + +export const whatsNewRoute: RouteProps = { + path: "/what-s-new" +} + +export const whatsNewURL = buildURL(whatsNewRoute.path) diff --git a/src/renderer/components/+whats-new/whats-new.tsx b/src/renderer/components/+whats-new/whats-new.tsx index f08ecf7693..ab9069d46b 100644 --- a/src/renderer/components/+whats-new/whats-new.tsx +++ b/src/renderer/components/+whats-new/whats-new.tsx @@ -1,9 +1,8 @@ import "./whats-new.scss" import React from "react"; +import { observer } from "mobx-react"; -interface Props { -} - +@observer export class WhatsNew extends React.Component { render() { return ( diff --git a/src/renderer/components/+workspaces/index.ts b/src/renderer/components/+workspaces/index.ts index b25d55a129..6aa0afedd3 100644 --- a/src/renderer/components/+workspaces/index.ts +++ b/src/renderer/components/+workspaces/index.ts @@ -1 +1,2 @@ +export * from "./workspaces.route" export * from "./workspaces" diff --git a/src/renderer/components/+workspaces/workspaces.route.ts b/src/renderer/components/+workspaces/workspaces.route.ts new file mode 100644 index 0000000000..bfdbe012a6 --- /dev/null +++ b/src/renderer/components/+workspaces/workspaces.route.ts @@ -0,0 +1,8 @@ +import { RouteProps } from "react-router"; +import { buildURL } from "../../navigation"; + +export const workspacesRoute: RouteProps = { + path: "/workspaces" +} + +export const workspacesURL = buildURL(workspacesRoute.path) diff --git a/src/renderer/components/+workspaces/workspaces.tsx b/src/renderer/components/+workspaces/workspaces.tsx index c3bb76cf5a..fa6cf28271 100644 --- a/src/renderer/components/+workspaces/workspaces.tsx +++ b/src/renderer/components/+workspaces/workspaces.tsx @@ -1,6 +1,8 @@ import "./workspaces.scss" import React from "react"; +import { observer } from "mobx-react"; +@observer export class Workspaces extends React.Component { render() { return ( diff --git a/src/renderer/components/app.tsx b/src/renderer/components/app.tsx index 673587381c..c1c011d0d8 100755 --- a/src/renderer/components/app.tsx +++ b/src/renderer/components/app.tsx @@ -1,6 +1,7 @@ import "./app.scss"; import React, { Fragment } from "react"; +import { observer } from "mobx-react"; import { i18nStore } from "../i18n"; import { configStore } from "../config.store"; import { Terminal } from "./dock/terminal"; @@ -29,7 +30,12 @@ import { DeploymentScaleDialog } from "./+workloads-deployments/deployment-scale import { CustomResources } from "./+custom-resources/custom-resources"; import { crdRoute } from "./+custom-resources"; import { isAllowedResource } from "../api/rbac"; +import { AddCluster, addClusterRoute } from "./+add-cluster"; +import { LandingPage } from "./+landing-page"; +import { clusterStore } from "../../common/cluster-store"; +import { ClusterSettings, clusterSettingsRoute } from "./+cluster-settings"; +@observer export class App extends React.Component { static rootElem = document.getElementById('app'); @@ -40,11 +46,15 @@ export class App extends React.Component { } render() { + const showLanding = clusterStore.clusters.size === 0; const homeUrl = isAllowedResource(["events", "nodes", "pods"]) ? clusterURL() : workloadsURL(); return ( + {showLanding && } + + diff --git a/src/renderer/components/cluster-manager/bottom-bar.scss b/src/renderer/components/cluster-manager/bottom-bar.scss index 74a33494a8..7193084180 100644 --- a/src/renderer/components/cluster-manager/bottom-bar.scss +++ b/src/renderer/components/cluster-manager/bottom-bar.scss @@ -1,10 +1,13 @@ .BottomBar { + $spacing: $padding / 2; + font-size: $font-size-small; background-color: #3d90ce; - padding: $padding / 2 $padding; + padding: $spacing $padding; color: white; - #workspace { + #current-workspace { + --flex-gap: #{$spacing}; cursor: pointer; } } diff --git a/src/renderer/components/cluster-manager/bottom-bar.tsx b/src/renderer/components/cluster-manager/bottom-bar.tsx index b09b16d8c5..fe3153ecad 100644 --- a/src/renderer/components/cluster-manager/bottom-bar.tsx +++ b/src/renderer/components/cluster-manager/bottom-bar.tsx @@ -1,5 +1,4 @@ import "./bottom-bar.scss" - import React from "react"; import { observable } from "mobx"; import { observer } from "mobx-react"; @@ -7,41 +6,47 @@ import { Link } from "react-router-dom"; import { Trans } from "@lingui/macro"; import { Icon } from "../icon"; import { Menu, MenuItem } from "../menu"; -import { prevDefault } from "../../utils"; -import { workspaceStore } from "../../../common/workspace-store"; - -// todo: remove dummy actions + console.log +import { WorkspaceId, workspaceStore } from "../../../common/workspace-store"; +import { workspacesURL } from "../+workspaces"; @observer export class BottomBar extends React.Component { @observable menuVisible = false; + selectWorkspace = (workspaceId: WorkspaceId) => { + workspaceStore.currentWorkspaceId = workspaceId; + } + render() { const { currentWorkspace, workspacesList } = workspaceStore; + const menuId = "workspaces-menu" return (
-
- {currentWorkspace} +
+ + {currentWorkspace.name}
this.menuVisible = true} close={() => this.menuVisible = false} > - console.log('/navigate: workspaces page'))}> + Workspaces - {workspacesList.map(workspace => { - const { id, name, description } = workspace; + {workspacesList.map(({ id, name, description }) => { return ( - console.log(`navigate: /workspaces/${id}`)} title={description}> - {name} + this.selectWorkspace(id)} + title={description} + > + + {name} ) })} diff --git a/src/renderer/components/cluster-manager/cluster-context.tsx b/src/renderer/components/cluster-manager/cluster-context.tsx index 9214d7cbe6..6314e0f933 100644 --- a/src/renderer/components/cluster-manager/cluster-context.tsx +++ b/src/renderer/components/cluster-manager/cluster-context.tsx @@ -13,7 +13,7 @@ export interface ClusterContextValue { export function getClusterContext(): ClusterContextValue { return { clusterId: clusterStore.activeClusterId, - workspaceId: workspaceStore.currentWorkspace, + workspaceId: workspaceStore.currentWorkspaceId, } } diff --git a/src/renderer/components/cluster-manager/clusters-menu.tsx b/src/renderer/components/cluster-manager/clusters-menu.tsx index 91dfc7e926..589f78d453 100644 --- a/src/renderer/components/cluster-manager/clusters-menu.tsx +++ b/src/renderer/components/cluster-manager/clusters-menu.tsx @@ -6,12 +6,15 @@ import { _i18n } from "../../i18n"; import { t, Trans } from "@lingui/macro"; import type { Cluster } from "../../../main/cluster"; import { userStore } from "../../../common/user-store"; -import { clusterStore } from "../../../common/cluster-store"; +import { ClusterId, clusterStore } from "../../../common/cluster-store"; import { workspaceStore } from "../../../common/workspace-store"; import { ClusterIcon } from "../+cluster-settings/cluster-icon"; import { Icon } from "../icon"; import { cssNames, IClassName } from "../../utils"; import { Badge } from "../badge"; +import { navigate } from "../../navigation"; +import { addClusterURL } from "../+add-cluster"; +import { clusterSettingsURL } from "../+cluster-settings"; // fixme: allow to rearrange clusters with drag&drop // fixme: make add-icon's tooltip visible on init @@ -22,13 +25,16 @@ interface Props { @observer export class ClustersMenu extends React.Component { - showCluster = (cluster: Cluster) => { - clusterStore.activeClusterId = cluster.id; - console.log('show lens for cluster:', cluster.contextName); + showCluster = (clusterId: ClusterId) => { + if (clusterStore.activeClusterId === clusterId) { + navigate("/"); // redirect to index + } else { + clusterStore.activeClusterId = clusterId; + } } addCluster = () => { - console.log('navigate: /add-cluster') + navigate(addClusterURL()); } showContextMenu = (cluster: Cluster) => { @@ -37,15 +43,17 @@ export class ClustersMenu extends React.Component { menu.append(new MenuItem({ label: _i18n._(t`Settings`), - click: () => console.log(`navigate to cluster settings`, cluster) + click: () => navigate(clusterSettingsURL()) })); if (cluster.initialized) { menu.append(new MenuItem({ label: _i18n._(t`Disconnect`), - click: () => console.log(`disconnect cluster and navigate to workspaces`, cluster.contextName) + click: () => { + console.log(`//fixme: disconnect cluster`, cluster); + navigate("/"); + } })) } - menu.popup({ window: remote.getCurrentWindow() }) @@ -54,8 +62,8 @@ export class ClustersMenu extends React.Component { render() { const { className } = this.props; const { newContexts } = userStore; - const { currentWorkspace } = workspaceStore; - const clusters = clusterStore.getByWorkspaceId(currentWorkspace); + const { currentWorkspaceId } = workspaceStore; + const clusters = clusterStore.getByWorkspaceId(currentWorkspaceId); return (
{clusters.map(cluster => { @@ -65,7 +73,7 @@ export class ClustersMenu extends React.Component { showErrors={true} cluster={cluster} isActive={cluster.id === clusterStore.activeClusterId} - onClick={() => this.showCluster(cluster)} + onClick={() => this.showCluster(cluster.id)} onContextMenu={() => this.showContextMenu(cluster)} /> )