diff --git a/src/components/MobileCalendarAgenda.tsx b/src/components/MobileCalendarAgenda.tsx index bc6d29b..3f5d3c5 100644 --- a/src/components/MobileCalendarAgenda.tsx +++ b/src/components/MobileCalendarAgenda.tsx @@ -1,141 +1,85 @@ "use client"; -import { useEffect, useMemo, useRef, useState } from "react"; +import { useEffect, useMemo, useRef } from "react"; import { CalendarEventData } from "@/components/CalendarEventCard"; import CalendarEventLink from "@/components/CalendarEventLink"; import { buildMonthDays } from "@/lib/calendarView"; -const SWIPE_THRESHOLD_PX = 50; - interface Props { monthKey: string; entries: CalendarEventData[]; } -function initialIndexForMonth(days: ReturnType) { - const inMonthDays = days.filter((day) => day.inMonth); - const todayIndex = inMonthDays.findIndex((day) => day.isToday); - return todayIndex >= 0 ? todayIndex : 0; -} - export default function MobileCalendarAgenda({ monthKey, entries }: Props) { const days = useMemo( () => buildMonthDays(monthKey, entries).filter((day) => day.inMonth), [monthKey, entries] ); - const [selectedIndex, setSelectedIndex] = useState(() => initialIndexForMonth(buildMonthDays(monthKey, entries))); - const touchStartYRef = useRef(null); - const touchStartXRef = useRef(null); + const todaySectionRef = useRef(null); + const scrolledMonthRef = useRef(null); useEffect(() => { - setSelectedIndex(initialIndexForMonth(buildMonthDays(monthKey, entries))); - }, [monthKey, entries]); - - const selectedDay = days[selectedIndex] ?? days[0]; - - function moveDay(delta: number) { - setSelectedIndex((current) => { - const next = current + delta; - if (next < 0 || next >= days.length) return current; - return next; - }); - } - - if (!selectedDay) { - return ( -
- No days available for this month. -
- ); - } - - const dayTitle = selectedDay.date.toLocaleDateString("en-US", { - weekday: "long", - month: "long", - day: "numeric", - year: "numeric", - }); + if (!todaySectionRef.current) return; + if (scrolledMonthRef.current === monthKey) return; + scrolledMonthRef.current = monthKey; + todaySectionRef.current.scrollIntoView({ block: "start" }); + }, [monthKey]); return ( -
{ - const touch = event.changedTouches[0]; - touchStartYRef.current = touch.clientY; - touchStartXRef.current = touch.clientX; - }} - onTouchEnd={(event) => { - if (touchStartYRef.current === null || touchStartXRef.current === null) return; - const touch = event.changedTouches[0]; - const deltaY = touch.clientY - touchStartYRef.current; - const deltaX = touch.clientX - touchStartXRef.current; - touchStartYRef.current = null; - touchStartXRef.current = null; - - if (Math.abs(deltaY) < SWIPE_THRESHOLD_PX || Math.abs(deltaY) <= Math.abs(deltaX)) { - return; - } - - if (deltaY < 0) moveDay(1); - else moveDay(-1); - }} - > -
-
-
-

- {dayTitle} -

-

- {selectedDay.events.length === 0 - ? "No meetings on this day" - : `${selectedDay.events.length} meeting${selectedDay.events.length === 1 ? "" : "s"}`} -

-
- {selectedDay.isToday && ( - - Today - - )} -
-

- Swipe up for next day, down for previous day. -

-
+
+ {days.map((day) => { + const dayTitle = day.date.toLocaleDateString("en-US", { + weekday: "long", + month: "long", + day: "numeric", + year: "numeric", + }); -
- {selectedDay.events.length === 0 ? ( -
- Nothing scheduled from invite emails. -
- ) : ( - selectedDay.events.map((event) => ( - - )) - )} -
+ return ( +
+
+
+
+

+ {dayTitle} +

+

+ {day.events.length === 0 + ? "No meetings" + : `${day.events.length} meeting${day.events.length === 1 ? "" : "s"}`} +

+
+ {day.isToday && ( + + Today + + )} +
+
-
- - - {selectedIndex + 1} / {days.length} - - -
-
+
+ {day.events.length === 0 ? ( +
+ Nothing scheduled from invite emails. +
+ ) : ( + day.events.map((event) => ( + + )) + )} +
+
+ ); + })} + ); }