@@ -28,8 +28,8 @@ enum SegmentListTab {
2828 Chapter
2929}
3030
31- interface segmentWithNesting extends SponsorTime {
32- innerChapters ?: ( segmentWithNesting | SponsorTime ) [ ] ;
31+ interface SegmentWithNesting extends SponsorTime {
32+ innerChapters ?: ( SegmentWithNesting | SponsorTime ) [ ] ;
3333}
3434
3535export const SegmentListComponent = ( props : SegmentListComponentProps ) => {
@@ -58,37 +58,41 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
5858 }
5959 } ;
6060
61- const segmentsWithNesting : segmentWithNesting [ ] = [ ] ;
62- let nbTrailingNonChapters = 0 ;
63- function nestChapters ( segments : segmentWithNesting [ ] , seg : SponsorTime , topLevel ?: boolean ) {
64- if ( seg . actionType === ActionType . Chapter && segments . length ) {
65- // trailing non-chapters can only exist at top level
66- const lastElement = segments [ segments . length - ( topLevel ? nbTrailingNonChapters + 1 : 1 ) ]
67-
68- if ( lastElement . actionType === ActionType . Chapter
69- && lastElement . segment [ 0 ] <= seg . segment [ 0 ]
70- && lastElement . segment [ 1 ] >= seg . segment [ 1 ] ) {
71- if ( lastElement . innerChapters ) {
72- nestChapters ( lastElement . innerChapters , seg ) ;
61+ const segmentsWithNesting = React . useMemo ( ( ) => {
62+ const result : SegmentWithNesting [ ] = [ ] ;
63+ let nbTrailingNonChapters = 0 ;
64+ function nestChapters ( segments : SegmentWithNesting [ ] , seg : SponsorTime , topLevel ?: boolean ) {
65+ if ( seg . actionType === ActionType . Chapter && segments . length ) {
66+ // trailing non-chapters can only exist at top level
67+ const lastElement = segments [ segments . length - ( topLevel ? nbTrailingNonChapters + 1 : 1 ) ]
68+
69+ if ( lastElement . actionType === ActionType . Chapter
70+ && lastElement . segment [ 0 ] <= seg . segment [ 0 ]
71+ && lastElement . segment [ 1 ] >= seg . segment [ 1 ] ) {
72+ if ( lastElement . innerChapters ) {
73+ nestChapters ( lastElement . innerChapters , seg ) ;
74+ } else {
75+ lastElement . innerChapters = [ seg ] ;
76+ }
7377 } else {
74- lastElement . innerChapters = [ seg ] ;
75- }
76- } else {
77- if ( topLevel ) {
78- nbTrailingNonChapters = 0 ;
79- }
78+ if ( topLevel ) {
79+ nbTrailingNonChapters = 0 ;
80+ }
8081
81- segments . push ( seg ) ;
82+ segments . push ( seg ) ;
83+ }
84+ } else {
85+ if ( seg . actionType !== ActionType . Chapter ) {
86+ nbTrailingNonChapters ++ ;
8287 }
83- } else {
84- if ( seg . actionType !== ActionType . Chapter ) {
85- nbTrailingNonChapters ++ ;
86- }
8788
88- segments . push ( seg ) ;
89+ segments . push ( seg ) ;
90+ }
8991 }
90- }
91- props . segments . forEach ( ( seg ) => nestChapters ( segmentsWithNesting , { ...seg } , true ) ) ;
92+ props . segments . forEach ( ( seg ) => nestChapters ( result , { ...seg } , true ) ) ;
93+ return result ;
94+ } , [ props . segments ] )
95+
9296
9397 return (
9498 < div id = "issueReporterContainer" >
@@ -136,7 +140,7 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
136140} ;
137141
138142function SegmentListItem ( { segment, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage } : {
139- segment : segmentWithNesting ;
143+ segment : SegmentWithNesting ;
140144 videoID : VideoID ;
141145 currentTime : number ;
142146 isVip : boolean ;
@@ -351,7 +355,7 @@ function SegmentListItem({ segment, videoID, currentTime, isVip, loopedChapter,
351355}
352356
353357function InnerChapterList ( { chapters, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage } : {
354- chapters : ( segmentWithNesting ) [ ] ;
358+ chapters : ( SegmentWithNesting ) [ ] ;
355359 videoID : VideoID ;
356360 currentTime : number ;
357361 isVip : boolean ;
0 commit comments