'use client'; import * as React from 'react'; import { cn } from '@/lib/utils/cn'; import type { AutomationPoint as AutomationPointType } from '@/types/automation'; export interface AutomationPointProps { point: AutomationPointType; x: number; // Pixel position y: number; // Pixel position isSelected?: boolean; onDragStart?: (pointId: string, startX: number, startY: number) => void; onDrag?: (pointId: string, deltaX: number, deltaY: number) => void; onDragEnd?: (pointId: string) => void; onClick?: (pointId: string, event: React.MouseEvent) => void; onDoubleClick?: (pointId: string) => void; } export function AutomationPoint({ point, x, y, isSelected = false, onDragStart, onDrag, onDragEnd, onClick, onDoubleClick, }: AutomationPointProps) { const [isDragging, setIsDragging] = React.useState(false); const dragStartRef = React.useRef({ x: 0, y: 0 }); const handleMouseDown = React.useCallback( (e: React.MouseEvent) => { if (e.button !== 0) return; // Only left click e.stopPropagation(); setIsDragging(true); dragStartRef.current = { x: e.clientX, y: e.clientY }; if (onDragStart) { onDragStart(point.id, e.clientX, e.clientY); } }, [point.id, onDragStart] ); const handleClick = React.useCallback( (e: React.MouseEvent) => { if (!isDragging && onClick) { onClick(point.id, e); } }, [isDragging, point.id, onClick] ); const handleDoubleClick = React.useCallback( (e: React.MouseEvent) => { e.stopPropagation(); if (onDoubleClick) { onDoubleClick(point.id); } }, [point.id, onDoubleClick] ); // Global mouse handlers React.useEffect(() => { if (!isDragging) return; const handleMouseMove = (e: MouseEvent) => { if (!isDragging) return; const deltaX = e.clientX - dragStartRef.current.x; const deltaY = e.clientY - dragStartRef.current.y; if (onDrag) { onDrag(point.id, deltaX, deltaY); } // Update drag start position for next delta calculation dragStartRef.current = { x: e.clientX, y: e.clientY }; }; const handleMouseUp = () => { if (isDragging) { setIsDragging(false); if (onDragEnd) { onDragEnd(point.id); } } }; window.addEventListener('mousemove', handleMouseMove); window.addEventListener('mouseup', handleMouseUp); return () => { window.removeEventListener('mousemove', handleMouseMove); window.removeEventListener('mouseup', handleMouseUp); }; }, [isDragging, point.id, onDrag, onDragEnd]); return (
); }