summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pages/projects/find_file/ref_switcher/ref_switcher_utils.js
blob: 5fecd024f1a44b5168613dfbb9371051d90aae7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { joinPaths } from '~/lib/utils/url_utility';

/**
 * Generates a ref destination url based on the selected ref and current url.
 * @param {string} selectedRef - The selected ref from the ref dropdown.
 * @param {string} namespace - The destination namespace for the path.
 */
export function generateRefDestinationPath(selectedRef, namespace) {
  if (!selectedRef || !namespace) {
    return window.location.href;
  }

  const { pathname } = window.location;
  const encodedHash = '%23';

  const [projectRootPath] = pathname.split(namespace);

  const destinationPath = joinPaths(
    projectRootPath,
    namespace,
    encodeURI(selectedRef).replace(/#/g, encodedHash),
  );

  const newURL = new URL(window.location);
  newURL.pathname = destinationPath;

  return newURL.href;
}