/* 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 ? (

{this.renderTitleIcon()} {title}

) : 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 ( ); } renderButton() { const { props } = this; if (!this._shouldRenderButton()) { return null; } return ( ); } renderText() { const { props } = this; return ( ); } wrapSectionHeader(url) { return function(children) { return {children}; }; } wrapSnippetContent(children) { return
{children}
; } 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 (

{props.content.section_title_text}

); } 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 (
{sectionHeader} {props.content.icon_alt_text {props.content.icon_alt_text
{this.renderTitle()}

{this.renderText()}

{this.props.extraContent}
{
{this.renderButton()}
}
); } }