"use client"; import React, { useEffect, useId, useMemo, useRef, useState } from "react"; import { ChevronDown, Search, X } from "lucide-react"; export type PickerEvent = { id: string; title: string; startDate: string; endDate?: string | null; campus?: { name: string } | null; isActive?: boolean; }; // Search results shown at once; typing more narrows them. const MAX_RESULTS = 60; const shortDate = (d: string) => new Date(d).toLocaleDateString("en-GB", { day: "numeric", month: "short", year: "numeric" }); const isPast = (ev: PickerEvent, cutoff: number) => new Date(ev.endDate || ev.startDate).getTime() < cutoff; /** * A searchable event chooser in place of a long { setQuery(e.target.value); setActive(0); }} onKeyDown={onKey} /> {query && ( )}
{query ? `${rows.filter(r => r.kind === "event").length + hidden} match${rows.filter(r => r.kind === "event").length + hidden === 1 ? "" : "es"}${hidden ? ` · showing the first ${MAX_RESULTS}, keep typing` : ""}` : `${upcoming.length} upcoming${showPast ? ` · ${past.length} past` : ""}`} {!query && past.length > 0 && ( )}
)} ); } /** The event a page opens on: the next one coming up, or else the most recent. */ export function defaultEventId(events: PickerEvent[], now = Date.now()): string { let next: PickerEvent | null = null; let last: PickerEvent | null = null; for (const ev of events) { const start = new Date(ev.startDate).getTime(); if (!isPast(ev, now)) { if (!next || start < new Date(next.startDate).getTime()) next = ev; } else if (!last || start > new Date(last.startDate).getTime()) last = ev; } return (next || last)?.id || ""; }