export const getUrlSearchParams = () => { const params = new URLSearchParams(window.location.search) return Array.from(params.entries()).reduce( (acc, [key, val]) => ({ ...acc, [key]: val }), {}, ) } // Return search parameters as a string export const getUrlSearchString = (params: Record) => new URLSearchParams(params).toString() // Return search parameters found in URL or sessionStorage export const getSearchParamsByKeys = ( ...keys: T[] ): Record => { const url = new URL(window.location.href) const values = keys.reduce( (acc, key) => { const searchParam = url.searchParams.get(key) const param = searchParam || sessionStorage.getItem(key) if (param) { acc[key] = param } return acc }, {} as Record, ) return values } // Adds a search parameter to the url export const setSearchParam = (key: string, value: string): void => { const url = new URL(window.location.href) const { searchParams } = url searchParams.set(key, value) url.search = searchParams.toString() window.location.href = url.toString() } // Adds search parameters to the url export const setSearchParams = (params: Record): void => { const url = new URL(window.location.href) const { searchParams } = url Object.entries(params).forEach(([key, value]) => { searchParams.set(key, value) }) url.search = searchParams.toString() window.location.href = url.toString() } // Removes a search parameter from the url export const removeSearchParam = (...keys: string[]): void => { const url = new URL(window.location.href) const { searchParams } = url keys.forEach((key) => searchParams.delete(key)) url.search = searchParams.toString() window.history.replaceState({}, '', url) } // Replace search parameters with those found in URL search parameters or sessionStorage export const replaceSearchParams = ( ...keys: T[] ): Record => { const url = new URL(window.location.href) const params = keys.reduce((acc, key) => { const paramValue = url.searchParams.get(key) const value = paramValue || sessionStorage.getItem(key) // If the query parameter is present, add it to the sessionStorage if (paramValue) { sessionStorage.setItem(key, paramValue) } // Bail if there is nothing to set if (!value) return acc acc.set(key, value) return acc }, new URLSearchParams()) // To keep other params in place, we merge them with the current URL parameters const combinedParams = new URLSearchParams({ ...Object.fromEntries(url.searchParams), ...Object.fromEntries(params), }) if (combinedParams.toString()) { window.history.replaceState(null, '', `?${combinedParams}`) } return getSearchParamsByKeys(...params.keys()) } // Sets up click handlers for elements with data-attribute `data-reset-search-params`. // Clicking these will remove all given keys from the sessionStorage and remove the search parameters from the url // If you want to reset both 'apiKey' and 'locale' when the event is executed, you would use it as followed // import { initResetSearchParams } from 'lib/url-helpers' // Setup the event handlers for the search parameters you want to be reset after execution. // initResetSearchParams('apiKey', 'locale') // Somewhere in the HTML add an element with the 'data-reset-search-params' attribute. // Reset account and source locale export const initResetSearchParams = (...keys: T[]): void => { const resetLink = document.querySelector('[data-reset-search-params]') if (!resetLink) { return } resetLink.addEventListener('click', (e) => { e.preventDefault() const url = new URL(window.location.href) keys.forEach((key) => { // Clear current session for each key sessionStorage.removeItem(key) // Delete key from url url.searchParams.delete(key) }) // Reload page window.location.href = url.toString() }) }