-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
website: Add Algolia Docsearch (#2546)
* Initial spike * Cleanup * manypkg fix * Remove unused vars * hardcode keys * Add docsearch * Remove old search * Remove dotenv * Remove comment * Sidebar cleanup * Fix navigate * Remove getHash
- Loading branch information
1 parent
abac6ad
commit 1b188f7
Showing
15 changed files
with
132 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,50 @@ | ||
/** @jsx jsx */ | ||
|
||
import { useState, useEffect, useCallback } from 'react'; | ||
import debounce from 'lodash.debounce'; | ||
import { jsx } from '@emotion/core'; | ||
import Select from '@arch-ui/select'; | ||
import { useEffect, useRef } from 'react'; | ||
import { navigate } from 'gatsby'; | ||
import { Input } from '@arch-ui/input'; | ||
|
||
import { getResults } from '../utils/search'; | ||
import { addCallback } from '../utils/async-load-search'; | ||
import { algoliaStyles } from '../utils/algolia-styles'; | ||
|
||
const buildOptions = arr => { | ||
let ops = []; | ||
|
||
arr.forEach(i => { | ||
if (!i.navGroup) { | ||
return; | ||
} | ||
|
||
const groupLabel = i.navGroup.replace('-', ' '); | ||
|
||
if (!ops.some(o => o.label === groupLabel)) { | ||
ops.push({ | ||
label: groupLabel, | ||
options: [], | ||
}); | ||
} | ||
|
||
ops.find(o => o.label === groupLabel).options.push(i); | ||
}); | ||
|
||
return ops; | ||
}; | ||
const searchId = 'algolia-doc-search'; | ||
|
||
export const Search = () => { | ||
let [query, setQuery] = useState(''); | ||
let [results, setResults] = useState([]); | ||
|
||
const setQueryDebounced = useCallback( | ||
debounce(value => setQuery(value), 200), | ||
[setQuery] | ||
); | ||
const inputRef = useRef(); | ||
|
||
useEffect(() => { | ||
let cancelled = false; | ||
|
||
if (!query) { | ||
return; | ||
} | ||
|
||
getResults(query, { limit: 20 }).then(queryResults => { | ||
if (cancelled) { | ||
return; | ||
addCallback(loaded => { | ||
if (loaded) { | ||
window.docsearch({ | ||
apiKey: '211e94c001e6b4c6744ae72fb252eaba', | ||
indexName: 'keystonejs', | ||
inputSelector: `#${searchId}`, | ||
handleSelected: (input, e, suggestion) => { | ||
e.preventDefault(); | ||
input.setVal(''); | ||
input.close(); | ||
inputRef.current.blur(); | ||
const url = suggestion.url.replace(/https*:\/\/[www.]*keystonejs\.com/, ''); | ||
navigate(url); | ||
}, | ||
}); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn('Search has failed to load and is now disabled'); | ||
} | ||
|
||
setResults(buildOptions(queryResults.results)); | ||
}); | ||
|
||
return () => { | ||
cancelled = true; | ||
}; | ||
}, [query]); | ||
}, []); | ||
|
||
return ( | ||
<Select | ||
key="select" | ||
components={{ Control, DropdownIndicator, IndicatorSeparator, Input }} | ||
placeholder="Search..." | ||
options={results} | ||
value={null} | ||
onInputChange={setQueryDebounced} | ||
openMenuOnClick={false} | ||
tabSelectsValue={false} | ||
onChange={result => { | ||
setQueryDebounced.cancel(); | ||
navigate(result.slug); | ||
setQuery(''); | ||
}} | ||
css={{ zIndex: 2 }} | ||
filterOption={filterOption} | ||
getOptionValue={result => result.slug} | ||
getOptionLabel={result => result.title} | ||
noOptionsMessage={() => (query ? 'No results found' : 'Enter a search term')} | ||
/> | ||
); | ||
}; | ||
|
||
// ============================== | ||
// Styled Components | ||
// ============================== | ||
|
||
const DropdownIndicator = ({ innerProps, isFocused }) => ( | ||
<div css={{ padding: '4px 6px', opacity: isFocused ? 0.8 : 0.6 }} {...innerProps}> | ||
<svg width="24" height="24" viewBox="0 0 24 24" focusable="false" role="presentation"> | ||
<path | ||
d="M14.823 15.883a5.5 5.5 0 1 1 1.06-1.06l2.647 2.647c.293.293.53.59 0 1.06-.53.47-.767.293-1.06 0l-2.647-2.647zM11.5 15.5a4 4 0 1 0 0-8 4 4 0 0 0 0 8z" | ||
fill="currentColor" | ||
<form css={[algoliaStyles]}> | ||
<Input | ||
ref={inputRef} | ||
id={searchId} | ||
type="search" | ||
placeholder="Search..." | ||
aria-label="Search..." | ||
/> | ||
</svg> | ||
</div> | ||
); | ||
const Control = ({ children, innerProps, innerRef, isFocused }) => { | ||
const backgroundColor = isFocused ? 'rgba(9, 30, 66, 0.1)' : 'rgba(9, 30, 66, 0.05)'; | ||
return ( | ||
<div | ||
ref={innerRef} | ||
css={{ | ||
backgroundColor, | ||
boxSizing: 'border-box', | ||
borderRadius: 4, | ||
display: 'flex', | ||
paddingLeft: 2, | ||
paddingRight: 2, | ||
}} | ||
{...innerProps} | ||
> | ||
{children} | ||
</div> | ||
</form> | ||
); | ||
}; | ||
const Input = ({ | ||
className, | ||
cx, | ||
getStyles, | ||
innerRef, | ||
isDisabled, | ||
isHidden, | ||
selectProps, | ||
theme, | ||
...props | ||
}) => ( | ||
<div | ||
css={{ | ||
margin: 2, | ||
paddingBottom: 2, | ||
paddingTop: 2, | ||
visibility: isDisabled ? 'hidden' : 'visible', | ||
}} | ||
> | ||
<input | ||
ref={innerRef} | ||
css={{ | ||
background: 0, | ||
border: 0, | ||
color: 'inherit', | ||
fontSize: 'inherit', | ||
opacity: isHidden ? 0 : 1, | ||
outline: 0, | ||
padding: 0, | ||
|
||
'&.focus-visible': { | ||
outline: 0, | ||
}, | ||
}} | ||
{...props} | ||
/> | ||
</div> | ||
); | ||
const IndicatorSeparator = null; | ||
|
||
// ============================== | ||
// Utilities | ||
// ============================== | ||
|
||
// remove options that aren't present in the sidebar | ||
let filterOption = ({ data }) => Boolean(data.navGroup); |
Oops, something went wrong.