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

View File

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

View File

@@ -342,7 +342,7 @@ if (protocols && protocols[0] &&
function setDeeplinkingUrl(url) {
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();
setDeeplinkingUrl(url);
mainWindow.webContents.send('protocol-deeplink', deeplinkingUrl);
mainWindow.show();
});
// This method will be called when Electron has finished
@@ -367,9 +368,13 @@ app.on('ready', () => {
// Protocol handler for win32
if (process.platform === 'win32') {
// Keep only command line / deep linked arguments
if (Array.isArray(process.argv.slice(1)) && process.argv.slice(1).length > 0) {
setDeeplinkingUrl(process.argv.slice(1)[0]);
// Keep only command line / deep linked argument. Make sure it's not squirrel command
const tmpArgs = process.argv.slice(1);
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) {
const matched = url.match(REGEXP_DOMAIN);
return matched ? matched[0] : null;
try {
const objectUrl = new URL(url);
return objectUrl.origin;
} catch (e) {
return null;
}
}