zepio/app/components/with-deeplink.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

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