Cache chapter nesting results

This commit is contained in:
mini-bomba
2025-09-19 16:17:30 +02:00
parent 5043a5fbe2
commit 915fd70274

View File

@@ -28,8 +28,8 @@ enum SegmentListTab {
Chapter Chapter
} }
interface segmentWithNesting extends SponsorTime { interface SegmentWithNesting extends SponsorTime {
innerChapters?: (segmentWithNesting|SponsorTime)[]; innerChapters?: (SegmentWithNesting|SponsorTime)[];
} }
export const SegmentListComponent = (props: SegmentListComponentProps) => { export const SegmentListComponent = (props: SegmentListComponentProps) => {
@@ -58,9 +58,10 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
} }
}; };
const segmentsWithNesting: segmentWithNesting[] = []; const segmentsWithNesting = React.useMemo(() => {
const result: SegmentWithNesting[] = [];
let nbTrailingNonChapters = 0; let nbTrailingNonChapters = 0;
function nestChapters(segments: segmentWithNesting[], seg: SponsorTime, topLevel?: boolean) { function nestChapters(segments: SegmentWithNesting[], seg: SponsorTime, topLevel?: boolean) {
if (seg.actionType === ActionType.Chapter && segments.length) { if (seg.actionType === ActionType.Chapter && segments.length) {
// trailing non-chapters can only exist at top level // trailing non-chapters can only exist at top level
const lastElement = segments[segments.length - (topLevel ? nbTrailingNonChapters + 1 : 1)] const lastElement = segments[segments.length - (topLevel ? nbTrailingNonChapters + 1 : 1)]
@@ -88,7 +89,10 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
segments.push(seg); segments.push(seg);
} }
} }
props.segments.forEach((seg) => nestChapters(segmentsWithNesting, {...seg}, true)); props.segments.forEach((seg) => nestChapters(result, {...seg}, true));
return result;
}, [props.segments])
return ( return (
<div id="issueReporterContainer"> <div id="issueReporterContainer">
@@ -136,7 +140,7 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
}; };
function SegmentListItem({ segment, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage }: { function SegmentListItem({ segment, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage }: {
segment: segmentWithNesting; segment: SegmentWithNesting;
videoID: VideoID; videoID: VideoID;
currentTime: number; currentTime: number;
isVip: boolean; isVip: boolean;
@@ -351,7 +355,7 @@ function SegmentListItem({ segment, videoID, currentTime, isVip, loopedChapter,
} }
function InnerChapterList({ chapters, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage }: { function InnerChapterList({ chapters, videoID, currentTime, isVip, loopedChapter, tabFilter, sendMessage }: {
chapters: (segmentWithNesting)[]; chapters: (SegmentWithNesting)[];
videoID: VideoID; videoID: VideoID;
currentTime: number; currentTime: number;
isVip: boolean; isVip: boolean;