async function closeMenu() {
  if (document.querySelector('.navbar-toggler')) {
    document.querySelector('.navbar-toggler').classList.add('collapsed');
    document
      .querySelector('.navbar-toggler')
      .setAttribute('aria-expanded', 'false');
  }
  if (document.querySelector('.navbar-collapse.collapse')) {
    document
      .querySelector('.navbar-collapse.collapse')
      .classList.remove('show');
  }
}

async function goToHashInPage(fromSamePage = false) {
  if (window.location.hash) {
    const hashes = window.location.hash.split('#');
    if (
      hashes.length > 1 &&
      document.querySelector('a[name="' + hashes[hashes.length - 1] + '"]')
    ) {
      const hash = hashes[hashes.length - 1];

      let offsetTop = document.querySelector('a[name="' + hash + '"]')
        .offsetParent.offsetTop;
      window.scrollTo(0, offsetTop);

      let timer = window.setInterval(() => {
        if (
          offsetTop ===
          document.querySelector('a[name="' + hash + '"]').offsetParent
            .offsetTop
        ) {
          window.clearInterval(timer);
        } else {
          offsetTop = document.querySelector('a[name="' + hash + '"]')
            .offsetParent.offsetTop;
          window.scrollTo(0, offsetTop);
        }
      }, 500);
      closeMenu();
      return;
    }
  }
  if (fromSamePage) {
    window.scrollTo(0, 0);
    closeMenu();
  }
}
