-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.js
89 lines (78 loc) · 2.65 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React from 'react'
import Authentication from '../Authentication/Authentication'
import './App.css'
export default class App extends React.Component{
constructor(props){
super(props)
this.Authentication = new Authentication()
//if the extension is running on twitch or dev rig, set the shorthand here. otherwise, set to null.
this.twitch = window.Twitch ? window.Twitch.ext : null
this.state={
finishedLoading:false,
theme:'light',
animal:'',
fact:''
}
}
contextUpdate(context, delta){
if(delta.includes('theme')){
this.setState(()=>{
return {theme:context.theme}
})
}
}
componentDidMount(){
if(this.twitch){
this.twitch.onAuthorized((auth)=>{
this.Authentication.setToken(auth.token, auth.userId)
if(!this.state.finishedLoading){
this.setState(()=>{
return {finishedLoading:true}
})
}
})
this.twitch.onContext((context,delta)=>{
this.contextUpdate(context,delta)
})
this.twitch.configuration.onChanged(()=>{
let animal = this.twitch.configuration.broadcaster ? this.twitch.configuration.broadcaster.content : ''
let fact = this.twitch.configuration.developer ? this.twitch.configuration.developer.content : ''
this.setState(()=>{
return{
animal,
fact
}
})
})
}
}
componentWillUnmount(){
if(this.twitch){
this.twitch.unlisten('broadcast', ()=>console.log('successfully unlistened'))
}
}
render(){
if(this.state.finishedLoading && this.state.animal && this.state.fact){
return (
<div className={this.state.theme === 'light' ? "App App-light" : "App App-dark"}>
<p>Random fact about {this.state.animal}s!</p>
<p>Did you know that: {this.state.fact}</p>
</div>
)
}
else if(this.state.finishedLoading){
return(
<div className={this.state.theme === 'light' ? "App App-light" : "App App-dark"}>
Extension not configured.
</div>
)
}
else{
return (
<div className="App">
Loading...
</div>
)
}
}
}