{% extends "!layout.html" %}
{% block menu %}
  <style>
    /* style mobile top nav to look like main nav */
    .wy-nav-top {
        background: {{ theme_style_nav_header_background }}
    }
  </style>
  {{ super() }}
  <!-- Include Index in the sidebar -->
  <!-- https://stackoverflow.com/a/37843854 -->
  <a href="{{ pathto('genindex.html', 1) }}">Index</a>
  <!-- Add versions for selected branches + tags -->
  <p class="caption"><span class="caption-text">Versions</span></p>
  <ul id="versions"/>
  <script>
    // Add any branches to appear in the side pane here, tags will be added below
    // Will only appear if docs are built and pushed in gh-pages
    var versions = ['master', 'main'];
    var dirs = new Set();
    function addVersion(name) {
      if (dirs.has(name)) {
        var li = document.createElement("li");
        var a = document.createElement("a");
        a.href = 'https://DiamondLightSource.github.io/{{ project }}/' + name;
        a.innerText = name;
        li.appendChild(a)
        document.getElementById('versions').appendChild(li);
      }
    }
    Promise.all([
      // Find gh-pages directories and populate `dirs`
      fetch("https://api.github.com/repos/DiamondLightSource/{{ project }}/contents?ref=gh-pages")
      .then(response => response.json())
      .then(data => data.forEach(function(e) {
        if (e.type == "dir") dirs.add(e.name);
      })),
      // Add tags to `versions`
      fetch('https://api.github.com/repos/DiamondLightSource/{{ project }}/tags')
        .then(response => response.json())
        .then(data => data.forEach(function(e) {
          versions.push(e.name);
        }))
      ]).then(_ => versions.forEach(addVersion))
  </script>
{% endblock %}
