1
0
Fork 0
freesewing/sites/dev/components/search.mjs

70 lines
2.3 KiB
JavaScript
Raw Normal View History

import algoliasearch from 'algoliasearch/lite'
2023-05-24 16:18:59 +02:00
import { InstantSearch, SearchBox, Hits, Highlight, Snippet } from 'react-instantsearch-hooks-web'
import { siteConfig } from 'site/site.config.mjs'
import Link from 'next/link'
2023-05-24 16:18:59 +02:00
import { ClearIcon } from 'shared/components/icons.mjs'
export const ns = ['search']
const searchClient = algoliasearch(siteConfig.algolia.appId, siteConfig.algolia.apiKey)
2022-11-23 21:42:22 +01:00
const Hit = (props) => (
<div
className={`
2021-12-31 10:15:51 +01:00
px-2 py-1 ounded mt-1
text-base text-base-content
sm:rounded
lg:px-4 lg:py-2
2023-05-24 16:18:59 +02:00
border border-solid border-secondary
bg-secondary bg-opacity-10
hover:bg-secondary hover:bg-opacity-30
`}
>
2022-12-04 13:19:42 +01:00
<Link href={props.hit.page} className="flex flex-row justify-between gap-2">
<span className="text-base sm:text-xl font-bold leading-5">
{props.hit._highlightResult?.title ? (
2023-05-24 16:18:59 +02:00
<Highlight hit={props.hit} attribute="title" />
2022-12-04 13:19:42 +01:00
) : (
props.hit.title
)}
</span>
2023-05-24 16:18:59 +02:00
<div>
<badge className="badge badge-primary uppercase mr-1 badge-sm">{props.hit.type}</badge>
<badge className="badge badge-secondary uppercase badge-sm">
{props.hit.page.split('/')[1]}
</badge>
</div>
</Link>
2023-05-24 16:18:59 +02:00
{props.hit._snippetResult?.body?.value ? (
2022-12-04 13:19:42 +01:00
<Link href={props.hit.page} className="text-sm sm:text-base block py-1">
2023-05-24 16:18:59 +02:00
<Snippet hit={props.hit} attribute="body" />
2021-12-31 10:15:51 +01:00
</Link>
2023-05-24 16:18:59 +02:00
) : (
2022-12-04 13:19:42 +01:00
<Link href={props.hit.page} className="text-xs sm:text-sm block opacity-70">
2023-05-24 16:18:59 +02:00
<Highlight hit={props.hit} attribute="body" />
2021-12-31 10:15:51 +01:00
</Link>
)}
</div>
)
2023-05-24 16:18:59 +02:00
export const Search = () => {
return (
2023-05-24 16:18:59 +02:00
<InstantSearch indexName={siteConfig.algolia.index} searchClient={searchClient}>
<SearchBox
searchAsYouType={true}
autoFocus={true}
classNames={{
root: 'relative mb-4',
input: 'input lg:input-lg input-bordered input-neutral w-full pr-16',
reset:
'absolute right-0 top-0 rounded-l-none btn btn-neutral lg:btn-lg text-neutral-content',
submit: 'absolute right-0 top-0 rounded-l-none btn btn-neutral lg:btn-lg',
}}
resetIconComponent={() => <ClearIcon />}
/>
{/* Widgets */}
<Hits hitComponent={Hit} />
2021-12-31 10:15:51 +01:00
</InstantSearch>
)
}