Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(App): Handle protocol logic on OSX #434

Merged
merged 2 commits into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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