diff options
Diffstat (limited to 'src/components/services/content/ServiceView.js')
-rw-r--r-- | src/components/services/content/ServiceView.js | 77 |
1 files changed, 64 insertions, 13 deletions
diff --git a/src/components/services/content/ServiceView.js b/src/components/services/content/ServiceView.js index 3b09518c5..273653ea2 100644 --- a/src/components/services/content/ServiceView.js +++ b/src/components/services/content/ServiceView.js | |||
@@ -1,7 +1,7 @@ | |||
1 | import React, { Component, Fragment } from 'react'; | 1 | import React, { Component, Fragment } from 'react'; |
2 | import PropTypes from 'prop-types'; | 2 | import PropTypes from 'prop-types'; |
3 | import { autorun } from 'mobx'; | 3 | import { autorun, reaction } from 'mobx'; |
4 | import { observer } from 'mobx-react'; | 4 | import { observer, inject } from 'mobx-react'; |
5 | import classnames from 'classnames'; | 5 | import classnames from 'classnames'; |
6 | 6 | ||
7 | import ServiceModel from '../../../models/Service'; | 7 | import ServiceModel from '../../../models/Service'; |
@@ -10,12 +10,12 @@ import WebviewLoader from '../../ui/WebviewLoader'; | |||
10 | import WebviewCrashHandler from './WebviewCrashHandler'; | 10 | import WebviewCrashHandler from './WebviewCrashHandler'; |
11 | import WebviewErrorHandler from './ErrorHandlers/WebviewErrorHandler'; | 11 | import WebviewErrorHandler from './ErrorHandlers/WebviewErrorHandler'; |
12 | import ServiceDisabled from './ServiceDisabled'; | 12 | import ServiceDisabled from './ServiceDisabled'; |
13 | import ServiceRestricted from './ServiceRestricted'; | ||
14 | import ServiceWebview from './ServiceWebview'; | 13 | import ServiceWebview from './ServiceWebview'; |
14 | import SettingsStore from '../../../stores/SettingsStore'; | ||
15 | import WebControlsScreen from '../../../features/webControls/containers/WebControlsScreen'; | 15 | import WebControlsScreen from '../../../features/webControls/containers/WebControlsScreen'; |
16 | import { CUSTOM_WEBSITE_ID } from '../../../features/webControls/constants'; | 16 | import { CUSTOM_WEBSITE_ID } from '../../../features/webControls/constants'; |
17 | 17 | ||
18 | export default @observer class ServiceView extends Component { | 18 | export default @observer @inject('stores') class ServiceView extends Component { |
19 | static propTypes = { | 19 | static propTypes = { |
20 | service: PropTypes.instanceOf(ServiceModel).isRequired, | 20 | service: PropTypes.instanceOf(ServiceModel).isRequired, |
21 | setWebviewReference: PropTypes.func.isRequired, | 21 | setWebviewReference: PropTypes.func.isRequired, |
@@ -24,7 +24,9 @@ export default @observer class ServiceView extends Component { | |||
24 | edit: PropTypes.func.isRequired, | 24 | edit: PropTypes.func.isRequired, |
25 | enable: PropTypes.func.isRequired, | 25 | enable: PropTypes.func.isRequired, |
26 | isActive: PropTypes.bool, | 26 | isActive: PropTypes.bool, |
27 | upgrade: PropTypes.func.isRequired, | 27 | stores: PropTypes.shape({ |
28 | settings: PropTypes.instanceOf(SettingsStore).isRequired, | ||
29 | }).isRequired, | ||
28 | }; | 30 | }; |
29 | 31 | ||
30 | static defaultProps = { | 32 | static defaultProps = { |
@@ -35,12 +37,20 @@ export default @observer class ServiceView extends Component { | |||
35 | forceRepaint: false, | 37 | forceRepaint: false, |
36 | targetUrl: '', | 38 | targetUrl: '', |
37 | statusBarVisible: false, | 39 | statusBarVisible: false, |
40 | hibernate: false, | ||
41 | hibernationTimer: null, | ||
38 | }; | 42 | }; |
39 | 43 | ||
40 | autorunDisposer = null; | 44 | autorunDisposer = null; |
41 | 45 | ||
42 | forceRepaintTimeout = null; | 46 | forceRepaintTimeout = null; |
43 | 47 | ||
48 | constructor(props) { | ||
49 | super(props); | ||
50 | |||
51 | this.startHibernationTimer = this.startHibernationTimer.bind(this); | ||
52 | } | ||
53 | |||
44 | componentDidMount() { | 54 | componentDidMount() { |
45 | this.autorunDisposer = autorun(() => { | 55 | this.autorunDisposer = autorun(() => { |
46 | if (this.props.service.isActive) { | 56 | if (this.props.service.isActive) { |
@@ -50,6 +60,31 @@ export default @observer class ServiceView extends Component { | |||
50 | }, 100); | 60 | }, 100); |
51 | } | 61 | } |
52 | }); | 62 | }); |
63 | |||
64 | reaction( | ||
65 | () => this.props.service.isActive, | ||
66 | () => { | ||
67 | if (!this.props.service.isActive && this.props.stores.settings.all.app.hibernate) { | ||
68 | // Service is inactive - start hibernation countdown | ||
69 | this.startHibernationTimer(); | ||
70 | } else { | ||
71 | if (this.state.hibernationTimer) { | ||
72 | // Service is active but we have an active hibernation timer: Clear timeout | ||
73 | clearTimeout(this.state.hibernationTimer); | ||
74 | } | ||
75 | |||
76 | // Service is active, wake up service from hibernation | ||
77 | this.setState({ | ||
78 | hibernate: false, | ||
79 | }); | ||
80 | } | ||
81 | }, | ||
82 | ); | ||
83 | |||
84 | // Start hibernation counter if we are in background | ||
85 | if (!this.props.service.isActive && this.props.stores.settings.all.app.hibernate) { | ||
86 | this.startHibernationTimer(); | ||
87 | } | ||
53 | } | 88 | } |
54 | 89 | ||
55 | componentWillUnmount() { | 90 | componentWillUnmount() { |
@@ -68,6 +103,20 @@ export default @observer class ServiceView extends Component { | |||
68 | }); | 103 | }); |
69 | }; | 104 | }; |
70 | 105 | ||
106 | startHibernationTimer() { | ||
107 | const timerDuration = (Number(this.props.stores.settings.all.app.hibernationStrategy) || 300) * 1000; | ||
108 | |||
109 | const hibernationTimer = setTimeout(() => { | ||
110 | this.setState({ | ||
111 | hibernate: true, | ||
112 | }); | ||
113 | }, timerDuration); | ||
114 | |||
115 | this.setState({ | ||
116 | hibernationTimer, | ||
117 | }); | ||
118 | } | ||
119 | |||
71 | render() { | 120 | render() { |
72 | const { | 121 | const { |
73 | detachService, | 122 | detachService, |
@@ -76,7 +125,6 @@ export default @observer class ServiceView extends Component { | |||
76 | reload, | 125 | reload, |
77 | edit, | 126 | edit, |
78 | enable, | 127 | enable, |
79 | upgrade, | ||
80 | } = this.props; | 128 | } = this.props; |
81 | 129 | ||
82 | const webviewClasses = classnames({ | 130 | const webviewClasses = classnames({ |
@@ -132,13 +180,10 @@ export default @observer class ServiceView extends Component { | |||
132 | </Fragment> | 180 | </Fragment> |
133 | ) : ( | 181 | ) : ( |
134 | <> | 182 | <> |
135 | {service.isServiceAccessRestricted ? ( | 183 | {service.recipe.id === 'franz-custom-website' && ( |
136 | <ServiceRestricted | 184 | <WebControlsScreen service={service} /> |
137 | name={service.recipe.name} | 185 | )} |
138 | upgrade={upgrade} | 186 | {!this.state.hibernate ? ( |
139 | type={service.restrictionType} | ||
140 | /> | ||
141 | ) : ( | ||
142 | <> | 187 | <> |
143 | {service.recipe.id === CUSTOM_WEBSITE_ID && ( | 188 | {service.recipe.id === CUSTOM_WEBSITE_ID && ( |
144 | <WebControlsScreen service={service} /> | 189 | <WebControlsScreen service={service} /> |
@@ -149,6 +194,12 @@ export default @observer class ServiceView extends Component { | |||
149 | detachService={detachService} | 194 | detachService={detachService} |
150 | /> | 195 | /> |
151 | </> | 196 | </> |
197 | ) : ( | ||
198 | <div> | ||
199 | <span role="img" aria-label="Sleeping Emoji">😴</span> | ||
200 | {' '} | ||
201 | This service is currently hibernating. If this page doesn't close soon, please try reloading Ferdi. | ||
202 | </div> | ||
152 | )} | 203 | )} |
153 | </> | 204 | </> |
154 | )} | 205 | )} |