|
| 1 | +import algoliasearch from 'algoliasearch/lite'; |
| 2 | +import type { InstantSearchServerState } from 'react-instantsearch-hooks-web'; |
| 3 | +import { |
| 4 | + DynamicWidgets, |
| 5 | + Hits, |
| 6 | + InstantSearch, |
| 7 | + InstantSearchSSRProvider, |
| 8 | + Pagination, |
| 9 | + RefinementList, |
| 10 | + SearchBox, |
| 11 | + useInstantSearch, |
| 12 | +} from 'react-instantsearch-hooks-web'; |
| 13 | +import { getServerState } from 'react-instantsearch-hooks-server'; |
| 14 | +import { history } from 'instantsearch.js/cjs/lib/routers/index.js'; |
| 15 | +import instantSearchStyles from 'instantsearch.css/themes/satellite-min.css'; |
| 16 | + |
| 17 | +import type { LinksFunction, LoaderFunction } from '@remix-run/node'; |
| 18 | +import { json } from '@remix-run/node'; |
| 19 | +import { useLoaderData } from '@remix-run/react'; |
| 20 | + |
| 21 | +import { Hit } from '../../components/Hit'; |
| 22 | +import { Panel } from '../../components/Panel'; |
| 23 | +import { ScrollTo } from '../../components/ScrollTo'; |
| 24 | +import { NoResultsBoundary } from '../../components/NoResultsBoundary'; |
| 25 | +import { SearchErrorToast } from '../../components/SearchErrorToast'; |
| 26 | + |
| 27 | +import tailwindStyles from '../tailwind.css'; |
| 28 | + |
| 29 | +const searchClient = algoliasearch( |
| 30 | + 'latency', |
| 31 | + '6be0576ff61c053d5f9a3225e2a90f76' |
| 32 | +); |
| 33 | + |
| 34 | +export const links: LinksFunction = () => [ |
| 35 | + { rel: 'stylesheet', href: instantSearchStyles }, |
| 36 | + { rel: 'stylesheet', href: tailwindStyles }, |
| 37 | +]; |
| 38 | + |
| 39 | +export const loader: LoaderFunction = async ({ request }) => { |
| 40 | + const serverUrl = request.url; |
| 41 | + const serverState = await getServerState(<Search serverUrl={serverUrl} />); |
| 42 | + |
| 43 | + return json({ |
| 44 | + serverState, |
| 45 | + serverUrl, |
| 46 | + }); |
| 47 | +}; |
| 48 | + |
| 49 | +type SearchProps = { |
| 50 | + serverState?: InstantSearchServerState; |
| 51 | + serverUrl?: string; |
| 52 | +}; |
| 53 | + |
| 54 | +function Search({ serverState, serverUrl }: SearchProps) { |
| 55 | + return ( |
| 56 | + <InstantSearchSSRProvider {...serverState}> |
| 57 | + <InstantSearch |
| 58 | + searchClient={searchClient} |
| 59 | + indexName="instant_search" |
| 60 | + routing={{ |
| 61 | + router: history({ |
| 62 | + getLocation() { |
| 63 | + if (typeof window === 'undefined') { |
| 64 | + return new URL(serverUrl!) as unknown as Location; |
| 65 | + } |
| 66 | + |
| 67 | + return window.location; |
| 68 | + }, |
| 69 | + }), |
| 70 | + }} |
| 71 | + > |
| 72 | + <SearchErrorToast /> |
| 73 | + |
| 74 | + <ScrollTo className="max-w-6xl p-4 flex gap-4 m-auto"> |
| 75 | + <div> |
| 76 | + <DynamicWidgets fallbackComponent={FallbackComponent} /> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div className="flex flex-col w-full gap-8"> |
| 80 | + <SearchBox /> |
| 81 | + <NoResultsBoundary fallback={<NoResults />}> |
| 82 | + <Hits |
| 83 | + hitComponent={Hit} |
| 84 | + classNames={{ |
| 85 | + list: 'grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4', |
| 86 | + item: 'p-2 w-full', |
| 87 | + }} |
| 88 | + /> |
| 89 | + <Pagination className="flex self-center" /> |
| 90 | + </NoResultsBoundary> |
| 91 | + </div> |
| 92 | + </ScrollTo> |
| 93 | + </InstantSearch> |
| 94 | + </InstantSearchSSRProvider> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +function FallbackComponent({ attribute }: { attribute: string }) { |
| 99 | + return ( |
| 100 | + <Panel header={attribute}> |
| 101 | + <RefinementList attribute={attribute} /> |
| 102 | + </Panel> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +function NoResults() { |
| 107 | + const { indexUiState } = useInstantSearch(); |
| 108 | + |
| 109 | + return ( |
| 110 | + <div> |
| 111 | + <p> |
| 112 | + No results for <q>{indexUiState.query}</q>. |
| 113 | + </p> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +export default function HomePage() { |
| 119 | + const { serverState, serverUrl } = useLoaderData(); |
| 120 | + |
| 121 | + return <Search serverState={serverState} serverUrl={serverUrl} />; |
| 122 | +} |
0 commit comments