From 9c3febd4c9ee2bcb617e3a55166d50437b7f5e6e Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Tue, 29 Aug 2023 10:07:33 -0400 Subject: [PATCH] [MM-53847] Expose isDev API to allow webapp to check if the app is in dev mode (#2800) * [MM-53847] Expose isDev API to allow webapp to check if the app is in dev mode * Fix test --- src/common/communication.ts | 2 ++ src/main/app/initialize.ts | 2 +- src/main/app/utils.test.js | 2 ++ src/main/app/utils.ts | 4 +++- src/main/preload/mattermost.js | 5 +++++ src/main/views/viewManager.ts | 3 +++ 6 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/common/communication.ts b/src/common/communication.ts index 16b1a3e3..781806e0 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -173,3 +173,5 @@ export const MAIN_WINDOW_RESIZED = 'main-window-resized'; export const MAIN_WINDOW_FOCUSED = 'main-window-focused'; export const VALIDATE_SERVER_URL = 'validate-server-url'; + +export const GET_IS_DEV_MODE = 'get-is-dev-mode'; diff --git a/src/main/app/initialize.ts b/src/main/app/initialize.ts index ee716460..1f8415c5 100644 --- a/src/main/app/initialize.ts +++ b/src/main/app/initialize.ts @@ -235,7 +235,7 @@ function initializeBeforeAppReady() { AllowProtocolDialog.init(); if (isDev && process.env.NODE_ENV !== 'test') { - log.info('In development mode, deeplinking is disabled'); + app.setAsDefaultProtocolClient('mattermost-dev'); } else if (mainProtocol) { app.setAsDefaultProtocolClient(mainProtocol); } diff --git a/src/main/app/utils.test.js b/src/main/app/utils.test.js index 6b431c88..fd096525 100644 --- a/src/main/app/utils.test.js +++ b/src/main/app/utils.test.js @@ -35,6 +35,8 @@ jest.mock('electron', () => ({ }, })); +jest.mock('electron-is-dev', () => false); + jest.mock('common/config', () => ({ setServers: jest.fn(), })); diff --git a/src/main/app/utils.ts b/src/main/app/utils.ts index 00388146..b5457686 100644 --- a/src/main/app/utils.ts +++ b/src/main/app/utils.ts @@ -6,6 +6,7 @@ import path from 'path'; import fs from 'fs-extra'; import {app, BrowserWindow, Menu, Rectangle, Session, session, dialog, nativeImage, screen} from 'electron'; +import isDev from 'electron-is-dev'; import {MigrationInfo} from 'types/config'; import {RemoteInfo} from 'types/server'; @@ -72,7 +73,8 @@ export function getDeeplinkingURL(args: string[]) { if (Array.isArray(args) && args.length) { // deeplink urls should always be the last argument, but may not be the first (i.e. Windows with the app already running) const url = args[args.length - 1]; - if (url && mainProtocol && url.startsWith(mainProtocol) && isValidURI(url)) { + const protocol = isDev ? 'mattermost-dev' : mainProtocol; + if (url && protocol && url.startsWith(protocol) && isValidURI(url)) { return url; } } diff --git a/src/main/preload/mattermost.js b/src/main/preload/mattermost.js index b1647096..ac3665ad 100644 --- a/src/main/preload/mattermost.js +++ b/src/main/preload/mattermost.js @@ -37,6 +37,7 @@ import { CLOSE_DOWNLOADS_DROPDOWN, CALLS_ERROR, CALLS_JOIN_REQUEST, + GET_IS_DEV_MODE, } from 'common/communication'; const UNREAD_COUNT_INTERVAL = 1000; @@ -56,6 +57,10 @@ if (process.env.NODE_ENV === 'test') { }); } +contextBridge.exposeInMainWorld('desktopAPI', { + isDev: () => ipcRenderer.invoke(GET_IS_DEV_MODE), +}); + ipcRenderer.invoke('get-app-version').then(({name, version}) => { appVersion = version; appName = name; diff --git a/src/main/views/viewManager.ts b/src/main/views/viewManager.ts index 6a88e02a..d5396008 100644 --- a/src/main/views/viewManager.ts +++ b/src/main/views/viewManager.ts @@ -2,6 +2,7 @@ // See LICENSE.txt for license information. import {BrowserView, dialog, ipcMain, IpcMainEvent, IpcMainInvokeEvent, Event} from 'electron'; +import isDev from 'electron-is-dev'; import ServerViewState from 'app/serverViewState'; @@ -30,6 +31,7 @@ import { MAIN_WINDOW_RESIZED, MAIN_WINDOW_FOCUSED, SWITCH_TAB, + GET_IS_DEV_MODE, } from 'common/communication'; import Config from 'common/config'; import {Logger} from 'common/log'; @@ -67,6 +69,7 @@ export class ViewManager { MainWindow.on(MAIN_WINDOW_RESIZED, this.handleSetCurrentViewBounds); MainWindow.on(MAIN_WINDOW_FOCUSED, this.focusCurrentView); ipcMain.handle(GET_VIEW_INFO_FOR_TEST, this.handleGetViewInfoForTest); + ipcMain.handle(GET_IS_DEV_MODE, () => isDev); ipcMain.on(HISTORY, this.handleHistory); ipcMain.on(REACT_APP_INITIALIZED, this.handleReactAppInitialized); ipcMain.on(BROWSER_HISTORY_PUSH, this.handleBrowserHistoryPush);