-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
132 lines (118 loc) · 3.37 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-env browser */
import React from 'react';
import ReactDOM from 'react-dom';
import YouTube from '@u-wave/react-youtube'; // eslint-disable-line import/no-unresolved
const {
useCallback,
useState,
} = React;
const videos = [
{ id: 'ZuuVjuLNvFY', name: 'JUNNY - kontra (Feat. Lil Gimch, Keeflow)' },
{ id: 'PYE7jXNjFWw', name: 'T W L V - Follow' },
{ id: 'ld8ugY47cps', name: 'SLCHLD - I can\'t love you anymore' },
{ id: null, name: '<none>' },
];
const qualities = ['auto', '240', '380', '480', '720', '1080', '1440', '2160'];
const hashVideoRx = /^#!\/video\/(\d)$/;
const hash = typeof window.location !== 'undefined'
? window.location.hash : ''; // eslint-disable-line no-undef
const defaultVideo = hashVideoRx.test(hash)
? parseInt(hash.replace(hashVideoRx, '$1'), 10)
: 0;
function App() {
const [videoIndex, setVideoIndex] = useState(defaultVideo);
const [suggestedQuality, setSuggestedQuality] = useState('auto');
const [volume, setVolume] = useState(1);
const [paused, setPaused] = useState(false);
const video = videos[videoIndex];
function selectVideo(index) {
setVideoIndex(index);
}
const handlePause = useCallback((event) => {
setPaused(event.target.checked);
}, []);
const handlePlayerPause = useCallback(() => {
setPaused(true);
}, []);
const handlePlayerPlay = useCallback(() => {
setPaused(false);
}, []);
const handleVolume = useCallback((event) => {
setVolume(parseFloat(event.target.value));
}, []);
const handleQuality = useCallback((event) => {
setSuggestedQuality(qualities[event.target.selectedIndex]);
}, []);
return (
<div className="row">
<div className="col s4">
<h5>
Video
</h5>
<div className="collection">
{videos.map((choice, index) => (
<a
key={choice.id}
href={`#!/video/${index}`}
className={`collection-item ${video === choice ? 'active' : ''}`}
onClick={() => selectVideo(index)}
>
{choice.name}
</a>
))}
</div>
<h5>
Paused
</h5>
<p>
<label htmlFor="paused">
<input
type="checkbox"
id="paused"
checked={paused}
onChange={handlePause}
/>
<span>Paused</span>
</label>
</p>
<h5>
Volume
</h5>
<input
type="range"
value={volume}
min={0}
max={1}
step={0.01}
onChange={handleVolume}
/>
<h5>
Quality
</h5>
<select className="browser-default" onChange={handleQuality}>
{qualities.map((quality) => (
<option key={quality} value={quality}>
{quality}
</option>
))}
</select>
</div>
<div className="col s8 center-align">
<YouTube
video={video.id}
width={640}
height={480}
autoplay
controls={false}
suggestedQuality={suggestedQuality}
volume={volume}
paused={paused}
onPause={handlePlayerPause}
onPlaying={handlePlayerPlay}
/>
</div>
</div>
);
}
// eslint-disable-next-line react/no-deprecated
ReactDOM.render(<App />, document.getElementById('example'));