Refactorings to make code cleaner. Fixes to not execute anything when windows squirrel installer.

This commit is contained in:
David Meza
2017-10-20 13:23:51 -05:00
parent 7a3552dfdc
commit c624a9a009
4 changed files with 29 additions and 17 deletions

View File

@@ -44,8 +44,7 @@ const MainPage = createReactClass({
unreadAtActive: new Array(this.props.teams.length), unreadAtActive: new Array(this.props.teams.length),
mentionAtActiveCounts: new Array(this.props.teams.length), mentionAtActiveCounts: new Array(this.props.teams.length),
loginQueue: [], loginQueue: [],
targetURL: '', targetURL: ''
deeplinkingUrl
}; };
}, },
componentDidMount() { componentDidMount() {
@@ -122,15 +121,14 @@ const MainPage = createReactClass({
this.focusOnWebView(); this.focusOnWebView();
}); });
ipcRenderer.on('protocol-deeplink', (event, lastUrl) => { ipcRenderer.on('protocol-deeplink', (event, deepLinkUrl) => {
const mattermostViews = document.getElementsByClassName('mattermostView mattermostView-with-tab'); const lastUrlDomain = Utils.getDomain(deepLinkUrl);
const lastUrlDomain = Utils.getDomain(lastUrl); for (var i = 0; i < this.props.teams.length; i++) {
for (var i = 0; i < mattermostViews.length; i++) { if (lastUrlDomain === Utils.getDomain(self.refs[`mattermostView${i}`].getSrc())) {
if (lastUrlDomain === Utils.getDomain(mattermostViews[i].src)) {
self.refs[`mattermostView${i}`].handleDeepLink(lastUrl.replace(lastUrlDomain, ''));
if (this.state.key !== i) { if (this.state.key !== i) {
this.handleSelect(i); this.handleSelect(i);
} }
self.refs[`mattermostView${i}`].handleDeepLink(deepLinkUrl.replace(lastUrlDomain, ''));
break; break;
} }
} }
@@ -276,7 +274,7 @@ const MainPage = createReactClass({
var isActive = self.state.key === index; var isActive = self.state.key === index;
let teamUrl = team.url; let teamUrl = team.url;
const deeplinkingUrl = this.state.deeplinkingUrl; const deeplinkingUrl = remote.getCurrentWindow().deeplinkingUrl;
if (deeplinkingUrl !== null && deeplinkingUrl.includes(teamUrl)) { if (deeplinkingUrl !== null && deeplinkingUrl.includes(teamUrl)) {
teamUrl = deeplinkingUrl; teamUrl = deeplinkingUrl;
} }

View File

@@ -205,10 +205,15 @@ const MattermostView = createReactClass({
webview.getWebContents().goForward(); webview.getWebContents().goForward();
}, },
getSrc() {
const webview = findDOMNode(this.refs.webview);
return webview.src;
},
handleDeepLink(relativeUrl) { handleDeepLink(relativeUrl) {
const webview = findDOMNode(this.refs.webview); const webview = findDOMNode(this.refs.webview);
webview.executeJavaScript( webview.executeJavaScript(
'history.pushState(null, null, "/' + relativeUrl + '");' 'history.pushState(null, null, "' + relativeUrl + '");'
); );
webview.executeJavaScript( webview.executeJavaScript(
'dispatchEvent(new PopStateEvent("popstate", null));' 'dispatchEvent(new PopStateEvent("popstate", null));'

View File

@@ -342,7 +342,7 @@ if (protocols && protocols[0] &&
function setDeeplinkingUrl(url) { function setDeeplinkingUrl(url) {
if (scheme) { if (scheme) {
deeplinkingUrl = url.replace(scheme, 'https'); deeplinkingUrl = url.replace(new RegExp('^' + scheme), 'https');
} }
} }
@@ -351,6 +351,7 @@ app.on('open-url', (event, url) => {
event.preventDefault(); event.preventDefault();
setDeeplinkingUrl(url); setDeeplinkingUrl(url);
mainWindow.webContents.send('protocol-deeplink', deeplinkingUrl); mainWindow.webContents.send('protocol-deeplink', deeplinkingUrl);
mainWindow.show();
}); });
// This method will be called when Electron has finished // This method will be called when Electron has finished
@@ -367,9 +368,13 @@ app.on('ready', () => {
// Protocol handler for win32 // Protocol handler for win32
if (process.platform === 'win32') { if (process.platform === 'win32') {
// Keep only command line / deep linked arguments // Keep only command line / deep linked argument. Make sure it's not squirrel command
if (Array.isArray(process.argv.slice(1)) && process.argv.slice(1).length > 0) { const tmpArgs = process.argv.slice(1);
setDeeplinkingUrl(process.argv.slice(1)[0]); if (
Array.isArray(tmpArgs) && tmpArgs.length > 0 &&
tmpArgs[0].match(/^--squirrel-/) === null
) {
setDeeplinkingUrl(tmpArgs[0]);
} }
} }

View File

@@ -1,6 +1,10 @@
const REGEXP_DOMAIN = /(?:[^/]*\/){3}/; const {URL} = require('url');
export function getDomain(url) { export function getDomain(url) {
const matched = url.match(REGEXP_DOMAIN); try {
return matched ? matched[0] : null; const objectUrl = new URL(url);
return objectUrl.origin;
} catch (e) {
return null;
}
} }