/* 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/. */ // @nova-cleanup(move-directory): Move to components/CustomizeMenu/WidgetsManagementPanel/ after Nova ships import React, { useEffect, useRef } from "react"; import { batch, useDispatch, useSelector } from "react-redux"; import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs"; import { WIDGET_REGISTRY, resolveWidgetSize } from "common/WidgetsRegistry.mjs"; // eslint-disable-next-line no-shadow import { CSSTransition } from "react-transition-group"; function WidgetsManagementPanel({ onSubpanelToggle, togglePanel, showPanel, enabledSections, enabledWidgets, mayHaveWeather, mayHaveTimerWidget, mayHaveListsWidget, mayHaveSportsWidget, mayHaveClocksWidget, mayHavePrivacyWidget, mayHaveCrosswordWidget, mayHaveStocksWidget, mayHavePictureOfTheDayWidget, setPref, }) { const prefs = useSelector(state => state.Prefs.values); const arrowButtonRef = useRef(null); const panelRef = useRef(null); const dispatch = useDispatch(); // Notify parent menu when subpanel opens/closes useEffect(() => { if (onSubpanelToggle) { onSubpanelToggle(showPanel); } }, [showPanel, onSubpanelToggle]); const handlePanelEntered = () => { arrowButtonRef.current?.focus(); }; const onToggleWidget = e => { const { preference, eventSource } = e.target.dataset; const value = e.target.pressed; batch(() => { dispatch( ac.UserEvent({ event: "PREF_CHANGED", source: eventSource, value: { status: value, menu_source: "CUSTOMIZE_MENU" }, }) ); let widgetName; switch (eventSource) { case "WEATHER": widgetName = "weather"; break; case "WIDGET_LISTS": widgetName = "lists"; break; case "WIDGET_TIMER": widgetName = "focus_timer"; break; case "WIDGET_SPORTS": widgetName = "sports"; break; case "WIDGET_CLOCKS": widgetName = "clocks"; break; case "WIDGET_PRIVACY": widgetName = "privacy"; break; case "WIDGET_CROSSWORD": widgetName = "crossword"; break; case "WIDGET_STOCKS": widgetName = "stocks"; break; case "WIDGET_PICTURE_OF_THE_DAY": widgetName = "picture_of_the_day"; break; } if (widgetName) { const widget = WIDGET_REGISTRY.find( w => w.telemetryName === widgetName ); const widgetSize = resolveWidgetSize(widget, prefs); dispatch( ac.OnlyToMain({ type: at.WIDGETS_ENABLED, data: { widget_name: widgetName, widget_source: "customize_panel", enabled: value, widget_size: widgetSize, }, }) ); } setPref(preference, value); }); }; const { weatherEnabled } = enabledSections; const { timerEnabled, listsEnabled, sportsWidgetEnabled, clocksEnabled, privacyEnabled, crosswordEnabled, stocksEnabled, pictureOfTheDayEnabled, } = enabledWidgets; const isRTL = typeof document !== "undefined" && document.dir === "rtl"; const arrowIconSrc = `chrome://global/skin/icons/shaft-arrow-${isRTL ? "right" : "left"}.svg`; return (