MCPcopy
hub / github.com/github/docs / Search

Function Search

components/search/index.tsx:19–105  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17import { useMainContext } from 'components/context/MainContext'
18
19export function Search() {
20 const { locale } = useRouter()
21 const { formatInteger } = useNumberFormatter()
22 const { t } = useTranslation('search')
23 const { currentVersion } = useVersion()
24 const { query, debug } = useQuery()
25 const { page } = usePage()
26
27 // A reference to the `content/search/index.md` Page object.
28 // Not to be confused with the "page" that is for paginating
29 // results.
30 const { allVersions, page: documentPage } = useMainContext()
31 const searchVersion = allVersions[currentVersion].versionTitle
32
33 const sp = new URLSearchParams()
34 const hasQuery = Boolean(query.trim())
35 if (hasQuery) {
36 sp.set('query', query.trim())
37 sp.set('language', locale || 'en')
38 if (debug) sp.set('debug', 'true')
39 sp.set('version', currentVersion)
40 if (page !== 1) {
41 sp.set('page', `${page}`)
42 }
43 }
44
45 const inDebugMode = process.env.NODE_ENV === 'development'
46
47 const { data: results, error } = useSWR<SearchResultsT | null, Error | null>(
48 hasQuery ? `/api/search/v1?${sp.toString()}` : null,
49 async (url) => {
50 const response = await fetch(url)
51 if (!response.ok) {
52 throw new Error(`${response.status} on ${url}`)
53 }
54 return await response.json()
55 },
56 {
57 onSuccess: () => {
58 sendEvent({
59 type: EventType.search,
60 search_query: query,
61 })
62 },
63 // Because the backend never changes between fetches, we can treat
64 // it as an immutable resource and disable these revalidation
65 // checks.
66 revalidateIfStale: inDebugMode,
67 revalidateOnFocus: inDebugMode,
68 revalidateOnReconnect: inDebugMode,
69 }
70 )
71
72 let pageTitle = documentPage.fullTitle
73 if (hasQuery) {
74 pageTitle = `${t('search_results_for')} "${query.trim()}"`
75 if (currentVersion !== DEFAULT_VERSION) {
76 pageTitle += ` (${searchVersion})`

Callers

nothing calls this directly

Calls 7

useNumberFormatterFunction · 0.90
useTranslationFunction · 0.90
useVersionFunction · 0.90
useQueryFunction · 0.90
usePageFunction · 0.90
useMainContextFunction · 0.90
sendEventFunction · 0.90

Tested by

no test coverage detected