-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin docs to algolia search index
Also move search bar to primary navigation and improve the design. Committed-by: Eduard Heimbuch <[email protected]>
- Loading branch information
Showing
11 changed files
with
172 additions
and
36 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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import SEO from "../components/SEO"; | ||
import React, { useEffect, useMemo, useRef, useState } from "react"; | ||
import Title from "../components/Title"; | ||
import PageContainer from "../layout/PageContainer"; | ||
import { InstantSearch } from "react-instantsearch-dom"; | ||
import algoliasearch from "algoliasearch/lite"; | ||
import SearchResult from "../components/search/SearchResult"; | ||
import SearchBox from "../components/search/SearchBox"; | ||
import qs from "qs"; | ||
import { navigate } from "@reach/router"; | ||
import styled from "styled-components"; | ||
|
||
const DEBOUNCE_TIME = 400; | ||
|
||
const createURL = state => `?${qs.stringify(state)}`; | ||
|
||
const searchStateToUrl = searchState => | ||
searchState ? createURL(searchState) : ""; | ||
|
||
const urlToSearchState = ({ search }) => qs.parse(search.slice(1)); | ||
|
||
const SEARCH_INDEX = { | ||
name: process.env.GATSBY_ALGOLIA_INDEX || "Pages", | ||
title: process.env.GATSBY_ALGOLIA_INDEX || "Pages", | ||
}; | ||
|
||
const StyledResult = styled(SearchResult)` | ||
.HitCount { | ||
display: flex; | ||
justify-content: flex-end; | ||
} | ||
hr { | ||
margin: 1rem 0; | ||
} | ||
`; | ||
|
||
const SearchPage = ({ location }) => { | ||
const [searchState, setSearchState] = useState(urlToSearchState(location)); | ||
const debouncedSetStateRef = useRef(null); | ||
const searchClient = useMemo( | ||
() => | ||
algoliasearch( | ||
process.env.GATSBY_ALGOLIA_APP_ID, | ||
process.env.GATSBY_ALGOLIA_SEARCH_KEY | ||
), | ||
[] | ||
); | ||
|
||
function onSearchStateChange(updatedSearchState) { | ||
clearTimeout(debouncedSetStateRef.current); | ||
|
||
debouncedSetStateRef.current = setTimeout( | ||
() => navigate(searchStateToUrl(updatedSearchState)), | ||
DEBOUNCE_TIME | ||
); | ||
|
||
setSearchState(updatedSearchState); | ||
} | ||
|
||
useEffect(() => { | ||
setSearchState(urlToSearchState(location)); | ||
}, [location]); | ||
|
||
return ( | ||
<PageContainer> | ||
<SEO title="Search" /> | ||
<Title>Search</Title> | ||
<InstantSearch | ||
searchClient={searchClient} | ||
indexName={SEARCH_INDEX.name} | ||
searchState={searchState} | ||
onSearchStateChange={onSearchStateChange} | ||
createURL={createURL} | ||
> | ||
<SearchBox className="mb-4" /> | ||
<StyledResult indices={[SEARCH_INDEX]} /> | ||
</InstantSearch> | ||
</PageContainer> | ||
); | ||
}; | ||
|
||
export default SearchPage; |
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 |
---|---|---|
|
@@ -10804,7 +10804,7 @@ [email protected]: | |
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.6.tgz#26ed3c8243a431b2924aca84cc90471f35d5a0ee" | ||
integrity sha512-TIRk4aqYLNoJUbd+g2lEdz5kLWIuTMRagAXxl78Q0RiVjAOugHmeKNGdd3cwo/ktpf9aL9epCfFqWDEKysUlLQ== | ||
|
||
qs@^6.9.4: | ||
qs@^6.11.0, qs@^6.9.4: | ||
version "6.11.0" | ||
resolved "https://registry.yarnpkg.com/qs/-/qs-6.11.0.tgz#fd0d963446f7a65e1367e01abd85429453f0c37a" | ||
integrity sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q== | ||
|