Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

HTML
<div id="publicSpacesTable">Loading...</div>

<script>
(async function() {
  const tableContainer = document.getElementById('publicSpacesTable');
  tableContainer.innerHTML = 'Fetching public spaces...';

  async function fetchAllSpacesfetchPublicSpaces(start = 0, limit = 50, accumulated = []) {
    const res = await fetch(`/rest/api/space?label=public&limit=${limit}&start=${start}`);
    const data = await res.json();
    const all = accumulated.concat(data.results);
    if (data._links.next) {
      return fetchAllSpacesfetchPublicSpaces(start + limit, limit, all);
    }
    return all;
  }

  async function getSpaceLabels(spaceKey) {
    const res = await fetch(`/rest/api/space/${spaceKey}/label`);
    const data = await res.json();
    return data.results.map(label => label.name);
  }

  async function countPagesInSpace(spaceKey) {
    const res = await fetch(`/rest/api/search?cql=space=${spaceKey}+AND+type=page&limit=1`);
    const data = await res.json();
    return data.totalSize || 0;
  }

  try {
    const allSpacespublicSpaces = await fetchAllSpacesfetchPublicSpaces();

    constif (publicSpaces.length = [];

== 0) {
      tableContainer.innerHTML = '<p>No public spaces found.</p>';
     for (constreturn;
 space of allSpaces) {}

    // Now constfetch labelspage =counts await getSpaceLabels(space.key);
in parallel
    const results = ifawait Promise.all(labelspublicSpaces.includes('public'))map(async space => {
        const pageCount = await countPagesInSpace(space.key);
      return  publicSpaces.push({
          name: space.name,
          key: space.key,
          link: space._links.webui,
          count: pageCount
        });
      }
    }}));

    if (publicSpaces.length === 0) {
      tableContainer.innerHTML = '<p>No public spaces found.</p>';
      return;// Build table
    }

    const table = document.createElement('table');
    table.style.borderCollapse = 'collapse';
    table.style.width = '100%';

    const headerRow = table.insertRow();
    ['Space Name', 'Page Count'].forEach(text => {
      const th = document.createElement('th');
      th.innerText = text;
      th.style.borderBottom = '1px solid #ccc';
      th.style.textAlign = 'left';
      th.style.padding = '6px';
      headerRow.appendChild(th);
    });

    for (const space of publicSpacesresults) {
      const row = table.insertRow();

      const nameCell = row.insertCell();
      const link = document.createElement('a');
      link.href = space.link;
      link.target = '_blank';
      link.innerText = space.name;
      nameCell.appendChild(link);
      nameCell.style.padding = '6px';

      const countCell = row.insertCell();
      countCell.innerText = space.count;
      countCell.style.padding = '6px';
    }

    tableContainer.innerHTML = '';
    tableContainer.appendChild(table);
  } catch (error) {
    console.error(error);
    tableContainer.innerHTML = 'Error fetching public spaces.';
  }
})();
</script>