@@ -2,7 +2,8 @@ import React, { memo, useState } from 'react';
22import cn from 'classnames' ;
33import { Link } from 'gatsby' ;
44
5- import { useChallengePartIndex , usePersistScrollPosition } from '../../hooks' ;
5+ import { usePersistScrollPosition } from '../../hooks' ;
6+ import { buildPartHash } from '../../utils' ;
67
78import * as css from './OverviewTimeline.module.css' ;
89
@@ -14,20 +15,35 @@ const usePaths = (chapters, track, trackPosition) => {
1415 ? video . parts . map ( ( _ , partIndex ) => ( { slug : video . slug , partIndex } ) )
1516 : [ { slug : video . slug , partIndex : 0 } ]
1617 ) ;
17- const partIndex = useChallengePartIndex ( ) ;
1818 const currentVideo =
1919 chapters [ trackPosition . chapterIndex ] . videos [ trackPosition . videoIndex ] ;
2020 const currentIndex = flatTrack . findIndex (
21- ( video ) => video . slug === currentVideo . slug && video . partIndex === partIndex
21+ ( video ) =>
22+ video . slug === currentVideo . slug &&
23+ video . partIndex === trackPosition . partIndex
2224 ) ;
2325 const prevVideo = flatTrack [ currentIndex - 1 ] ;
2426 const nextVideo = flatTrack [ currentIndex + 1 ] ;
25- const computePath = ( video ) =>
26- video ? { ...video , path : `/tracks/${ track . slug } /${ video . slug } ` } : null ;
27+ const computePath = ( video ) => {
28+ if ( video ) {
29+ const hash = buildPartHash ( video . partIndex ) ;
30+ return {
31+ ...video ,
32+ path : `/tracks/${ track . slug } /${ video . slug } ${ hash } `
33+ } ;
34+ }
35+ return null ;
36+ } ;
2737 return [ computePath ( prevVideo ) , computePath ( nextVideo ) ] ;
2838} ;
2939
30- const OverviewTimeline = ( { className, chapters, track, trackPosition } ) => {
40+ const OverviewTimeline = ( {
41+ className,
42+ chapters,
43+ track,
44+ trackPosition,
45+ onSelection = ( ) => { }
46+ } ) => {
3147 const [ previousVideo , nextVideo ] = usePaths ( chapters , track , trackPosition ) ;
3248
3349 const timelineRef = usePersistScrollPosition ( track . slug , 'tracks' ) ;
@@ -42,6 +58,7 @@ const OverviewTimeline = ({ className, chapters, track, trackPosition }) => {
4258 chapters = { chapters }
4359 track = { track }
4460 trackPosition = { trackPosition }
61+ onSelection = { onSelection }
4562 />
4663 ) ) }
4764 </ div >
@@ -50,15 +67,15 @@ const OverviewTimeline = ({ className, chapters, track, trackPosition }) => {
5067 < Link
5168 className = { css . navButton }
5269 to = { previousVideo . path }
53- state = { { challengePartIndex : previousVideo . partIndex } } >
70+ onClick = { onSelection } >
5471 Previous
5572 </ Link >
5673 ) }
5774 { nextVideo !== null && (
5875 < Link
5976 className = { css . navButton }
6077 to = { nextVideo . path }
61- state = { { challengePartIndex : nextVideo . partIndex } } >
78+ onClick = { onSelection } >
6279 Next
6380 </ Link >
6481 ) }
@@ -68,14 +85,13 @@ const OverviewTimeline = ({ className, chapters, track, trackPosition }) => {
6885} ;
6986
7087const ChapterSection = memo (
71- ( { chapter, chapterIndex, chapters, track, trackPosition } ) => {
88+ ( { chapter, chapterIndex, chapters, track, trackPosition, onSelection } ) => {
7289 const hasSeenChapter = chapterIndex < trackPosition . chapterIndex ;
7390 const isThisChapter = chapterIndex === trackPosition . chapterIndex ;
7491 const trackPath = `/tracks/${ track . slug } ` ;
7592 const [ collapsed , setCollapsed ] = useState ( false ) ;
7693
7794 const { videoIndex : currentVideoIndex } = trackPosition ;
78- const currentPartIndex = useChallengePartIndex ( ) ;
7995
8096 return (
8197 < ul className = { css . chapterList } >
@@ -105,6 +121,7 @@ const ChapterSection = memo(
105121
106122 return isMultiPart ? (
107123 video . parts . map ( ( part , partIndex ) => {
124+ const currentPartIndex = trackPosition . partIndex ;
108125 const hasSeenPart =
109126 hasSeenVideo &&
110127 ( videoIndex < currentVideoIndex ||
@@ -117,8 +134,10 @@ const ChapterSection = memo(
117134 [ css . last ] : isLastVideo && partIndex === currentPartIndex
118135 } ) } >
119136 < Link
120- to = { `${ trackPath } /${ video . slug } ` }
121- state = { { challengePartIndex : partIndex } } >
137+ to = { `${ trackPath } /${ video . slug } ${ buildPartHash (
138+ partIndex
139+ ) } `}
140+ onClick = { onSelection } >
122141 { video . title } - { part . title }
123142 </ Link >
124143 </ li >
@@ -131,7 +150,9 @@ const ChapterSection = memo(
131150 [ css . seen ] : hasSeenVideo ,
132151 [ css . last ] : isLastVideo
133152 } ) } >
134- < Link to = { `${ trackPath } /${ video . slug } ` } > { video . title } </ Link >
153+ < Link to = { `${ trackPath } /${ video . slug } ` } onClick = { onSelection } >
154+ { video . title }
155+ </ Link >
135156 </ li >
136157 ) ;
137158 } ) }
0 commit comments