2019-02-14 14:22:00 -08:00
|
|
|
// @flow
|
|
|
|
import React, { type ComponentType, Component } from 'react';
|
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
2019-02-15 07:15:31 -08:00
|
|
|
import { ipcRenderer, remote } from 'electron';
|
|
|
|
import { type RouterHistory, type Location } from 'react-router-dom';
|
|
|
|
import { searchUriInArgv } from '../../config/handle-deeplink';
|
2019-02-18 17:56:40 -08:00
|
|
|
import electronStore from '../../config/electron-store';
|
2019-02-14 14:22:00 -08:00
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
type PassedProps = {
|
|
|
|
history: RouterHistory,
|
|
|
|
location: Location,
|
|
|
|
isRunning: boolean,
|
|
|
|
};
|
|
|
|
|
2019-02-18 17:56:40 -08:00
|
|
|
const OSX_DEEPLINK_URL_KEY = 'OSX_DEEPLINK_URL';
|
2019-02-18 17:22:07 -08:00
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
export const withDeepLink = (
|
2019-02-14 14:22:00 -08:00
|
|
|
WrappedComponent: ComponentType<PassedProps>,
|
|
|
|
): ComponentType<$Diff<PassedProps, {}>> => class extends Component<PassedProps> {
|
2019-02-15 07:15:31 -08:00
|
|
|
componentDidMount() {
|
2019-02-18 17:56:40 -08:00
|
|
|
const arg = searchUriInArgv([
|
|
|
|
...remote.process.argv,
|
|
|
|
electronStore.get(OSX_DEEPLINK_URL_KEY) || '',
|
|
|
|
]);
|
2019-02-15 07:15:31 -08:00
|
|
|
|
|
|
|
if (arg) this.redirect(arg);
|
|
|
|
|
2019-02-18 05:55:25 -08:00
|
|
|
remote.app.on('open-url', (event, url) => {
|
|
|
|
this.redirect(url);
|
|
|
|
});
|
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
ipcRenderer.on('on-deep-link', (event: Object, message: string) => {
|
|
|
|
this.redirect(message);
|
|
|
|
});
|
|
|
|
}
|
2019-02-14 14:22:00 -08:00
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
componentWillUnmount() {
|
|
|
|
ipcRenderer.removeAllListeners('on-deep-link');
|
|
|
|
}
|
2019-02-14 14:22:00 -08:00
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
redirect(message: string) {
|
|
|
|
const { history } = this.props;
|
2019-02-14 14:22:00 -08:00
|
|
|
|
2019-02-18 17:22:07 -08:00
|
|
|
// clean osx deeplink storage
|
|
|
|
if (electronStore.has(OSX_DEEPLINK_URL_KEY)) {
|
2019-02-18 17:56:40 -08:00
|
|
|
electronStore.delete(OSX_DEEPLINK_URL_KEY);
|
2019-02-18 17:22:07 -08:00
|
|
|
}
|
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
history.replace(`/send/${message.replace(/zcash:(\/\/)?/, '')}`);
|
|
|
|
}
|
2019-02-14 14:22:00 -08:00
|
|
|
|
2019-02-15 07:15:31 -08:00
|
|
|
render() {
|
|
|
|
return <WrappedComponent {...this.props} {...this.state} />;
|
|
|
|
}
|
|
|
|
};
|