From a00cb4c81436630686fd6809b9e9910c252b7989 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Wed, 24 Jul 2024 18:36:36 -0400 Subject: [PATCH] [MM-59489] Change logged in tracking code for new API, check for `/` when navigating tabs (#3105) * [MM-59489] Change logged in tracking code for new API, check for `/` when navigating tabs * Better naming --- src/common/communication.ts | 1 + src/main/preload/externalAPI.ts | 5 +++-- src/main/views/viewManager.ts | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/common/communication.ts b/src/common/communication.ts index df8659bd..841d7b92 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -98,6 +98,7 @@ export const NO_UPDATE_AVAILABLE = 'no-update-available'; 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 TAB_LOGIN_CHANGED = 'tab-login-changed'; export const GET_AVAILABLE_SPELL_CHECKER_LANGUAGES = 'get-available-spell-checker-languages'; diff --git a/src/main/preload/externalAPI.ts b/src/main/preload/externalAPI.ts index 7db9512a..28a71aaf 100644 --- a/src/main/preload/externalAPI.ts +++ b/src/main/preload/externalAPI.ts @@ -42,6 +42,7 @@ import { GET_DESKTOP_SOURCES, UNREADS_AND_MENTIONS, LEGACY_OFF, + TAB_LOGIN_CHANGED, } from 'common/communication'; import type {ExternalAPI} from 'types/externalAPI'; @@ -73,8 +74,8 @@ const desktopAPI: DesktopAPI = { setSessionExpired: (isExpired) => ipcRenderer.send(SESSION_EXPIRED, isExpired), onUserActivityUpdate: (listener) => createListener(USER_ACTIVITY_UPDATE, listener), - onLogin: () => ipcRenderer.send(APP_LOGGED_IN), - onLogout: () => ipcRenderer.send(APP_LOGGED_OUT), + onLogin: () => ipcRenderer.send(TAB_LOGIN_CHANGED, true), + onLogout: () => ipcRenderer.send(TAB_LOGIN_CHANGED, false), // Unreads/mentions/notifications sendNotification: (title, body, channelId, teamId, url, silent, soundName) => diff --git a/src/main/views/viewManager.ts b/src/main/views/viewManager.ts index 7e173d62..65d2402c 100644 --- a/src/main/views/viewManager.ts +++ b/src/main/views/viewManager.ts @@ -33,6 +33,7 @@ import { REQUEST_BROWSER_HISTORY_STATUS, LEGACY_OFF, UNREADS_AND_MENTIONS, + TAB_LOGIN_CHANGED, } from 'common/communication'; import Config from 'common/config'; import {Logger} from 'common/log'; @@ -80,6 +81,7 @@ export class ViewManager { ipcMain.on(BROWSER_HISTORY_PUSH, this.handleBrowserHistoryPush); ipcMain.on(APP_LOGGED_IN, this.handleAppLoggedIn); ipcMain.on(APP_LOGGED_OUT, this.handleAppLoggedOut); + ipcMain.on(TAB_LOGIN_CHANGED, this.handleTabLoginChanged); ipcMain.on(RELOAD_CURRENT_VIEW, this.handleReloadCurrentView); ipcMain.on(UNREAD_RESULT, this.handleUnreadChanged); ipcMain.on(UNREADS_AND_MENTIONS, this.handleUnreadsAndMentionsChanged); @@ -504,6 +506,25 @@ export class ViewManager { flushCookiesStore(); }; + private handleTabLoginChanged = (event: IpcMainEvent, loggedIn: boolean) => { + log.debug('handleTabLoggedIn', event.sender.id); + const view = this.getViewByWebContentsId(event.sender.id); + if (!view) { + return; + } + + // This method is only called when the specific view is logged in + // so we need to call the `onLogin` for all of the views for the same server + [...this.views.values()]. + filter((v) => v.view.server.id === view.view.server.id). + forEach((v) => v.onLogin(loggedIn)); + + if (!loggedIn) { + AppState.clear(view.id); + } + flushCookiesStore(); + }; + private handleBrowserHistoryPush = (e: IpcMainEvent, pathName: string) => { log.debug('handleBrowserHistoryPush', e.sender.id, pathName); @@ -522,7 +543,7 @@ export class ViewManager { return; } let redirectedView = this.getView(redirectedviewId) || currentView; - if (redirectedView !== currentView && redirectedView?.view.server.id === ServerViewState.getCurrentServer().id && redirectedView?.isLoggedIn) { + if (redirectedView !== currentView && redirectedView?.view.server.id === ServerViewState.getCurrentServer().id && (redirectedView?.isLoggedIn || cleanedPathName === '/')) { log.info('redirecting to a new view', redirectedView?.id || currentView.id); this.showById(redirectedView?.id || currentView.id); } else {