|
| 1 | +import { autocomplete } from "@algolia/autocomplete-js"; |
| 2 | +import React, { createElement, Fragment, useEffect, useRef } from "react"; |
| 3 | +import { render } from "react-dom"; |
| 4 | + |
| 5 | +import { ListItemPreview } from "./ListItemPreview"; |
| 6 | +import { TableItemPreview } from "./TableItemPreview"; |
| 7 | +import { QuoteItemPreview } from "./QuoteItemPreview"; |
| 8 | +import { ContentPreview } from "./ContentPreview"; |
| 9 | +import { CodeBlockPreview } from "./CodeBlockPreview"; |
| 10 | + |
| 11 | +export function Autocomplete(props) { |
| 12 | + const containerRef = useRef(null); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + if (!containerRef.current) { |
| 16 | + return undefined; |
| 17 | + } |
| 18 | + |
| 19 | + const search = autocomplete({ |
| 20 | + container: containerRef.current, |
| 21 | + debug: true, |
| 22 | + renderer: { createElement, Fragment }, |
| 23 | + renderNoResults({ state }, root) { |
| 24 | + render( |
| 25 | + <div className="aa-NoResults"> |
| 26 | + <p> |
| 27 | + No results for <strong>"{state.query}"</strong>. |
| 28 | + </p> |
| 29 | + </div>, |
| 30 | + root, |
| 31 | + ); |
| 32 | + }, |
| 33 | + render({ children, state, components }, root) { |
| 34 | + const { preview } = state.context; |
| 35 | + render( |
| 36 | + <div className="aa-Grid"> |
| 37 | + <div className="aa-Results aa-Column">{children}</div> |
| 38 | + <div className="aa-Preview aa-Column"> |
| 39 | + {preview && children && ( |
| 40 | + <div className="aa-PreviewSection"> |
| 41 | + <div className="aa-PreviewSectionContent"> |
| 42 | + <div className="aa-PreviewBreadcrumb"> |
| 43 | + <span> |
| 44 | + {preview.urlSegments.level1}{" "} |
| 45 | + {preview.urlSegments.level2 && |
| 46 | + `> ${preview.urlSegments.level2}`} |
| 47 | + </span> |
| 48 | + </div> |
| 49 | + <div className="aa-TitlePreview"> |
| 50 | + <a href={preview.url}> |
| 51 | + <h1> |
| 52 | + <components.Highlight |
| 53 | + hit={preview} |
| 54 | + attribute="title" |
| 55 | + /> |
| 56 | + </h1> |
| 57 | + </a> |
| 58 | + </div> |
| 59 | + <div className="aa-ContentPreview"> |
| 60 | + {preview.schema === "LIST_ITEM" && ( |
| 61 | + <ListItemPreview |
| 62 | + preview={preview} |
| 63 | + components={components} |
| 64 | + /> |
| 65 | + )} |
| 66 | + {preview.schema === "TABLE_ITEM" && |
| 67 | + preview.tableAttributes && |
| 68 | + preview.tableAttributes.headers.length > 0 && ( |
| 69 | + <TableItemPreview |
| 70 | + preview={preview} |
| 71 | + components={components} |
| 72 | + /> |
| 73 | + )} |
| 74 | + {preview.schema === "QUOTE" && ( |
| 75 | + <QuoteItemPreview |
| 76 | + preview={preview} |
| 77 | + components={components} |
| 78 | + /> |
| 79 | + )} |
| 80 | + {preview.schema.startsWith("PARAGRAPH") && ( |
| 81 | + <ContentPreview |
| 82 | + preview={preview} |
| 83 | + components={components} |
| 84 | + /> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + {preview.schema === "PARAGRAPH_WITH_CODE" && ( |
| 88 | + <CodeBlockPreview /> |
| 89 | + )} |
| 90 | + {preview.pageOutlines.length > 0 && ( |
| 91 | + <div className="aa-Outline"> |
| 92 | + <h2 className="aa-OutlineTitle">On this page</h2> |
| 93 | + <ol className="aa-OutlineList"> |
| 94 | + {preview.pageOutlines.map((pageOutline) => ( |
| 95 | + <li |
| 96 | + key={pageOutline.url} |
| 97 | + className="aa-OutlineLink" |
| 98 | + > |
| 99 | + <a href={pageOutline.url}>{pageOutline.title}</a> |
| 100 | + </li> |
| 101 | + ))} |
| 102 | + </ol> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + )} |
| 108 | + </div> |
| 109 | + </div>, |
| 110 | + root, |
| 111 | + ); |
| 112 | + }, |
| 113 | + ...props, |
| 114 | + }); |
| 115 | + |
| 116 | + return () => { |
| 117 | + search.destroy(); |
| 118 | + }; |
| 119 | + }, [props]); |
| 120 | + |
| 121 | + return <div className="aa-AutocompleteSearchbar" ref={containerRef} />; |
| 122 | +} |
0 commit comments