|
18 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 | */ |
20 | 20 |
|
| 21 | +import { Component } from 'preact' |
| 22 | +import { withBus } from 'preact-suber' |
| 23 | +import { connect } from 'preact-redux' |
| 24 | +import { getAutoResolveRemoteGuidesUrl } from 'shared/modules/settings/settingsDuck' |
| 25 | +import { fetchRemoteGuideAction } from 'shared/modules/commands/commandsDuck' |
| 26 | + |
21 | 27 | import Guides from '../Guides/Guides' |
22 | 28 | import * as html from '../Guides/html' |
23 | 29 | import FrameTemplate from './FrameTemplate' |
| 30 | +import ErrorsView from './Views/ErrorsView' |
24 | 31 |
|
25 | | -const PlayFrame = ({frame}) => { |
26 | | - let guide = 'Play guide not specified' |
27 | | - if (frame.result) { |
28 | | - guide = <Guides withDirectives html={frame.result} /> |
29 | | - } else { |
30 | | - const guideName = frame.cmd.replace(':play', '').replace(/\s|-/g, '').trim() || 'start' |
31 | | - if (guideName !== '') { |
32 | | - const content = html[guideName] |
33 | | - if (content !== undefined) { |
34 | | - guide = <Guides withDirectives html={content} /> |
35 | | - } else { |
36 | | - if (frame.error && frame.error.error) { |
37 | | - guide = frame.error.error |
38 | | - } else { |
39 | | - guide = <Guides withDirectives html={html['unfound']} /> |
| 32 | +export class PlayFrame extends Component { |
| 33 | + constructor (props) { |
| 34 | + super(props) |
| 35 | + this.state = { |
| 36 | + guide: null |
| 37 | + } |
| 38 | + } |
| 39 | + componentDidMount () { |
| 40 | + if (this.props.frame.result) { // Found remote guide |
| 41 | + this.setState({ guide: <Guides withDirectives html={this.props.frame.result} /> }) |
| 42 | + return |
| 43 | + } |
| 44 | + if (this.props.frame.response && this.props.frame.error && this.props.frame.error.error) { // Not found remotely (or other error) |
| 45 | + if (this.props.frame.response.status === 404) return this.setState({ guide: <Guides withDirectives html={html['unfound']} /> }) |
| 46 | + return this.setState({ |
| 47 | + guide: ( |
| 48 | + <ErrorsView |
| 49 | + error={{ |
| 50 | + message: 'Error: The remote server responded with the following error: ' + this.props.frame.response.status, |
| 51 | + code: 'Remote guide error' |
| 52 | + }} |
| 53 | + />) |
| 54 | + }) |
| 55 | + } |
| 56 | + if (this.props.frame.error && this.props.frame.error.error) { // Some other error. Whitelist error etc. |
| 57 | + return this.setState({ guide: <ErrorsView error={{ |
| 58 | + message: this.props.frame.error.error, |
| 59 | + code: 'Remote guide error' |
| 60 | + }} /> }) |
| 61 | + } |
| 62 | + const guideName = this.props.frame.cmd.replace(':play', '').replace(/\s|-/g, '').trim() || 'start' |
| 63 | + if (typeof html[guideName] !== 'undefined') { // Found it locally |
| 64 | + this.setState({ guide: <Guides withDirectives html={html[guideName]} /> }) |
| 65 | + return |
| 66 | + } |
| 67 | + // Not found remotely or locally |
| 68 | + // Try to find it remotely by name |
| 69 | + if (this.props.bus) { |
| 70 | + const action = fetchRemoteGuideAction(this.props.remoteUrl + guideName) |
| 71 | + this.props.bus.self(action.type, action, (res) => { |
| 72 | + if (!res.success) { // No luck |
| 73 | + return this.setState({ guide: <Guides withDirectives html={html['unfound']} /> }) |
40 | 74 | } |
41 | | - } |
| 75 | + this.setState({ guide: <Guides withDirectives html={res.result} /> }) |
| 76 | + }) |
| 77 | + } else { // No bus. Give up |
| 78 | + return this.setState({ guide: <Guides withDirectives html={html['unfound']} /> }) |
42 | 79 | } |
43 | 80 | } |
44 | | - return ( |
45 | | - <FrameTemplate |
46 | | - className='playFrame' |
47 | | - header={frame} |
48 | | - contents={guide} |
49 | | - /> |
50 | | - ) |
| 81 | + render () { |
| 82 | + return ( |
| 83 | + <FrameTemplate |
| 84 | + className='playFrame' |
| 85 | + header={this.props.frame} |
| 86 | + contents={this.state.guide} |
| 87 | + /> |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +const mapStateToProps = (state) => { |
| 93 | + return { |
| 94 | + remoteUrl: getAutoResolveRemoteGuidesUrl(state) |
| 95 | + } |
51 | 96 | } |
52 | | -export default PlayFrame |
| 97 | +export default withBus(connect(mapStateToProps)(PlayFrame)) |
0 commit comments