/* 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, { useCallback, useRef } from "react"; import { useSelector, batch } from "react-redux"; import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; import { useIntersectionObserver, useSizeSubmenu } from "../../../lib/utils"; import { WIDGET_REGISTRY, resolveWidgetSize, resolveCrosswordEndpoint, } from "common/WidgetsRegistry.mjs"; import { MoveSubmenu } from "../MoveSubmenu"; const USER_ACTION_TYPES = { CHANGE_SIZE: "change_size", }; const CROSSWORD_ENTRY = WIDGET_REGISTRY.find(w => w.id === "crossword"); function Crossword({ dispatch, widgetsMayBeMaximized, widgetEnabledMap }) { const prefs = useSelector(state => state.Prefs.values); const widgetSize = resolveWidgetSize(CROSSWORD_ENTRY, prefs); const crosswordEndpoint = resolveCrosswordEndpoint(prefs); const impressionFired = useRef(false); const handleIntersection = useCallback(() => { if (impressionFired.current) { return; } impressionFired.current = true; dispatch( ac.AlsoToMain({ type: at.WIDGETS_IMPRESSION, data: { widget_name: "crossword", widget_size: widgetSize, }, }) ); }, [dispatch, widgetSize]); const widgetRef = useIntersectionObserver(handleIntersection); function handleCrosswordHide() { batch(() => { dispatch( ac.OnlyToMain({ type: at.SET_PREF, data: { name: CROSSWORD_ENTRY.enabledPref, value: false }, }) ); dispatch( ac.OnlyToMain({ type: at.WIDGETS_ENABLED, data: { widget_name: "crossword", widget_source: "context_menu", enabled: false, widget_size: widgetSize, }, }) ); }); } const handleChangeSize = useCallback( size => { batch(() => { dispatch( ac.OnlyToMain({ type: at.SET_PREF, data: { name: CROSSWORD_ENTRY.sizePref, value: size }, }) ); dispatch( ac.OnlyToMain({ type: at.WIDGETS_USER_EVENT, data: { widget_name: "crossword", widget_source: "context_menu", user_action: USER_ACTION_TYPES.CHANGE_SIZE, action_value: size, widget_size: size, }, }) ); }); }, [dispatch] ); const sizeSubmenuRef = useSizeSubmenu(handleChangeSize); function handleLearnMore() { batch(() => { dispatch( ac.OnlyToMain({ type: at.OPEN_LINK, data: { url: "https://support.mozilla.org/kb/firefox-new-tab-widgets", }, }) ); dispatch( ac.OnlyToMain({ type: at.WIDGETS_USER_EVENT, data: { widget_name: "crossword", widget_source: "context_menu", user_action: "learn_more", widget_size: widgetSize, }, }) ); }); } return (
{ widgetRef.current = [el]; }} >

{widgetsMayBeMaximized && ( {["medium", "large"].map(size => ( ))} )}