diff options
Diffstat (limited to 'app/assets/javascripts/lib/utils/url_utility.js')
-rw-r--r-- | app/assets/javascripts/lib/utils/url_utility.js | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/app/assets/javascripts/lib/utils/url_utility.js b/app/assets/javascripts/lib/utils/url_utility.js index ea0520e3157..a579b010877 100644 --- a/app/assets/javascripts/lib/utils/url_utility.js +++ b/app/assets/javascripts/lib/utils/url_utility.js @@ -1,3 +1,5 @@ +import { getGlobalAlerts, setGlobalAlerts } from './global_alerts'; + export const DASH_SCOPE = '-'; export const PATH_SEPARATOR = '/'; @@ -241,7 +243,11 @@ export function removeParams(params, url = window.location.href, skipEncoding = return `${root}${writableQuery}${writableFragment}`; } -export const getLocationHash = (hash = window.location.hash) => hash.split('#')[1]; +/** + * Returns value after the '#' in the location hash + * @returns Current value of the hash, undefined if not set + */ +export const getLocationHash = () => window.location.hash?.split('#')[1]; /** * Returns a boolean indicating whether the URL hash contains the given string value @@ -717,6 +723,20 @@ export function visitUrl(destination, external = false) { } } +/** + * Navigates to a URL and display alerts. + * + * If destination is a querystring, it will be automatically transformed into a fully qualified URL. + * If the URL is not a safe URL (see isSafeURL implementation), this function will log an exception into Sentry. + * + * @param {*} destination - url to navigate to. This can be a fully qualified URL or a querystring. + * @param {{id: String, title?: String, message: String, variant: String, dismissible?: Boolean, persistOnPages?: String[]}[]} alerts - Alerts to display + */ +export function visitUrlWithAlerts(destination, alerts) { + setGlobalAlerts([...getGlobalAlerts(), ...alerts]); + visitUrl(destination); +} + export function refreshCurrentPage() { visitUrl(window.location.href); } |