-
Notifications
You must be signed in to change notification settings - Fork 111
/
VideoSection.js
175 lines (160 loc) Β· 5.56 KB
/
VideoSection.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
import React, { memo, useCallback, useState, useEffect, useRef } from 'react';
import cn from 'classnames';
import Tags from '../Tags';
import ShareButton from '../ShareButton';
import YouTubeVideo from '../YouTubeVideo';
import TimestampTimeline from '../TimestampTimeline';
import PartsTimeline from './PartsTimeline';
import NebulaVideoRow from '../NebulaVideoRow';
import { filteredPath } from '../../utils';
import { useChallengePartIndex } from '../../hooks';
import * as css from './VideoSection.module.css';
const VideoSection = ({ challenge }) => {
const variant = 'cyan';
const { topics, languages, videoNumber, title } = challenge;
const youTubeVideoRef = useRef();
const [showTimeline, setShowTimeline] = useState(false);
const [timestamp, setTimestamp] = useState();
const hasMultiParts = challenge.parts?.length > 0;
const [showTimestamps, setShowTimestamps] = useState(!hasMultiParts);
const activePartIndex = useChallengePartIndex(challenge.parts?.length || 1);
const activePart = challenge.parts?.[activePartIndex] ?? challenge;
const { videoId, nebulaSlug, timestamps } = activePart;
const hasTimestamps = timestamps?.length > 0;
const hasTimeline = hasMultiParts || hasTimestamps;
const updateTimestamp = useCallback((value) => {
setTimestamp(value);
setShowTimeline(false);
}, []);
useEffect(() => {
if (showTimeline) {
document.body.style.overflow = 'hidden';
youTubeVideoRef.current.scrollIntoView();
} else {
document.body.style.overflow = 'unset';
}
return () => {
document.body.style.overflow = 'unset';
};
}, [showTimeline]);
return (
<div className={css.root}>
<header className={css.header}>
<div className={css.title}>
<h1>
{videoNumber ? `#${videoNumber} β ` : ''} {title}
</h1>
</div>
<div
className={cn(css.details, {
[css.unCollapsed]: showTimeline
})}>
<Tags
className={css.tags}
heading="Languages"
items={languages}
linkTo={(value) =>
filteredPath('challenges', { lang: value, topic: 'all' })
}
headerType="h3"
/>
<Tags
className={css.tags}
heading="Topics"
items={topics}
linkTo={(value) =>
filteredPath('challenges', { lang: 'all', topic: value })
}
headerType="h3"
/>
<ShareButton
className={cn(css.share, {
[css.onlyShare]: timestamps.length === 0
})}
variant={variant}
/>
{timestamps.length > 0 && (
<button
className={css.timelinesToggle}
onClick={() => setShowTimeline((v) => !v)}
tabIndex="0"
aria-label="Toggle timeline">
<span className={css.back}>Back</span>
<span className={css.arrow}> </span>
</button>
)}
</div>
</header>
<div className={css.videoPlayer}>
{!hasTimeline && <div className={css.spacer} />}
<div className={css.videoContainer}>
<div className={css.video} ref={youTubeVideoRef}>
<YouTubeVideo
className={css.videoWrapper}
videoId={videoId}
timestamp={timestamp}
/>
</div>
</div>
{!hasTimeline && <div className={css.spacer} />}
{hasTimeline && (
<div
className={cn(css.timelineContainer, {
[css.unCollapsed]: showTimeline
})}>
<nav className={css.timelinesContent}>
<div className={css.tabs}>
{hasMultiParts && (
<div
className={cn(css.tab, {
[css.selected]: !showTimestamps,
[css.clickable]: hasMultiParts && hasTimestamps
})}>
<button onClick={() => setShowTimestamps(false)}>
Parts
</button>
</div>
)}
{hasTimestamps && (
<div
className={cn(css.tab, {
[css.selected]: showTimestamps,
[css.clickable]: hasMultiParts && hasTimestamps
})}>
<button onClick={() => setShowTimestamps(true)}>
Timestamps
</button>
</div>
)}
</div>
<div className={css.timeline}>
{hasMultiParts && (
<PartsTimeline
className={cn(css.partTimeline, {
[css.hide]: showTimestamps
})}
parts={challenge.parts}
currentPartIndex={activePartIndex}
onSelection={() => setShowTimeline(false)}
/>
)}
{hasTimestamps && (
<TimestampTimeline
className={cn(css.timestampsTimeline, {
[css.hide]: !showTimestamps
})}
variant={variant}
timestamps={timestamps}
updateTimestamp={updateTimestamp}
/>
)}
</div>
</nav>
</div>
)}
</div>
<NebulaVideoRow nebulaSlug={nebulaSlug} variant={variant} />
</div>
);
};
export default memo(VideoSection);