You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
import {
|
|
useCallback,
|
|
useMemo,
|
|
useRef,
|
|
} from 'react'
|
|
|
|
import debounce from 'lodash/debounce'
|
|
|
|
import { getSearchItems } from 'requests'
|
|
|
|
import { useRequest } from 'hooks'
|
|
|
|
import { SEARCH_DELAY } from 'features/Search/config'
|
|
import type { NormalizedSearchResults } from 'features/Search/helpers'
|
|
import { normalizeItems } from 'features/Search/helpers'
|
|
|
|
type Updater = (results: NormalizedSearchResults) => void
|
|
|
|
export const useSearchRequest = (updater: Updater) => {
|
|
const abortRef = useRef<AbortController | null>(null)
|
|
|
|
const {
|
|
isFetching,
|
|
request: searchItemsRequest,
|
|
} = useRequest(getSearchItems)
|
|
|
|
const search = useMemo(
|
|
() => debounce((query: string) => {
|
|
const abortController = new AbortController()
|
|
searchItemsRequest(query, abortController.signal).then((searchResult) => {
|
|
abortRef.current = null
|
|
updater(normalizeItems(searchResult))
|
|
})
|
|
abortRef.current = abortController
|
|
}, SEARCH_DELAY),
|
|
[searchItemsRequest, updater],
|
|
)
|
|
|
|
const cancelSearch = useCallback(() => {
|
|
abortRef.current?.abort()
|
|
abortRef.current = null
|
|
}, [])
|
|
|
|
return {
|
|
cancelSearch,
|
|
isFetching,
|
|
search,
|
|
}
|
|
}
|
|
|