aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/NoticeTextSectionComponent.tsx
blob: 042622892d3f555901159b15d5c3e7e51fcb13c4 (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
import * as React from "react";

export interface NoticeTextSelectionProps {
    icon?: string;
    text: string;
    idSuffix: string;
    onClick?: (event: React.MouseEvent) => unknown;
    children?: React.ReactNode;
}

export interface NoticeTextSelectionState {

}

class NoticeTextSelectionComponent extends React.Component<NoticeTextSelectionProps, NoticeTextSelectionState> {

    constructor(props: NoticeTextSelectionProps) {
        super(props);
    }

    render(): React.ReactElement {
        const style: React.CSSProperties = {};
        if (this.props.onClick) {
            style.cursor = "pointer";
            style.textDecoration = "underline"
        }

        return (
            <tr id={"sponsorTimesInfoMessage" + this.props.idSuffix}
                onClick={this.props.onClick}
                style={style}
                className="sponsorTimesInfoMessage">
                    
                <td>
                    {this.props.icon ? 
                        <img src={chrome.runtime.getURL(this.props.icon)} className="sponsorTimesInfoIcon" /> 
                    : null}

                    <span>
                        {this.getTextElements(this.props.text)}
                    </span>
                </td>
            </tr>
        );
    }

    private getTextElements(text: string): Array<string | React.ReactElement> {
        const elements: Array<string | React.ReactElement> = [];
        const textParts = text.split(/(?=\s+)/);
        for (const textPart of textParts) {
            if (textPart.match(/^\s*http/)) {
                elements.push(
                    <a href={textPart} target="_blank" rel="noreferrer">
                        {textPart}
                    </a>
                );
            } else {
                elements.push(textPart);
            }

        }

        return elements;
    }
}

export default NoticeTextSelectionComponent;