From cfc0613d0f443a7eb7ac189c2d4b42d38ec7daa1 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Fri, 3 Sep 2021 14:55:52 -0400 Subject: [PATCH] [MM-38310][MM-38313][MM-38315] Fixed some issues caused by having multiple tabs (#1722) * [MM-38313][MM-38315] Fixed some issues caused by having multiple tabs * [MM-38310] Make sure notification navigates to correct tab --- src/common/communication.ts | 2 +- src/common/tabs/BaseTabView.ts | 3 +++ src/common/tabs/MessagingTabView.ts | 4 ++++ src/common/tabs/TabView.ts | 1 + src/main/main.ts | 13 +++++++++++++ src/main/notifications/index.ts | 3 ++- src/main/preload/mattermost.js | 12 ++++++++---- src/main/views/MattermostView.ts | 4 ++-- src/main/views/viewManager.ts | 15 ++++++--------- src/main/windows/windowManager.ts | 8 +++++++- src/renderer/components/MainPage.tsx | 2 +- src/renderer/components/TabBar.tsx | 2 +- 12 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/common/communication.ts b/src/common/communication.ts index 07686cf2..c87efb84 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -68,7 +68,7 @@ export const SESSION_EXPIRED = 'session_expired'; export const UPDATE_TRAY = 'update_tray'; export const UPDATE_BADGE = 'update_badge'; -export const SET_VIEW_NAME = 'set-view-name'; +export const SET_VIEW_OPTIONS = 'set-view-name'; export const REACT_APP_INITIALIZED = 'react-app-initialized'; export const TOGGLE_BACK_BUTTON = 'toggle-back-button'; diff --git a/src/common/tabs/BaseTabView.ts b/src/common/tabs/BaseTabView.ts index 34ae4eeb..a6338bab 100644 --- a/src/common/tabs/BaseTabView.ts +++ b/src/common/tabs/BaseTabView.ts @@ -20,4 +20,7 @@ export default abstract class BaseTabView implements TabView { get type(): TabType { throw new Error('Not implemented'); } + get shouldNotify(): boolean { + return false; + } } diff --git a/src/common/tabs/MessagingTabView.ts b/src/common/tabs/MessagingTabView.ts index 67146dc5..15b4920f 100644 --- a/src/common/tabs/MessagingTabView.ts +++ b/src/common/tabs/MessagingTabView.ts @@ -12,4 +12,8 @@ export default class MessagingTabView extends BaseTabView { get type(): TabType { return TAB_MESSAGING; } + + get shouldNotify(): boolean { + return true; + } } diff --git a/src/common/tabs/TabView.ts b/src/common/tabs/TabView.ts index 3c2d83b2..97c047b9 100644 --- a/src/common/tabs/TabView.ts +++ b/src/common/tabs/TabView.ts @@ -20,6 +20,7 @@ export interface TabView { get name(): string; get type(): TabType; get url(): URL; + get shouldNotify(): boolean; } export function getDefaultTeamWithTabsFromTeam(team: Team) { diff --git a/src/main/main.ts b/src/main/main.ts index c171ad4f..9acef850 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -42,6 +42,7 @@ import { SHOW_REMOVE_SERVER_MODAL, UPDATE_SHORTCUT_MENU, OPEN_TEAMS_DROPDOWN, + SET_ACTIVE_VIEW, } from 'common/communication'; import Config from 'common/config'; import {MattermostServer} from 'common/servers/MattermostServer'; @@ -241,6 +242,7 @@ function initializeInterCommunicationEventListeners() { ipcMain.on('update-menu', handleUpdateMenuEvent); ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateShortcutMenuEvent); ipcMain.on(FOCUS_BROWSERVIEW, WindowManager.focusBrowserView); + ipcMain.on(SET_ACTIVE_VIEW, handleUpdateLastActiveTab); if (process.platform !== 'darwin') { ipcMain.on('open-app-menu', handleOpenAppMenu); @@ -948,3 +950,14 @@ function resizeScreen(browserWindow: BrowserWindow) { browserWindow.on('restore', handle); handle(); } +function handleUpdateLastActiveTab(event: IpcMainEvent, serverName: string, viewName: string) { + const teams = config.teams; + teams.forEach((team) => { + if (team.name === serverName) { + const viewIndex = team?.tabs.findIndex((tab) => tab.name === viewName); + team.lastActiveTab = viewIndex; + } + }); + config.set('teams', teams); +} + diff --git a/src/main/notifications/index.ts b/src/main/notifications/index.ts index 405217ae..90b92c6f 100644 --- a/src/main/notifications/index.ts +++ b/src/main/notifications/index.ts @@ -8,6 +8,7 @@ import {MentionData} from 'types/notification'; import {ServerFromURL} from 'types/utils'; import {PLAY_SOUND} from 'common/communication'; +import {TAB_MESSAGING} from 'common/tabs/TabView'; import * as windowManager from '../windows/windowManager'; @@ -52,7 +53,7 @@ export function displayMention(title: string, body: string, channel: {id: string mention.on('click', () => { if (serverName) { - windowManager.switchServer(serverName); + windowManager.switchTab(serverName, TAB_MESSAGING); webcontents.send('notification-clicked', {channel, teamId, url}); } }); diff --git a/src/main/preload/mattermost.js b/src/main/preload/mattermost.js index 2e7ac41a..9e9c36ae 100644 --- a/src/main/preload/mattermost.js +++ b/src/main/preload/mattermost.js @@ -17,7 +17,7 @@ import { IS_UNREAD, UNREAD_RESULT, SESSION_EXPIRED, - SET_VIEW_NAME, + SET_VIEW_OPTIONS, REACT_APP_INITIALIZED, USER_ACTIVITY_UPDATE, CLOSE_TEAMS_DROPDOWN, @@ -31,6 +31,7 @@ let appVersion; let appName; let sessionExpired; let viewName; +let shouldSendNotifications; console.log('Preload initialized'); @@ -120,8 +121,10 @@ window.addEventListener('message', ({origin, data = {}} = {}) => { // it will be captured by itself too break; case 'dispatch-notification': { - const {title, body, channel, teamId, url, silent, data: messageData} = message; - ipcRenderer.send(NOTIFY_MENTION, title, body, channel, teamId, url, silent, messageData); + if (shouldSendNotifications) { + const {title, body, channel, teamId, url, silent, data: messageData} = message; + ipcRenderer.send(NOTIFY_MENTION, title, body, channel, teamId, url, silent, messageData); + } break; } case 'browser-history-push': { @@ -179,8 +182,9 @@ ipcRenderer.on(IS_UNREAD, (event, favicon, server) => { } }); -ipcRenderer.on(SET_VIEW_NAME, (_, name) => { +ipcRenderer.on(SET_VIEW_OPTIONS, (_, name, shouldNotify) => { viewName = name; + shouldSendNotifications = shouldNotify; }); function getUnreadCount() { diff --git a/src/main/views/MattermostView.ts b/src/main/views/MattermostView.ts index d5b82abe..48905157 100644 --- a/src/main/views/MattermostView.ts +++ b/src/main/views/MattermostView.ts @@ -18,7 +18,7 @@ import { IS_UNREAD, UNREAD_RESULT, TOGGLE_BACK_BUTTON, - SET_VIEW_NAME, + SET_VIEW_OPTIONS, LOADSCREEN_END, } from 'common/communication'; @@ -97,7 +97,7 @@ export class MattermostView extends EventEmitter { } this.view.webContents.on('did-finish-load', () => { - this.view.webContents.send(SET_VIEW_NAME, this.tab.name); + this.view.webContents.send(SET_VIEW_OPTIONS, this.tab.name, this.tab.shouldNotify); }); this.contextMenu = new ContextMenu({}, this.view); diff --git a/src/main/views/viewManager.ts b/src/main/views/viewManager.ts index 7c6200ce..d85dd363 100644 --- a/src/main/views/viewManager.ts +++ b/src/main/views/viewManager.ts @@ -242,18 +242,15 @@ export class ViewManager { } } - findByWebContent(webContentId: number) { + findViewByWebContent(webContentId: number) { let found = null; - let serverName; let view; - const entries = this.views.entries(); + const entries = this.views.values(); - for ([serverName, view] of entries) { - if (typeof serverName !== 'undefined') { - const wc = view.getWebContents(); - if (wc && wc.id === webContentId) { - found = serverName; - } + for (view of entries) { + const wc = view.getWebContents(); + if (wc && wc.id === webContentId) { + found = view; } } return found; diff --git a/src/main/windows/windowManager.ts b/src/main/windows/windowManager.ts index c7d3575e..4b4ed8f0 100644 --- a/src/main/windows/windowManager.ts +++ b/src/main/windows/windowManager.ts @@ -420,8 +420,14 @@ export function updateLoadingScreenDarkMode(darkMode: boolean) { } } +export function getViewNameByWebContentsId(webContentsId: number) { + const view = status.viewManager?.findViewByWebContent(webContentsId); + return view?.name; +} + export function getServerNameByWebContentsId(webContentsId: number) { - return status.viewManager?.findByWebContent(webContentsId); + const view = status.viewManager?.findViewByWebContent(webContentsId); + return view?.tab.server.name; } export function close() { diff --git a/src/renderer/components/MainPage.tsx b/src/renderer/components/MainPage.tsx index 103b0eb0..8a008fc3 100644 --- a/src/renderer/components/MainPage.tsx +++ b/src/renderer/components/MainPage.tsx @@ -102,7 +102,7 @@ export default class MainPage extends React.PureComponent { this.threeDotMenu = React.createRef(); const firstServer = this.props.teams.find((team) => team.order === 0); - const firstTab = firstServer?.tabs.find((tab) => tab.order === (firstServer.lastActiveTab || 0)) || firstServer?.tabs[0]; + const firstTab = firstServer?.tabs.find((tab, index) => index === (firstServer.lastActiveTab || 0)) || firstServer?.tabs[0]; this.state = { activeServerName: firstServer?.name, diff --git a/src/renderer/components/TabBar.tsx b/src/renderer/components/TabBar.tsx index bc15b074..29cfe767 100644 --- a/src/renderer/components/TabBar.tsx +++ b/src/renderer/components/TabBar.tsx @@ -101,7 +101,7 @@ export default class TabBar extends React.PureComponent { as='li' id={`teamTabItem${index}`} draggable={false} - title={tab.name} + title={getTabDisplayName(tab.name as TabType)} className={classNames('teamTabItem', { active: this.props.activeTabName === tab.name, dragging: snapshot.isDragging,