aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/render/UpcomingNotice.tsx
blob: b3380ce13d0e172c39774abf89cb2c666a9e5e3e (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
55
56
57
58
59
60
61
62
63
64
65
66
import * as React from "react";
import { createRoot, Root } from "react-dom/client";
import { ContentContainer, SponsorTime } from "../types";

import Utils from "../utils";
import SkipNoticeComponent from "../components/SkipNoticeComponent";
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<SkipNoticeComponent>;
    root: Root;

    closed = false;

    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(
            <SkipNoticeComponent segments={segments} 
                autoSkip={autoSkip} 
                upcomingNotice={true}
                contentContainer={contentContainer}
                ref={this.upcomingNoticeRef}
                closeListener={() => this.close()}
                smaller={true}
                fadeIn={true}
                maxCountdownTime={timeLeft} />
        );
    }

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

        this.closed = true;
    }

    sameNotice(segments: SponsorTime[]): boolean {
        if (segments.length !== this.segments.length) return false;

        for (let i = 0; i < segments.length; i++) {
            if (segments[i].UUID !== this.segments[i].UUID) return false;
        }

        return true;
    }
}

export default UpcomingNotice;