fix(ux): calendar 'Past' opens on the newest month with events

The calendar always initialised to the current month, so the Past tab (all
events in the past) opened on an empty month. It now anchors to the newest
past event, and re-anchors when the tab changes.
This commit is contained in:
SkateSpots 2026-09-26 19:09:21 +07:00
parent 0aee937880
commit bf7fdbd621
2 changed files with 23 additions and 3 deletions

View file

@ -29,6 +29,8 @@ interface EventsCalendarProps {
goingCounts?: Map<string, number>;
/** Which kinds to display. */
show: 'events' | 'tours';
/** Unix seconds to open the calendar on (e.g. the newest past event). */
anchorUnix?: number;
}
// ─── Date helpers (local time) ────────────────────────────────────────────────
@ -119,10 +121,23 @@ function formatDayLong(d: Date): string {
// ─── Component ────────────────────────────────────────────────────────────────
export function EventsCalendar({ events, tours, goingCounts, show }: EventsCalendarProps) {
export function EventsCalendar({ events, tours, goingCounts, show, anchorUnix }: EventsCalendarProps) {
const today = useMemo(() => startOfDay(new Date()), []);
const [view, setView] = useState<Date>(() => new Date(today.getFullYear(), today.getMonth(), 1));
const [selected, setSelected] = useState<Date>(today);
const [view, setView] = useState<Date>(() => {
const base = anchorUnix ? new Date(anchorUnix * 1000) : today;
return new Date(base.getFullYear(), base.getMonth(), 1);
});
const [selected, setSelected] = useState<Date>(() => startOfDay(anchorUnix ? new Date(anchorUnix * 1000) : today));
// Follow the anchor when the caller changes tabs (React state is only the
// initial value otherwise). Without this, "Past" opened on the CURRENT month
// — which has no past events — so the calendar looked empty.
useEffect(() => {
if (!anchorUnix) return;
const d = new Date(anchorUnix * 1000);
setView(new Date(d.getFullYear(), d.getMonth(), 1));
setSelected(startOfDay(d));
}, [anchorUnix]);
const accent = show === 'events'
? { dot: 'bg-[#ccff00]', ring: 'ring-[#ccff00]', text: 'text-[#ccff00]', soft: 'bg-[#ccff00]/15', border: 'border-[#ccff00]/40', solid: 'bg-[#ccff00] text-black' }

View file

@ -1195,6 +1195,11 @@ export default function EventsPage() {
events={activeTab === 'upcoming' ? filteredEvents.upcoming : filteredEvents.past}
tours={[]}
goingCounts={goingById}
anchorUnix={
activeTab === 'past' && filteredEvents.past.length > 0
? filteredEvents.past.reduce((m, e) => Math.max(m, e.start), 0)
: undefined
}
/>
)
)}