Tidy some stuff up

This commit is contained in:
Marshall Polaris 2022-08-14 00:30:26 -07:00
parent 5cda286b8f
commit 5fe39590b5

View File

@ -11,10 +11,10 @@ export type Sort =
| 'last-updated' | 'last-updated'
| 'score' | 'score'
type NewQueryParams = { [k: string]: string | null | undefined } type UpdatedQueryParams = { [k: string]: string }
function withURLParams(params: NewQueryParams) { function withURLParams(location: Location, params: UpdatedQueryParams) {
const newParams = new URLSearchParams(window.location.search) const newParams = new URLSearchParams(location.search)
for (const [k, v] of Object.entries(params)) { for (const [k, v] of Object.entries(params)) {
if (!v) { if (!v) {
newParams.delete(k) newParams.delete(k)
@ -22,13 +22,14 @@ function withURLParams(params: NewQueryParams) {
newParams.set(k, v) newParams.set(k, v)
} }
} }
const newUrl = new URL(window.location.href) const newUrl = new URL(location.href)
newUrl.search = newParams.toString() newUrl.search = newParams.toString()
return newUrl return newUrl
} }
function updateURL(params: NewQueryParams) { function updateURL(params: UpdatedQueryParams) {
const url = withURLParams(params).toString() // 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 } const updatedState = { ...window.history.state, as: url, url }
window.history.replaceState(updatedState, '', url) window.history.replaceState(updatedState, '', url)
} }