11import React , { useMemo , useEffect , useState , createRef } from 'react' ;
2- import { graphql , useStaticQuery } from 'gatsby' ;
32import { FormattedMessage } from 'react-intl' ;
43import { LocalizedLink as Link } from 'gatsby-theme-i18n' ;
54import TravelExploreIcon from '@mui/icons-material/TravelExplore' ;
65import CloseIcon from '@mui/icons-material/Close' ;
76import classNames from 'classnames' ;
87import { AnimatePresence , motion } from 'framer-motion' ;
98import { useClickOutside } from 'react-click-outside-hook' ;
10- import { Index , SerialisedIndexData } from 'elasticlunr' ;
119import SectionTitle from '../SectionTitle' ;
1210import { SearchResult } from '../../types' ;
13- import styles from './index.module.scss' ;
1411import useKeyPress from '../../hooks/useKeyPress' ;
12+ import { useSearchResults } from '../../hooks/useSearchResults' ;
13+ import styles from './index.module.scss' ;
1514
1615const containerTransition = { type : 'spring' , damping : 22 , stiffness : 150 } ;
1716const containerVariants = {
@@ -32,46 +31,22 @@ const containerVariants = {
3231const MotionCloseIcon = motion ( CloseIcon ) ;
3332
3433const SearchBar = ( ) : JSX . Element => {
35- const { siteSearchIndex } = useStaticQuery ( graphql `
36- query localSearchLearnPages {
37- siteSearchIndex {
38- index
39- }
40- }
41- ` ) ;
42-
43- const resultData = siteSearchIndex . index as SerialisedIndexData < SearchResult > ;
44-
4534 const [ query , setQuery ] = useState ( '' ) ;
46- const [ results , setResults ] = useState < SearchResult [ ] > ( [ ] ) ;
4735 const searchInputRef = createRef < HTMLInputElement > ( ) ;
4836
49- const isEmpty = ! results || results . length === 0 ;
5037 const [ isExpanded , setExpanded ] = useState ( false ) ;
5138 const [ parentRef , isClickedOutside ] = useClickOutside ( ) ;
5239
53- const storeIndex = useMemo (
54- ( ) => Index . load < SearchResult > ( resultData ) ,
55- [ resultData ]
56- ) ;
40+ const search = useSearchResults ( ) ;
5741
58- const searchForResults = ( currentQuery : string ) => {
59- const currentResults = storeIndex
60- . search ( currentQuery , { expand : true } )
61- . map ( ( { ref } ) => storeIndex . documentStore . getDoc ( ref ) as SearchResult ) ;
42+ const results = useMemo ( ( ) => search ( query ) , [ query , search ] ) ;
6243
63- setResults ( currentResults . slice ( 0 , 20 ) ) ;
64- } ;
44+ const isEmpty = ! results || results . length === 0 ;
6545
6646 const changeHandler = ( e : React . ChangeEvent < HTMLInputElement > ) => {
6747 e . preventDefault ( ) ;
6848
69- if ( e . target . value === '' ) {
70- setResults ( [ ] ) ;
71- }
72-
7349 setQuery ( e . target . value ) ;
74- searchForResults ( e . target . value ) ;
7550 } ;
7651
7752 const expandContainer = ( ) => {
@@ -81,7 +56,6 @@ const SearchBar = (): JSX.Element => {
8156 const collapseContainer = ( ) => {
8257 setExpanded ( false ) ;
8358 setQuery ( '' ) ;
84- setResults ( [ ] ) ;
8559 } ;
8660
8761 useEffect ( ( ) => {
@@ -179,14 +153,19 @@ const SearchBar = (): JSX.Element => {
179153 { ! isEmpty && (
180154 < ul >
181155 { results . map ( ( result : SearchResult ) => {
182- const sectionPath = result . displayTitle
183- ? [ 'home' , result . category , result . title ]
184- : [ 'home' , result . category ] ;
156+ const sectionPath =
157+ result . category === 'api'
158+ ? [ 'home' , result . category , result . title ]
159+ : [ 'home' , result . category ] ;
160+
161+ const displayTitle = result . displayTitle || result . title ;
185162
186163 return (
187164 < li key = { result . id } >
188165 < Link to = { result . slug } >
189- < span > { result . displayTitle || result . title } </ span >
166+ { ( result . wrapInCode && < code > { displayTitle } </ code > ) || (
167+ < span > { displayTitle } </ span >
168+ ) }
190169 </ Link >
191170 < SectionTitle path = { sectionPath } />
192171 </ li >
0 commit comments