mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Make openBrowser->openLinkInBrowser injectable
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
d9785ad4dc
commit
2f0acb5655
@ -0,0 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { getGlobalOverride } from "../test-utils/get-global-override";
|
||||||
|
import { noop } from "./noop";
|
||||||
|
import openLinkInBrowserInjectable from "./open-link-in-browser.injectable";
|
||||||
|
|
||||||
|
export default getGlobalOverride(openLinkInBrowserInjectable, () => noop);
|
||||||
28
src/common/utils/open-link-in-browser.injectable.ts
Normal file
28
src/common/utils/open-link-in-browser.injectable.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import { shell } from "electron";
|
||||||
|
|
||||||
|
const allowedProtocols = new Set(["http:", "https:"]);
|
||||||
|
|
||||||
|
export type OpenLinkInBrowser = (url: string) => Promise<void>;
|
||||||
|
|
||||||
|
const openLinkInBrowserInjectable = getInjectable({
|
||||||
|
id: "open-link-in-browser",
|
||||||
|
instantiate: (): OpenLinkInBrowser => (
|
||||||
|
async (url) => {
|
||||||
|
const { protocol } = new URL(url);
|
||||||
|
|
||||||
|
if (!allowedProtocols.has(protocol)) {
|
||||||
|
throw new TypeError("not an http(s) URL");
|
||||||
|
}
|
||||||
|
|
||||||
|
await shell.openExternal(url);
|
||||||
|
}
|
||||||
|
),
|
||||||
|
causesSideEffects: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default openLinkInBrowserInjectable;
|
||||||
@ -1,29 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { shell } from "electron";
|
|
||||||
|
|
||||||
const allowedProtocols = new Set(["http:", "https:"]);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a link using the program configured as the default browser
|
|
||||||
* on the target platform. Will reject URLs with a scheme other than
|
|
||||||
* http or https to prevent programs other than the default browser
|
|
||||||
* running.
|
|
||||||
*
|
|
||||||
* @param url The URL to open
|
|
||||||
*/
|
|
||||||
export function openBrowser(url: string): Promise<void> {
|
|
||||||
if (allowedProtocols.has(new URL(url).protocol)) {
|
|
||||||
return shell.openExternal(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.reject(new TypeError("not an http(s) URL"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated use openBrowser
|
|
||||||
*/
|
|
||||||
export const openExternal = openBrowser;
|
|
||||||
@ -3,6 +3,15 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export { Singleton, openExternal, openBrowser, getAppVersion } from "../../common/utils";
|
import openLinkInBrowserInjectable from "../../common/utils/open-link-in-browser.injectable";
|
||||||
|
import { asLegacyGlobalFunctionForExtensionApi } from "../as-legacy-globals-for-extension-api/as-legacy-global-function-for-extension-api";
|
||||||
|
|
||||||
|
export { Singleton, getAppVersion } from "../../common/utils";
|
||||||
export { prevDefault, stopPropagation } from "../../renderer/utils/prevDefault";
|
export { prevDefault, stopPropagation } from "../../renderer/utils/prevDefault";
|
||||||
export { cssNames } from "../../renderer/utils/cssNames";
|
export { cssNames } from "../../renderer/utils/cssNames";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link openBrowser} instead
|
||||||
|
*/
|
||||||
|
export const openExternal = asLegacyGlobalFunctionForExtensionApi(openLinkInBrowserInjectable);
|
||||||
|
export const openBrowser = asLegacyGlobalFunctionForExtensionApi(openLinkInBrowserInjectable);
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import { docsUrl, productName, supportUrl } from "../../common/vars";
|
import { docsUrl, productName, supportUrl } from "../../common/vars";
|
||||||
import { broadcastMessage } from "../../common/ipc";
|
import { broadcastMessage } from "../../common/ipc";
|
||||||
import { openBrowser } from "../../common/utils";
|
|
||||||
import type { MenuItemConstructorOptions } from "electron";
|
import type { MenuItemConstructorOptions } from "electron";
|
||||||
import { webContents } from "electron";
|
import { webContents } from "electron";
|
||||||
import loggerInjectable from "../../common/logger.injectable";
|
import loggerInjectable from "../../common/logger.injectable";
|
||||||
@ -25,6 +24,7 @@ import applicationWindowInjectable from "../start-main-application/lens-window/a
|
|||||||
import reloadWindowInjectable from "../start-main-application/lens-window/reload-window.injectable";
|
import reloadWindowInjectable from "../start-main-application/lens-window/reload-window.injectable";
|
||||||
import showApplicationWindowInjectable from "../start-main-application/lens-window/show-application-window.injectable";
|
import showApplicationWindowInjectable from "../start-main-application/lens-window/show-application-window.injectable";
|
||||||
import processCheckingForUpdatesInjectable from "../application-update/check-for-updates/process-checking-for-updates.injectable";
|
import processCheckingForUpdatesInjectable from "../application-update/check-for-updates/process-checking-for-updates.injectable";
|
||||||
|
import openLinkInBrowserInjectable from "../../common/utils/open-link-in-browser.injectable";
|
||||||
|
|
||||||
function ignoreIf(check: boolean, menuItems: MenuItemOpts[]) {
|
function ignoreIf(check: boolean, menuItems: MenuItemOpts[]) {
|
||||||
return check ? [] : menuItems;
|
return check ? [] : menuItems;
|
||||||
@ -54,6 +54,7 @@ const applicationMenuItemsInjectable = getInjectable({
|
|||||||
const navigateToAddCluster = di.inject(navigateToAddClusterInjectable);
|
const navigateToAddCluster = di.inject(navigateToAddClusterInjectable);
|
||||||
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
|
const stopServicesAndExitApp = di.inject(stopServicesAndExitAppInjectable);
|
||||||
const processCheckingForUpdates = di.inject(processCheckingForUpdatesInjectable);
|
const processCheckingForUpdates = di.inject(processCheckingForUpdatesInjectable);
|
||||||
|
const openLinkInBrowser = di.inject(openLinkInBrowserInjectable);
|
||||||
|
|
||||||
logger.info(`[MENU]: autoUpdateEnabled=${updatingIsEnabled}`);
|
logger.info(`[MENU]: autoUpdateEnabled=${updatingIsEnabled}`);
|
||||||
|
|
||||||
@ -260,7 +261,7 @@ const applicationMenuItemsInjectable = getInjectable({
|
|||||||
label: "Documentation",
|
label: "Documentation",
|
||||||
id: "documentation",
|
id: "documentation",
|
||||||
click: async () => {
|
click: async () => {
|
||||||
openBrowser(docsUrl).catch((error) => {
|
openLinkInBrowser(docsUrl).catch((error) => {
|
||||||
logger.error("[MENU]: failed to open browser", { error });
|
logger.error("[MENU]: failed to open browser", { error });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -269,7 +270,7 @@ const applicationMenuItemsInjectable = getInjectable({
|
|||||||
label: "Support",
|
label: "Support",
|
||||||
id: "support",
|
id: "support",
|
||||||
click: async () => {
|
click: async () => {
|
||||||
openBrowser(supportUrl).catch((error) => {
|
openLinkInBrowser(supportUrl).catch((error) => {
|
||||||
logger.error("[MENU]: failed to open browser", { error });
|
logger.error("[MENU]: failed to open browser", { error });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@ -6,10 +6,10 @@ import { getInjectable } from "@ogre-tools/injectable";
|
|||||||
import loggerInjectable from "../../../../common/logger.injectable";
|
import loggerInjectable from "../../../../common/logger.injectable";
|
||||||
import applicationWindowStateInjectable from "./application-window-state.injectable";
|
import applicationWindowStateInjectable from "./application-window-state.injectable";
|
||||||
import { BrowserWindow } from "electron";
|
import { BrowserWindow } from "electron";
|
||||||
import { openBrowser } from "../../../../common/utils";
|
|
||||||
import sendToChannelInElectronBrowserWindowInjectable from "./send-to-channel-in-electron-browser-window.injectable";
|
import sendToChannelInElectronBrowserWindowInjectable from "./send-to-channel-in-electron-browser-window.injectable";
|
||||||
import type { ElectronWindow } from "./create-lens-window.injectable";
|
import type { ElectronWindow } from "./create-lens-window.injectable";
|
||||||
import type { RequireExactlyOne } from "type-fest";
|
import type { RequireExactlyOne } from "type-fest";
|
||||||
|
import openLinkInBrowserInjectable from "../../../../common/utils/open-link-in-browser.injectable";
|
||||||
|
|
||||||
export type ElectronWindowTitleBarStyle = "hiddenInset" | "hidden" | "default" | "customButtonsOnHover";
|
export type ElectronWindowTitleBarStyle = "hiddenInset" | "hidden" | "default" | "customButtonsOnHover";
|
||||||
|
|
||||||
@ -46,6 +46,7 @@ const createElectronWindowInjectable = getInjectable({
|
|||||||
instantiate: (di): CreateElectronWindow => {
|
instantiate: (di): CreateElectronWindow => {
|
||||||
const logger = di.inject(loggerInjectable);
|
const logger = di.inject(loggerInjectable);
|
||||||
const sendToChannelInLensWindow = di.inject(sendToChannelInElectronBrowserWindowInjectable);
|
const sendToChannelInLensWindow = di.inject(sendToChannelInElectronBrowserWindowInjectable);
|
||||||
|
const openLinkInBrowser = di.inject(openLinkInBrowserInjectable);
|
||||||
|
|
||||||
return (configuration) => {
|
return (configuration) => {
|
||||||
const applicationWindowState = di.inject(
|
const applicationWindowState = di.inject(
|
||||||
@ -158,7 +159,7 @@ const createElectronWindowInjectable = getInjectable({
|
|||||||
})
|
})
|
||||||
|
|
||||||
.setWindowOpenHandler((details) => {
|
.setWindowOpenHandler((details) => {
|
||||||
openBrowser(details.url).catch((error) => {
|
openLinkInBrowser(details.url).catch((error) => {
|
||||||
logger.error("[CREATE-ELECTRON-WINDOW]: failed to open browser", {
|
logger.error("[CREATE-ELECTRON-WINDOW]: failed to open browser", {
|
||||||
error,
|
error,
|
||||||
});
|
});
|
||||||
|
|||||||
@ -6,7 +6,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { autoBind, cssNames } from "../../utils";
|
import { autoBind, cssNames } from "../../utils";
|
||||||
import type { PortForwardItem, PortForwardStore } from "../../port-forward";
|
import type { PortForwardItem, PortForwardStore } from "../../port-forward";
|
||||||
import { openPortForward } from "../../port-forward";
|
|
||||||
import type { MenuActionsProps } from "../menu/menu-actions";
|
import type { MenuActionsProps } from "../menu/menu-actions";
|
||||||
import { MenuActions } from "../menu/menu-actions";
|
import { MenuActions } from "../menu/menu-actions";
|
||||||
import { MenuItem } from "../menu";
|
import { MenuItem } from "../menu";
|
||||||
@ -15,6 +14,8 @@ import { Notifications } from "../notifications";
|
|||||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||||
import portForwardDialogModelInjectable from "../../port-forward/port-forward-dialog-model/port-forward-dialog-model.injectable";
|
import portForwardDialogModelInjectable from "../../port-forward/port-forward-dialog-model/port-forward-dialog-model.injectable";
|
||||||
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
||||||
|
import type { OpenPortForward } from "../../port-forward/open-port-forward.injectable";
|
||||||
|
import openPortForwardInjectable from "../../port-forward/open-port-forward.injectable";
|
||||||
|
|
||||||
export interface PortForwardMenuProps extends MenuActionsProps {
|
export interface PortForwardMenuProps extends MenuActionsProps {
|
||||||
portForward: PortForwardItem;
|
portForward: PortForwardItem;
|
||||||
@ -24,6 +25,7 @@ export interface PortForwardMenuProps extends MenuActionsProps {
|
|||||||
interface Dependencies {
|
interface Dependencies {
|
||||||
portForwardStore: PortForwardStore;
|
portForwardStore: PortForwardStore;
|
||||||
openPortForwardDialog: (item: PortForwardItem) => void;
|
openPortForwardDialog: (item: PortForwardItem) => void;
|
||||||
|
openPortForward: OpenPortForward;
|
||||||
}
|
}
|
||||||
|
|
||||||
class NonInjectedPortForwardMenu<Props extends PortForwardMenuProps & Dependencies> extends React.Component<Props> {
|
class NonInjectedPortForwardMenu<Props extends PortForwardMenuProps & Dependencies> extends React.Component<Props> {
|
||||||
@ -94,7 +96,7 @@ class NonInjectedPortForwardMenu<Props extends PortForwardMenuProps & Dependenci
|
|||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{ portForward.status === "Active" && (
|
{ portForward.status === "Active" && (
|
||||||
<MenuItem onClick={() => openPortForward(portForward)}>
|
<MenuItem onClick={() => this.props.openPortForward(portForward)}>
|
||||||
<Icon
|
<Icon
|
||||||
material="open_in_browser"
|
material="open_in_browser"
|
||||||
interactive={toolbar}
|
interactive={toolbar}
|
||||||
@ -139,6 +141,7 @@ export const PortForwardMenu = withInjectables<Dependencies, PortForwardMenuProp
|
|||||||
getProps: (di, props) => ({
|
getProps: (di, props) => ({
|
||||||
portForwardStore: di.inject(portForwardStoreInjectable),
|
portForwardStore: di.inject(portForwardStoreInjectable),
|
||||||
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
||||||
|
openPortForward: di.inject(openPortForwardInjectable),
|
||||||
...props,
|
...props,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import { cssNames } from "../../utils";
|
|||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import type { ForwardedPort, PortForwardStore } from "../../port-forward";
|
import type { ForwardedPort, PortForwardStore } from "../../port-forward";
|
||||||
import { openPortForward, predictProtocol } from "../../port-forward";
|
import { predictProtocol } from "../../port-forward";
|
||||||
import { Spinner } from "../spinner";
|
import { Spinner } from "../spinner";
|
||||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||||
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
||||||
@ -21,6 +21,8 @@ import portForwardDialogModelInjectable from "../../port-forward/port-forward-di
|
|||||||
import logger from "../../../common/logger";
|
import logger from "../../../common/logger";
|
||||||
import aboutPortForwardingInjectable from "../../port-forward/about-port-forwarding.injectable";
|
import aboutPortForwardingInjectable from "../../port-forward/about-port-forwarding.injectable";
|
||||||
import notifyErrorPortForwardingInjectable from "../../port-forward/notify-error-port-forwarding.injectable";
|
import notifyErrorPortForwardingInjectable from "../../port-forward/notify-error-port-forwarding.injectable";
|
||||||
|
import type { OpenPortForward } from "../../port-forward/open-port-forward.injectable";
|
||||||
|
import openPortForwardInjectable from "../../port-forward/open-port-forward.injectable";
|
||||||
|
|
||||||
export interface ServicePortComponentProps {
|
export interface ServicePortComponentProps {
|
||||||
service: Service;
|
service: Service;
|
||||||
@ -32,6 +34,7 @@ interface Dependencies {
|
|||||||
openPortForwardDialog: (item: ForwardedPort, options: { openInBrowser: boolean; onClose: () => void }) => void;
|
openPortForwardDialog: (item: ForwardedPort, options: { openInBrowser: boolean; onClose: () => void }) => void;
|
||||||
aboutPortForwarding: () => void;
|
aboutPortForwarding: () => void;
|
||||||
notifyErrorPortForwarding: (message: string) => void;
|
notifyErrorPortForwarding: (message: string) => void;
|
||||||
|
openPortForward: OpenPortForward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
@ -88,7 +91,7 @@ class NonInjectedServicePortComponent extends React.Component<ServicePortCompone
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
async portForward() {
|
async portForward() {
|
||||||
const { service, port } = this.props;
|
const { service, port, openPortForward } = this.props;
|
||||||
let portForward: ForwardedPort = {
|
let portForward: ForwardedPort = {
|
||||||
kind: "service",
|
kind: "service",
|
||||||
name: service.getName(),
|
name: service.getName(),
|
||||||
@ -180,7 +183,7 @@ class NonInjectedServicePortComponent extends React.Component<ServicePortCompone
|
|||||||
<span title="Open in a browser" onClick={() => this.portForward()}>
|
<span title="Open in a browser" onClick={() => this.portForward()}>
|
||||||
{port.toString()}
|
{port.toString()}
|
||||||
</span>
|
</span>
|
||||||
<Button primary onClick={portForwardAction}>
|
<Button primary onClick={portForwardAction}>
|
||||||
{" "}
|
{" "}
|
||||||
{this.isPortForwarded ? (this.isActive ? "Stop/Remove" : "Remove") : "Forward..."}
|
{this.isPortForwarded ? (this.isActive ? "Stop/Remove" : "Remove") : "Forward..."}
|
||||||
{" "}
|
{" "}
|
||||||
@ -202,6 +205,7 @@ export const ServicePortComponent = withInjectables<Dependencies, ServicePortCom
|
|||||||
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
||||||
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
||||||
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
||||||
|
openPortForward: di.inject(openPortForwardInjectable),
|
||||||
...props,
|
...props,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import { cssNames } from "../../utils";
|
|||||||
import { Notifications } from "../notifications";
|
import { Notifications } from "../notifications";
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import type { ForwardedPort, PortForwardStore } from "../../port-forward";
|
import type { ForwardedPort, PortForwardStore } from "../../port-forward";
|
||||||
import { openPortForward, predictProtocol } from "../../port-forward";
|
import { predictProtocol } from "../../port-forward";
|
||||||
import { Spinner } from "../spinner";
|
import { Spinner } from "../spinner";
|
||||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||||
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
import portForwardStoreInjectable from "../../port-forward/port-forward-store/port-forward-store.injectable";
|
||||||
@ -21,6 +21,8 @@ import portForwardDialogModelInjectable from "../../port-forward/port-forward-di
|
|||||||
import logger from "../../../common/logger";
|
import logger from "../../../common/logger";
|
||||||
import aboutPortForwardingInjectable from "../../port-forward/about-port-forwarding.injectable";
|
import aboutPortForwardingInjectable from "../../port-forward/about-port-forwarding.injectable";
|
||||||
import notifyErrorPortForwardingInjectable from "../../port-forward/notify-error-port-forwarding.injectable";
|
import notifyErrorPortForwardingInjectable from "../../port-forward/notify-error-port-forwarding.injectable";
|
||||||
|
import type { OpenPortForward } from "../../port-forward/open-port-forward.injectable";
|
||||||
|
import openPortForwardInjectable from "../../port-forward/open-port-forward.injectable";
|
||||||
|
|
||||||
export interface PodContainerPortProps {
|
export interface PodContainerPortProps {
|
||||||
pod: Pod;
|
pod: Pod;
|
||||||
@ -32,6 +34,7 @@ interface Dependencies {
|
|||||||
openPortForwardDialog: (item: ForwardedPort, options: { openInBrowser: boolean; onClose: () => void }) => void;
|
openPortForwardDialog: (item: ForwardedPort, options: { openInBrowser: boolean; onClose: () => void }) => void;
|
||||||
aboutPortForwarding: () => void;
|
aboutPortForwarding: () => void;
|
||||||
notifyErrorPortForwarding: (message: string) => void;
|
notifyErrorPortForwarding: (message: string) => void;
|
||||||
|
openPortForward: OpenPortForward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
@ -86,7 +89,7 @@ class NonInjectedPodContainerPort extends React.Component<PodContainerPortProps
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
async portForward() {
|
async portForward() {
|
||||||
const { pod, port } = this.props;
|
const { pod, port, openPortForward } = this.props;
|
||||||
let portForward: ForwardedPort = {
|
let portForward: ForwardedPort = {
|
||||||
kind: "pod",
|
kind: "pod",
|
||||||
name: pod.getName(),
|
name: pod.getName(),
|
||||||
@ -178,7 +181,7 @@ class NonInjectedPodContainerPort extends React.Component<PodContainerPortProps
|
|||||||
<span title="Open in a browser" onClick={() => this.portForward()}>
|
<span title="Open in a browser" onClick={() => this.portForward()}>
|
||||||
{text}
|
{text}
|
||||||
</span>
|
</span>
|
||||||
<Button primary onClick={portForwardAction}>
|
<Button primary onClick={portForwardAction}>
|
||||||
{" "}
|
{" "}
|
||||||
{this.isPortForwarded ? (this.isActive ? "Stop/Remove" : "Remove") : "Forward..."}
|
{this.isPortForwarded ? (this.isActive ? "Stop/Remove" : "Remove") : "Forward..."}
|
||||||
{" "}
|
{" "}
|
||||||
@ -200,6 +203,7 @@ export const PodContainerPort = withInjectables<Dependencies, PodContainerPortPr
|
|||||||
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
openPortForwardDialog: di.inject(portForwardDialogModelInjectable).open,
|
||||||
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
||||||
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
||||||
|
openPortForward: di.inject(openPortForwardInjectable),
|
||||||
...props,
|
...props,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
38
src/renderer/port-forward/open-port-forward.injectable.ts
Normal file
38
src/renderer/port-forward/open-port-forward.injectable.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
|
import loggerInjectable from "../../common/logger.injectable";
|
||||||
|
import openLinkInBrowserInjectable from "../../common/utils/open-link-in-browser.injectable";
|
||||||
|
import showErrorNotificationInjectable from "../components/notifications/show-error-notification.injectable";
|
||||||
|
import type { ForwardedPort } from "./port-forward-item";
|
||||||
|
import { portForwardAddress } from "./port-forward-utils";
|
||||||
|
|
||||||
|
export type OpenPortForward = (portForward: ForwardedPort) => void;
|
||||||
|
|
||||||
|
const openPortForwardInjectable = getInjectable({
|
||||||
|
id: "open-port-forward",
|
||||||
|
instantiate: (di): OpenPortForward => {
|
||||||
|
const openLinkInBrowser = di.inject(openLinkInBrowserInjectable);
|
||||||
|
const showErrorNotification = di.inject(showErrorNotificationInjectable);
|
||||||
|
const logger = di.inject(loggerInjectable);
|
||||||
|
|
||||||
|
return (portForward) => {
|
||||||
|
const browseTo = portForwardAddress(portForward);
|
||||||
|
|
||||||
|
openLinkInBrowser(browseTo)
|
||||||
|
.catch(error => {
|
||||||
|
logger.error(`failed to open in browser: ${error}`, {
|
||||||
|
port: portForward.port,
|
||||||
|
kind: portForward.kind,
|
||||||
|
namespace: portForward.namespace,
|
||||||
|
name: portForward.name,
|
||||||
|
});
|
||||||
|
showErrorNotification(`Failed to open ${browseTo} in browser`);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default openPortForwardInjectable;
|
||||||
@ -14,7 +14,6 @@ import { Wizard, WizardStep } from "../components/wizard";
|
|||||||
import { Input } from "../components/input";
|
import { Input } from "../components/input";
|
||||||
import { cssNames } from "../utils";
|
import { cssNames } from "../utils";
|
||||||
import type { PortForwardStore } from "./port-forward-store/port-forward-store";
|
import type { PortForwardStore } from "./port-forward-store/port-forward-store";
|
||||||
import { openPortForward } from "./port-forward-utils";
|
|
||||||
import { Checkbox } from "../components/checkbox";
|
import { Checkbox } from "../components/checkbox";
|
||||||
import { withInjectables } from "@ogre-tools/injectable-react";
|
import { withInjectables } from "@ogre-tools/injectable-react";
|
||||||
import type { PortForwardDialogData, PortForwardDialogModel } from "./port-forward-dialog-model/port-forward-dialog-model";
|
import type { PortForwardDialogData, PortForwardDialogModel } from "./port-forward-dialog-model/port-forward-dialog-model";
|
||||||
@ -23,6 +22,8 @@ import logger from "../../common/logger";
|
|||||||
import portForwardStoreInjectable from "./port-forward-store/port-forward-store.injectable";
|
import portForwardStoreInjectable from "./port-forward-store/port-forward-store.injectable";
|
||||||
import aboutPortForwardingInjectable from "./about-port-forwarding.injectable";
|
import aboutPortForwardingInjectable from "./about-port-forwarding.injectable";
|
||||||
import notifyErrorPortForwardingInjectable from "./notify-error-port-forwarding.injectable";
|
import notifyErrorPortForwardingInjectable from "./notify-error-port-forwarding.injectable";
|
||||||
|
import type { OpenPortForward } from "./open-port-forward.injectable";
|
||||||
|
import openPortForwardInjectable from "./open-port-forward.injectable";
|
||||||
|
|
||||||
export interface PortForwardDialogProps extends Partial<DialogProps> {}
|
export interface PortForwardDialogProps extends Partial<DialogProps> {}
|
||||||
|
|
||||||
@ -31,6 +32,7 @@ interface Dependencies {
|
|||||||
model: PortForwardDialogModel;
|
model: PortForwardDialogModel;
|
||||||
aboutPortForwarding: () => void;
|
aboutPortForwarding: () => void;
|
||||||
notifyErrorPortForwarding: (message: string) => void;
|
notifyErrorPortForwarding: (message: string) => void;
|
||||||
|
openPortForward: OpenPortForward;
|
||||||
}
|
}
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
@ -89,7 +91,7 @@ class NonInjectedPortForwardDialog extends Component<PortForwardDialogProps & De
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (portForward.status === "Active" && data.openInBrowser) {
|
if (portForward.status === "Active" && data.openInBrowser) {
|
||||||
openPortForward(portForward);
|
this.props.openPortForward(portForward);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`[PORT-FORWARD-DIALOG]: ${error}`, portForward);
|
logger.error(`[PORT-FORWARD-DIALOG]: ${error}`, portForward);
|
||||||
@ -177,6 +179,7 @@ export const PortForwardDialog = withInjectables<Dependencies, PortForwardDialog
|
|||||||
model: di.inject(portForwardDialogModelInjectable),
|
model: di.inject(portForwardDialogModelInjectable),
|
||||||
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
aboutPortForwarding: di.inject(aboutPortForwardingInjectable),
|
||||||
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
notifyErrorPortForwarding: di.inject(notifyErrorPortForwardingInjectable),
|
||||||
|
openPortForward: di.inject(openPortForwardInjectable),
|
||||||
...props,
|
...props,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
@ -3,34 +3,12 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
import { openBrowser } from "../utils";
|
|
||||||
import { Notifications } from "../components/notifications";
|
|
||||||
import type { ForwardedPort } from "./port-forward-item";
|
import type { ForwardedPort } from "./port-forward-item";
|
||||||
import logger from "../../common/logger";
|
|
||||||
|
|
||||||
export function portForwardAddress(portForward: ForwardedPort) {
|
export function portForwardAddress(portForward: ForwardedPort) {
|
||||||
return `${portForward.protocol ?? "http"}://localhost:${portForward.forwardPort}`;
|
return `${portForward.protocol ?? "http"}://localhost:${portForward.forwardPort}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function openPortForward(portForward: ForwardedPort) {
|
|
||||||
const browseTo = portForwardAddress(portForward);
|
|
||||||
|
|
||||||
openBrowser(browseTo)
|
|
||||||
.catch(error => {
|
|
||||||
logger.error(`failed to open in browser: ${error}`, {
|
|
||||||
port: portForward.port,
|
|
||||||
kind: portForward.kind,
|
|
||||||
namespace: portForward.namespace,
|
|
||||||
name: portForward.name,
|
|
||||||
});
|
|
||||||
Notifications.error(`Failed to open ${browseTo} in browser`);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export function predictProtocol(name: string | undefined) {
|
export function predictProtocol(name: string | undefined) {
|
||||||
return name === "https" ? "https" : "http";
|
return name === "https" ? "https" : "http";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user