/* 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 https://mozilla.org/MPL/2.0/. */
// eslint-disable-next-line no-unused-vars
import React, { useEffect, useRef, useState } from "react";
import { useSelector } from "react-redux";
import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs";
import { SafeAnchor } from "../../DiscoveryStreamComponents/SafeAnchor/SafeAnchor";
// Map known backend entitlement strings to localized tag IDs. Anything not in
// this map falls back to the raw string from `stream.entitlement`.
const ENTITLEMENT_L10N_IDS = {
free: "newtab-sports-widget-watch-stream-free",
"free trial": "newtab-sports-widget-watch-stream-free-trial",
"free and paid": "newtab-sports-widget-watch-stream-free-paid",
paid: "newtab-sports-widget-watch-stream-paid",
"select games only": "newtab-sports-widget-watch-stream-select-games-only",
};
const WIDGET_NAME = "sports";
const WIDGET_SOURCE = "widget";
const USER_ACTION_TYPES = {
OPEN: "open",
DISMISS: "dismiss",
STREAM_CLICK: "stream_click",
};
function StreamRow({ stream, dispatch, widgetSize }) {
const entitlementL10nId =
ENTITLEMENT_L10N_IDS[stream.entitlement?.toLowerCase()];
const handleClick = () => {
dispatch(
ac.OnlyToMain({
type: at.WIDGETS_USER_EVENT,
data: {
widget_name: WIDGET_NAME,
widget_source: WIDGET_SOURCE,
user_action: USER_ACTION_TYPES.STREAM_CLICK,
widget_size: widgetSize,
action_value: stream.product_name,
},
})
);
};
return (
{stream.product_name}
{stream.entitlement}
);
}
function WatchLiveModal({ onClose, dispatch, widgetSize }) {
const dialogRef = useRef(null);
const otherRegionsToggleRef = useRef(null);
const watchLive = useSelector(state => state.SportsWidget.watchLive);
const loaded = watchLive?.loaded ?? false;
const data = watchLive?.data ?? null;
const [otherRegionsExpanded, setOtherRegionsExpanded] = useState(false);
const handleDismiss = () => {
dispatch(
ac.OnlyToMain({
type: at.WIDGETS_USER_EVENT,
data: {
widget_name: WIDGET_NAME,
widget_source: WIDGET_SOURCE,
user_action: USER_ACTION_TYPES.DISMISS,
action_value: "watch_live_modal",
widget_size: widgetSize,
},
})
);
onClose();
};
// When the user expands Other regions, scroll the toggle to the top of the
// modal so the just-revealed content below it is visible without an extra
// manual scroll.
useEffect(() => {
if (otherRegionsExpanded) {
otherRegionsToggleRef.current?.scrollIntoView({
behavior: "smooth",
block: "start",
});
}
}, [otherRegionsExpanded]);
useEffect(() => {
dialogRef.current?.showModal();
dispatch(ac.AlsoToMain({ type: at.WIDGETS_SPORTS_WATCH_LIVE_REQUEST }));
dispatch(
ac.OnlyToMain({
type: at.WIDGETS_USER_EVENT,
data: {
widget_name: WIDGET_NAME,
widget_source: WIDGET_SOURCE,
user_action: USER_ACTION_TYPES.OPEN,
action_value: "watch_live_modal",
widget_size: widgetSize,
},
})
);
}, [dispatch, widgetSize]);
return (
);
}
export { WatchLiveModal };