2022-01-02 22:46:04 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
|
|
|
|
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-01-02 22:46:04 +00:00
|
|
|
| 'resolved'
|
|
|
|
| 'all'
|
|
|
|
|
|
|
|
export function useQueryAndSortParams(options?: { defaultSort: Sort }) {
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
|
|
|
const setQuery = (query: string | undefined) => {
|
|
|
|
if (query) {
|
|
|
|
router.query.q = query
|
|
|
|
} else {
|
|
|
|
delete router.query.q
|
|
|
|
}
|
|
|
|
|
|
|
|
router.push(router, undefined, { shallow: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
sort: sort ?? options?.defaultSort ?? 'creator',
|
|
|
|
query: query ?? '',
|
|
|
|
setSort,
|
|
|
|
setQuery,
|
|
|
|
}
|
|
|
|
}
|