-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathCompactVideoCard.js
More file actions
404 lines (384 loc) · 12.8 KB
/
CompactVideoCard.js
File metadata and controls
404 lines (384 loc) · 12.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import React from 'react'
import { Button, ButtonGroup, Grid, IconButton, InputBase, Typography, Box, Checkbox } from '@mui/material'
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
import VisibilityIcon from '@mui/icons-material/Visibility'
import EditIcon from '@mui/icons-material/Edit'
import LinkIcon from '@mui/icons-material/Link'
import { CopyToClipboard } from 'react-copy-to-clipboard'
import { getPublicWatchUrl, getServedBy, getUrl, toHHMMSS, useDebounce, getVideoUrl } from '../../common/utils'
import VideoService from '../../services/VideoService'
import _ from 'lodash'
import UpdateDetailsModal from '../modal/UpdateDetailsModal'
import LightTooltip from '../misc/LightTooltip'
const URL = getUrl()
const PURL = getPublicWatchUrl()
const SERVED_BY = getServedBy()
const CompactVideoCard = ({
video,
openVideoHandler,
alertHandler,
cardWidth,
authenticated,
deleted,
editMode = false,
isSelected = false,
onSelect = () => {},
}) => {
const [intVideo, setIntVideo] = React.useState(video)
const [videoId, setVideoId] = React.useState(video.video_id)
const [title, setTitle] = React.useState(video.info?.title)
const [description, setDescription] = React.useState(video.info?.description)
const [updatedTitle, setUpdatedTitle] = React.useState(null)
const debouncedTitle = useDebounce(updatedTitle, 1500)
const [hover, setHover] = React.useState(false)
const [privateView, setPrivateView] = React.useState(video.info?.private)
const [detailsModalOpen, setDetailsModalOpen] = React.useState(false)
const previousVideoRef = React.useRef()
const previousVideo = previousVideoRef.current
if (!_.isEqual(video, previousVideo) && !_.isEqual(video, intVideo)) {
setIntVideo(video)
setVideoId(video.video_id)
setTitle(video.info?.title)
setDescription(video.info?.description)
setPrivateView(video.info?.private)
setUpdatedTitle(null)
}
React.useEffect(() => {
previousVideoRef.current = video
})
const debouncedMouseEnter = React.useRef(
_.debounce(() => {
setHover(true)
}, 750),
).current
const handleMouseLeave = () => {
debouncedMouseEnter.cancel()
setHover(false)
}
React.useEffect(() => {
async function update() {
try {
await VideoService.updateTitle(video.video_id, debouncedTitle)
setTitle(updatedTitle)
alertHandler({
type: 'success',
message: 'Title Updated',
open: true,
})
} catch (err) {
alertHandler({
type: 'error',
message: 'An error occurred trying to update the title',
open: true,
})
}
}
if (debouncedTitle && debouncedTitle !== title) {
update()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedTitle])
const handleMouseDown = (e) => {
if (e.button === 1) {
window.open(`${PURL}${video.video_id}`, '_blank')
}
}
const handlePrivacyChange = async () => {
try {
await VideoService.updatePrivacy(video.video_id, !privateView)
alertHandler({
type: privateView ? 'info' : 'warning',
message: privateView ? `Added to your public feed` : `Removed from your public feed`,
open: true,
})
setPrivateView(!privateView)
} catch (err) {
console.log(err)
}
}
const handleDetailsModalClose = (update) => {
setDetailsModalOpen(false)
if (update) {
if (update === 'delete') {
deleted(videoId)
} else {
if (update.title !== title) setTitle(update.title)
if (update.description !== description) setDescription(update.description)
}
}
}
const previewVideoHeight =
video.info?.width && video.info?.height ? cardWidth * (video.info.height / video.info.width) : cardWidth / 1.77
const getPreviewVideoUrl = () => {
// Prefer 720p if available, else 1080p, else original
const has720p = video.info?.has_720p
const has1080p = video.info?.has_1080p
if (has720p) {
return getVideoUrl(video.video_id, '720p', video.extension)
}
if (has1080p) {
return getVideoUrl(video.video_id, '1080p', video.extension)
}
// Fall back to original
return getVideoUrl(video.video_id, 'original', video.extension)
}
return (
<>
<UpdateDetailsModal
open={detailsModalOpen}
close={handleDetailsModalClose}
videoId={video.video_id}
currentTitle={title || ''}
currentDescription={description || ''}
alertHandler={alertHandler}
/>
<Box
sx={{
width: '100%',
bgcolor: 'rgba(0, 0, 0, 0)',
lineHeight: 0,
border: isSelected ? '3px solid' : '3px solid transparent',
borderColor: isSelected ? 'primary.main' : 'transparent',
borderRadius: '6px',
transition: 'border 0.3s ease',
}}
>
<ButtonGroup
variant="contained"
size="small"
sx={{
width: '100%',
background: '#0b132b',
borderRadius: '6px',
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
borderLeft: '1px solid #3399FFAE',
borderTop: '1px solid #3399FFAE',
borderRight: '1px solid #3399FFAE',
'.MuiButtonGroup-grouped:not(:last-of-type)': {
border: 'none',
},
}}
>
{authenticated && (
<Button
onClick={() => setDetailsModalOpen(true)}
sx={{
bgcolor: 'rgba(0,0,0,0)',
borderBottomLeftRadius: 0,
borderTopLeftRadius: '6px',
m: 0,
}}
>
<EditIcon />
</Button>
)}
<LightTooltip
title={title || ''}
placement="bottom"
enterDelay={1000}
leaveDelay={500}
enterNextDelay={1000}
arrow
>
<InputBase
sx={{
pl: authenticated ? 0 : 1.5,
pr: 1.5,
width: cardWidth,
bgcolor: 'rgba(0,0,0,0)',
WebkitTextFillColor: '#fff',
fontWeight: 575,
'& .MuiInputBase-input.Mui-disabled': {
WebkitTextFillColor: '#fff',
fontWeight: 575,
},
}}
placeholder="Video Title..."
value={updatedTitle !== null ? updatedTitle : title}
onChange={(e) => authenticated && setUpdatedTitle(e.target.value)}
disabled={!authenticated}
inputProps={{ 'aria-label': 'search google maps' }}
/>
</LightTooltip>
{authenticated && (
<Button
onClick={handlePrivacyChange}
edge="end"
sx={{
borderBottomRightRadius: 0,
borderTopRightRadius: '6px',
bgcolor: 'rgba(0,0,0,0)',
color: privateView ? '#FF2323B2' : '#2382FFB7',
':hover': {
bgcolor: privateView ? '#FF232340' : '#2382FF40',
},
}}
>
{privateView ? <VisibilityOffIcon /> : <VisibilityIcon />}
</Button>
)}
</ButtonGroup>
<Box
sx={{
lineHeight: 0,
bgcolor: 'rgba(0,0,0,0)',
p: 0,
'&:last-child': { p: 0 },
}}
>
<div
style={{ position: 'relative', cursor: 'pointer', display: 'flex' }}
onClick={() => (editMode ? onSelect(video.video_id) : openVideoHandler(video.video_id))}
onMouseEnter={debouncedMouseEnter}
onMouseLeave={handleMouseLeave}
onMouseDown={handleMouseDown}
>
{/* Checkbox for edit mode */}
{editMode && (
<Checkbox
checked={isSelected}
onChange={(e) => {
e.stopPropagation()
onSelect(video.video_id)
}}
sx={{
position: 'absolute',
top: 8,
left: 8,
zIndex: 2,
color: 'white',
bgcolor: 'rgba(0, 0, 0, 0.5)',
borderRadius: '4px',
'&.Mui-checked': {
color: 'primary.main',
},
}}
/>
)}
{!video.available && (
<Box
sx={{ position: 'absolute', top: 0, left: 0, background: '#FF000060', width: '100%', height: '100%' }}
>
<Grid container direction="row" alignItems="center" sx={{ width: '100%', height: '100%' }}>
<Typography
variant="overline"
sx={{
width: '100%',
fontSize: 28,
fontWeight: 750,
}}
align="center"
>
File Missing
</Typography>
</Grid>
</Box>
)}
<img
src={`${
SERVED_BY === 'nginx'
? `${URL}/_content/derived/${video.video_id}/poster.jpg`
: `${URL}/api/video/poster?id=${video.video_id}`
}`}
alt=""
style={{
width: cardWidth,
minHeight: previewVideoHeight,
border: '1px solid #3399FFAE',
borderBottomRightRadius: '6px',
borderBottomLeftRadius: '6px',
borderTop: 'none',
background: 'repeating-linear-gradient(45deg,#606dbc,#606dbc 10px,#465298 10px,#465298 20px)',
}}
/>
{hover && (
<video
style={{
position: 'absolute',
top: 0,
left: 0,
opacity: 0,
animationName: 'fadeIn',
animationDuration: '1.5s',
animationFillMode: 'both',
WebkitAnimationName: 'fadeIn',
WebkitAnimationDuration: '1.5s',
WebkitAnimationFillMode: 'both',
border: '1px solid #3399FFAE',
borderBottomRightRadius: '6px',
borderBottomLeftRadius: '6px',
borderTop: 'none',
}}
width={cardWidth}
height={previewVideoHeight}
src={getPreviewVideoUrl()}
muted
autoPlay
disablePictureInPicture
/>
)}
<Box sx={{ position: 'absolute', bottom: 3, left: 3 }}>
<CopyToClipboard text={`${PURL}${video.video_id}`}>
<IconButton
sx={{
background: 'rgba(0, 0, 0, 0.4)',
'&:hover': {
background: '#2684FF88',
},
}}
aria-label="copy link"
size="small"
onClick={(e) => {
e.stopPropagation()
alertHandler({
type: 'info',
message: 'Link copied to clipboard',
open: true,
})
}}
onMouseDown={handleMouseDown}
>
<LinkIcon />
</IconButton>
</CopyToClipboard>
</Box>
<Box sx={{ position: 'absolute', bottom: 39, right: 3 }}>
<Typography
variant="div"
color="white"
sx={{
p: 0.5,
fontWeight: 700,
fontSize: 12,
fontFamily: 'monospace',
background: 'rgba(0, 0, 0, 0.6)',
borderRadius: '4px',
}}
>
{toHHMMSS(video.info.duration)}
</Typography>
</Box>
<Box sx={{ position: 'absolute', bottom: 14, right: 3 }}>
<Typography
variant="div"
color="white"
sx={{
p: 0.5,
fontWeight: 700,
fontSize: 12,
fontFamily: 'monospace',
background: 'rgba(0, 0, 0, 0.6)',
borderRadius: '4px',
}}
>
{`${video.view_count} ${video.view_count === 1 ? 'View' : 'Views'}`}
</Typography>
</Box>
</div>
</Box>
</Box>
</>
)
}
export default CompactVideoCard