2022-08-07 23:50:12 +00:00
|
|
|
import { 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-07-19 22:29:41 +00:00
|
|
|
import { DEFAULT_SORT } from 'web/components/contract-search'
|
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-07-19 22:29:41 +00:00
|
|
|
| 'score'
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-06-23 23:47:03 +00:00
|
|
|
export function getSavedSort() {
|
|
|
|
// TODO: this obviously doesn't work with SSR, common sense would suggest
|
|
|
|
// that we should save things like this in cookies so the server has them
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
return localStorage.getItem(MARKETS_SORT) as Sort | null
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:32:02 +00:00
|
|
|
export interface QuerySortOptions {
|
2022-07-30 00:08:51 +00:00
|
|
|
defaultSort?: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
2022-08-11 21:32:02 +00:00
|
|
|
/** Use normal react state instead of url query string */
|
|
|
|
disableQueryString?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useQueryAndSortParams({
|
|
|
|
defaultSort = DEFAULT_SORT,
|
|
|
|
shouldLoadFromStorage = true,
|
|
|
|
disableQueryString,
|
|
|
|
}: QuerySortOptions = {}) {
|
2022-05-09 17:38:33 +00:00
|
|
|
const router = useRouter()
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-07-30 00:08:51 +00:00
|
|
|
const { s: sort, q: query } = router.query as {
|
|
|
|
q?: string
|
|
|
|
s?: Sort
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:46:04 +00:00
|
|
|
const setSort = (sort: Sort | undefined) => {
|
2022-07-30 00:08:51 +00:00
|
|
|
router.replace({ query: { ...router.query, s: sort } }, 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-07-30 00:08:51 +00:00
|
|
|
const [queryState, setQueryState] = useState(query)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setQueryState(query)
|
|
|
|
}, [query])
|
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-07-30 01:13:53 +00:00
|
|
|
const queryObj = { ...router.query, q: query }
|
2022-07-30 00:37:34 +00:00
|
|
|
if (!query) delete queryObj.q
|
|
|
|
router.replace({ query: queryObj }, undefined, {
|
2022-07-19 19:33:53 +00:00
|
|
|
shallow: true,
|
|
|
|
})
|
2022-07-30 00:08:51 +00:00
|
|
|
}, 100),
|
2022-02-24 04:57:59 +00:00
|
|
|
[router]
|
|
|
|
)
|
|
|
|
|
|
|
|
const setQuery = (query: string | undefined) => {
|
2022-07-30 00:08:51 +00:00
|
|
|
setQueryState(query)
|
2022-08-11 21:32:02 +00:00
|
|
|
if (!disableQueryString) {
|
|
|
|
pushQuery(query)
|
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 00:08:51 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// If there's no sort option, then set the one from localstorage
|
|
|
|
if (router.isReady && !sort && shouldLoadFromStorage) {
|
|
|
|
const localSort = localStorage.getItem(MARKETS_SORT) as Sort
|
|
|
|
if (localSort && localSort !== defaultSort) {
|
|
|
|
// Use replace to not break navigating back.
|
|
|
|
router.replace(
|
|
|
|
{ query: { ...router.query, s: localSort } },
|
|
|
|
undefined,
|
|
|
|
{ shallow: true }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-08-11 21:32:02 +00:00
|
|
|
// use normal state if querydisableQueryString
|
|
|
|
const [sortState, setSortState] = useState(defaultSort)
|
|
|
|
|
2022-01-02 22:46:04 +00:00
|
|
|
return {
|
2022-08-11 21:32:02 +00:00
|
|
|
sort: disableQueryString ? sortState : sort ?? defaultSort,
|
2022-07-30 00:08:51 +00:00
|
|
|
query: queryState ?? '',
|
2022-08-11 21:32:02 +00:00
|
|
|
setSort: disableQueryString ? setSortState : setSort,
|
2022-01-02 22:46:04 +00:00
|
|
|
setQuery,
|
|
|
|
}
|
|
|
|
}
|