Skip to content

Commit c22d1b5

Browse files
authored
Merge pull request ajayyy#2340 from FelixFourcolor/master
Auto-select the chapters tab if video only has chapters
2 parents 17896b2 + 444b90c commit c22d1b5

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/popup/SegmentListComponent.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ interface SegmentWithNesting extends SponsorTime {
3232
innerChapters?: (SegmentWithNesting|SponsorTime)[];
3333
}
3434

35+
function isSegment(segment) {
36+
return segment.actionType !== ActionType.Chapter;
37+
}
38+
39+
function isChapter(segment) {
40+
return segment.actionType === ActionType.Chapter;
41+
}
42+
3543
export const SegmentListComponent = (props: SegmentListComponentProps) => {
3644
const [tab, setTab] = React.useState(SegmentListTab.Segments);
3745
const [isVip, setIsVip] = React.useState(Config.config?.isVip ?? false);
@@ -46,17 +54,19 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
4654
}
4755
}, []);
4856

49-
React.useEffect(() => {
50-
setTab(SegmentListTab.Segments);
51-
}, [props.videoID]);
57+
const [hasSegments, hasChapters] = React.useMemo(() => {
58+
const hasSegments = Boolean(props.segments.find(isSegment))
59+
const hasChapters = Boolean(props.segments.find(isChapter))
60+
return [hasSegments, hasChapters];
61+
}, [props.segments]);
5262

53-
const tabFilter = (segment: SponsorTime) => {
54-
if (tab === SegmentListTab.Chapter) {
55-
return segment.actionType === ActionType.Chapter;
63+
React.useEffect(() => {
64+
if (hasSegments){
65+
setTab(SegmentListTab.Segments);
5666
} else {
57-
return segment.actionType !== ActionType.Chapter;
67+
setTab(SegmentListTab.Chapter);
5868
}
59-
};
69+
}, [props.videoID, hasSegments]);
6070

6171
const segmentsWithNesting = React.useMemo(() => {
6272
const result: SegmentWithNesting[] = [];
@@ -98,7 +108,7 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
98108

99109
return (
100110
<div id="issueReporterContainer">
101-
<div id="issueReporterTabs" className={props.segments && props.segments.find(s => s.actionType === ActionType.Chapter) ? "" : "hidden"}>
111+
<div id="issueReporterTabs" className={hasSegments && hasChapters ? "" : "hidden"}>
102112
<span id="issueReporterTabSegments" className={tab === SegmentListTab.Segments ? "sbSelected" : ""} onClick={() => {
103113
setTab(SegmentListTab.Segments);
104114
}}>
@@ -125,7 +135,7 @@ export const SegmentListComponent = (props: SegmentListComponentProps) => {
125135
isVip={isVip}
126136
loopedChapter={props.loopedChapter} // UUID instead of boolean so it can be passed down to nested chapters
127137

128-
tabFilter={tabFilter}
138+
tabFilter={tab === SegmentListTab.Chapter ? isChapter : isSegment}
129139
sendMessage={props.sendMessage}
130140
/>
131141
))

0 commit comments

Comments
 (0)