index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
author Yousef Al-Harbi <yo2w86@gmail.com> 2026-11-18 09:38:33 +0300
committer Yousef Al-Harbi <yo2w86@gmail.com> 2026-11-18 09:38:33 +0300
commit 7f1e32027faa1f600aa103eebcbf21dacac7c133 [patch]
tree e44e3a1ba59b3c9b23f3fcd8d4047a881ec29c8b
parent f0a69f56d54875c37f93393ab9d0930e51151842
download 7f1e32027faa1f600aa103eebcbf21dacac7c133.tar.gz

fix: initial page sort and tag sorting

Diffstat

-rw-r--r-- templates/base.html 26
-rw-r--r-- src/git/tag.rs 27
 2 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/templates/base.html b/templates/base.html
index 5315b50..e0c93ca 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -53,19 +53,17 @@
              th.setAttribute("aria-sort", "none");
            }

            const applySort = () => {
              // 1st click -> ascending (top-down), 2nd click -> descending (bottom-up)
              const nextDir = th.classList.contains("sort-asc") ? "desc" : "asc";
              const factor = nextDir === "asc" ? 1 : -1;
            const sortTableBy = (dir) => {
              const factor = dir === "asc" ? 1 : -1;

              headers.forEach((h) => {
                h.classList.remove("sort-asc", "sort-desc");
                h.setAttribute("aria-sort", "none");
              });
              th.classList.add(nextDir === "asc" ? "sort-asc" : "sort-desc");
              th.classList.add(dir === "asc" ? "sort-asc" : "sort-desc");
              th.setAttribute(
                "aria-sort",
                nextDir === "asc" ? "ascending" : "descending"
                dir === "asc" ? "ascending" : "descending"
              );

              const allRows = Array.from(tbody.querySelectorAll(":scope > tr"));
@@ -96,13 +94,25 @@
              });

              tbody.append(...topPinned, ...sortableRows, ...bottomPinned);
            };

            // Apply initial sort immediately on page load if header has sort-asc or sort-desc
            if (th.classList.contains("sort-desc")) {
              sortTableBy("desc");
            } else if (th.classList.contains("sort-asc")) {
              sortTableBy("asc");
            }

            const toggleSort = () => {
              const nextDir = th.classList.contains("sort-asc") ? "desc" : "asc";
              sortTableBy(nextDir);
            };

            th.addEventListener("click", applySort);
            th.addEventListener("click", toggleSort);
            th.addEventListener("keydown", (e) => {
              if (e.key === "Enter" || e.key === " ") {
                e.preventDefault();
                applySort();
                toggleSort();
              }
            });
          });
diff --git a/src/git/tag.rs b/src/git/tag.rs
index 54f4b78..f31302e 100644
--- a/src/git/tag.rs
+++ b/src/git/tag.rs
@@ -81,19 +81,24 @@
    pub fn tags(&self, offset: usize, limit: usize) -> anyhow::Result<(Vec<Tag>, bool)> {
        let gix_repo = self.gix();
        let references = gix_repo.references()?;
        let gix_tags = references.tags()?;
        let mut all_tags: Vec<Tag> = references
            .tags()?
            .map(|rev| {
                let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
                Tag::from_gix(rev)
            })
            .collect::<anyhow::Result<Vec<_>>>()?;

        let mut tags = Vec::new();
        let mut has_next = false;
        // Sort tags descending (newest / highest version first, e.g. v0.2.0 -> v0.1.1 -> v0.1.0)
        all_tags.sort_by(|a, b| {
            b.time
                .seconds()
                .cmp(&a.time.seconds())
                .then_with(|| b.name.cmp(&a.name))
        });

        for (i, rev) in gix_tags.skip(offset).enumerate() {
            if i >= limit {
                has_next = true;
                break;
            }
            let rev = rev.map_err(|e| anyhow::anyhow!(e))?;
            tags.push(Tag::from_gix(rev)?);
        }
        let has_next = all_tags.len() > offset.saturating_add(limit);
        let tags = all_tags.into_iter().skip(offset).take(limit).collect();
        Ok((tags, has_next))
    }
}