-
Notifications
You must be signed in to change notification settings - Fork 111
/
VideoSection.js
196 lines (179 loc) Β· 6.15 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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 OverviewTimeline from './OverviewTimeline';
import NebulaVideoRow from '../NebulaVideoRow';
import { filteredPath } from '../../utils';
import { useChallengePartIndex } from '../../hooks';
import * as css from './VideoSection.module.css';
const getOverallPositionInTrack = (trackPosition, chapters) => {
let videoIndex = 0;
let trackTotal = 0;
for (let chapter = 0; chapter < chapters.length; chapter++) {
trackTotal += chapters[chapter].videos.length;
if (trackPosition.chapterIndex > chapter) {
videoIndex += chapters[chapter].videos.length;
} else if (trackPosition.chapterIndex === chapter) {
videoIndex += trackPosition.videoIndex;
}
}
return [videoIndex + 1, trackTotal];
};
const VideoSection = ({ track, video, trackPosition, mainTitle }) => {
const variant = 'red';
const chapters = track.chapters ? track.chapters : [{ videos: track.videos }];
const hasSingleVideo =
chapters.length === 1 && chapters[0].videos.length === 1;
const { title, topics, languages } = video;
const partIndex = useChallengePartIndex(video.parts?.length || 1);
trackPosition = { ...trackPosition, partIndex };
const part = video.parts?.[partIndex];
const videoId = part?.videoId ?? video.videoId;
const nebulaSlug = part?.nebulaSlug ?? video.nebulaSlug;
const timestamps = part?.timestamps ?? video.timestamps;
const [videoIndex, trackTotal] = getOverallPositionInTrack(
trackPosition,
chapters,
track
);
const [showTimeline, setShowTimeline] = useState(false);
const youTubeVideoRef = useRef();
const [showTimestamps, setShowTimestamps] = useState(hasSingleVideo);
const [timestamp, setTimestamp] = useState();
const Header = mainTitle ? 'h1' : 'h2';
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}>
<Header>{title}</Header>
</div>
<div
className={cn(css.details, {
[css.unCollapsed]: showTimeline
})}>
<Tags
className={css.tags}
heading="Languages"
items={languages}
linkTo={(value) =>
filteredPath('tracks', { lang: value, topic: 'all' })
}
headerType="h3"
/>
<Tags
className={css.tags}
heading="Topics"
items={topics}
linkTo={(value) =>
filteredPath('tracks', { lang: 'all', topic: value })
}
headerType="h3"
/>
<ShareButton className={css.share} variant={variant} />
<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>
<span className={css.progress}>
{videoIndex} / {trackTotal}{' '}
</span>
</button>
</div>
</header>
<div className={css.videoPlayer}>
<div className={css.videoContainer}>
<div className={css.video} ref={youTubeVideoRef}>
<YouTubeVideo
className={css.videoWrapper}
videoId={videoId}
timestamp={timestamp}
/>
</div>
</div>
<div
className={cn(css.timelineContainer, {
[css.unCollapsed]: showTimeline
})}>
<nav className={css.timelinesContent}>
<div className={css.tabs}>
{!hasSingleVideo && (
<div
className={cn(css.tab, {
[css.selected]: !showTimestamps,
[css.clickable]: timestamps.length > 0
})}>
<button
onClick={() =>
timestamps.length > 0 && setShowTimestamps(false)
}>
track stops
</button>
</div>
)}
{timestamps.length > 0 && (
<div
className={cn(css.tab, {
[css.selected]: showTimestamps,
[css.clickable]: timestamps.length > 0
})}>
<button
onClick={() =>
timestamps.length > 0 && setShowTimestamps(true)
}>
timestamps
</button>
</div>
)}
</div>
<div className={css.timeline}>
{timestamps.length > 0 && (
<TimestampTimeline
className={cn(css.timestampsTimeline, {
[css.hide]: !showTimestamps
})}
variant={variant}
timestamps={timestamps}
updateTimestamp={updateTimestamp}
/>
)}
{!hasSingleVideo && (
<OverviewTimeline
className={cn(css.overviewTimeline, {
[css.hide]: showTimestamps
})}
chapters={chapters}
track={track}
trackPosition={trackPosition}
onSelection={() => setShowTimeline(false)}
/>
)}
</div>
</nav>
</div>
</div>
<NebulaVideoRow nebulaSlug={nebulaSlug} variant={variant} />
</div>
);
};
export default memo(VideoSection);