From 3b4437e48061a0cae785e7ce4d0bb9267ddae2f2 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Mon, 22 Nov 2021 09:49:53 -0500 Subject: [PATCH] [MM-40025] Stop app from redirecting to tabs when not logged in (#1870) --- src/common/communication.ts | 1 + src/main/preload/mattermost.js | 4 ++++ src/main/views/MattermostView.ts | 2 ++ src/main/windows/windowManager.ts | 21 ++++++++++++++++++--- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/common/communication.ts b/src/common/communication.ts index 764e2b4c..0ca3e7ec 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -98,6 +98,7 @@ export const SEND_DROPDOWN_MENU_SIZE = 'send-dropdown-menu-size'; export const BROWSER_HISTORY_PUSH = 'browser-history-push'; export const APP_LOGGED_IN = 'app-logged-in'; +export const APP_LOGGED_OUT = 'app-logged-out'; export const GET_AVAILABLE_SPELL_CHECKER_LANGUAGES = 'get-available-spell-checker-languages'; diff --git a/src/main/preload/mattermost.js b/src/main/preload/mattermost.js index b58c6b2d..8c0f4bda 100644 --- a/src/main/preload/mattermost.js +++ b/src/main/preload/mattermost.js @@ -23,6 +23,7 @@ import { CLOSE_TEAMS_DROPDOWN, BROWSER_HISTORY_PUSH, APP_LOGGED_IN, + APP_LOGGED_OUT, GET_VIEW_NAME, GET_VIEW_WEBCONTENTS_ID, } from 'common/communication'; @@ -256,6 +257,9 @@ window.addEventListener('storage', (e) => { if (e.key === '__login__' && e.storageArea === localStorage && e.newValue) { ipcRenderer.send(APP_LOGGED_IN, viewName); } + if (e.key === '__logout__' && e.storageArea === localStorage && e.newValue) { + ipcRenderer.send(APP_LOGGED_OUT, viewName); + } }); /* eslint-enable no-magic-numbers */ diff --git a/src/main/views/MattermostView.ts b/src/main/views/MattermostView.ts index 31325a33..5effebcd 100644 --- a/src/main/views/MattermostView.ts +++ b/src/main/views/MattermostView.ts @@ -47,6 +47,7 @@ export class MattermostView extends EventEmitter { window: BrowserWindow; view: BrowserView; isVisible: boolean; + isLoggedIn: boolean; options: BrowserViewConstructorOptions; serverInfo: ServerInfo; @@ -85,6 +86,7 @@ export class MattermostView extends EventEmitter { ...options.webPreferences, }; this.isVisible = false; + this.isLoggedIn = false; this.view = new BrowserView(this.options); this.resetLoadingStatus(); diff --git a/src/main/windows/windowManager.ts b/src/main/windows/windowManager.ts index 0e1b96ba..5b417644 100644 --- a/src/main/windows/windowManager.ts +++ b/src/main/windows/windowManager.ts @@ -20,6 +20,7 @@ import { APP_LOGGED_IN, GET_VIEW_NAME, GET_VIEW_WEBCONTENTS_ID, + APP_LOGGED_OUT, } from 'common/communication'; import urlUtils from 'common/utils/url'; @@ -56,6 +57,7 @@ ipcMain.on(REACT_APP_INITIALIZED, handleReactAppInitialized); ipcMain.on(LOADING_SCREEN_ANIMATION_FINISHED, handleLoadingScreenAnimationFinished); ipcMain.on(BROWSER_HISTORY_PUSH, handleBrowserHistoryPush); ipcMain.on(APP_LOGGED_IN, handleAppLoggedIn); +ipcMain.on(APP_LOGGED_OUT, handleAppLoggedOut); ipcMain.handle(GET_VIEW_NAME, handleGetViewName); ipcMain.handle(GET_VIEW_WEBCONTENTS_ID, handleGetWebContentsId); @@ -570,10 +572,12 @@ function handleBrowserHistoryPush(e: IpcMainEvent, viewName: string, pathName: s if (status.viewManager?.closedViews.has(redirectedViewName)) { status.viewManager.openClosedTab(redirectedViewName, `${currentView?.tab.server.url}${pathName}`); } - const redirectedView = status.viewManager?.views.get(redirectedViewName) || currentView; - if (redirectedView !== currentView && redirectedView?.tab.server.name === status.currentServerName) { + let redirectedView = status.viewManager?.views.get(redirectedViewName) || currentView; + if (redirectedView !== currentView && redirectedView?.tab.server.name === status.currentServerName && redirectedView?.isLoggedIn) { log.info('redirecting to a new view', redirectedView?.name || viewName); status.viewManager?.showByName(redirectedView?.name || viewName); + } else { + redirectedView = currentView; } // Special case check for Channels to not force a redirect to "/", causing a refresh @@ -587,7 +591,18 @@ export function getCurrentTeamName() { } function handleAppLoggedIn(event: IpcMainEvent, viewName: string) { - status.viewManager?.reloadViewIfNeeded(viewName); + const view = status.viewManager?.views.get(viewName); + if (view) { + view.isLoggedIn = true; + status.viewManager?.reloadViewIfNeeded(viewName); + } +} + +function handleAppLoggedOut(event: IpcMainEvent, viewName: string) { + const view = status.viewManager?.views.get(viewName); + if (view) { + view.isLoggedIn = false; + } } function handleGetViewName(event: IpcMainInvokeEvent) {