import React, {FC, useState} from "react"; import {__} from "../../globals"; import {useQuery} from "@tanstack/react-query"; import {FeaturedPreset, Preset} from "./FeaturedPreset"; import {EmulatedButton} from "../EmulatedButton"; import classNames from "classnames"; import {Quantum} from 'ldrs/react' import 'ldrs/react/Quantum.css' import {colors} from "../Colors"; import {motion, AnimatePresence} from "framer-motion"; import {Button} from "../cards/Button"; import {TreeDashboardSection} from "../TreeDashboardSections"; import {useIsMobile} from "../hooks"; export type GuidedPresetsProps = { onBack?: () => void } export type PresetOption = { name: string; presets?: Preset[]; // Terminal node - has presets next?: PresetOptions; // Non-terminal node - leads to more options } export type PresetOptions = { name: string; options: PresetOption[]; } export type PresetStep = { name: string; examples?: string[]; next: PresetOptions; } type RootOptions = PresetStep[]; type SelectionPath = { options: PresetOptions; selectedIndex: number | null; } type OptionButtonProps = { name: string, isSelected: boolean, onClick: () => void, hasSelectedSiblings?: boolean } const OptionButton: FC = ({name, isSelected, onClick, hasSelectedSiblings}) => { return ( {name} ); }; type OptionRowProps = { title: string; options: Array<{ name: string }>; selectedIndex: number | null; onSelect: (index: number) => void; ButtonComponent?: FC } const OptionRow: FC = ({title, options, selectedIndex, onSelect, ButtonComponent}) => { return (

{title}

{options.map((option, index) => ( onSelect(index)} /> ))}
); }; export const GuidedPresets: FC = ({onBack}) => { const [selectionPath, setSelectionPath] = useState([]); const [finalPresets, setFinalPresets] = useState(null); const [selectedRootIndex, setSelectedRootIndex] = useState(null); const isMobile = useIsMobile(); const {data, isLoading, error} = useQuery({ queryKey: ['guided-presets'], queryFn: async () => { const url = (window as any).CouponsPlus.urls.rest.presets.steps; const res = await fetch(url, { headers: { 'X-WP-Nonce': (window as any).CouponsPlus.security.nonces.rest } }); if (!res.ok) { throw new Error(`HTTP error! status: ${res.status}`); } // WordPress REST API returns JSON object, we need to convert it back to string return res.json(); } }) if (isLoading) { return
} else if (error) { return
{__('Error loading presets')}
} const handleRootSelection = (step: PresetStep, index: number) => { setSelectedRootIndex(index); setSelectionPath([{ options: step.next, selectedIndex: null }]); setFinalPresets(null); }; const handleOptionSelection = (rowIndex: number, optionIndex: number, option: PresetOption) => { // Update selection for this row const newPath = selectionPath.slice(0, rowIndex + 1); newPath[rowIndex] = { ...newPath[rowIndex], selectedIndex: optionIndex }; // Check if this option has presets (terminal) or next options if (option.presets) { setFinalPresets(option.presets); setSelectionPath(newPath); } else if (option.next) { newPath.push({ options: option.next, selectedIndex: null }); setFinalPresets(null); setSelectionPath(newPath); } }; const hasNotSelectedAnPath = (typeof selectedRootIndex !== 'number' || !selectionPath.length) return
{false && hasNotSelectedAnPath &&
}
{/* Root row - "What kind of Deal?" */} {data && (
{onBack && }

What kind of Deal?

{false && handleRootSelection(data[index], index)} />}
{data.map((option, index) => ( handleRootSelection(option, index)} key={index} className={classNames('relative w-full text-left rounded-1 flex flex-col justify-between gap-y-2 z-[100000] group hover:bg-opacity-40 transition-all hover:scale-[1.02] active:scale-[0.98]', { 'bg-gray-750 bg-opacity-[0.60] border-gray-400 border-px ring-[2px] ring-gray-100 ring-opacity-60 ring-offset-1 hover:bg-opacity-40 text-gray-100': true, 'opacity-20 hover:!opacity-60 hover:!bg-opacity-80': selectedRootIndex !== null && selectedRootIndex !== index, 'p-3': isMobile, 'p-4 --h-[98%]': !isMobile, })} style={{ /* background: 'linear-gradient(90deg, rgba(0,0,0,0) 0%, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0.1) 55%, rgba(0,0,0,0) 100%)', */ backdropFilter: 'blur(1px)', }}>
{__('Import')}

{option.name}

{option.examples && option.examples.map((example, exIndex) => (

{example}

))}
))}
)}
{/* Progressive rows based on selections */} {selectionPath.map((pathItem, rowIndex) => ( handleOptionSelection(rowIndex, optionIndex, pathItem.options.options[optionIndex])} /> ))} {/* Show final presets */} {finalPresets && (

{__('Available Presets')}

{finalPresets.map((preset, index) => ( ))}
)}
; }