forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.tsx
277 lines (257 loc) · 9.23 KB
/
Search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { useState, useEffect, useRef, ReactNode } from 'react'
import { useRouter } from 'next/router'
import debounce from 'lodash/debounce'
import cx from 'classnames'
import { useTranslation } from 'components/hooks/useTranslation'
import { sendEvent, EventType } from 'components/lib/events'
import { useMainContext } from './context/MainContext'
import { useVersion } from 'components/hooks/useVersion'
import { useLanguages } from './context/LanguagesContext'
import styles from './Search.module.scss'
type SearchResult = {
url: string
breadcrumbs: string
heading: string
title: string
content: string
}
type Props = {
isOverlay?: boolean
variant?: 'compact' | 'expanded'
autoFocus?: boolean
updateSearchParams?: boolean
children?: (props: { SearchInput: ReactNode; SearchResults: ReactNode }) => ReactNode
}
export function Search({
autoFocus = false,
isOverlay = false,
updateSearchParams = true,
variant = 'compact',
children,
}: Props) {
const router = useRouter()
const [query, setQuery] = useState(router.query.query || '')
const [results, setResults] = useState<Array<SearchResult>>([])
const [isLoading, setIsLoading] = useState(false)
const [activeHit, setActiveHit] = useState(-1)
const inputRef = useRef<HTMLInputElement>(null)
const { t } = useTranslation('search')
const { currentVersion } = useVersion()
const { languages } = useLanguages()
// Figure out language and version for index
const { searchVersions, nonEnterpriseDefaultVersion } = useMainContext()
// fall back to the non-enterprise default version (FPT currently) on the homepage, 404 page, etc.
const version = searchVersions[currentVersion] || searchVersions[nonEnterpriseDefaultVersion]
const language = (Object.keys(languages).includes(router.locale || '') && router.locale) || 'en'
// If the user shows up with a query in the URL, go ahead and search for it
useEffect(() => {
if (updateSearchParams && router.query.query) {
/* await */ fetchSearchResults((router.query.query as string).trim())
}
}, [])
// Search with your keyboard
useEffect(() => {
document.addEventListener('keydown', searchWithYourKeyboard)
return () => document.removeEventListener('keydown', searchWithYourKeyboard)
}, [results, activeHit])
function searchWithYourKeyboard(event: KeyboardEvent) {
switch (event.key) {
case '/':
// when an input is focused, `/` should have no special behavior
if (['INPUT', 'TEXTAREA', 'SEARCH'].includes(document?.activeElement?.tagName || '')) break
event.preventDefault() // prevent slash from being typed into input
inputRef.current?.focus()
break
case 'Escape':
closeSearch()
break
case 'ArrowDown':
if (!results.length) break
event.preventDefault() // prevent window scrolling
if (activeHit >= results.length) break
setActiveHit(activeHit + 1)
break
case 'ArrowUp':
if (!results.length) break
event.preventDefault() // prevent window scrolling
if (activeHit === 0) break
setActiveHit(activeHit - 1)
break
case 'Enter':
// look for a link in the given hit, then visit it
if (activeHit === 0 || !results.length) break
window.location.href = results[activeHit - 1]?.url
break
}
}
// When the user finishes typing, update the results
async function onSearch(e: React.ChangeEvent<HTMLInputElement>) {
const xquery = e.target?.value?.trim()
setQuery(xquery)
// Update the URL with the search parameters in the query string
if (updateSearchParams) {
const pushUrl = new URL(location.toString())
pushUrl.searchParams.set('query', xquery)
history.pushState({}, '', pushUrl.toString())
}
// deactivate any active hit when typing in search box
setActiveHit(0)
return await fetchSearchResults(xquery)
}
// If there's a query, call the endpoint
// Otherwise, there's no results by default
async function fetchSearchResults(xquery: string) {
setIsLoading(true)
try {
if (xquery) {
const endpointUrl = new URL(location.origin)
endpointUrl.pathname = '/search'
const endpointParams: Record<string, string> = {
language,
version,
query: xquery,
}
endpointUrl.search = new URLSearchParams(endpointParams).toString()
const response = await fetch(endpointUrl.toString(), {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
setResults(response.ok ? await response.json() : [])
} else {
setResults([])
}
} finally {
setIsLoading(false)
}
// Analytics tracking
if (xquery) {
sendEvent({
type: EventType.search,
search_query: xquery,
// search_context
})
}
}
// Close panel if overlay is clicked
function closeSearch() {
setQuery('')
setResults([])
}
// Prevent the page from refreshing when you "submit" the form
function preventRefresh(evt: React.FormEvent) {
evt.preventDefault()
}
const SearchResults = (
<>
<div
id="search-results-container"
className={cx(
'z-1 pb-4 px-3',
styles.resultsContainer,
isOverlay && styles.resultsContainerOverlay,
query && styles.resultsContainerOpen
)}
>
{results.length > 0 ? (
<ol data-testid="search-results" className="d-block mt-4">
{results.map(({ url, breadcrumbs, heading, title, content }, index) => {
const isActive = index === activeHit
return (
<li
key={url}
data-testid="search-result"
className={cx(
'list-style-none overflow-hidden rounded-3 color-text-primary border',
isActive ? 'color-bg-tertiary' : 'color-border-transparent'
)}
onMouseEnter={() => setActiveHit(index)}
>
<div className={cx('py-3 px-3', isActive && 'color-border-secondary')}>
<a className="no-underline color-text-primary" href={url}>
{/* Breadcrumbs in search records don't include the page title. These fields may contain <mark> elements that we need to render */}
<div
className={'d-block opacity-60 text-small pb-1'}
dangerouslySetInnerHTML={{ __html: breadcrumbs }}
/>
<div
className={cx(styles.searchResultTitle, 'd-block f4 text-semibold')}
dangerouslySetInnerHTML={{
__html: heading ? `${title}: ${heading}` : title,
}}
/>
<div
className={cx(styles.searchResultContent, 'd-block overflow-hidden')}
style={{ maxHeight: '4rem' }}
dangerouslySetInnerHTML={{ __html: content }}
/>
</a>
</div>
</li>
)
})}
</ol>
) : (
isOverlay && (
<div className="mt-2 px-6">
{isLoading ? <span>{t('loading')}...</span> : <span>{t('no_results')}.</span>}
</div>
)
)}
</div>
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div
className={cx('-z-1', isOverlay && query ? styles.searchOverlayOpen : 'd-none')}
onClick={closeSearch}
/>
</>
)
const SearchInput = (
<div data-testid="search" aria-hidden="true">
<div className="position-relative z-2">
<form role="search" className="width-full d-flex" noValidate onSubmit={preventRefresh}>
<input
data-testid="site-search-input"
ref={inputRef}
className={cx(
styles.searchInput,
'form-control px-5 f4',
variant === 'compact' && 'py-2',
variant === 'expanded' && 'py-3',
isOverlay && styles.searchInputOverlay,
!isOverlay && 'width-full',
isOverlay && query && styles.searchInputExpanded
)}
style={{
background:
'var(--color-bg-primary) url("/assets/images/octicons/search.svg") no-repeat 6px',
}}
type="search"
placeholder={t`placeholder`}
/* eslint-disable-next-line jsx-a11y/no-autofocus */
autoFocus={autoFocus}
autoComplete="off"
autoCorrect="off"
autoCapitalize="off"
spellCheck="false"
maxLength={512}
onChange={debounce(onSearch, 200)}
defaultValue={query}
/>
<button className="d-none" type="submit" title="Submit the search query." hidden />
</form>
</div>
</div>
)
return (
<>
{typeof children === 'function' ? (
children({ SearchInput, SearchResults })
) : (
<>
{SearchInput}
{SearchResults}
</>
)}
</>
)
}