import React, {FC} from "react"; import {__, CouponsPlus, hasProInstalled} from "../../globals"; import {EmulatedButton} from "../EmulatedButton"; import {AppImporter} from "../export/AppImporter"; import {GroveActions} from "../GroveActions"; import {useNodesStore} from "../store"; import {TreeNodeFactory} from "../TreeNodeFactory"; import {Offers} from "../Offers"; import {useSetAtom} from "jotai"; import {CurrentDashboardSectionIdAtom} from "../atoms"; import {TreeDashboardSection} from "../TreeDashboardSections"; import classNames from "classnames"; import {ProBadge} from "../../ProBadge"; import {useQuery} from "@tanstack/react-query"; import {Superballs} from 'ldrs/react' import 'ldrs/react/Superballs.css' import toast from "react-hot-toast"; export type Preset = { id: string name: string description: string example: string, isPro?: boolean, source?: string }; export type FeaturedPresetProps = { preset: Preset, style?: 'light' | 'dark-light' } export const FeaturedPreset: FC = ({preset, style = 'dark-light'}) => { const importFromSource = useNodesStore(store => store.importFromSource) const setSection = useSetAtom(CurrentDashboardSectionIdAtom); const reset = CouponsPlus.implementations.engine.trees === false const [imported, setImported] = React.useState(false); const {data, error, isLoading, refetch} = useQuery({ queryKey: ['import-preset', preset.id], queryFn: async () => { const url = `${(window as any).CouponsPlus.urls.rest.presets.byId}${preset.id}`; 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 const jsonData = await res.json(); const source = JSON.stringify(jsonData); // Verify it's valid JSON (it should be since we just stringified it) try { JSON.parse(source); } catch (e) { throw new Error('Invalid JSON response'); } importFromSource(source, reset, () => { // switch to the main offers section setSection(TreeDashboardSection.Offers) }) return source; }, enabled: false, retry: false, gcTime: 0, // Don't cache the result }); React.useEffect(() => { if (error) { toast.custom((t) => (
{__('Error loading preset. Please try again.')}
), { id: 'preset-error-' + preset.id, duration: 3000, position: 'bottom-center' }) } }, [error, preset.id]); return { if (preset.isPro && !hasProInstalled) { toast.custom((t) => (
{__('This preset has features only available on Coupons+ Pro')}
), { id: 'preset-error-' + preset.id, duration: 3000, position: 'bottom-center' }) return } refetch() }} className={classNames('relative w-full text-left rounded-1 flex flex-col justify-between gap-y-2 z-[100000] --h-[98%] p-4 group hover:bg-opacity-40 transition-all hover:scale-[1.02] active:scale-[0.98] overflow-hidden', { 'bg-gray-750 bg-opacity-[0.60] border-gray-400 border-px ring-[2px] ring-gray-100 ring-opacity-30 ring-offset-0 hover:bg-opacity-40 text-gray-100': style === 'dark-light', 'bg-gray-150 text-gray-700': style === 'light', })} 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(30px)', }}> {isLoading &&
}
{preset.isPro && !hasProInstalled && } {__('Import')}

{preset.name}

{preset.description}

{__('Example')}

{preset.example}

; }