2016-12-27 11:15:02 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
import store from 'store';
|
2017-01-05 21:15:01 +01:00
|
|
|
import { parse as parseUrl, format as formatUrl } from 'url';
|
|
|
|
import { parse as parseQuery } from 'querystring';
|
2016-12-27 11:15:02 +01:00
|
|
|
|
|
|
|
import AddressBar from './AddressBar';
|
|
|
|
|
|
|
|
import styles from './web.css';
|
|
|
|
|
|
|
|
const LS_LAST_ADDRESS = '_parity::webLastAddress';
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
const hasProtocol = /^https?:\/\//;
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
export default class Web extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
api: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
static propTypes = {
|
|
|
|
params: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
state = {
|
2017-01-05 21:15:01 +01:00
|
|
|
displayedUrl: null,
|
2016-12-27 11:15:02 +01:00
|
|
|
isLoading: true,
|
|
|
|
token: null,
|
2017-01-05 21:15:01 +01:00
|
|
|
url: null
|
2016-12-27 11:15:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
2017-01-05 21:15:01 +01:00
|
|
|
const { api } = this.context;
|
|
|
|
const { params } = this.props;
|
2016-12-27 11:15:02 +01:00
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
api
|
|
|
|
.signer
|
|
|
|
.generateWebProxyAccessToken()
|
|
|
|
.then((token) => {
|
|
|
|
this.setState({ token });
|
|
|
|
});
|
2016-12-27 11:15:02 +01:00
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
this.setUrl(params.url);
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
componentWillReceiveProps (props) {
|
|
|
|
this.setUrl(props.params.url);
|
2016-12-27 11:15:02 +01:00
|
|
|
}
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
setUrl = (url) => {
|
|
|
|
url = url || store.get(LS_LAST_ADDRESS) || 'https://mkr.market';
|
|
|
|
if (!hasProtocol.test(url)) {
|
|
|
|
url = `https://${url}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ url, displayedUrl: url });
|
|
|
|
};
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
render () {
|
|
|
|
const { displayedUrl, isLoading, token } = this.state;
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
return (
|
|
|
|
<div className={ styles.wrapper }>
|
|
|
|
<h1 className={ styles.loading }>
|
|
|
|
Requesting access token...
|
|
|
|
</h1>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
const { dappsUrl } = this.context.api;
|
|
|
|
const { url } = this.state;
|
|
|
|
if (!url || !token) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = parseUrl(url);
|
|
|
|
const { protocol, host, path } = parsed;
|
|
|
|
const address = `${dappsUrl}/web/${token}/${protocol.slice(0, -1)}/${host}${path}`;
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
return (
|
|
|
|
<div className={ styles.wrapper }>
|
|
|
|
<AddressBar
|
|
|
|
className={ styles.url }
|
|
|
|
isLoading={ isLoading }
|
2017-01-05 21:15:01 +01:00
|
|
|
onChange={ this.onUrlChange }
|
|
|
|
onRefresh={ this.onRefresh }
|
2016-12-27 11:15:02 +01:00
|
|
|
url={ displayedUrl }
|
|
|
|
/>
|
|
|
|
<iframe
|
|
|
|
className={ styles.frame }
|
|
|
|
frameBorder={ 0 }
|
|
|
|
name={ name }
|
2017-01-05 21:15:01 +01:00
|
|
|
onLoad={ this.iframeOnLoad }
|
2016-12-27 11:15:02 +01:00
|
|
|
sandbox='allow-forms allow-same-origin allow-scripts'
|
|
|
|
scrolling='auto'
|
2017-01-18 13:05:01 +01:00
|
|
|
src={ address }
|
|
|
|
/>
|
2016-12-27 11:15:02 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
onUrlChange = (url) => {
|
|
|
|
if (!hasProtocol.test(url)) {
|
|
|
|
url = `https://${url}`;
|
|
|
|
}
|
|
|
|
|
2016-12-27 11:15:02 +01:00
|
|
|
store.set(LS_LAST_ADDRESS, url);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
|
|
|
displayedUrl: url,
|
|
|
|
url: url
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
onRefresh = () => {
|
2016-12-27 11:15:02 +01:00
|
|
|
const { displayedUrl } = this.state;
|
2017-01-05 21:15:01 +01:00
|
|
|
|
|
|
|
// Insert timestamp
|
|
|
|
// This is a hack to prevent caching.
|
|
|
|
const parsed = parseUrl(displayedUrl);
|
|
|
|
parsed.query = parseQuery(parsed.query);
|
|
|
|
parsed.query.t = Date.now().toString();
|
|
|
|
delete parsed.search;
|
2016-12-27 11:15:02 +01:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isLoading: true,
|
2017-01-05 21:15:01 +01:00
|
|
|
url: formatUrl(parsed)
|
2016-12-27 11:15:02 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-01-05 21:15:01 +01:00
|
|
|
iframeOnLoad = () => {
|
2016-12-27 11:15:02 +01:00
|
|
|
this.setState({
|
|
|
|
isLoading: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|