import React, { useState, Suspense } from 'react'; import { __ } from '@wordpress/i18n'; import { Trash2, ChevronDown, ChevronRight, GripVertical, Plus } from 'lucide-react'; import { MetaBox as MetaBoxType, Button as ButtonType } from '../types'; import { Button as ButtonComponent } from './Button'; import { ProBoxFields } from '../pro'; interface MetaBoxProps { group: MetaBoxType; index: number; onUpdate: (index: number, group: MetaBoxType) => void; onDelete: (index: number) => void; onDragStart: (e: React.DragEvent, index: number) => void; onDragOver: (e: React.DragEvent) => void; onDrop: (e: React.DragEvent, dropIndex: number) => void; isDragging: boolean; } export const newButton = (): ButtonType => ({ id: `button-${Date.now()}${Math.floor(Math.random() * 10000)}`, enabled: false, title: '', url: '', message: '', selectOption: 'Confirmation' }); export const MetaBox: React.FC = ({ group, index, onUpdate, onDelete, onDragStart, onDragOver, onDrop, isDragging }) => { // New boxes (no title yet) open expanded const [isExpanded, setIsExpanded] = useState(() => !group.title); const [draggedButtonIndex, setDraggedButtonIndex] = useState(null); const change = (patch: Partial) => onUpdate(index, { ...group, ...patch }); const addButton = () => change({ buttons: [...group.buttons, newButton()] }); const updateButton = (buttonIndex: number, updatedButton: ButtonType) => { const updatedButtons = [...group.buttons]; updatedButtons[buttonIndex] = updatedButton; change({ buttons: updatedButtons }); }; const deleteButton = (buttonIndex: number) => change({ buttons: group.buttons.filter((_, i) => i !== buttonIndex) }); // Drag and drop handlers for buttons const handleButtonDragStart = (e: React.DragEvent, buttonIndex: number) => { setDraggedButtonIndex(buttonIndex); e.dataTransfer.effectAllowed = 'move'; }; const handleButtonDragOver = (e: React.DragEvent) => { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; }; const handleButtonDrop = (e: React.DragEvent, dropButtonIndex: number) => { e.preventDefault(); if (draggedButtonIndex === null || draggedButtonIndex === dropButtonIndex) { setDraggedButtonIndex(null); return; } const updatedButtons = [...group.buttons]; const draggedItem = updatedButtons[draggedButtonIndex]; updatedButtons.splice(draggedButtonIndex, 1); updatedButtons.splice(dropButtonIndex, 0, draggedItem); change({ buttons: updatedButtons }); setDraggedButtonIndex(null); }; return (
onDrop(e, index)} >
onDragStart(e, index)} >
change({ title: e.target.value })} />
{isExpanded && (
{ProBoxFields && ( )}

{__('Buttons', 'webhook-buttons')}

{group.buttons.map((button, buttonIndex) => ( handleButtonDragStart(e, buttonIndex)} onDragOver={handleButtonDragOver} onDrop={(e) => handleButtonDrop(e, buttonIndex)} isDragging={draggedButtonIndex === buttonIndex} /> ))} {group.buttons.length === 0 && (

{__('No buttons yet', 'webhook-buttons')}

)}
)}
); };