2022-05-22 08:36:05 +00:00
|
|
|
import { defaults, debounce } from 'lodash'
|
2022-01-02 22:46:04 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-02-24 04:57:59 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2022-05-09 17:38:33 +00:00
|
|
|
import { useSearchBox } from 'react-instantsearch-hooks-web'
|
2022-06-16 16:49:57 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-03-13 21:46:09 +00:00
|
|
|
const MARKETS_SORT = 'markets_sort'
|
|
|
|
|
2022-01-02 22:46:04 +00:00
|
|
|
export type Sort =
|
|
|
|
| 'newest'
|
2022-02-04 22:04:30 +00:00
|
|
|
| 'oldest'
|
2022-01-02 22:46:04 +00:00
|
|
|
| 'most-traded'
|
2022-01-24 06:31:52 +00:00
|
|
|
| '24-hour-vol'
|
2022-05-21 17:44:51 +00:00
|
|
|
| 'close-date'
|
|
|
|
| 'resolve-date'
|
|
|
|
| 'last-updated'
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
export function useInitialQueryAndSort(options?: {
|
2022-03-13 21:46:09 +00:00
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}) {
|
2022-05-22 08:36:05 +00:00
|
|
|
const { defaultSort, shouldLoadFromStorage } = defaults(options, {
|
2022-03-19 16:34:22 +00:00
|
|
|
defaultSort: '24-hour-vol',
|
2022-03-13 21:46:09 +00:00
|
|
|
shouldLoadFromStorage: true,
|
|
|
|
})
|
2022-01-02 22:46:04 +00:00
|
|
|
const router = useRouter()
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
const [initialSort, setInitialSort] = useState<Sort | undefined>(undefined)
|
|
|
|
const [initialQuery, setInitialQuery] = useState('')
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// If there's no sort option, then set the one from localstorage
|
|
|
|
if (router.isReady) {
|
|
|
|
const { s: sort, q: query } = router.query as {
|
|
|
|
q?: string
|
|
|
|
s?: Sort
|
|
|
|
}
|
|
|
|
|
|
|
|
setInitialQuery(query ?? '')
|
|
|
|
|
|
|
|
if (!sort && shouldLoadFromStorage) {
|
|
|
|
console.log('ready loading from storage ', sort ?? defaultSort)
|
|
|
|
const localSort = localStorage.getItem(MARKETS_SORT) as Sort
|
|
|
|
if (localSort) {
|
|
|
|
router.query.s = localSort
|
|
|
|
// Use replace to not break navigating back.
|
|
|
|
router.replace(router, undefined, { shallow: true })
|
|
|
|
}
|
|
|
|
setInitialSort(localSort ?? defaultSort)
|
|
|
|
} else {
|
|
|
|
setInitialSort(sort ?? defaultSort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [defaultSort, router.isReady, shouldLoadFromStorage])
|
|
|
|
|
|
|
|
return {
|
|
|
|
initialSort,
|
|
|
|
initialQuery,
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
2022-05-09 17:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useUpdateQueryAndSort(props: {
|
|
|
|
shouldLoadFromStorage: boolean
|
|
|
|
}) {
|
|
|
|
const { shouldLoadFromStorage } = props
|
|
|
|
const router = useRouter()
|
2022-01-02 22:46:04 +00:00
|
|
|
|
|
|
|
const setSort = (sort: Sort | undefined) => {
|
2022-05-09 17:38:33 +00:00
|
|
|
if (sort !== router.query.s) {
|
|
|
|
router.query.s = sort
|
|
|
|
router.push(router, undefined, { shallow: true })
|
|
|
|
if (shouldLoadFromStorage) {
|
|
|
|
localStorage.setItem(MARKETS_SORT, sort || '')
|
|
|
|
}
|
2022-03-13 21:46:09 +00:00
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
const { query, refine } = useSearchBox()
|
2022-02-24 04:57:59 +00:00
|
|
|
|
|
|
|
// Debounce router query update.
|
|
|
|
const pushQuery = useMemo(
|
|
|
|
() =>
|
2022-05-22 08:36:05 +00:00
|
|
|
debounce((query: string | undefined) => {
|
2022-02-24 04:57:59 +00:00
|
|
|
if (query) {
|
|
|
|
router.query.q = query
|
|
|
|
} else {
|
|
|
|
delete router.query.q
|
|
|
|
}
|
|
|
|
router.push(router, undefined, { shallow: true })
|
2022-06-16 16:49:57 +00:00
|
|
|
track('search', { query })
|
2022-02-24 04:57:59 +00:00
|
|
|
}, 500),
|
|
|
|
[router]
|
|
|
|
)
|
|
|
|
|
|
|
|
const setQuery = (query: string | undefined) => {
|
2022-05-09 17:38:33 +00:00
|
|
|
refine(query ?? '')
|
2022-02-24 04:57:59 +00:00
|
|
|
pushQuery(query)
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
setSort,
|
|
|
|
setQuery,
|
2022-05-09 17:38:33 +00:00
|
|
|
query,
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
}
|