fix: initial page sort and tag sorting
2 files changed, 34 insertions(+), 19 deletions(-)
@@ -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();
}
});
});
@@ -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))
}
}