/* eslint-disable react/jsx-no-useless-fragment */
import { Component } from 'react';
import PropTypes from 'prop-types';
import { autorun } from 'mobx';
import { observer, inject } from 'mobx-react';
import classnames from 'classnames';
import TopBarProgress from 'react-topbar-progress-indicator';
import ServiceModel from '../../../models/Service';
import StatusBarTargetUrl from '../../ui/StatusBarTargetUrl';
import WebviewLoader from '../../ui/WebviewLoader';
import WebviewCrashHandler from './WebviewCrashHandler';
import WebviewErrorHandler from './ErrorHandlers/WebviewErrorHandler';
import ServiceDisabled from './ServiceDisabled';
import ServiceWebview from './ServiceWebview';
import SettingsStore from '../../../stores/SettingsStore';
import WebControlsScreen from '../../../features/webControls/containers/WebControlsScreen';
import { CUSTOM_WEBSITE_RECIPE_ID } from '../../../config';
class ServiceView extends Component {
static propTypes = {
service: PropTypes.instanceOf(ServiceModel).isRequired,
setWebviewReference: PropTypes.func.isRequired,
detachService: PropTypes.func.isRequired,
reload: PropTypes.func.isRequired,
edit: PropTypes.func.isRequired,
enable: PropTypes.func.isRequired,
isActive: PropTypes.bool,
stores: PropTypes.shape({
settings: PropTypes.instanceOf(SettingsStore).isRequired,
}).isRequired,
isSpellcheckerEnabled: PropTypes.bool.isRequired,
};
static defaultProps = {
isActive: false,
};
state = {
forceRepaint: false,
targetUrl: '',
statusBarVisible: false,
};
hibernationTimer = null;
autorunDisposer = null;
forceRepaintTimeout = null;
componentDidMount() {
this.autorunDisposer = autorun(() => {
if (this.props.service.isActive) {
this.setState({ forceRepaint: true });
this.forceRepaintTimeout = setTimeout(() => {
this.setState({ forceRepaint: false });
}, 100);
}
});
}
componentWillUnmount() {
this.autorunDisposer();
clearTimeout(this.forceRepaintTimeout);
clearTimeout(this.hibernationTimer);
}
render() {
const {
detachService,
service,
setWebviewReference,
reload,
edit,
enable,
stores,
isSpellcheckerEnabled,
} = this.props;
const { navigationBarBehaviour, navigationBarManualActive } =
stores.settings.app;
const showNavBar =
navigationBarBehaviour === 'always' ||
(navigationBarBehaviour === 'custom' &&
service.recipe.id === CUSTOM_WEBSITE_RECIPE_ID) ||
navigationBarManualActive;
const webviewClasses = classnames({
services__webview: true,
'services__webview-wrapper': true,
'is-active': service.isActive,
'services__webview--force-repaint': this.state.forceRepaint,
});
let statusBar = null;
if (this.state.statusBarVisible) {
statusBar =