|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useMemo, useEffect, useRef } from "react"; |
| 4 | +import type { Event } from "@/lib/types"; |
| 5 | +import { EVENT_TYPES } from "@/lib/types"; |
| 6 | +import { formatYear } from "@/lib/date-utils"; |
| 7 | +import { ERAS, groupByEra } from "@/lib/eras"; |
| 8 | +import CategoryIcon from "@/components/CategoryIcon"; |
| 9 | +import browseStyles from "@/styles/Browse.module.css"; |
| 10 | +import modalStyles from "@/styles/BrowseModal.module.css"; |
| 11 | + |
| 12 | +interface BrowseModalProps { |
| 13 | + events: Event[]; |
| 14 | + selectedIds: number[]; |
| 15 | + onSelect: (event: Event) => void; |
| 16 | + onClose: () => void; |
| 17 | +} |
| 18 | + |
| 19 | +function capitalize(s: string): string { |
| 20 | + return s.charAt(0).toUpperCase() + s.slice(1); |
| 21 | +} |
| 22 | + |
| 23 | +export default function BrowseModal({ events, selectedIds, onSelect, onClose }: BrowseModalProps) { |
| 24 | + const modalRef = useRef<HTMLDivElement>(null); |
| 25 | + const [openEras, setOpenEras] = useState<Set<string>>(() => new Set()); |
| 26 | + const [categoryFilter, setCategoryFilter] = useState<string | null>(null); |
| 27 | + |
| 28 | + const eras = useMemo(() => { |
| 29 | + const groups = groupByEra(events); |
| 30 | + return ERAS.map((era) => ({ |
| 31 | + id: era.id, |
| 32 | + label: era.label, |
| 33 | + description: era.description, |
| 34 | + events: groups.get(era.id) || [], |
| 35 | + })); |
| 36 | + }, [events]); |
| 37 | + |
| 38 | + const categoryCounts = useMemo(() => { |
| 39 | + const map = new Map<string, number>(); |
| 40 | + for (const e of events) { |
| 41 | + map.set(e.type, (map.get(e.type) || 0) + 1); |
| 42 | + } |
| 43 | + return map; |
| 44 | + }, [events]); |
| 45 | + |
| 46 | + const filteredEras = useMemo(() => { |
| 47 | + if (!categoryFilter) return eras; |
| 48 | + return eras.map((era) => ({ |
| 49 | + ...era, |
| 50 | + events: era.events.filter((e) => e.type === categoryFilter), |
| 51 | + })); |
| 52 | + }, [eras, categoryFilter]); |
| 53 | + |
| 54 | + const totalFiltered = filteredEras.reduce((sum, e) => sum + e.events.length, 0); |
| 55 | + |
| 56 | + const toggle = (id: string) => { |
| 57 | + setOpenEras((prev) => { |
| 58 | + const next = new Set(prev); |
| 59 | + if (next.has(id)) next.delete(id); |
| 60 | + else next.add(id); |
| 61 | + return next; |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + // Focus trap + Escape |
| 66 | + useEffect(() => { |
| 67 | + function handleKey(e: KeyboardEvent) { |
| 68 | + if (e.key === "Escape") onClose(); |
| 69 | + if (e.key === "Tab" && modalRef.current) { |
| 70 | + const focusable = modalRef.current.querySelectorAll<HTMLElement>( |
| 71 | + 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' |
| 72 | + ); |
| 73 | + if (focusable.length === 0) return; |
| 74 | + const first = focusable[0]; |
| 75 | + const last = focusable[focusable.length - 1]; |
| 76 | + if (e.shiftKey && document.activeElement === first) { |
| 77 | + e.preventDefault(); |
| 78 | + last.focus(); |
| 79 | + } else if (!e.shiftKey && document.activeElement === last) { |
| 80 | + e.preventDefault(); |
| 81 | + first.focus(); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + document.addEventListener("keydown", handleKey); |
| 86 | + modalRef.current?.focus(); |
| 87 | + return () => document.removeEventListener("keydown", handleKey); |
| 88 | + }, [onClose]); |
| 89 | + |
| 90 | + return ( |
| 91 | + <div className={modalStyles.overlay} onClick={onClose} role="presentation"> |
| 92 | + <div |
| 93 | + ref={modalRef} |
| 94 | + className={modalStyles.modal} |
| 95 | + onClick={(e) => e.stopPropagation()} |
| 96 | + role="dialog" |
| 97 | + aria-modal="true" |
| 98 | + aria-labelledby="browse-title" |
| 99 | + tabIndex={-1} |
| 100 | + > |
| 101 | + <div className={modalStyles.header}> |
| 102 | + <h3 id="browse-title" className={modalStyles.title}>Browse by era</h3> |
| 103 | + <button |
| 104 | + className={modalStyles.closeButton} |
| 105 | + onClick={onClose} |
| 106 | + aria-label="Close" |
| 107 | + > |
| 108 | + × |
| 109 | + </button> |
| 110 | + </div> |
| 111 | + <p className={browseStyles.subtitle}> |
| 112 | + {categoryFilter |
| 113 | + ? `${totalFiltered} ${categoryFilter} events.` |
| 114 | + : `${totalFiltered} events.`} |
| 115 | + {" "}Click to add to your timeline. |
| 116 | + </p> |
| 117 | + <div className={browseStyles.chips}> |
| 118 | + {EVENT_TYPES.map((type) => ( |
| 119 | + <button |
| 120 | + key={type} |
| 121 | + className={`${browseStyles.chip}${categoryFilter === type ? ` ${browseStyles.chipActive}` : ""}`} |
| 122 | + onClick={() => setCategoryFilter(categoryFilter === type ? null : type)} |
| 123 | + aria-pressed={categoryFilter === type} |
| 124 | + title={capitalize(type)} |
| 125 | + > |
| 126 | + <CategoryIcon type={type} size={16} /> |
| 127 | + <span>{capitalize(type)} ({categoryCounts.get(type) || 0})</span> |
| 128 | + </button> |
| 129 | + ))} |
| 130 | + </div> |
| 131 | + <div className={browseStyles.eras}> |
| 132 | + {filteredEras.map((era) => { |
| 133 | + if (era.events.length === 0) return null; |
| 134 | + const isOpen = openEras.has(era.id); |
| 135 | + return ( |
| 136 | + <div key={era.id} className={browseStyles.era}> |
| 137 | + <button |
| 138 | + className={browseStyles.eraHeader} |
| 139 | + onClick={() => toggle(era.id)} |
| 140 | + aria-expanded={isOpen} |
| 141 | + aria-controls={`browse-era-${era.id}`} |
| 142 | + > |
| 143 | + <div className={browseStyles.eraInfo}> |
| 144 | + <span className={browseStyles.eraLabel}>{era.label}</span> |
| 145 | + <span className={browseStyles.eraDescription}>{era.description}</span> |
| 146 | + </div> |
| 147 | + <span className={browseStyles.eraCount}>{era.events.length} events</span> |
| 148 | + <span className={`${browseStyles.chevron} ${isOpen ? browseStyles.chevronOpen : ""}`} aria-hidden="true"> |
| 149 | + ▾ |
| 150 | + </span> |
| 151 | + </button> |
| 152 | + {isOpen && ( |
| 153 | + <div id={`browse-era-${era.id}`} className={browseStyles.eventList} role="list"> |
| 154 | + {era.events.map((event) => { |
| 155 | + const alreadySelected = selectedIds.includes(event.id); |
| 156 | + return ( |
| 157 | + <button |
| 158 | + key={event.id} |
| 159 | + className={`${browseStyles.eventItem} ${alreadySelected ? modalStyles.eventDisabled : ""}`} |
| 160 | + role="listitem" |
| 161 | + disabled={alreadySelected} |
| 162 | + onClick={() => { |
| 163 | + onSelect(event); |
| 164 | + onClose(); |
| 165 | + }} |
| 166 | + > |
| 167 | + <span className={browseStyles.eventIcon}> |
| 168 | + <CategoryIcon type={event.type} size={20} /> |
| 169 | + </span> |
| 170 | + <span className={browseStyles.eventName}>{capitalize(event.name)}</span> |
| 171 | + <span className={browseStyles.eventYear}>{formatYear(event.year)}</span> |
| 172 | + </button> |
| 173 | + ); |
| 174 | + })} |
| 175 | + </div> |
| 176 | + )} |
| 177 | + </div> |
| 178 | + ); |
| 179 | + })} |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +} |
0 commit comments