summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/asrouter/templates/SimpleSnippet/SimpleSnippet.jsx
blob: 96570e2dbd835c970f8a8454b234ba27afde03d3 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import { Button } from "../../components/Button/Button";
import ConditionalWrapper from "../../components/ConditionalWrapper/ConditionalWrapper";
import React from "react";
import { RichText } from "../../components/RichText/RichText";
import { safeURI } from "../../template-utils";
import { SnippetBase } from "../../components/SnippetBase/SnippetBase";

const DEFAULT_ICON_PATH = "chrome://branding/content/icon64.png";
// Alt text placeholder in case the prop from the server isn't available
const ICON_ALT_TEXT = "";

export class SimpleSnippet extends React.PureComponent {
  constructor(props) {
    super(props);
    this.onButtonClick = this.onButtonClick.bind(this);
  }

  onButtonClick() {
    if (this.props.provider !== "preview") {
      this.props.sendUserActionTelemetry({
        event: "CLICK_BUTTON",
        id: this.props.UISurface,
      });
    }
    const { button_url, button_entrypoint_value, button_entrypoint_name } =
      this.props.content;
    // If button_url is defined handle it as OPEN_URL action
    const type = this.props.content.button_action || (button_url && "OPEN_URL");
    // Assign the snippet referral for the action
    const entrypoint = button_entrypoint_name
      ? new URLSearchParams([
          [button_entrypoint_name, button_entrypoint_value],
        ]).toString()
      : button_entrypoint_value;
    this.props.onAction({
      type,
      data: {
        args: this.props.content.button_action_args || button_url,
        ...(entrypoint && { entrypoint }),
      },
    });
    if (!this.props.content.do_not_autoblock) {
      this.props.onBlock();
    }
  }

  _shouldRenderButton() {
    return (
      this.props.content.button_action ||
      this.props.onButtonClick ||
      this.props.content.button_url
    );
  }

  renderTitle() {
    const { title } = this.props.content;
    return title ? (
      <h3
        className={`title ${this._shouldRenderButton() ? "title-inline" : ""}`}
      >
        {this.renderTitleIcon()} {title}
      </h3>
    ) : null;
  }

  renderTitleIcon() {
    const titleIconLight = safeURI(this.props.content.title_icon);
    const titleIconDark = safeURI(
      this.props.content.title_icon_dark_theme || this.props.content.title_icon
    );
    if (!titleIconLight) {
      return null;
    }

    return (
      <React.Fragment>
        <span
          className="titleIcon icon-light-theme"
          style={{ backgroundImage: `url("${titleIconLight}")` }}
        />
        <span
          className="titleIcon icon-dark-theme"
          style={{ backgroundImage: `url("${titleIconDark}")` }}
        />
      </React.Fragment>
    );
  }

  renderButton() {
    const { props } = this;
    if (!this._shouldRenderButton()) {
      return null;
    }

    return (
      <Button
        onClick={props.onButtonClick || this.onButtonClick}
        color={props.content.button_color}
        backgroundColor={props.content.button_background_color}
      >
        {props.content.button_label}
      </Button>
    );
  }

  renderText() {
    const { props } = this;
    return (
      <RichText
        text={props.content.text}
        customElements={this.props.customElements}
        localization_id="text"
        links={props.content.links}
        sendClick={props.sendClick}
      />
    );
  }

  wrapSectionHeader(url) {
    return function (children) {
      return <a href={url}>{children}</a>;
    };
  }

  wrapSnippetContent(children) {
    return <div className="innerContentWrapper">{children}</div>;
  }

  renderSectionHeader() {
    const { props } = this;

    // an icon and text must be specified to render the section header
    if (props.content.section_title_icon && props.content.section_title_text) {
      const sectionTitleIconLight = safeURI(props.content.section_title_icon);
      const sectionTitleIconDark = safeURI(
        props.content.section_title_icon_dark_theme ||
          props.content.section_title_icon
      );
      const sectionTitleURL = props.content.section_title_url;

      return (
        <div className="section-header">
          <h3 className="section-title">
            <ConditionalWrapper
              condition={sectionTitleURL}
              wrap={this.wrapSectionHeader(sectionTitleURL)}
            >
              <span
                className="icon icon-small-spacer icon-light-theme"
                style={{ backgroundImage: `url("${sectionTitleIconLight}")` }}
              />
              <span
                className="icon icon-small-spacer icon-dark-theme"
                style={{ backgroundImage: `url("${sectionTitleIconDark}")` }}
              />
              <span className="section-title-text">
                {props.content.section_title_text}
              </span>
            </ConditionalWrapper>
          </h3>
        </div>
      );
    }

    return null;
  }

  render() {
    const { props } = this;
    const sectionHeader = this.renderSectionHeader();
    let className = "SimpleSnippet";

    if (props.className) {
      className += ` ${props.className}`;
    }
    if (props.content.tall) {
      className += " tall";
    }
    if (sectionHeader) {
      className += " has-section-header";
    }

    return (
      <div className="snippet-hover-wrapper">
        <SnippetBase
          {...props}
          className={className}
          textStyle={this.props.textStyle}
        >
          {sectionHeader}
          <ConditionalWrapper
            condition={sectionHeader}
            wrap={this.wrapSnippetContent}
          >
            <img
              src={safeURI(props.content.icon) || DEFAULT_ICON_PATH}
              className="icon icon-light-theme"
              alt={props.content.icon_alt_text || ICON_ALT_TEXT}
            />
            <img
              src={
                safeURI(props.content.icon_dark_theme || props.content.icon) ||
                DEFAULT_ICON_PATH
              }
              className="icon icon-dark-theme"
              alt={props.content.icon_alt_text || ICON_ALT_TEXT}
            />
            <div>
              {this.renderTitle()} <p className="body">{this.renderText()}</p>
              {this.props.extraContent}
            </div>
            {<div>{this.renderButton()}</div>}
          </ConditionalWrapper>
        </SnippetBase>
      </div>
    );
  }
}