diff --git a/explorer/src/lib/utils.ts b/explorer/src/lib/utils.ts index 68fd692b3..071955029 100644 --- a/explorer/src/lib/utils.ts +++ b/explorer/src/lib/utils.ts @@ -368,6 +368,39 @@ export function getBranchesFromRefs( return branches; } +export function compareTagNames(a: string, b: string): number { + if (b.startsWith(`${a}-`)) return -1; + if (a.startsWith(`${b}-`)) return 1; + return b.localeCompare(a, undefined, { numeric: true }); +} + +export interface TagGroup { + namespace: string; + tags: [string, T][]; +} + +export function groupTags( + tags: [string, T][], + timestampOf: (tag: T) => number = () => 0, +): TagGroup[] { + const groups = new Map(); + for (const tag of tags) { + const slash = tag[0].lastIndexOf("/"); + const namespace = slash === -1 ? "" : tag[0].slice(0, slash); + const group = groups.get(namespace) ?? { tags: [], newest: 0 }; + group.tags.push(tag); + group.newest = Math.max(group.newest, timestampOf(tag[1])); + groups.set(namespace, group); + } + + return [...groups.entries()] + .sort(([nsA, a], [nsB, b]) => b.newest - a.newest || nsA.localeCompare(nsB)) + .map(([namespace, group]) => ({ + namespace, + tags: group.tags.sort(([a], [b]) => compareTagNames(a, b)), + })); +} + export function getTagsFromRefs( refs: Record, ): Record { diff --git a/explorer/src/views/repos/Source/PeerBranchSelector.svelte b/explorer/src/views/repos/Source/PeerBranchSelector.svelte index 4ceed3566..4c7ddcea3 100644 --- a/explorer/src/views/repos/Source/PeerBranchSelector.svelte +++ b/explorer/src/views/repos/Source/PeerBranchSelector.svelte @@ -12,6 +12,7 @@ getBranchesFromRefs, getTagsFromRefs, gravatarURL, + groupTags, safeDecodeURIComponent, } from "@app/lib/utils"; @@ -125,14 +126,10 @@ (r.obj.peer === undefined ? 10 : 1) * (r.obj.peer?.alias ? 2 : 1), }); - $: canonicalTags = Object.entries(canonicalTagsInfo).sort( - ([nameA, infoA], [nameB, infoB]) => { - const tsA = infoA.tagger?.timestamp ?? 0; - const tsB = infoB.tagger?.timestamp ?? 0; - if (tsA !== tsB) return tsB - tsA; - return nameB.localeCompare(nameA); - }, - ); + $: canonicalTags = groupTags( + Object.entries(canonicalTagsInfo), + info => info.tagger?.timestamp ?? 0, + ).flatMap(group => group.tags); $: canonicalBranches = Object.entries(canonicalBranchesMap).filter( ([branchName]) => branchName !== repo.payloads["xyz.radicle.project"].data.defaultBranch, diff --git a/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte b/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte index 7a9f51f4d..cfb5f772e 100644 --- a/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte +++ b/explorer/src/views/repos/Source/PeerBranchSelector/Peer.svelte @@ -7,6 +7,7 @@ formatCommit, getBranchesFromRefs, getTagsFromRefs, + groupTags, } from "@app/lib/utils"; import { replace } from "@app/lib/router"; @@ -32,8 +33,10 @@ $: refs = type === "branches" - ? getBranchesFromRefs(peer.remote.refs) - : getTagsFromRefs(peer.remote.refs); + ? Object.entries(getBranchesFromRefs(peer.remote.refs)) + : groupTags(Object.entries(getTagsFromRefs(peer.remote.refs))).flatMap( + group => group.tags, + ); $: if (peer.selected) { expanded = true; @@ -66,7 +69,7 @@ {#if expanded} - {#each Object.entries(refs) as [name, head]} + {#each refs as [name, head]} { expect(utils.fragmentId(hash)).toEqual(expected); }); }); + +describe("compareTagNames", () => { + test("sorts version tags numerically, newest first", () => { + const tags = [ + "v0.1.0", + "v0.10.0", + "v0.2.0", + "v1.0.0-rc.10", + "v1.0.0", + "v1.0.0-rc.2", + "v0.10.1", + ]; + expect(tags.sort(utils.compareTagNames)).toEqual([ + "v1.0.0", + "v1.0.0-rc.10", + "v1.0.0-rc.2", + "v0.10.1", + "v0.10.0", + "v0.2.0", + "v0.1.0", + ]); + }); +}); + +describe("groupTags", () => { + test("keeps namespaces together, most recent first", () => { + const tags: [string, number][] = [ + ["releases/0.29.0", 3], + ["releases/0.9.0", 1], + ["releases/desktop/0.16.0", 3], + ["releases/desktop/0.17.0", 4], + ["releases/httpd/0.29.0", 3], + ["releases/httpd/0.30.0", 5], + ]; + expect( + utils + .groupTags(tags, timestamp => timestamp) + .map(group => ({ + namespace: group.namespace, + tags: group.tags.map(([name]) => name), + })), + ).toEqual([ + { + namespace: "releases/httpd", + tags: ["releases/httpd/0.30.0", "releases/httpd/0.29.0"], + }, + { + namespace: "releases/desktop", + tags: ["releases/desktop/0.17.0", "releases/desktop/0.16.0"], + }, + { namespace: "releases", tags: ["releases/0.29.0", "releases/0.9.0"] }, + ]); + }); + + test("orders namespaces by name without timestamps", () => { + const tags: [string, undefined][] = [ + ["v1.0.0", undefined], + ["releases/httpd/0.30.0", undefined], + ["releases/desktop/0.17.0", undefined], + ]; + expect( + utils.groupTags(tags).flatMap(group => group.tags.map(([name]) => name)), + ).toEqual(["v1.0.0", "releases/desktop/0.17.0", "releases/httpd/0.30.0"]); + }); +});