first commit
This commit is contained in:
150
apps/suman-bosna/src/modules/layout/components/search/index.tsx
Normal file
150
apps/suman-bosna/src/modules/layout/components/search/index.tsx
Normal file
@@ -0,0 +1,150 @@
|
||||
"use client"
|
||||
|
||||
import { Search as SearchIcon } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useParams } from "next/navigation"
|
||||
import { useCallback, useEffect, useRef, useState } from "react"
|
||||
import type { SearchClient } from "instantsearch.js"
|
||||
import {
|
||||
Configure,
|
||||
InstantSearch,
|
||||
useHits,
|
||||
useInstantSearch,
|
||||
useSearchBox,
|
||||
} from "react-instantsearch"
|
||||
|
||||
import useSearchSettled from "@lib/hooks/use-search-settled"
|
||||
import useToggleState from "@lib/hooks/use-toggle-state"
|
||||
import { PRODUCT_INDEX_NAME, searchClient } from "@lib/search-client"
|
||||
import SearchDrawer from "./drawer"
|
||||
import SearchHit, { ProductHit } from "./hit"
|
||||
|
||||
const HITS_PER_PAGE = 12
|
||||
const DEBOUNCE_MS = 250
|
||||
|
||||
const SearchPanel = ({ onNavigate }: { onNavigate: () => void }) => {
|
||||
const timer = useRef<number | undefined>(undefined)
|
||||
|
||||
const queryHook = useCallback(
|
||||
(nextQuery: string, search: (value: string) => void) => {
|
||||
window.clearTimeout(timer.current)
|
||||
timer.current = window.setTimeout(() => search(nextQuery), DEBOUNCE_MS)
|
||||
},
|
||||
[]
|
||||
)
|
||||
|
||||
const { query, refine } = useSearchBox({ queryHook })
|
||||
const { items } = useHits<ProductHit>()
|
||||
const { status, error } = useInstantSearch()
|
||||
const { isSettled } = useSearchSettled()
|
||||
|
||||
const [inputValue, setInputValue] = useState(query)
|
||||
|
||||
useEffect(() => () => window.clearTimeout(timer.current), [])
|
||||
|
||||
const hasInput = Boolean(inputValue.trim())
|
||||
const isPending = inputValue.trim() !== query.trim()
|
||||
const hasResults = Boolean(query.trim()) && items.length > 0
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="flex items-center gap-x-3 border-b border-border px-5 py-2">
|
||||
<SearchIcon className="h-4 w-4 shrink-0 text-muted-foreground" />
|
||||
<input
|
||||
type="search"
|
||||
value={inputValue}
|
||||
onChange={(event) => {
|
||||
setInputValue(event.target.value)
|
||||
refine(event.target.value)
|
||||
}}
|
||||
placeholder="Unesite pojam za pretragu..."
|
||||
aria-label="Pretraži proizvode"
|
||||
autoFocus
|
||||
className="w-full bg-transparent py-3 text-sm text-foreground outline-none placeholder:text-muted-foreground"
|
||||
data-testid="search-input"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto p-4">
|
||||
{!hasInput ? (
|
||||
<p
|
||||
className="py-12 text-center text-sm text-muted-foreground"
|
||||
data-testid="search-empty"
|
||||
>
|
||||
Počnite tipkati da pretražite suncobrane, pergole i dodatke.
|
||||
</p>
|
||||
) : status === "error" ? (
|
||||
<p
|
||||
className="py-12 text-center text-sm text-destructive"
|
||||
data-testid="search-error"
|
||||
>
|
||||
Greška pri pretrazi
|
||||
{error?.message ? `: ${error.message}` : "."}
|
||||
</p>
|
||||
) : !isSettled ? (
|
||||
<p
|
||||
className="py-12 text-center text-sm text-muted-foreground"
|
||||
data-testid="search-loading"
|
||||
>
|
||||
Pretražujem…
|
||||
</p>
|
||||
) : hasResults ? (
|
||||
<ul className="divide-y divide-border/60" data-testid="search-results">
|
||||
{items.map((hit) => (
|
||||
<SearchHit key={hit.objectID} hit={hit} onNavigate={onNavigate} />
|
||||
))}
|
||||
</ul>
|
||||
) : isPending || !query.trim() ? (
|
||||
<p
|
||||
className="py-12 text-center text-sm text-muted-foreground"
|
||||
data-testid="search-loading"
|
||||
>
|
||||
Pretražujem…
|
||||
</p>
|
||||
) : (
|
||||
<p
|
||||
className="py-12 text-center text-sm text-muted-foreground"
|
||||
data-testid="search-no-results"
|
||||
>
|
||||
Nema rezultata za upit "{query}"
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const Search = () => {
|
||||
const { state: isOpen, open, close } = useToggleState()
|
||||
const { countryCode } = useParams<{ countryCode: string }>()
|
||||
|
||||
return (
|
||||
<>
|
||||
<Link
|
||||
href={`/${countryCode}/search`}
|
||||
onClick={(event) => {
|
||||
event.preventDefault()
|
||||
open()
|
||||
}}
|
||||
aria-label="Pretraga"
|
||||
className="inline-flex items-center justify-center h-9 w-9 rounded-md text-primary-foreground/90 hover:text-white hover:bg-white/10 transition-colors"
|
||||
data-testid="nav-search-button"
|
||||
>
|
||||
<SearchIcon className="h-4 w-4" />
|
||||
</Link>
|
||||
|
||||
<SearchDrawer isOpen={isOpen} close={close}>
|
||||
<InstantSearch
|
||||
indexName={PRODUCT_INDEX_NAME}
|
||||
searchClient={searchClient as unknown as SearchClient}
|
||||
future={{ preserveSharedStateOnUnmount: true }}
|
||||
>
|
||||
<Configure hitsPerPage={HITS_PER_PAGE} />
|
||||
<SearchPanel onNavigate={close} />
|
||||
</InstantSearch>
|
||||
</SearchDrawer>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default Search
|
||||
Reference in New Issue
Block a user