forked from rage/java-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutube.js
More file actions
97 lines (87 loc) · 2.16 KB
/
Youtube.js
File metadata and controls
97 lines (87 loc) · 2.16 KB
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
import React from "react"
import styled from "styled-components"
import ReduxActionAnalytics from "redux-action-analytics"
import * as storejs from "store"
import YouTube from "react-youtube"
import withSimpleErrorBoundary from "../util/withSimpleErrorBoundary"
let analytics = undefined
initAnalytics()
function initAnalytics() {
if (typeof window === "undefined") {
return
}
analytics = new ReduxActionAnalytics(
"https://usage.testmycode.io/api/v0/data",
"youtube-watch-stats",
"youtube-watch-stats",
10000,
() => {
const user = storejs.get("tmc.user")
if (user === undefined) {
return {}
}
return {
username: user.username,
}
},
)
}
function onPlayerStateChange(event) {
const player = event.target
const eventCode = event.data
const action = Object.entries(YouTube.PlayerState).find((o) => {
return o[1] === eventCode
})[0]
logAction(action, player)
}
function onPlaybackRateChange(event) {
const player = event.target
logAction("PLAYBACK_SPEED_CHANGED", player)
}
function logAction(action, player) {
const happenedAt = new Date().getTime()
const videoTime = player.getCurrentTime()
const youtubeIdentifier = player.___youtube_identifier
const playBackRate = player.getPlaybackRate()
const snapshot = {
action: action,
video_time: videoTime,
youtube_identifier: youtubeIdentifier,
happened_at_milliseconds: happenedAt,
playback_speed: playBackRate,
}
analytics.saveAction(snapshot)
}
const VideoWrapper = styled.div`
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
margin-bottom: 2rem;
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
`
const Youtube = (props) => {
return (
<VideoWrapper>
<YouTube
videoId={props.id}
onStateChange={onPlayerStateChange}
onPlaybackRateChange={onPlaybackRateChange}
opts={{
height: "390",
width: "640",
playerVars: {
modestbranding: true,
},
}}
/>
</VideoWrapper>
)
}
export default withSimpleErrorBoundary(Youtube)