import { useMemo } from "react" ; import { OptionTypeBase } from "react-select/src/types" ; import { useSearchParams } from "src/shared/hooks/useSearchParams" ; import { SearchSort } from "src/shared/constants/search" ; const defaultOptions = [ { label : "alphabetically" , value : SearchSort . ALPHABETICAL } , { label : "by review count" , value : SearchSort . NUM_REVIEWS } , { label : "by rating" , value : SearchSort . RATING } , { label : "by review date" , value : SearchSort . CREATION_DATE } , ] ; const getOptionFromValue = ( options , value ) => options . filter ( ( option ) => option . value === value ) [ 0 ] ; export const useSearchSort = ( availableSort ? ) => { const { searchSort , setSearchSort } = useSearchParams ( ) ; const options = useMemo ( ( ) => availableSort ? defaultOptions . filter ( ( option ) => availableSort . includes ( option . value ) ) : defaultOptions , [ availableSort ] ) ; return { options , value : searchSort && getOptionFromValue ( options , searchSort ) , onChange : ( option ? ) => setSearchSort ( option && option . value ) , } ; } ;