import { useRef, useState, useEffect } from 'react'; import { motion } from 'framer-motion'; import { Event } from '../types'; import { draggable } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'; interface DraggableEventProps { event: Event; date: string; onEventClick: (eventData: { event: Event; date: string }) => void; onDragStart: () => void; onDragEnd: () => void; onDayChange: (direction: 'left' | 'right') => void; isClient: boolean; } export default function DraggableEvent({ event, date, onEventClick, onDragStart, onDragEnd, onDayChange, isClient }: DraggableEventProps) { const ref = useRef(null); const [isDragging, setIsDragging] = useState(false); const [isTransitioning, setIsTransitioning] = useState(false); const screenWidth = useRef(isClient ? window.innerWidth : 0); const lastChangeTime = useRef(0); useEffect(() => { const element = ref.current; if (!element) return; const cleanup = draggable({ element, onDragStart: () => { screenWidth.current = window.innerWidth; setIsDragging(true); setIsTransitioning(true); onDragStart(); lastChangeTime.current = Date.now(); }, onDrag: ({ location }) => { const currentX = location.current.input.clientX; const now = Date.now(); if (now - lastChangeTime.current > 1000) { const edgeThreshold = 100; if (currentX < edgeThreshold) { lastChangeTime.current = now; onDayChange('left'); } else if (currentX > screenWidth.current - edgeThreshold) { lastChangeTime.current = now; onDayChange('right'); } } }, onDrop: () => { setIsDragging(false); setIsTransitioning(true); onDragEnd(); setTimeout(() => setIsTransitioning(false), 300); }, getInitialData: () => ({ id: event.id, date }), dragHandle: element }); return cleanup; }, [date, event.id, onDragEnd, onDragStart, onDayChange]); return ( !isDragging && onEventClick({ event, date })} className="bg-white p-4 rounded shadow-md mb-2 cursor-grab active:cursor-grabbing transition-all relative select-none" whileHover={{ scale: 1.01 }} transition={{ duration: 1.2 }} style={{ opacity: isDragging ? 0.7 : 1, userSelect: 'none', WebkitUserSelect: 'none', touchAction: 'manipulation', willChange: 'transform' }} >
{event.imageUrl && ( )} {event.time}
{event.title}
); }