2023-12-30 05:29:53 +00:00
|
|
|
export default function navigate(path, query, options = {}) {
|
|
|
|
const curatedQuery = Object.fromEntries(Object.entries(query || {}).map(([key, value]) => (value === undefined ? null : [key, value])).filter(Boolean));
|
|
|
|
|
|
|
|
const queryString = new URLSearchParams({
|
|
|
|
...(options.preserveQuery ? Object.fromEntries(new URL(window.location).searchParams.entries()) : {}),
|
|
|
|
...curatedQuery,
|
|
|
|
}).toString();
|
|
|
|
|
|
|
|
const url = queryString
|
|
|
|
? `${path}?${encodeURI(decodeURIComponent(queryString))}` // URLSearchParams encodes commas, we don't want that
|
|
|
|
: path;
|
|
|
|
|
|
|
|
if (options.redirect) {
|
|
|
|
window.location.href = url;
|
2024-01-26 01:35:26 +00:00
|
|
|
} else if (options.replace) {
|
|
|
|
history.replaceState({}, '', url); // eslint-disable-line no-restricted-globals
|
2023-12-30 05:29:53 +00:00
|
|
|
} else {
|
|
|
|
history.pushState({}, '', url); // eslint-disable-line no-restricted-globals
|
|
|
|
}
|
|
|
|
}
|