/* 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 { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; import { DSImage } from "../DSImage/DSImage.jsx"; import { DSLinkMenu } from "../DSLinkMenu/DSLinkMenu"; import { ImpressionStats } from "../../DiscoveryStreamImpressionStats/ImpressionStats"; import { getActiveCardSize } from "../../../lib/utils"; import React from "react"; import { SafeAnchor } from "../SafeAnchor/SafeAnchor"; import { DSContextFooter, SponsorLabel, DSMessageFooter, } from "../DSContextFooter/DSContextFooter.jsx"; import { DSThumbsUpDownButtons } from "../DSThumbsUpDownButtons/DSThumbsUpDownButtons.jsx"; import { FluentOrText } from "../../FluentOrText/FluentOrText.jsx"; import { connect } from "react-redux"; import { LinkMenuOptions } from "content-src/lib/link-menu-options"; const READING_WPM = 220; /** * READ TIME FROM WORD COUNT * @param {int} wordCount number of words in an article * @returns {int} number of words per minute in minutes */ export function readTimeFromWordCount(wordCount) { if (!wordCount) { return false; } return Math.ceil(parseInt(wordCount, 10) / READING_WPM); } export const DSSource = ({ source, timeToRead, newSponsoredLabel, context, sponsor, sponsored_by_override, icon_src, refinedCardsLayout, }) => { // refinedCard styles will have a larger favicon size const faviconSize = refinedCardsLayout ? 20 : 16; // First try to display sponsored label or time to read here. if (newSponsoredLabel) { // If we can display something for spocs, do so. if (sponsored_by_override || sponsor || context) { return ( ); } } // If we are not a spoc, and can display a time to read value. if (source && timeToRead) { return (

); } // Otherwise display a default source. return (
{icon_src && ( )}

{source}

); }; export const DefaultMeta = ({ source, title, excerpt, timeToRead, newSponsoredLabel, context, context_type, sponsor, sponsored_by_override, saveToPocketCard, ctaButtonVariant, dispatch, spocMessageVariant, mayHaveSectionsCards, mayHaveThumbsUpDown, onThumbsUpClick, onThumbsDownClick, isListCard, state, format, topic, isSectionsCard, showTopics, icon_src, refinedCardsLayout, }) => { const shouldHaveThumbs = !isListCard && format !== "rectangle" && mayHaveSectionsCards && mayHaveThumbsUpDown; const shouldHaveFooterSection = isSectionsCard && (shouldHaveThumbs || showTopics); return (
{ctaButtonVariant !== "variant-b" && format !== "rectangle" && !refinedCardsLayout && ( )}

{format === "rectangle" ? "Sponsored" : title}

{format === "rectangle" ? (

Sponsored content supports our mission to build a better web.

) : ( excerpt &&

{excerpt}

)}
{!isListCard && format !== "rectangle" && !mayHaveSectionsCards && mayHaveThumbsUpDown && !refinedCardsLayout && ( )} {(shouldHaveFooterSection || refinedCardsLayout) && (
{refinedCardsLayout && format !== "rectangle" && format !== "spoc" && ( )} {(shouldHaveThumbs || refinedCardsLayout) && ( )} {showTopics && ( )}
)} {!newSponsoredLabel && ( )} {/* Sponsored label is normally in the way of any message. newSponsoredLabel cards sponsored label is moved to just under the thumbnail, so we can display both, so we specifically don't pass in context. */} {newSponsoredLabel && ( )}
); }; export class _DSCard extends React.PureComponent { constructor(props) { super(props); this.onLinkClick = this.onLinkClick.bind(this); this.doesLinkTopicMatchSelectedTopic = this.doesLinkTopicMatchSelectedTopic.bind(this); this.onMenuUpdate = this.onMenuUpdate.bind(this); this.onMenuShow = this.onMenuShow.bind(this); this.onThumbsUpClick = this.onThumbsUpClick.bind(this); this.onThumbsDownClick = this.onThumbsDownClick.bind(this); const refinedCardsLayout = this.props.Prefs.values["discoverystream.refinedCardsLayout.enabled"]; this.setContextMenuButtonHostRef = element => { this.contextMenuButtonHostElement = element; }; this.setPlaceholderRef = element => { this.placeholderElement = element; }; this.state = { isSeen: false, isThumbsUpActive: false, isThumbsDownActive: false, }; // If this is for the about:home startup cache, then we always want // to render the DSCard, regardless of whether or not its been seen. if (props.App.isForStartupCache.App) { this.state.isSeen = true; } // We want to choose the optimal thumbnail for the underlying DSImage, but // want to do it in a performant way. The breakpoints used in the // CSS of the page are, unfortuntely, not easy to retrieve without // causing a style flush. To avoid that, we hardcode them here. // // The values chosen here were the dimensions of the card thumbnails as // computed by getBoundingClientRect() for each type of viewport width // across both high-density and normal-density displays. this.dsImageSizes = [ { mediaMatcher: "(min-width: 1122px)", width: 296, height: 148, }, { mediaMatcher: "(min-width: 866px)", width: 218, height: 109, }, { mediaMatcher: "(max-width: 610px)", width: 202, height: 101, }, ]; this.standardCardImageSizes = [ { mediaMatcher: "default", width: 296, height: 148, }, ]; this.listCardImageSizes = [ { mediaMatcher: "(min-width: 1122px)", width: 75, height: 75, }, { mediaMatcher: "default", width: 50, height: 50, }, ]; this.sectionsCardImagesSizes = { small: { width: 110, height: 117, }, medium: { width: 300, height: refinedCardsLayout ? 160 : 150, }, large: { width: 190, height: 250, }, }; this.sectionsColumnMediaMatcher = { 1: "default", 2: "(min-width: 724px)", 3: "(min-width: 1122px)", 4: "(min-width: 1390px)", }; } getSectionImageSize(column, size) { const cardImageSize = { mediaMatcher: this.sectionsColumnMediaMatcher[column], width: this.sectionsCardImagesSizes[size].width, height: this.sectionsCardImagesSizes[size].height, }; return cardImageSize; } doesLinkTopicMatchSelectedTopic() { // Edge case for clicking on a card when topic selections have not be set if (!this.props.selectedTopics) { return "not-set"; } // Edge case the topic of the card is not one of the available topics if (!this.props.availableTopics.includes(this.props.topic)) { return "topic-not-selectable"; } if (this.props.selectedTopics.includes(this.props.topic)) { return "true"; } return "false"; } onLinkClick() { const matchesSelectedTopic = this.doesLinkTopicMatchSelectedTopic(); if (this.props.dispatch) { if (this.props.isFakespot) { this.props.dispatch( ac.DiscoveryStreamUserEvent({ event: "FAKESPOT_CLICK", value: { product_id: this.props.id, category: this.props.category || "", }, }) ); } else { this.props.dispatch( ac.DiscoveryStreamUserEvent({ event: "CLICK", source: this.props.type.toUpperCase(), action_position: this.props.pos, value: { event_source: "card", card_type: this.props.flightId ? "spoc" : "organic", recommendation_id: this.props.recommendation_id, tile_id: this.props.id, ...(this.props.shim && this.props.shim.click ? { shim: this.props.shim.click } : {}), fetchTimestamp: this.props.fetchTimestamp, firstVisibleTimestamp: this.props.firstVisibleTimestamp, corpus_item_id: this.props.corpus_item_id, scheduled_corpus_item_id: this.props.scheduled_corpus_item_id, recommended_at: this.props.recommended_at, received_rank: this.props.received_rank, topic: this.props.topic, features: this.props.features, matches_selected_topic: matchesSelectedTopic, selected_topics: this.props.selectedTopics, is_list_card: this.props.isListCard, ...(this.props.format ? { format: this.props.format } : { format: getActiveCardSize( window.innerWidth, this.props.sectionsClassNames, this.props.section, this.props.flightId ), }), ...(this.props.section ? { section: this.props.section, section_position: this.props.sectionPosition, is_section_followed: this.props.sectionFollowed, } : {}), }, }) ); this.props.dispatch( ac.ImpressionStats({ source: this.props.type.toUpperCase(), click: 0, window_inner_width: this.props.windowObj.innerWidth, window_inner_height: this.props.windowObj.innerHeight, tiles: [ { id: this.props.id, pos: this.props.pos, ...(this.props.shim && this.props.shim.click ? { shim: this.props.shim.click } : {}), type: this.props.flightId ? "spoc" : "organic", recommendation_id: this.props.recommendation_id, topic: this.props.topic, selected_topics: this.props.selectedTopics, is_list_card: this.props.isListCard, ...(this.props.format ? { format: this.props.format } : { format: getActiveCardSize( window.innerWidth, this.props.sectionsClassNames, this.props.section, this.props.flightId ), }), ...(this.props.section ? { section: this.props.section, section_position: this.props.sectionPosition, is_section_followed: this.props.sectionFollowed, } : {}), }, ], }) ); } } } onThumbsUpClick(event) { event.stopPropagation(); event.preventDefault(); // Toggle active state for thumbs up button to show CSS animation const currentState = this.state.isThumbsUpActive; // If thumbs up has been clicked already, do nothing. if (currentState) { return; } this.setState({ isThumbsUpActive: !currentState }); // Record thumbs up telemetry event this.props.dispatch( ac.DiscoveryStreamUserEvent({ event: "POCKET_THUMBS_UP", source: "THUMBS_UI", value: { recommendation_id: this.props.recommendation_id, tile_id: this.props.id, corpus_item_id: this.props.corpus_item_id, scheduled_corpus_item_id: this.props.scheduled_corpus_item_id, recommended_at: this.props.recommended_at, received_rank: this.props.received_rank, thumbs_up: true, thumbs_down: false, topic: this.props.topic, format: getActiveCardSize( window.innerWidth, this.props.sectionsClassNames, this.props.section, false // (thumbs up/down only exist on organic content) ), ...(this.props.section ? { section: this.props.section, section_position: this.props.sectionPosition, is_section_followed: this.props.sectionFollowed, } : {}), }, }) ); // Show Toast this.props.dispatch( ac.OnlyToOneContent( { type: at.SHOW_TOAST_MESSAGE, data: { showNotifications: true, toastId: "thumbsUpToast", }, }, "ActivityStream:Content" ) ); } onThumbsDownClick(event) { event.stopPropagation(); event.preventDefault(); // Toggle active state for thumbs down button to show CSS animation const currentState = this.state.isThumbsDownActive; this.setState({ isThumbsDownActive: !currentState }); // Run dismiss event after 0.5 second delay if ( this.props.dispatch && this.props.type && this.props.id && this.props.url ) { const index = this.props.pos; const source = this.props.type.toUpperCase(); const spocData = { url: this.props.url, guid: this.props.id, type: "CardGrid", card_type: "organic", recommendation_id: this.props.recommendation_id, tile_id: this.props.id, corpus_item_id: this.props.corpus_item_id, scheduled_corpus_item_id: this.props.scheduled_corpus_item_id, recommended_at: this.props.recommended_at, received_rank: this.props.received_rank, }; const blockUrlOption = LinkMenuOptions.BlockUrl(spocData, index, source); const { action, impression, userEvent } = blockUrlOption; setTimeout(() => { this.props.dispatch(action); this.props.dispatch( ac.DiscoveryStreamUserEvent({ event: userEvent, source, action_position: index, }) ); }, 500); if (impression) { this.props.dispatch(impression); } // Record thumbs down telemetry event this.props.dispatch( ac.DiscoveryStreamUserEvent({ event: "POCKET_THUMBS_DOWN", source: "THUMBS_UI", value: { recommendation_id: this.props.recommendation_id, tile_id: this.props.id, corpus_item_id: this.props.corpus_item_id, scheduled_corpus_item_id: this.props.scheduled_corpus_item_id, recommended_at: this.props.recommended_at, received_rank: this.props.received_rank, thumbs_up: false, thumbs_down: true, topic: this.props.topic, format: getActiveCardSize( window.innerWidth, this.props.sectionsClassNames, this.props.section, false // (thumbs up/down only exist on organic content) ), ...(this.props.section ? { section: this.props.section, section_position: this.props.sectionPosition, is_section_followed: this.props.sectionFollowed, } : {}), }, }) ); // Show Toast this.props.dispatch( ac.OnlyToOneContent( { type: at.SHOW_TOAST_MESSAGE, data: { showNotifications: true, toastId: "thumbsDownToast", }, }, "ActivityStream:Content" ) ); } } onMenuUpdate(showContextMenu) { if (!showContextMenu) { const dsLinkMenuHostDiv = this.contextMenuButtonHostElement; if (dsLinkMenuHostDiv) { dsLinkMenuHostDiv.classList.remove("active", "last-item"); } } } async onMenuShow() { const dsLinkMenuHostDiv = this.contextMenuButtonHostElement; if (dsLinkMenuHostDiv) { // Force translation so we can be sure it's ready before measuring. await this.props.windowObj.document.l10n.translateFragment( dsLinkMenuHostDiv ); if (this.props.windowObj.scrollMaxX > 0) { dsLinkMenuHostDiv.classList.add("last-item"); } dsLinkMenuHostDiv.classList.add("active"); } } onSeen(entries) { if (this.state) { const entry = entries.find(e => e.isIntersecting); if (entry) { if (this.placeholderElement) { this.observer.unobserve(this.placeholderElement); } // Stop observing since element has been seen this.setState({ isSeen: true, }); } } } onIdleCallback() { if (!this.state.isSeen) { if (this.observer && this.placeholderElement) { this.observer.unobserve(this.placeholderElement); } this.setState({ isSeen: true, }); } } componentDidMount() { this.idleCallbackId = this.props.windowObj.requestIdleCallback( this.onIdleCallback.bind(this) ); if (this.placeholderElement) { this.observer = new IntersectionObserver(this.onSeen.bind(this)); this.observer.observe(this.placeholderElement); } } componentWillUnmount() { // Remove observer on unmount if (this.observer && this.placeholderElement) { this.observer.unobserve(this.placeholderElement); } if (this.idleCallbackId) { this.props.windowObj.cancelIdleCallback(this.idleCallbackId); } } render() { const { isRecentSave, DiscoveryStream, Prefs, saveToPocketCard, isListCard, isFakespot, mayHaveSectionsCards, format, alt_text, } = this.props; if (this.props.placeholder || !this.state.isSeen) { // placeholder-seen is used to ensure the loading animation is only used if the card is visible. const placeholderClassName = this.state.isSeen ? `placeholder-seen` : ``; return (
); } let source = this.props.source || this.props.publisher; if (!source) { try { source = new URL(this.props.url).hostname; } catch (e) {} } const { pocketButtonEnabled, hideDescriptions, compactImages, imageGradient, newSponsoredLabel, titleLines = 3, descLines = 3, readTime: displayReadTime, } = DiscoveryStream; const layoutsVariantAEnabled = Prefs.values["newtabLayouts.variant-a"]; const layoutsVariantBEnabled = Prefs.values["newtabLayouts.variant-b"]; const sectionsEnabled = Prefs.values["discoverystream.sections.enabled"]; const refinedCardsLayout = Prefs.values["discoverystream.refinedCardsLayout.enabled"]; const layoutsVariantAorB = layoutsVariantAEnabled || layoutsVariantBEnabled; const smartCrop = Prefs.values["images.smart"]; const faviconEnabled = Prefs.values["discoverystream.publisherFavicon.enabled"]; // Refined cards have their own excerpt hiding logic. // We can ignore hideDescriptions if we are in sections and refined cards. const excerpt = !hideDescriptions || (sectionsEnabled && refinedCardsLayout) ? this.props.excerpt : ""; let timeToRead; if (displayReadTime) { timeToRead = this.props.time_to_read || readTimeFromWordCount(this.props.word_count); } const ctaButtonEnabled = this.props.ctaButtonSponsors?.includes( this.props.sponsor?.toLowerCase() ); let ctaButtonVariant = ""; if (ctaButtonEnabled) { ctaButtonVariant = this.props.ctaButtonVariant; } let ctaButtonVariantClassName = ctaButtonVariant; const ctaButtonClassName = ctaButtonEnabled ? `ds-card-cta-button` : ``; const compactImagesClassName = compactImages ? `ds-card-compact-image` : ``; const imageGradientClassName = imageGradient ? `ds-card-image-gradient` : ``; const listCardClassName = isListCard ? `list-feed-card` : ``; const fakespotClassName = isFakespot ? `fakespot` : ``; const sectionsCardsClassName = [ mayHaveSectionsCards ? `sections-card-ui` : ``, this.props.sectionsClassNames, ].join(" "); const sectionsCardsImageSizes = this.props.sectionsCardImageSizes; const titleLinesName = `ds-card-title-lines-${titleLines}`; const descLinesClassName = `ds-card-desc-lines-${descLines}`; const isMediumRectangle = format === "rectangle"; const spocFormatClassName = isMediumRectangle ? `ds-spoc-rectangle` : ``; const refinedCardsClassName = refinedCardsLayout ? `refined-cards` : ``; let sizes = []; if (!isMediumRectangle) { sizes = this.dsImageSizes; if (sectionsEnabled) { sizes = [ this.getSectionImageSize("4", sectionsCardsImageSizes["4"]), this.getSectionImageSize("3", sectionsCardsImageSizes["3"]), this.getSectionImageSize("2", sectionsCardsImageSizes["2"]), this.getSectionImageSize("1", sectionsCardsImageSizes["1"]), ]; } else if (layoutsVariantAorB) { sizes = this.standardCardImageSizes; } if (isListCard) { sizes = this.listCardImageSizes; } } return (
{this.props.showTopics && !this.props.mayHaveSectionsCards && this.props.topic && !isListCard && !refinedCardsLayout && ( )}
{ctaButtonVariant === "variant-b" && (
Shop Now
)} {isFakespot ? (

{this.props.title}

) : ( )}
{!isFakespot && ( )}
); } } _DSCard.defaultProps = { windowObj: window, // Added to support unit tests }; export const DSCard = connect(state => ({ App: state.App, DiscoveryStream: state.DiscoveryStream, Prefs: state.Prefs, }))(_DSCard); export const PlaceholderDSCard = () => ;