aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/render/GenericNotice.tsx
blob: 639edb86fd9dfdb7917b35a6b1f096da5c657b0f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as React from "react";
import * as ReactDOM from "react-dom";
import NoticeComponent from "../components/NoticeComponent";

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

import { ContentContainer } from "../types";
import NoticeTextSelectionComponent from "../components/NoticeTextSectionComponent";

export interface ButtonListener {
    name: string,
    listener: (e?: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void
}

export interface TextBox {
    icon: string,
    text: string
}

export interface NoticeOptions {
    title: string,
    referenceNode?: HTMLElement,
    textBoxes?: TextBox[],
    buttons?: ButtonListener[],
    fadeIn?: boolean,
    timed?: boolean
    style?: React.CSSProperties;
    extraClass?: string;
    maxCountdownTime?: () => number;
    dontPauseCountdown?: boolean;
    hideLogo?: boolean;
    hideRightInfo?: boolean;
}

export default class GenericNotice {
    // Contains functions and variables from the content script needed by the skip notice
    contentContainer: ContentContainer;

    noticeElement: HTMLDivElement;
    noticeRef: React.MutableRefObject<NoticeComponent>;
    idSuffix: string;

    constructor(contentContainer: ContentContainer, idSuffix: string, options: NoticeOptions) {
        this.noticeRef = React.createRef();
        this.idSuffix = idSuffix;

        this.contentContainer = contentContainer;

        const referenceNode = options.referenceNode ?? utils.findReferenceNode();
    
        this.noticeElement = document.createElement("div");
        this.noticeElement.id = "sponsorSkipNoticeContainer" + idSuffix;

        referenceNode.prepend(this.noticeElement);

        this.update(options);        
    }

    update(options: NoticeOptions): void {
        ReactDOM.render(
            <NoticeComponent
                noticeTitle={options.title}
                idSuffix={this.idSuffix}
                fadeIn={options.fadeIn ?? true}
                timed={options.timed ?? true}
                ref={this.noticeRef}
                style={options.style}
                extraClass={options.extraClass}
                maxCountdownTime={options.maxCountdownTime}
                dontPauseCountdown={options.dontPauseCountdown}
                hideLogo={options.hideLogo}
                hideRightInfo={options.hideRightInfo}
                closeListener={() => this.close()} >
                    
                    <tr id={"sponsorSkipNoticeMiddleRow" + this.idSuffix}
                        className="sponsorTimeMessagesRow"
                        style={{maxHeight: (this.contentContainer().v.offsetHeight - 200) + "px"}}>
                        <td style={{width: "100%"}}>
                            {this.getMessageBoxes(this.idSuffix, options.textBoxes)}
                        </td>
                    </tr>

                    <tr id={"sponsorSkipNoticeSpacer" + this.idSuffix}
                        className="sponsorBlockSpacer">
                    </tr>

                    <tr className="sponsorSkipNoticeRightSection"
                        style={{position: "relative"}}>
                        <td>
                            {this.getButtons(options.buttons)}
                        </td>
                    </tr>
            </NoticeComponent>,
            this.noticeElement
        );
    }

    getMessageBoxes(idSuffix: string, textBoxes: TextBox[]): JSX.Element[] { 
        if (textBoxes) {
            const result = [];
            for (let i = 0; i < textBoxes.length; i++) {
                result.push(
                    <NoticeTextSelectionComponent idSuffix={idSuffix}
                        key={i}
                        icon={textBoxes[i].icon}
                        text={textBoxes[i].text} />
                )
            }

            return result;
        } else {
            return null;
        }
    }

    getButtons(buttons?: ButtonListener[]): JSX.Element[] {
        if (buttons) {
            const result: JSX.Element[] = [];

            for (const button of buttons) {
                result.push(
                    <button className="sponsorSkipObject sponsorSkipNoticeButton sponsorSkipNoticeRightButton"
                            key={button.name}
                            onClick={(e) => button.listener(e)}>

                            {button.name}
                    </button>
                )
            }

            return result;
        } else {
            return null;
        }
    }

    close(): void {
        ReactDOM.unmountComponentAtNode(this.noticeElement);

        this.noticeElement.remove();
    }
}