2022-08-15 05:09:25 +00:00
|
|
|
import { useState } from 'react'
|
|
|
|
import { NextRouter, useRouter } from 'next/router'
|
2022-03-13 21:46:09 +00:00
|
|
|
|
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-08-15 05:09:25 +00:00
|
|
|
type UpdatedQueryParams = { [k: string]: string }
|
|
|
|
type QuerySortOpts = { useUrl: boolean }
|
2022-07-30 00:08:51 +00:00
|
|
|
|
2022-08-15 05:09:25 +00:00
|
|
|
function withURLParams(location: Location, params: UpdatedQueryParams) {
|
|
|
|
const newParams = new URLSearchParams(location.search)
|
|
|
|
for (const [k, v] of Object.entries(params)) {
|
|
|
|
if (!v) {
|
|
|
|
newParams.delete(k)
|
|
|
|
} else {
|
|
|
|
newParams.set(k, v)
|
2022-03-13 21:46:09 +00:00
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
2022-08-15 05:09:25 +00:00
|
|
|
const newUrl = new URL(location.href)
|
|
|
|
newUrl.search = newParams.toString()
|
|
|
|
return newUrl
|
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-08-15 05:09:25 +00:00
|
|
|
function updateURL(params: UpdatedQueryParams) {
|
|
|
|
// see relevant discussion here https://github.com/vercel/next.js/discussions/18072
|
|
|
|
const url = withURLParams(window.location, params).toString()
|
|
|
|
const updatedState = { ...window.history.state, as: url, url }
|
|
|
|
window.history.replaceState(updatedState, '', url)
|
|
|
|
}
|
2022-02-24 04:57:59 +00:00
|
|
|
|
2022-08-15 05:09:25 +00:00
|
|
|
function getStringURLParam(router: NextRouter, k: string) {
|
|
|
|
const v = router.query[k]
|
|
|
|
return typeof v === 'string' ? v : null
|
|
|
|
}
|
2022-02-24 04:57:59 +00:00
|
|
|
|
2022-08-15 05:09:25 +00:00
|
|
|
export function useQuery(defaultQuery: string, opts?: QuerySortOpts) {
|
|
|
|
const useUrl = opts?.useUrl ?? false
|
|
|
|
const router = useRouter()
|
|
|
|
const initialQuery = useUrl ? getStringURLParam(router, 'q') : null
|
|
|
|
const [query, setQuery] = useState(initialQuery ?? defaultQuery)
|
|
|
|
if (!useUrl) {
|
|
|
|
return [query, setQuery] as const
|
|
|
|
} else {
|
|
|
|
return [query, (q: string) => (setQuery(q), updateURL({ q }))] as const
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
2022-08-15 05:09:25 +00:00
|
|
|
}
|
2022-01-02 22:46:04 +00:00
|
|
|
|
2022-08-15 05:09:25 +00:00
|
|
|
export function useSort(defaultSort: Sort, opts?: QuerySortOpts) {
|
|
|
|
const useUrl = opts?.useUrl ?? false
|
|
|
|
const router = useRouter()
|
|
|
|
const initialSort = useUrl ? (getStringURLParam(router, 's') as Sort) : null
|
|
|
|
const [sort, setSort] = useState(initialSort ?? defaultSort)
|
|
|
|
if (!useUrl) {
|
|
|
|
return [sort, setSort] as const
|
|
|
|
} else {
|
|
|
|
return [sort, (s: Sort) => (setSort(s), updateURL({ s }))] as const
|
2022-01-02 22:46:04 +00:00
|
|
|
}
|
|
|
|
}
|