"use client"; import React, { useEffect, useMemo, useRef, useState } from "react"; import { ChevronDown, Search } from "lucide-react"; // The reports sidebar's events multi-select. With many events, the search box narrows the list // and "Select all" picks only what matches. export default function EventsDropdown({ options, value, onChange }: { // `hint`: shown beside the title (the date) and searched too. options: { value: string; label: string; hint?: string }[]; value: string[]; onChange: (v: string[]) => void; }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const ref = useRef(null); useEffect(() => { const onClick = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false); }; document.addEventListener("mousedown", onClick); return () => document.removeEventListener("mousedown", onClick); }, []); const shown = useMemo(() => { const words = query.trim().toLowerCase().split(/\s+/).filter(Boolean); if (!words.length) return options; return options.filter(o => { const hay = `${o.label} ${o.hint || ""}`.toLowerCase(); return words.every(w => hay.includes(w)); }); }, [options, query]); const toggle = (v: string) => { onChange(value.includes(v) ? value.filter(x => x !== v) : [...value, v]); }; const selectShown = () => onChange(Array.from(new Set([...value, ...shown.map(o => o.value)]))); const clearShown = () => { const drop = new Set(shown.map(o => o.value)); onChange(query.trim() ? value.filter(v => !drop.has(v)) : []); }; const summary = value.length === 0 ? "No events selected" : options.length > 0 && value.length === options.length ? "All events" : value.length === 1 ? (options.find(o => o.value === value[0])?.label || "1 event selected") : `${value.length} events selected`; return (
{open && (
setQuery(e.target.value)} onKeyDown={e => { if (e.key === "Escape") setOpen(false); }} />
{shown.map(opt => ( ))} {shown.length === 0 &&
{options.length ? "No events match." : "No events available."}
}
)}
); }