2022-02-24 04:57:59 +00:00
|
|
|
import _ 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-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 =
|
|
|
|
| 'creator'
|
|
|
|
| 'tag'
|
|
|
|
| '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-01-23 19:34:03 +00:00
|
|
|
| 'close-date'
|
2022-03-02 02:00:14 +00:00
|
|
|
| 'closed'
|
2022-01-02 22:46:04 +00:00
|
|
|
| 'resolved'
|
|
|
|
| 'all'
|
|
|
|
|
2022-03-13 21:46:09 +00:00
|
|
|
export function useQueryAndSortParams(options?: {
|
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}) {
|
|
|
|
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()
|
|
|
|
|
|
|
|
const { s: sort, q: query } = router.query as {
|
|
|
|
q?: string
|
|
|
|
s?: Sort
|
|
|
|
}
|
|
|
|
|
|
|
|
const setSort = (sort: Sort | undefined) => {
|
|
|
|
router.query.s = sort
|
|
|
|
router.push(router, undefined, { shallow: true })
|
2022-03-13 21:46:09 +00:00
|
|
|
if (shouldLoadFromStorage) {
|
|
|
|
localStorage.setItem(MARKETS_SORT, sort || '')
|
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 04:57:59 +00:00
|
|
|
const [queryState, setQueryState] = useState(query)
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-02-24 04:57:59 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setQueryState(query)
|
|
|
|
}, [query])
|
|
|
|
|
|
|
|
// Debounce router query update.
|
|
|
|
const pushQuery = useMemo(
|
|
|
|
() =>
|
|
|
|
_.debounce((query: string | undefined) => {
|
|
|
|
if (query) {
|
|
|
|
router.query.q = query
|
|
|
|
} else {
|
|
|
|
delete router.query.q
|
|
|
|
}
|
|
|
|
router.push(router, undefined, { shallow: true })
|
|
|
|
}, 500),
|
|
|
|
[router]
|
|
|
|
)
|
|
|
|
|
|
|
|
const setQuery = (query: string | undefined) => {
|
|
|
|
setQueryState(query)
|
|
|
|
pushQuery(query)
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 21:46:09 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// If there's no sort option, then set the one from localstorage
|
2022-03-19 16:34:22 +00:00
|
|
|
if (router.isReady && !sort && shouldLoadFromStorage) {
|
2022-03-13 21:46:09 +00:00
|
|
|
const localSort = localStorage.getItem(MARKETS_SORT) as Sort
|
|
|
|
if (localSort) {
|
2022-03-23 05:34:04 +00:00
|
|
|
router.query.s = localSort
|
|
|
|
// Use replace to not break navigating back.
|
|
|
|
router.replace(router, undefined, { shallow: true })
|
2022-03-13 21:46:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-02 22:46:04 +00:00
|
|
|
return {
|
2022-03-19 16:34:22 +00:00
|
|
|
sort: sort ?? defaultSort,
|
2022-02-24 04:57:59 +00:00
|
|
|
query: queryState ?? '',
|
2022-01-02 22:46:04 +00:00
|
|
|
setSort,
|
|
|
|
setQuery,
|
|
|
|
}
|
|
|
|
}
|