Skip to content

Commit

Permalink
feat(App): Handle protocol logic on OSX (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
yadPe authored Apr 18, 2021
1 parent 5cfac65 commit fee04f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/electron/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ const readableBits = (bytes, decimals) => {

const makeDownloadUrl = ({ beatmapSetId, uniqId }) => `https://beatconnect.io/b/${beatmapSetId}/${uniqId}/?nocf=1`;

const removeProtocolPrefix = (str = '', protocolString) => str.slice(protocolString.length);
const getBeatconnectProtocolParams = (argv = [''], protocol) => {
const protocolString = `${protocol}://`;
return argv.find(arg => arg.startsWith(protocolString))?.slice(protocolString.length);
const protocolLink = argv.find(arg => arg.startsWith(protocolString));
if (protocolLink) return removeProtocolPrefix(protocolLink, protocolString);
return undefined;
};

module.exports = {
readableBits,
makeDownloadUrl,
getBeatconnectProtocolParams,
removeProtocolPrefix,
};
15 changes: 6 additions & 9 deletions src/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { default: extensionInstaller, REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS } = r

const makeMainWindow = require('./MainWindow');
const { makeTracker } = require('./analytics');
const { getBeatconnectProtocolParams } = require('./helpers');
const { getBeatconnectProtocolParams, removeProtocolPrefix } = require('./helpers');
require('./ipcMessages');

log.transports.file.level = 'debug';
Expand Down Expand Up @@ -49,6 +49,11 @@ const main = async () => {
mainWindow = null;
});

app.on('open-url', (event, data) => {
event.preventDefault();
if (data) mainWindow.webContents.send('beatconnect-open', removeProtocolPrefix(data, `${CUSTOM_PROTOCOL}://`));
});

// init ga tracking and set tracking methods on global
const { trackEvent, trackNavigation } = makeTracker(mainWindow.webContents.session.getUserAgent());
global.tracking = { trackEvent, trackNavigation };
Expand Down Expand Up @@ -93,14 +98,6 @@ const main = async () => {
const isMainInstance = app.requestSingleInstanceLock();

if (isMainInstance || isDev) {
app.on('open-url', (event, data) => {
event.preventDefault();
// TODO: handle osx and linux ?
// TODO: Send data to renderer
// mainWindow.webContents.send('beatconnect-open', data)
console.log('Protocol called:', data);
});

if (!isDev) {
app.removeAsDefaultProtocolClient(CUSTOM_PROTOCOL);
const ok = app.setAsDefaultProtocolClient(CUSTOM_PROTOCOL);
Expand Down

0 comments on commit fee04f3

Please sign in to comment.