( updates: Record<string, QueryParamValue> )
| 274 | * Update query parameters while preserving the current hash. |
| 275 | */ |
| 276 | export function updateQueryParams( |
| 277 | updates: Record<string, QueryParamValue> |
| 278 | ): void { |
| 279 | if (typeof window === "undefined") return; |
| 280 | |
| 281 | const url = new URL(window.location.href); |
| 282 | |
| 283 | for (const [key, value] of Object.entries(updates)) { |
| 284 | url.searchParams.delete(key); |
| 285 | |
| 286 | if (Array.isArray(value)) { |
| 287 | for (const item of value) { |
| 288 | const normalized = item.trim(); |
| 289 | if (normalized) { |
| 290 | url.searchParams.append(key, normalized); |
| 291 | } |
| 292 | } |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | if (typeof value === "boolean") { |
| 297 | if (value) { |
| 298 | url.searchParams.set(key, "1"); |
| 299 | } |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | if (typeof value === "string") { |
| 304 | const normalized = value.trim(); |
| 305 | if (normalized) { |
| 306 | url.searchParams.set(key, normalized); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | const search = url.searchParams.toString(); |
| 312 | const nextUrl = `${url.pathname}${search ? `?${search}` : ""}${url.hash}`; |
| 313 | const currentUrl = `${window.location.pathname}${window.location.search}${window.location.hash}`; |
| 314 | |
| 315 | if (nextUrl !== currentUrl) { |
| 316 | history.replaceState(null, "", nextUrl); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Show a toast notification |
no outgoing calls
no test coverage detected