|
| 1 | +import { compositionClient } from '@algolia/composition'; |
| 2 | +import React from 'react'; |
| 3 | +import { |
| 4 | + Configure, |
| 5 | + EXPERIMENTAL_Autocomplete, |
| 6 | + Highlight, |
| 7 | + Hits, |
| 8 | + InstantSearch, |
| 9 | + Pagination, |
| 10 | +} from 'react-instantsearch'; |
| 11 | + |
| 12 | +import 'instantsearch.css/themes/satellite.css'; |
| 13 | +import './App.css'; |
| 14 | + |
| 15 | +import type { Hit as AlgoliaHit } from 'instantsearch.js'; |
| 16 | + |
| 17 | +const searchClient = compositionClient( |
| 18 | + '9HILZG6EJK', |
| 19 | + '65b3e0bb064c4172c4810fb2459bebd1' |
| 20 | +); |
| 21 | + |
| 22 | +const compositionID = 'comp1774447423386___products'; |
| 23 | + |
| 24 | +type ProductHit = AlgoliaHit<{ |
| 25 | + title: string; |
| 26 | + author?: string[]; |
| 27 | + largeImage: string; |
| 28 | +}>; |
| 29 | + |
| 30 | +type FashionHit = AlgoliaHit<{ |
| 31 | + name: string; |
| 32 | + image: string; |
| 33 | + brand: string; |
| 34 | + price: number; |
| 35 | + currency: string; |
| 36 | +}>; |
| 37 | + |
| 38 | +type AmazonHit = AlgoliaHit<{ |
| 39 | + product_title: string; |
| 40 | + product_brand: string; |
| 41 | +}>; |
| 42 | + |
| 43 | +export function App() { |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + <header className="header"> |
| 47 | + <h1 className="header-title"> |
| 48 | + <a href="/">Autocomplete multifeed</a> |
| 49 | + </h1> |
| 50 | + <p className="header-subtitle"> |
| 51 | + using{' '} |
| 52 | + <a href="https://github.com/algolia/instantsearch/tree/master/packages/react-instantsearch"> |
| 53 | + React InstantSearch |
| 54 | + </a> |
| 55 | + </p> |
| 56 | + </header> |
| 57 | + |
| 58 | + <div className="container"> |
| 59 | + <InstantSearch |
| 60 | + // @ts-expect-error compositionClient return type doesn't fully match SearchClient yet |
| 61 | + searchClient={searchClient} |
| 62 | + compositionID={compositionID} |
| 63 | + insights={true} |
| 64 | + > |
| 65 | + <Configure hitsPerPage={8} /> |
| 66 | + |
| 67 | + <main> |
| 68 | + <div className="container-wrapper"> |
| 69 | + <div className="search-panel"> |
| 70 | + <div className="search-panel__results"> |
| 71 | + <EXPERIMENTAL_Autocomplete |
| 72 | + placeholder="Search for products" |
| 73 | + feeds={[ |
| 74 | + { |
| 75 | + feedID: 'products', |
| 76 | + headerComponent: () => ( |
| 77 | + <> |
| 78 | + <span className="ais-AutocompleteIndexHeaderTitle"> |
| 79 | + Products |
| 80 | + </span> |
| 81 | + <span className="ais-AutocompleteIndexHeaderLine" /> |
| 82 | + </> |
| 83 | + ), |
| 84 | + itemComponent: ({ item, onSelect }) => ( |
| 85 | + <div onClick={onSelect}> |
| 86 | + {(item as ProductHit).title} |
| 87 | + </div> |
| 88 | + ), |
| 89 | + }, |
| 90 | + { |
| 91 | + feedID: 'Fashion', |
| 92 | + headerComponent: () => ( |
| 93 | + <> |
| 94 | + <span className="ais-AutocompleteIndexHeaderTitle"> |
| 95 | + Fashion |
| 96 | + </span> |
| 97 | + <span className="ais-AutocompleteIndexHeaderLine" /> |
| 98 | + </> |
| 99 | + ), |
| 100 | + itemComponent: ({ item, onSelect }) => ( |
| 101 | + <div onClick={onSelect}> |
| 102 | + {(item as FashionHit).name} |
| 103 | + </div> |
| 104 | + ), |
| 105 | + }, |
| 106 | + { |
| 107 | + feedID: 'Amazon', |
| 108 | + headerComponent: () => ( |
| 109 | + <> |
| 110 | + <span className="ais-AutocompleteIndexHeaderTitle"> |
| 111 | + Amazon |
| 112 | + </span> |
| 113 | + <span className="ais-AutocompleteIndexHeaderLine" /> |
| 114 | + </> |
| 115 | + ), |
| 116 | + itemComponent: ({ item, onSelect }) => ( |
| 117 | + <div onClick={onSelect}> |
| 118 | + {(item as AmazonHit).product_title} |
| 119 | + </div> |
| 120 | + ), |
| 121 | + }, |
| 122 | + ]} |
| 123 | + showRecent={{ |
| 124 | + headerComponent: () => ( |
| 125 | + <> |
| 126 | + <span className="ais-AutocompleteIndexHeaderTitle"> |
| 127 | + Recent Searches |
| 128 | + </span> |
| 129 | + <span className="ais-AutocompleteIndexHeaderLine" /> |
| 130 | + </> |
| 131 | + ), |
| 132 | + }} |
| 133 | + /> |
| 134 | + |
| 135 | + <Hits hitComponent={HitComponent} /> |
| 136 | + |
| 137 | + <div className="pagination"> |
| 138 | + <Pagination /> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </main> |
| 144 | + </InstantSearch> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +function HitComponent({ hit }: { hit: ProductHit }) { |
| 151 | + return ( |
| 152 | + <article> |
| 153 | + <h1> |
| 154 | + <Highlight attribute="title" hit={hit} /> |
| 155 | + </h1> |
| 156 | + <p>{hit.author?.join(', ')}</p> |
| 157 | + </article> |
| 158 | + ); |
| 159 | +} |
0 commit comments