import { useState, useMemo, useCallback, useEffect } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { CheckIcon, CloseIcon, EyeIcon, LinkPluginIcon, PlayIcon, SearchIcon, } from '../icons'; import { btnLg, btnPrimary, proBadge, selectInput, selectLabel, Toggle, } from '../tw'; import { isWidgetEffectivelyOn } from '../utils'; const WidgetsPage = ({ allWidgets, allSettings, onSave, saving, isPro, widgetType = 'wc', }) => { const [search, setSearch] = useState(''); const [filter, setFilter] = useState('all'); const [contentTypeFilter, setContentTypeFilter] = useState('all'); const widgetTypeConfig = { wc: { title: __('WooCommerce Widgets', 'ultimate-store-kit'), key: 'ultimate_store_kit_active_modules', }, edd: { title: __('Easy Digital Downloads Widgets', 'ultimate-store-kit'), key: 'ultimate_store_kit_edd_modules', }, other: { title: __('Other Widgets', 'ultimate-store-kit'), key: 'ultimate_store_kit_general_modules', }, }; const currentConfig = widgetTypeConfig[widgetType]; const widgets = allWidgets[currentConfig.key] || []; const settings = allSettings[currentConfig.key] || {}; const [localSettings, setLocalSettings] = useState(() => { const initial = {}; Object.keys(allSettings).forEach((sectionKey) => { const sectionSettings = allSettings[sectionKey] || {}; const sectionWidgets = allWidgets[sectionKey] || []; sectionWidgets.forEach((w) => { if (w.type !== 'checkbox') return; initial[w.name] = sectionSettings[w.name] !== undefined ? sectionSettings[w.name] : w.default || 'off'; }); }); return initial; }); useEffect(() => { setSearch(''); setFilter('all'); setContentTypeFilter('all'); }, [widgetType]); const contentTypes = useMemo(() => { const types = new Set(); widgets.forEach((w) => { if (w.content_type) { w.content_type.split(' ').forEach((t) => { if ( t && t !== 'woocommerce' && t !== 'edd' && t !== 'new' ) { types.add(t); } }); } }); return Array.from(types); }, [widgets]); const filteredWidgets = useMemo(() => { return widgets.filter((w) => { if (w.type !== 'checkbox') return false; if (search && !w.label.toLowerCase().includes(search.toLowerCase())) return false; if (filter === 'free' && w.widget_type !== 'free') return false; if (filter === 'pro' && w.widget_type !== 'pro') return false; if ( contentTypeFilter !== 'all' && (!w.content_type || !w.content_type.includes(contentTypeFilter)) ) return false; return true; }); }, [widgets, search, filter, contentTypeFilter]); const handleToggle = useCallback( (name) => { setLocalSettings((prev) => ({ ...prev, [name]: prev[name] === 'on' ? 'off' : 'on', })); }, [] ); const handleActivateAll = useCallback(() => { setLocalSettings((prev) => { const updated = { ...prev }; filteredWidgets.forEach((w) => { if (w.widget_type === 'pro' && !isPro) return; const dep = w.dependency; if (dep && (!dep.isInstalled || !dep.isActive)) return; updated[w.name] = 'on'; }); return updated; }); }, [filteredWidgets, isPro]); const handleDeactivateAll = useCallback(() => { setLocalSettings((prev) => { const updated = { ...prev }; filteredWidgets.forEach((w) => { if (w.widget_type === 'pro' && !isPro) return; updated[w.name] = 'off'; }); return updated; }); }, [filteredWidgets, isPro]); const handleSave = () => { const currentSettings = {}; widgets.forEach((w) => { if (w.type === 'checkbox' && localSettings[w.name] !== undefined) { currentSettings[w.name] = localSettings[w.name]; } }); onSave(currentConfig.key, currentSettings); }; const activeCount = useMemo(() => { return widgets.filter( (w) => w.type === 'checkbox' && isWidgetEffectivelyOn(w, localSettings[w.name], isPro) ).length; }, [widgets, localSettings, isPro]); const cardBase = 'flex flex-col justify-between gap-3 rounded-lg border border-solid border-gray-200 p-4'; const toolbarControl = 'h-9 min-h-9 py-0 text-[13px] leading-none'; const toolbarBtn = 'inline-flex h-9 min-h-9 shrink-0 cursor-pointer items-center gap-1.5 rounded-md border px-3 py-0 text-[13px] font-semibold leading-none no-underline transition-all duration-200'; return (