|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
| 4 | +import { Search } from 'lucide-react'; |
| 5 | +import { useRouter } from '@/i18n/routing'; |
| 6 | + |
| 7 | +type PostSearchItem = { |
| 8 | + _id: string; |
| 9 | + title?: string; |
| 10 | + body?: Array<{ _type: string; children?: Array<{ text?: string }> }>; |
| 11 | + slug?: { current?: string }; |
| 12 | +}; |
| 13 | + |
| 14 | +type SearchResult = PostSearchItem & { snippet?: string }; |
| 15 | + |
| 16 | +function extractSnippet(body: PostSearchItem['body'], query: string) { |
| 17 | + const text = (body || []) |
| 18 | + .filter(block => block?._type === 'block') |
| 19 | + .map(block => |
| 20 | + (block.children || []).map(child => child.text || '').join(' ') |
| 21 | + ) |
| 22 | + .join(' ') |
| 23 | + .replace(/\s+/g, ' ') |
| 24 | + .trim(); |
| 25 | + if (!text) return ''; |
| 26 | + const lower = text.toLowerCase(); |
| 27 | + const idx = lower.indexOf(query.toLowerCase()); |
| 28 | + if (idx === -1) return text.slice(0, 90); |
| 29 | + const start = Math.max(0, idx - 36); |
| 30 | + const end = Math.min(text.length, idx + 54); |
| 31 | + const prefix = start > 0 ? '...' : ''; |
| 32 | + const suffix = end < text.length ? '...' : ''; |
| 33 | + return `${prefix}${text.slice(start, end)}${suffix}`; |
| 34 | +} |
| 35 | + |
| 36 | +const SEARCH_ENDPOINT = '/api/blog-search'; |
| 37 | + |
| 38 | +export function BlogHeaderSearch() { |
| 39 | + const [open, setOpen] = useState(false); |
| 40 | + const [value, setValue] = useState(''); |
| 41 | + const [items, setItems] = useState<PostSearchItem[]>([]); |
| 42 | + const [isLoading, setIsLoading] = useState(false); |
| 43 | + const inputRef = useRef<HTMLInputElement>(null); |
| 44 | + const router = useRouter(); |
| 45 | + const debounceRef = useRef<number | null>(null); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!open) return; |
| 49 | + inputRef.current?.focus(); |
| 50 | + }, [open]); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (!open || items.length || isLoading) return; |
| 54 | + let active = true; |
| 55 | + setIsLoading(true); |
| 56 | + fetch(SEARCH_ENDPOINT, { cache: 'no-store' }) |
| 57 | + .then(response => (response.ok ? response.json() : [])) |
| 58 | + .then((result: PostSearchItem[]) => { |
| 59 | + if (!active) return; |
| 60 | + setItems(Array.isArray(result) ? result : []); |
| 61 | + }) |
| 62 | + .catch(() => { |
| 63 | + if (!active) return; |
| 64 | + setItems([]); |
| 65 | + }) |
| 66 | + .finally(() => { |
| 67 | + if (!active) return; |
| 68 | + setIsLoading(false); |
| 69 | + }); |
| 70 | + return () => { |
| 71 | + active = false; |
| 72 | + }; |
| 73 | + }, [open, items.length, isLoading]); |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + if (!open) return; |
| 77 | + if (debounceRef.current) window.clearTimeout(debounceRef.current); |
| 78 | + debounceRef.current = window.setTimeout(() => { |
| 79 | + const query = value.trim(); |
| 80 | + router.replace(query ? `/blog?search=${encodeURIComponent(query)}` : '/blog'); |
| 81 | + }, 300); |
| 82 | + return () => { |
| 83 | + if (debounceRef.current) window.clearTimeout(debounceRef.current); |
| 84 | + }; |
| 85 | + }, [open, router, value]); |
| 86 | + |
| 87 | + const results = useMemo<SearchResult[]>(() => { |
| 88 | + const query = value.trim().toLowerCase(); |
| 89 | + if (!query) return []; |
| 90 | + const words = query.split(/\s+/).filter(Boolean); |
| 91 | + return items |
| 92 | + .filter(item => { |
| 93 | + const title = (item.title || '').toLowerCase(); |
| 94 | + const bodyText = (item.body || []) |
| 95 | + .filter(block => block?._type === 'block') |
| 96 | + .map(block => |
| 97 | + (block.children || []).map(child => child.text || '').join(' ') |
| 98 | + ) |
| 99 | + .join(' ') |
| 100 | + .toLowerCase(); |
| 101 | + return words.every( |
| 102 | + word => title.includes(word) || bodyText.includes(word) |
| 103 | + ); |
| 104 | + }) |
| 105 | + .slice(0, 6) |
| 106 | + .map(item => ({ |
| 107 | + ...item, |
| 108 | + snippet: extractSnippet(item.body, query), |
| 109 | + })); |
| 110 | + }, [items, value]); |
| 111 | + |
| 112 | + const submit = (event?: React.FormEvent) => { |
| 113 | + if (event) event.preventDefault(); |
| 114 | + const query = value.trim(); |
| 115 | + router.push(query ? `/blog?search=${encodeURIComponent(query)}` : '/blog'); |
| 116 | + setOpen(false); |
| 117 | + }; |
| 118 | + |
| 119 | + const clear = () => { |
| 120 | + setValue(''); |
| 121 | + setOpen(false); |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <div className="relative flex items-center"> |
| 126 | + <button |
| 127 | + type="button" |
| 128 | + onClick={() => setOpen(prev => !prev)} |
| 129 | + className="flex h-9 w-9 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground" |
| 130 | + aria-label="Search blog" |
| 131 | + > |
| 132 | + <Search className="h-4 w-4" aria-hidden="true" /> |
| 133 | + </button> |
| 134 | + |
| 135 | + {open && ( |
| 136 | + <div |
| 137 | + id="wrap" |
| 138 | + className="absolute right-0 top-12 z-50 w-72 overflow-hidden rounded-lg border border-border bg-background shadow-lg" |
| 139 | + > |
| 140 | + <form |
| 141 | + action="" |
| 142 | + autoComplete="on" |
| 143 | + onSubmit={submit} |
| 144 | + className="flex items-center gap-2 px-3 py-2" |
| 145 | + > |
| 146 | + <input |
| 147 | + ref={inputRef} |
| 148 | + id="search" |
| 149 | + name="search" |
| 150 | + type="text" |
| 151 | + value={value} |
| 152 | + onChange={event => { |
| 153 | + setValue(event.target.value); |
| 154 | + if (!open) setOpen(true); |
| 155 | + }} |
| 156 | + onKeyDown={event => { |
| 157 | + if (event.key === 'Escape') setOpen(false); |
| 158 | + }} |
| 159 | + placeholder="What're we looking for ?" |
| 160 | + className="w-full bg-transparent text-sm text-foreground outline-none" |
| 161 | + style={{ fontFamily: 'Lato, system-ui, -apple-system, sans-serif' }} |
| 162 | + /> |
| 163 | + <input |
| 164 | + id="search_submit" |
| 165 | + value="" |
| 166 | + type="submit" |
| 167 | + className="text-sm font-medium text-muted-foreground hover:text-foreground" |
| 168 | + /> |
| 169 | + </form> |
| 170 | + {value && results.length > 0 && ( |
| 171 | + <div className="max-h-56 overflow-auto border-t border-border py-2"> |
| 172 | + {results.map(result => ( |
| 173 | + <button |
| 174 | + key={result._id} |
| 175 | + type="button" |
| 176 | + onClick={() => { |
| 177 | + const slug = result.slug?.current; |
| 178 | + if (slug) { |
| 179 | + router.push(`/blog/${slug}`); |
| 180 | + } else { |
| 181 | + router.push( |
| 182 | + `/blog?search=${encodeURIComponent(result.title || '')}` |
| 183 | + ); |
| 184 | + } |
| 185 | + setOpen(false); |
| 186 | + }} |
| 187 | + className="block w-full px-3 py-2 text-left text-sm text-muted-foreground hover:bg-secondary hover:text-foreground" |
| 188 | + > |
| 189 | + <div className="font-medium text-foreground"> |
| 190 | + {result.title} |
| 191 | + </div> |
| 192 | + {result.snippet && ( |
| 193 | + <div className="mt-1 text-xs text-muted-foreground"> |
| 194 | + {result.snippet} |
| 195 | + </div> |
| 196 | + )} |
| 197 | + </button> |
| 198 | + ))} |
| 199 | + </div> |
| 200 | + )} |
| 201 | + {value && !results.length && !isLoading && ( |
| 202 | + <div className="border-t border-border px-3 py-2 text-xs text-muted-foreground"> |
| 203 | + No matches |
| 204 | + </div> |
| 205 | + )} |
| 206 | + </div> |
| 207 | + )} |
| 208 | + </div> |
| 209 | + ); |
| 210 | +} |
0 commit comments