-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
260 lines (248 loc) · 7.43 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import React, { useState, useEffect, useRef, useContext } from 'react'
import Head from 'next/head'
import { withRouter } from 'next/router'
import { Query } from 'react-apollo'
import { gql } from 'apollo-boost'
import { AuthContext } from 'react-onegraph'
import YouTubePlayer from 'react-player/lib/players/YouTube'
import Loading from '../sections/Loading'
import Error from '../sections/Error'
import Captions from '../sections/Captions'
import NotFound from '../sections/NotFound'
import Placeholder from '../sections/Placeholder'
import Footer from '../sections/Footer'
import Spacer from '../components/Spacer'
import './index.css'
const GET_VIDEO = gql`
query VideoWithCaptionsQuery($videoId: String!) {
youTube {
video(id: $videoId) {
id
snippet {
title
}
captions {
items {
snippet {
language
status
}
body
}
}
}
}
}
`
function App({ initialVideoId }) {
const { status, headers, login, logout } = useContext(AuthContext)
const [videoId, setVideoId] = useState(initialVideoId)
const youTubeRef = useRef(null)
// this will update the URL query parameter so that
// we can share a direct URL to a specific package
useEffect(
() => {
// don't update on mount as this'd break
// the GitHub authentication redirect
if (videoId !== initialVideoId) {
window.history.replaceState(
null,
'',
window.location.origin +
'/' +
(videoId.length > 0 ? '?videoId=' + videoId : '')
)
}
},
// only run if the search input changes
[videoId]
)
return (
<div
style={{
backgroundColor: 'white',
minHeight: '100vh',
}}>
<Head>
<title>OneGraph YouTube Captions</title>
</Head>
<div
style={{
padding: 10,
backgroundColor: 'rgb(235, 235, 235)',
}}>
<div
style={{
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
fontFamily: 'Roboto, Inter UI, Helvetica, -apple-system',
}}>
<img
src="/static/logo.png"
style={{ width: 30, height: 30, paddingRight: 5 }}
/>
OneGraph YouTube Captions
</div>
<Spacer size={10} />
<div
style={{
fontSize: 18,
flexDirection: 'row',
WebkitAppearance: 'none',
backgroundColor: 'white',
appearance: 'none',
padding: '5px 10px',
width: '100%',
maxWidth: 880,
alignSelf: 'center',
border: '1px solid rgb(200, 200, 200)',
borderRadius: 2,
boxSizing: 'border-box',
color: 'rgb(180, 180, 180)',
}}>
<span className="desktop-only">https://www.youtube.com/watch?v=</span>
<input
type="text"
placeholder="VIDEO_ID"
value={videoId}
disabled={!status.youtube}
onChange={e => setVideoId(e.target.value)}
style={{
fontSize: 18,
flex: 1,
display: 'flex',
WebkitAppearance: 'none',
appearance: 'none',
color: 'black',
border: 'none',
outline: 'none',
}}
/>
</div>
</div>
{!status.youtube ? (
<div
style={{
flex: 1,
alignSelf: 'center',
maxWidth: 880,
width: '100%',
textAlign: 'center',
padding: '5vh 10% 40px',
color: 'rgb(50, 50, 50)',
fontSize: 18,
}}>
<h2>Login with YouTube</h2>
<br />
<p>
In order to fetch YouTube video captions, you must login with
YouTube.
</p>
<br />
<button
onClick={() => login('youtube')}
style={{
alignSelf: 'center',
cursor: 'pointer',
padding: '10px 20px',
backgroundColor: 'red',
borderRadius: 5,
color: 'white',
border: 0,
fontSize: 16,
}}>
Login with YouTube
</button>
</div>
) : null}
{status.youtube && videoId.length > 0 ? (
<Query
query={GET_VIDEO}
// passing the headers to the variables is a neat hack
// forcing Apollo to refetch the data using the new headers
variables={{ videoId }}
context={{ headers }}
// TODO: investigate why this is required
// without it won't always update on input change
// even if the input value is an existing package
fetchPolicy="cache-and-network"
// using this to display npm data even if GitHub auth throws
// otherwise Apollo would not pass any data at all
errorPolicy="all">
{({ loading, error, data }) => {
if (loading) {
return <Loading />
}
if (error) {
return <Error error={error} videoId={videoId} />
}
if (!data.youTube.video) {
return <NotFound videoId={videoId} />
}
return (
<div
style={{
padding: 10,
paddingBottom: 50,
flex: 1,
width: '100%',
maxWidth: 900,
alignSelf: 'center',
}}>
<div
style={{
backgroundColor: 'rgba(240, 240, 240, 0.4)',
color: 'rgb(80, 80, 80)',
padding: 10,
fontSize: 14,
}}>
<div
className="flag"
style={{
justifyContent: 'space-between',
textAlign: 'center',
}}>
You are logged into YouTube.
<Spacer size={10} />
<span
onClick={() => logout('youtube')}
style={{
cursor: 'pointer',
color: 'red',
}}>
Logout from YouTube
</span>
</div>
</div>
<Spacer size={10} />
<h1 style={{ textAlign: 'center', fontSize: 24 }}>
{data.youTube.video.snippet.title}
</h1>
<Spacer size={10} />
<YouTubePlayer
width="100%"
url={`https://www.youtube.com/watch?v=${videoId}`}
ref={youTubeRef}
controls
/>
<Spacer size={10} />
<Captions
data={data.youTube.video}
videoId={videoId}
youTubeRef={youTubeRef}
/>
</div>
)
}}
</Query>
) : status.youtube && videoId.length === 0 ? (
<Placeholder setVideoId={setVideoId} />
) : null}
<Footer />
</div>
)
}
export default withRouter(({ router }) => (
<App initialVideoId={router.query.videoId || ''} />
))