|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 4 | +import type Fuse from 'fuse.js'; |
| 5 | +import Link from 'next/link'; |
| 6 | +import { usePathname } from 'next/navigation'; |
| 7 | +import { Search } from '../icons/Search'; |
| 8 | +import styles from './SearchDialog.module.css'; |
| 9 | + |
| 10 | +type SearchIndexEntry = { |
| 11 | + id: string; |
| 12 | + title: string; |
| 13 | + dateTime: string; |
| 14 | + slug: string; |
| 15 | + description?: string; |
| 16 | + talks?: Array<{ title: string; speakers?: Array<{ name: string }> }>; |
| 17 | + sponsor?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +const dateFormatter = new Intl.DateTimeFormat('fr-FR', { |
| 21 | + day: 'numeric', |
| 22 | + month: 'long', |
| 23 | + year: 'numeric', |
| 24 | +}); |
| 25 | + |
| 26 | +function getSpeakers(entry: SearchIndexEntry) { |
| 27 | + if (!entry.talks) return ''; |
| 28 | + const speakers = entry.talks.flatMap((t) => t.speakers?.map((s) => s.name) || []); |
| 29 | + return speakers.join(', '); |
| 30 | +} |
| 31 | + |
| 32 | +function formatDate(dateTime: string) { |
| 33 | + try { |
| 34 | + return dateFormatter.format(new Date(dateTime)); |
| 35 | + } catch { |
| 36 | + return ''; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +export const SearchDialog = () => { |
| 41 | + const [open, setOpen] = useState(false); |
| 42 | + const [query, setQuery] = useState(''); |
| 43 | + const [results, setResults] = useState<SearchIndexEntry[]>([]); |
| 44 | + const [index, setIndex] = useState<SearchIndexEntry[] | null>(null); |
| 45 | + const [loading, setLoading] = useState(false); |
| 46 | + |
| 47 | + const fuseRef = useRef<Fuse<SearchIndexEntry> | null>(null); |
| 48 | + const inputRef = useRef<HTMLInputElement>(null); |
| 49 | + const overlayRef = useRef<HTMLDivElement>(null); |
| 50 | + const debounceRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined); |
| 51 | + const pathname = usePathname(); |
| 52 | + |
| 53 | + // Close on route change |
| 54 | + useEffect(() => { |
| 55 | + setOpen(false); |
| 56 | + }, [pathname]); |
| 57 | + |
| 58 | + // Scroll lock |
| 59 | + useEffect(() => { |
| 60 | + document.body.toggleAttribute('data-lock-scroll', open); |
| 61 | + return () => { |
| 62 | + document.body.removeAttribute('data-lock-scroll'); |
| 63 | + }; |
| 64 | + }, [open]); |
| 65 | + |
| 66 | + // Keyboard shortcut: Ctrl/Cmd+K to open |
| 67 | + useEffect(() => { |
| 68 | + const handleKeyDown = (e: KeyboardEvent) => { |
| 69 | + if ((e.metaKey || e.ctrlKey) && e.key === 'k') { |
| 70 | + e.preventDefault(); |
| 71 | + setOpen((prev) => !prev); |
| 72 | + } |
| 73 | + }; |
| 74 | + document.addEventListener('keydown', handleKeyDown); |
| 75 | + return () => document.removeEventListener('keydown', handleKeyDown); |
| 76 | + }, []); |
| 77 | + |
| 78 | + // Load index + Fuse.js on first open |
| 79 | + useEffect(() => { |
| 80 | + if (!open || index) return; |
| 81 | + |
| 82 | + setLoading(true); |
| 83 | + Promise.all([ |
| 84 | + fetch('/search-index.json').then((r) => r.json() as Promise<SearchIndexEntry[]>), |
| 85 | + import('fuse.js'), |
| 86 | + ]).then(([data, FuseModule]) => { |
| 87 | + const Fuse = FuseModule.default; |
| 88 | + setIndex(data); |
| 89 | + fuseRef.current = new Fuse(data, { |
| 90 | + keys: [ |
| 91 | + { name: 'title', weight: 2 }, |
| 92 | + { name: 'talks.title', weight: 1.5 }, |
| 93 | + { name: 'talks.speakers.name', weight: 1.5 }, |
| 94 | + { name: 'description', weight: 0.5 }, |
| 95 | + { name: 'sponsor', weight: 0.8 }, |
| 96 | + ], |
| 97 | + threshold: 0.3, |
| 98 | + includeScore: true, |
| 99 | + }); |
| 100 | + setLoading(false); |
| 101 | + }); |
| 102 | + }, [open, index]); |
| 103 | + |
| 104 | + // Focus input when dialog opens |
| 105 | + useEffect(() => { |
| 106 | + if (open) { |
| 107 | + requestAnimationFrame(() => inputRef.current?.focus()); |
| 108 | + } else { |
| 109 | + setQuery(''); |
| 110 | + setResults([]); |
| 111 | + } |
| 112 | + }, [open]); |
| 113 | + |
| 114 | + // Debounced search |
| 115 | + const search = useCallback((value: string) => { |
| 116 | + if (debounceRef.current) clearTimeout(debounceRef.current); |
| 117 | + debounceRef.current = setTimeout(() => { |
| 118 | + if (!fuseRef.current || !value.trim()) { |
| 119 | + setResults([]); |
| 120 | + return; |
| 121 | + } |
| 122 | + const fuseResults = fuseRef.current.search(value, { limit: 20 }); |
| 123 | + setResults(fuseResults.map((r) => r.item)); |
| 124 | + }, 200); |
| 125 | + }, []); |
| 126 | + |
| 127 | + const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 128 | + const value = e.target.value; |
| 129 | + setQuery(value); |
| 130 | + search(value); |
| 131 | + }; |
| 132 | + |
| 133 | + const handleOverlayClick = (e: React.MouseEvent) => { |
| 134 | + if (e.target === overlayRef.current) { |
| 135 | + setOpen(false); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 140 | + if (e.key === 'Escape') { |
| 141 | + setOpen(false); |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + return ( |
| 146 | + <> |
| 147 | + <button className={styles.searchButton} onClick={() => setOpen(true)} aria-label="Rechercher" type="button"> |
| 148 | + <Search size={20} /> |
| 149 | + <kbd className={styles.kbd}>⌘K</kbd> |
| 150 | + </button> |
| 151 | + |
| 152 | + {open && ( |
| 153 | + <div |
| 154 | + className={styles.overlay} |
| 155 | + ref={overlayRef} |
| 156 | + onClick={handleOverlayClick} |
| 157 | + onKeyDown={handleKeyDown} |
| 158 | + role="dialog" |
| 159 | + aria-label="Rechercher un événement" |
| 160 | + aria-modal="true" |
| 161 | + > |
| 162 | + <div className={styles.container}> |
| 163 | + <div className={styles.inputWrapper}> |
| 164 | + <Search size={18} className={styles.inputIcon} /> |
| 165 | + <input |
| 166 | + ref={inputRef} |
| 167 | + className={styles.input} |
| 168 | + type="search" |
| 169 | + placeholder="Rechercher un événement, un talk, un speaker..." |
| 170 | + value={query} |
| 171 | + onChange={handleInputChange} |
| 172 | + aria-label="Rechercher" |
| 173 | + /> |
| 174 | + </div> |
| 175 | + |
| 176 | + <div className={styles.results}> |
| 177 | + {loading && <div className={styles.emptyState}>Chargement...</div>} |
| 178 | + |
| 179 | + {!loading && query && results.length === 0 && ( |
| 180 | + <div className={styles.emptyState}>Aucun résultat pour « {query} »</div> |
| 181 | + )} |
| 182 | + |
| 183 | + {results.map((entry) => { |
| 184 | + const speakers = getSpeakers(entry); |
| 185 | + return ( |
| 186 | + <Link |
| 187 | + key={entry.id} |
| 188 | + href={`/evenement/${entry.slug}`} |
| 189 | + className={styles.resultItem} |
| 190 | + onClick={() => setOpen(false)} |
| 191 | + > |
| 192 | + <div className={styles.resultTitle}>{entry.title}</div> |
| 193 | + <div className={styles.resultMeta}> |
| 194 | + <span>{formatDate(entry.dateTime)}</span> |
| 195 | + {speakers && <span>{speakers}</span>} |
| 196 | + {entry.sponsor && <span>{entry.sponsor}</span>} |
| 197 | + </div> |
| 198 | + </Link> |
| 199 | + ); |
| 200 | + })} |
| 201 | + |
| 202 | + {!loading && !query && index && ( |
| 203 | + <div className={styles.emptyState}>Tapez pour rechercher parmi {index.length} événements</div> |
| 204 | + )} |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + )} |
| 209 | + </> |
| 210 | + ); |
| 211 | +}; |
0 commit comments