From e0db22871ab3f5edc5e9f4ed8d2176064acac825 Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Fri, 28 Mar 2025 13:42:38 -0400 Subject: [PATCH] Fix "ghost" events on mobile when dragging --- src/components/Calendar.tsx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/components/Calendar.tsx b/src/components/Calendar.tsx index 3d81769..24baa05 100644 --- a/src/components/Calendar.tsx +++ b/src/components/Calendar.tsx @@ -188,7 +188,7 @@ const Calendar = () => { }, []); const handleEventMove = (eventId: string, newDate: Date) => { - setEvents(prev => { + setEvents((prev: EventsByDate) => { const newEvents = { ...prev }; const oldDateKey = Object.keys(newEvents).find(key => newEvents[key].some((e: Event) => e.id === eventId) @@ -342,12 +342,19 @@ const DayColumn = ({ return hours * 60 + minutes; }; - const sortedEvents = useMemo(() => - [...events].sort((a, b) => - parseTimeToMinutes(a.time) - parseTimeToMinutes(b.time) - ), - [events] - ); + const sortedEvents = useMemo(() => { + const seenIds = new Set(); + return [...events] + .sort((a, b) => parseTimeToMinutes(a.time) - parseTimeToMinutes(b.time)) + .filter(event => { + if (seenIds.has(event.id)) { + //console.warn(`Duplicate event ID detected: ${event.id}`); + return false; + } + seenIds.add(event.id); + return true; + }); + }, [events]); useEffect(() => { setIsMobile(width < 768); // Update isMobile after hydration