@@ -4,8 +4,48 @@ import "./search.scss";
4
4
import "./algolia.css" ;
5
5
6
6
class Search extends React . Component {
7
+ constructor ( props ) {
8
+ super ( props ) ;
9
+
10
+ this . state = {
11
+ enabled : true
12
+ } ;
13
+ }
14
+ componentDidMount ( ) {
15
+ if ( window . docsearch ) {
16
+ window . docsearch ( {
17
+ // the following information is copied from the gatsby-config.js file
18
+ // if you change anything below, make sure to also change it in that
19
+ // file. We're duplicating this code to avoid a race condition in the
20
+ // MutationObserver logic, where the docsearch script (npm package)
21
+ // was loaded *before* this component was loaded.
22
+ apiKey : "acfb7def12803db2cd4ac0539b2b571a" ,
23
+ indexName : "hackerone" ,
24
+ inputSelector : "#algolia-doc-search" ,
25
+ transformData : suggestions => {
26
+ // ideally, we'd have an `id` or `name` attribute for all headers, so that DocSearch
27
+ // properly indexes the anchors. Since we don't have that yet, we've went with this
28
+ // temporary workaround to remove the ___gatsby anchor from the URL. This code can be
29
+ // removed when the appropriate attributes are added. Ref T19586.
30
+ return suggestions . map ( suggestion => {
31
+ delete suggestion . anchor ;
32
+
33
+ suggestion . url = suggestion . url . replace ( / # g a t s b y - f o c u s - w r a p p e r $ / , "" ) ;
34
+
35
+ return suggestion ;
36
+ } ) ;
37
+ }
38
+ } ) ;
39
+ } else {
40
+ console . warn ( "Search has failed to load and now is being disabled" ) ;
41
+ this . setState ( { enabled : false } ) ;
42
+ }
43
+ }
44
+
7
45
render ( ) {
8
- return (
46
+ const { enabled } = this . state ;
47
+
48
+ return enabled ? (
9
49
< form className = "search" >
10
50
< input
11
51
className = "search__input"
@@ -15,7 +55,7 @@ class Search extends React.Component {
15
55
aria-label = "Search docs"
16
56
/>
17
57
</ form >
18
- ) ;
58
+ ) : null ;
19
59
}
20
60
}
21
61
0 commit comments