[MM-23452][MM-36861] Fixed some configuration issues (#1725)

* [MM-23452] Show last used server and tab correctly when restarting the app

* [MM-36861] Ensure handleConfigUpdate runs
This commit is contained in:
Devin Binnie
2021-09-10 10:48:00 -04:00
committed by GitHub
parent 54d82493bf
commit 082c95b094
9 changed files with 40 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ export const SWITCH_TAB = 'switch-tab';
export const CLOSE_TAB = 'close-tab'; export const CLOSE_TAB = 'close-tab';
export const OPEN_TAB = 'open-tab'; export const OPEN_TAB = 'open-tab';
export const SET_ACTIVE_VIEW = 'set-active-view'; export const SET_ACTIVE_VIEW = 'set-active-view';
export const UPDATE_LAST_ACTIVE = 'update-last-active';
export const MARK_READ = 'mark-read'; export const MARK_READ = 'mark-read';
export const FOCUS_BROWSERVIEW = 'focus-browserview'; export const FOCUS_BROWSERVIEW = 'focus-browserview';
export const ZOOM = 'zoom'; export const ZOOM = 'zoom';

View File

@@ -42,6 +42,7 @@ function upgradeV2toV3(configV2: ConfigV2) {
lastActiveTab: 0, lastActiveTab: 0,
}; };
}); });
config.lastActiveTeam = 0;
return config; return config;
} }

View File

@@ -122,6 +122,7 @@ const configDataSchemaV3 = Joi.object<ConfigV3>({
spellCheckerURL: Joi.string().allow(null), spellCheckerURL: Joi.string().allow(null),
darkMode: Joi.boolean().default(false), darkMode: Joi.boolean().default(false),
downloadLocation: Joi.string(), downloadLocation: Joi.string(),
lastActiveTeam: Joi.number().integer().min(0).default(0),
}); });
// eg. data['community.mattermost.com'] = { data: 'certificate data', issuerName: 'COMODO RSA Domain Validation Secure Server CA'}; // eg. data['community.mattermost.com'] = { data: 'certificate data', issuerName: 'COMODO RSA Domain Validation Secure Server CA'};

View File

@@ -13,7 +13,7 @@ import installExtension, {REACT_DEVELOPER_TOOLS} from 'electron-devtools-install
import log from 'electron-log'; import log from 'electron-log';
import 'airbnb-js-shims/target/es2015'; import 'airbnb-js-shims/target/es2015';
import {Team, TeamWithTabs} from 'types/config'; import {CombinedConfig, Team, TeamWithTabs} from 'types/config';
import {MentionData} from 'types/notification'; import {MentionData} from 'types/notification';
import {RemoteInfo} from 'types/server'; import {RemoteInfo} from 'types/server';
import {Boundaries} from 'types/utils'; import {Boundaries} from 'types/utils';
@@ -42,7 +42,7 @@ import {
SHOW_REMOVE_SERVER_MODAL, SHOW_REMOVE_SERVER_MODAL,
UPDATE_SHORTCUT_MENU, UPDATE_SHORTCUT_MENU,
OPEN_TEAMS_DROPDOWN, OPEN_TEAMS_DROPDOWN,
SET_ACTIVE_VIEW, UPDATE_LAST_ACTIVE,
} from 'common/communication'; } from 'common/communication';
import Config from 'common/config'; import Config from 'common/config';
import {MattermostServer} from 'common/servers/MattermostServer'; import {MattermostServer} from 'common/servers/MattermostServer';
@@ -242,7 +242,7 @@ function initializeInterCommunicationEventListeners() {
ipcMain.on('update-menu', handleUpdateMenuEvent); ipcMain.on('update-menu', handleUpdateMenuEvent);
ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateShortcutMenuEvent); ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateShortcutMenuEvent);
ipcMain.on(FOCUS_BROWSERVIEW, WindowManager.focusBrowserView); ipcMain.on(FOCUS_BROWSERVIEW, WindowManager.focusBrowserView);
ipcMain.on(SET_ACTIVE_VIEW, handleUpdateLastActiveTab); ipcMain.on(UPDATE_LAST_ACTIVE, handleUpdateLastActive);
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
ipcMain.on('open-app-menu', handleOpenAppMenu); ipcMain.on('open-app-menu', handleOpenAppMenu);
@@ -272,8 +272,8 @@ function initializeInterCommunicationEventListeners() {
// config event handlers // config event handlers
// //
function handleConfigUpdate(newConfig: Config) { function handleConfigUpdate(newConfig: CombinedConfig) {
if (!newConfig.data) { if (!newConfig) {
return; return;
} }
if (process.platform === 'win32' || process.platform === 'linux') { if (process.platform === 'win32' || process.platform === 'linux') {
@@ -284,13 +284,15 @@ function handleConfigUpdate(newConfig: Config) {
}).catch((err) => { }).catch((err) => {
log.error('error:', err); log.error('error:', err);
}); });
WindowManager.setConfig(newConfig.data); WindowManager.setConfig(newConfig);
authManager.handleConfigUpdate(newConfig.data); if (authManager) {
setUnreadBadgeSetting(newConfig.data && newConfig.data.showUnreadBadge); authManager.handleConfigUpdate(newConfig);
}
setUnreadBadgeSetting(newConfig && newConfig.showUnreadBadge);
} }
ipcMain.emit('update-menu', true, config); ipcMain.emit('update-menu', true, config);
ipcMain.emit(EMIT_CONFIGURATION, true, newConfig.data); ipcMain.emit(EMIT_CONFIGURATION, true, newConfig);
} }
function handleConfigSynchronize() { function handleConfigSynchronize() {
@@ -318,9 +320,6 @@ function handleConfigSynchronize() {
handleNewServerModal(); handleNewServerModal();
} }
} }
ipcMain.emit('update-menu', true, config);
ipcMain.emit(EMIT_CONFIGURATION, true, config.data);
} }
function handleReloadConfig() { function handleReloadConfig() {
@@ -950,14 +949,15 @@ function resizeScreen(browserWindow: BrowserWindow) {
browserWindow.on('restore', handle); browserWindow.on('restore', handle);
handle(); handle();
} }
function handleUpdateLastActiveTab(event: IpcMainEvent, serverName: string, viewName: string) { function handleUpdateLastActive(event: IpcMainEvent, serverName: string, viewName: string) {
const teams = config.teams; const teams = config.teams;
teams.forEach((team) => { teams.forEach((team) => {
if (team.name === serverName) { if (team.name === serverName) {
const viewIndex = team?.tabs.findIndex((tab) => tab.name === viewName); const viewOrder = team?.tabs.find((tab) => tab.name === viewName)?.order || 0;
team.lastActiveTab = viewIndex; team.lastActiveTab = viewOrder;
} }
}); });
config.set('teams', teams); config.set('teams', teams);
config.set('lastActiveTeam', teams.find((team) => team.name === serverName)?.order || 0);
} }

View File

@@ -16,6 +16,7 @@ import {
LOADSCREEN_END, LOADSCREEN_END,
SET_ACTIVE_VIEW, SET_ACTIVE_VIEW,
OPEN_TAB, OPEN_TAB,
UPDATE_LAST_ACTIVE,
} from 'common/communication'; } from 'common/communication';
import urlUtils from 'common/utils/url'; import urlUtils from 'common/utils/url';
@@ -33,6 +34,7 @@ const URL_VIEW_HEIGHT = 36;
export class ViewManager { export class ViewManager {
configServers: TeamWithTabs[]; configServers: TeamWithTabs[];
lastActiveServer?: number;
viewOptions: BrowserViewConstructorOptions; viewOptions: BrowserViewConstructorOptions;
closedViews: Map<string, {srv: MattermostServer; tab: Tab}>; closedViews: Map<string, {srv: MattermostServer; tab: Tab}>;
views: Map<string, MattermostView>; views: Map<string, MattermostView>;
@@ -44,6 +46,7 @@ export class ViewManager {
constructor(config: CombinedConfig, mainWindow: BrowserWindow) { constructor(config: CombinedConfig, mainWindow: BrowserWindow) {
this.configServers = config.teams; this.configServers = config.teams;
this.lastActiveServer = config.lastActiveTeam;
this.viewOptions = {webPreferences: {spellcheck: config.useSpellChecker}}; this.viewOptions = {webPreferences: {spellcheck: config.useSpellChecker}};
this.views = new Map(); // keep in mind that this doesn't need to hold server order, only tabs on the renderer need that. this.views = new Map(); // keep in mind that this doesn't need to hold server order, only tabs on the renderer need that.
this.mainWindow = mainWindow; this.mainWindow = mainWindow;
@@ -121,10 +124,13 @@ export class ViewManager {
showInitial = () => { showInitial = () => {
if (this.configServers.length) { if (this.configServers.length) {
const element = this.configServers.find((e) => e.order === 0); const element = this.configServers.find((e) => e.order === this.lastActiveServer || 0);
if (element) { if (element && element.tabs.length) {
const openTabs = element.tabs.filter((tab) => !tab.isClosed); let tab = element.tabs.find((tab) => tab.order === element.lastActiveTab || 0);
const tab = openTabs.find((e) => e.order === 0) || openTabs[0]; if (tab?.isClosed) {
const openTabs = element.tabs.filter((tab) => !tab.isClosed);
tab = openTabs.find((e) => e.order === 0) || openTabs[0];
}
if (tab) { if (tab) {
const tabView = getTabViewName(element.name, tab.name); const tabView = getTabViewName(element.name, tab.name);
this.showByName(tabView); this.showByName(tabView);
@@ -155,6 +161,7 @@ export class ViewManager {
if (newView.isReady()) { if (newView.isReady()) {
// if view is not ready, the renderer will have something to display instead. // if view is not ready, the renderer will have something to display instead.
newView.show(); newView.show();
ipcMain.emit(UPDATE_LAST_ACTIVE, true, newView.tab.server.name, newView.tab.type);
if (newView.needsLoadingScreen()) { if (newView.needsLoadingScreen()) {
this.showLoadingScreen(); this.showLoadingScreen();
} else { } else {

View File

@@ -363,7 +363,7 @@ export function switchServer(serverName: string) {
return; return;
} }
status.currentServerName = serverName; status.currentServerName = serverName;
const lastActiveTab = server.tabs[server.lastActiveTab || 0]; const lastActiveTab = server.tabs.find((tab) => !tab.isClosed && tab.order === (server.lastActiveTab || 0)) || server.tabs[0];
const tabViewName = getTabViewName(serverName, lastActiveTab.name); const tabViewName = getTabViewName(serverName, lastActiveTab.name);
status.viewManager?.showByName(tabViewName); status.viewManager?.showByName(tabViewName);
ipcMain.emit(UPDATE_SHORTCUT_MENU); ipcMain.emit(UPDATE_SHORTCUT_MENU);
@@ -544,7 +544,7 @@ function handleBrowserHistoryPush(e: IpcMainEvent, viewName: string, pathName: s
status.viewManager.openClosedTab(redirectedViewName, `${currentView?.tab.server.url}${pathName}`); status.viewManager.openClosedTab(redirectedViewName, `${currentView?.tab.server.url}${pathName}`);
} }
const redirectedView = status.viewManager?.views.get(redirectedViewName) || currentView; const redirectedView = status.viewManager?.views.get(redirectedViewName) || currentView;
if (redirectedView !== currentView) { if (redirectedView !== currentView && redirectedView?.tab.server.name === status.currentServerName) {
log.info('redirecting to a new view', redirectedView?.name || viewName); log.info('redirecting to a new view', redirectedView?.name || viewName);
status.viewManager?.showByName(redirectedView?.name || viewName); status.viewManager?.showByName(redirectedView?.name || viewName);
} }

View File

@@ -61,6 +61,7 @@ enum Status {
type Props = { type Props = {
teams: TeamWithTabs[]; teams: TeamWithTabs[];
lastActiveTeam?: number;
moveTabs: (teamName: string, originalOrder: number, newOrder: number) => number | undefined; moveTabs: (teamName: string, originalOrder: number, newOrder: number) => number | undefined;
openMenu: () => void; openMenu: () => void;
darkMode: boolean; darkMode: boolean;
@@ -101,8 +102,12 @@ export default class MainPage extends React.PureComponent<Props, State> {
this.topBar = React.createRef(); this.topBar = React.createRef();
this.threeDotMenu = React.createRef(); this.threeDotMenu = React.createRef();
const firstServer = this.props.teams.find((team) => team.order === 0); const firstServer = this.props.teams.find((team) => team.order === this.props.lastActiveTeam || 0);
const firstTab = firstServer?.tabs.find((tab, index) => index === (firstServer.lastActiveTab || 0)) || firstServer?.tabs[0]; let firstTab = firstServer?.tabs.find((tab) => tab.order === firstServer.lastActiveTab || 0);
if (firstTab?.isClosed) {
const openTabs = firstServer?.tabs.filter((tab) => !tab.isClosed) || [];
firstTab = openTabs?.find((e) => e.order === 0) || openTabs[0];
}
this.state = { this.state = {
activeServerName: firstServer?.name, activeServerName: firstServer?.name,

View File

@@ -122,6 +122,7 @@ class Root extends React.PureComponent<Record<string, never>, State> {
return ( return (
<MainPage <MainPage
teams={config.teams} teams={config.teams}
lastActiveTeam={config.lastActiveTeam}
moveTabs={this.moveTabs} moveTabs={this.moveTabs}
openMenu={this.openMenu} openMenu={this.openMenu}
darkMode={config.darkMode} darkMode={config.darkMode}

View File

@@ -38,6 +38,7 @@ export type ConfigV3 = {
darkMode: boolean; darkMode: boolean;
downloadLocation: string; downloadLocation: string;
spellCheckerURL?: string; spellCheckerURL?: string;
lastActiveTeam?: number;
} }
export type ConfigV2 = { export type ConfigV2 = {