aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/render/UpcomingNotice.tsx
blob: 8c0aeffe5dc977dbcb3901d8f2ea2026c70b053a (plain)
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
import * as React from "react";
import { createRoot, Root } from "react-dom/client";
import { ContentContainer, SponsorTime } from "../types";
import UpcomingNoticeComponent from "../components/UpcomingNoticeComponent";

import Utils from "../utils";
const utils = new Utils();

class UpcomingNotice {
    segments: SponsorTime[];
    // Contains functions and variables from the content script needed by the skip notice
    contentContainer: ContentContainer;

    noticeElement: HTMLDivElement;

    upcomingNoticeRef: React.MutableRefObject<UpcomingNoticeComponent>;
    root: Root;

    constructor(segments: SponsorTime[], contentContainer: ContentContainer, timeLeft: number, autoSkip: boolean) {
        this.upcomingNoticeRef = React.createRef();

        this.segments = segments;
        this.contentContainer = contentContainer;

        const referenceNode = utils.findReferenceNode();

        this.noticeElement = document.createElement("div");
        this.noticeElement.className = "sponsorSkipNoticeContainer";

        referenceNode.prepend(this.noticeElement);

        this.root = createRoot(this.noticeElement);
        this.root.render(
            <UpcomingNoticeComponent
                segments={segments}
                autoSkip={autoSkip}
                contentContainer={contentContainer}
                timeUntilSegment={timeLeft}
                ref={this.upcomingNoticeRef}
                closeListener={() => this.close()} />
        );
    }

    close(): void {
        this.root.unmount();

        this.noticeElement.remove();

        const upcomingNotices = this.contentContainer().upcomingNotices;
        upcomingNotices.splice(upcomingNotices.indexOf(this), 1);
    }
}

export default UpcomingNotice;