mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-21 14:20:42 +08:00
Move ref sorting out of the peer selector
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
import type { Remote } from "@bindings/repo/Remote";
|
||||
import type { Tag } from "@bindings/repo/Tag";
|
||||
|
||||
import { sortedTags } from "@app/lib/refs";
|
||||
import * as router from "@app/lib/router";
|
||||
import { truncateId } from "@app/lib/utils";
|
||||
import { formatOid, truncateId } from "@app/lib/utils";
|
||||
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
import TagIcon from "@app/components/PeerSelector/TagIcon.svelte";
|
||||
@@ -34,12 +35,11 @@
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([name, oid]) => ({ name, oid }));
|
||||
}
|
||||
return Object.entries(remote.tags)
|
||||
.sort(([nameA, a], [nameB, b]) => {
|
||||
if (a.timestamp !== b.timestamp) return b.timestamp - a.timestamp;
|
||||
return nameB.localeCompare(nameA);
|
||||
})
|
||||
.map(([name, tag]) => ({ name, oid: tag.oid, tag }));
|
||||
return sortedTags(remote.tags).map(([name, tag]) => ({
|
||||
name,
|
||||
oid: tag.oid,
|
||||
tag,
|
||||
}));
|
||||
},
|
||||
);
|
||||
|
||||
@@ -50,10 +50,6 @@
|
||||
revision: name,
|
||||
});
|
||||
}
|
||||
|
||||
function shortOid(oid: string): string {
|
||||
return oid.slice(0, 7);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@@ -170,7 +166,7 @@
|
||||
<Icon name="branch" />
|
||||
{/if}
|
||||
<span class="ref-name">{name}</span>
|
||||
<span class="txt-id">{shortOid(oid)}</span>
|
||||
<span class="txt-id">{formatOid(oid)}</span>
|
||||
</a>
|
||||
{/each}
|
||||
{:else}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
import type { RepoRefs } from "@bindings/repo/RepoRefs";
|
||||
|
||||
import fuzzysort from "fuzzysort";
|
||||
import orderBy from "lodash/orderBy";
|
||||
|
||||
import { selectedRefType, sortedTags, sortRemotes } from "@app/lib/refs";
|
||||
import * as router from "@app/lib/router";
|
||||
import { truncateId } from "@app/lib/utils";
|
||||
import { formatOid, truncateId } from "@app/lib/utils";
|
||||
|
||||
import Button from "@app/components/Button.svelte";
|
||||
import Icon from "@app/components/Icon.svelte";
|
||||
@@ -39,14 +39,7 @@
|
||||
const canonicalBranchNames = $derived(
|
||||
Object.keys(refs?.canonical.branches ?? {}).sort(),
|
||||
);
|
||||
const canonicalTagsSorted = $derived(
|
||||
Object.entries(refs?.canonical.tags ?? {}).sort(
|
||||
([nameA, a], [nameB, b]) => {
|
||||
if (a.timestamp !== b.timestamp) return b.timestamp - a.timestamp;
|
||||
return nameB.localeCompare(nameA);
|
||||
},
|
||||
),
|
||||
);
|
||||
const canonicalTagsSorted = $derived(sortedTags(refs?.canonical.tags ?? {}));
|
||||
|
||||
const selectedPeer = $derived(refs?.remotes.find(r => r.id === peer));
|
||||
// Until refs load, fall back to the bare NID from the route so the trigger
|
||||
@@ -62,36 +55,9 @@
|
||||
(revision === undefined || revision === defaultBranch),
|
||||
);
|
||||
|
||||
const selectedRefType = $derived.by<"branch" | "tag" | undefined>(() => {
|
||||
if (revision === undefined) return "branch";
|
||||
if (selectedPeer) {
|
||||
if (revision in selectedPeer.branches) return "branch";
|
||||
if (revision in selectedPeer.tags) return "tag";
|
||||
} else {
|
||||
if (revision in (refs?.canonical.branches ?? {})) return "branch";
|
||||
if (revision in (refs?.canonical.tags ?? {})) return "tag";
|
||||
if (revision === defaultBranch) return "branch";
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
|
||||
// Delegates first; within each (delegate / non-delegate) group, peers with
|
||||
// an alias come before those without, alphabetically. No-alias peers fall
|
||||
// to the bottom and sort by NID.
|
||||
function sortRemotes<
|
||||
T extends { id: string; alias?: string; delegate: boolean },
|
||||
>(list: T[]): T[] {
|
||||
return orderBy(
|
||||
list,
|
||||
[
|
||||
r => !r.delegate,
|
||||
r => r.alias === undefined,
|
||||
r => (r.alias ?? "").toLowerCase(),
|
||||
r => r.id,
|
||||
],
|
||||
["asc", "asc", "asc", "asc"],
|
||||
);
|
||||
}
|
||||
const refType = $derived(
|
||||
selectedRefType(revision, selectedPeer, refs?.canonical, defaultBranch),
|
||||
);
|
||||
|
||||
const peersWithBranches = $derived(
|
||||
sortRemotes(
|
||||
@@ -112,7 +78,7 @@
|
||||
// freely after that.
|
||||
// svelte-ignore state_referenced_locally
|
||||
let activeTab: "branches" | "tags" = $state(
|
||||
selectedRefType === "tag" ? "tags" : "branches",
|
||||
refType === "tag" ? "tags" : "branches",
|
||||
);
|
||||
|
||||
$effect(() => {
|
||||
@@ -214,10 +180,6 @@
|
||||
if (revision !== undefined) return revision;
|
||||
return defaultBranch;
|
||||
}
|
||||
|
||||
function shortOid(oid: string): string {
|
||||
return oid.slice(0, 7);
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
@@ -442,7 +404,7 @@
|
||||
<span class="trigger-separator">/</span>
|
||||
{/if}
|
||||
<div class="trigger-ref">
|
||||
<Icon name={selectedRefType === "tag" ? "label" : "branch"} />
|
||||
<Icon name={refType === "tag" ? "label" : "branch"} />
|
||||
<span class="trigger-name">{displayLabel()}</span>
|
||||
</div>
|
||||
{#if peer === undefined}
|
||||
@@ -535,7 +497,7 @@
|
||||
<span class="canonical-badge">Canonical</span>
|
||||
{/if}
|
||||
</div>
|
||||
<span class="txt-id">{shortOid(el.oid)}</span>
|
||||
<span class="txt-id">{formatOid(el.oid)}</span>
|
||||
</a>
|
||||
{/each}
|
||||
{:else}
|
||||
@@ -563,7 +525,7 @@
|
||||
<span class="canonical-badge">Canonical</span>
|
||||
</div>
|
||||
<span class="txt-id">
|
||||
{shortOid(
|
||||
{formatOid(
|
||||
refs?.canonical.branches[name] ?? project.meta.head,
|
||||
)}
|
||||
</span>
|
||||
@@ -585,7 +547,7 @@
|
||||
<span class="result-name-text">{name}</span>
|
||||
<span class="canonical-badge">Canonical</span>
|
||||
</div>
|
||||
<span class="txt-id">{shortOid(tag.oid)}</span>
|
||||
<span class="txt-id">{formatOid(tag.oid)}</span>
|
||||
</a>
|
||||
{/each}
|
||||
{/if}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { Canonical } from "@bindings/repo/Canonical";
|
||||
import type { Remote } from "@bindings/repo/Remote";
|
||||
import type { Tag } from "@bindings/repo/Tag";
|
||||
|
||||
import orderBy from "lodash/orderBy";
|
||||
|
||||
export function sortedTags(tags: Record<string, Tag>): [string, Tag][] {
|
||||
return Object.entries(tags).sort(([nameA, a], [nameB, b]) => {
|
||||
if (a.timestamp !== b.timestamp) return b.timestamp - a.timestamp;
|
||||
return nameB.localeCompare(nameA);
|
||||
});
|
||||
}
|
||||
|
||||
// Delegates first; within each (delegate / non-delegate) group, peers with
|
||||
// an alias come before those without, alphabetically. No-alias peers fall
|
||||
// to the bottom and sort by NID.
|
||||
export function sortRemotes<
|
||||
T extends { id: string; alias?: string; delegate: boolean },
|
||||
>(list: T[]): T[] {
|
||||
return orderBy(list, [
|
||||
r => !r.delegate,
|
||||
r => r.alias === undefined,
|
||||
r => (r.alias ?? "").toLowerCase(),
|
||||
r => r.id,
|
||||
]);
|
||||
}
|
||||
|
||||
export function selectedRefType(
|
||||
revision: string | undefined,
|
||||
selectedPeer: Remote | undefined,
|
||||
canonical: Canonical | undefined,
|
||||
defaultBranch: string,
|
||||
): "branch" | "tag" | undefined {
|
||||
if (revision === undefined) return "branch";
|
||||
if (selectedPeer) {
|
||||
if (revision in selectedPeer.branches) return "branch";
|
||||
if (revision in selectedPeer.tags) return "tag";
|
||||
} else {
|
||||
if (revision in (canonical?.branches ?? {})) return "branch";
|
||||
if (revision in (canonical?.tags ?? {})) return "tag";
|
||||
if (revision === defaultBranch) return "branch";
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import type { Remote } from "@bindings/repo/Remote";
|
||||
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { selectedRefType, sortedTags, sortRemotes } from "@app/lib/refs";
|
||||
|
||||
function tag(timestamp: number) {
|
||||
return { oid: "o", timestamp };
|
||||
}
|
||||
|
||||
function remote(props: Partial<Remote> = {}): Remote {
|
||||
return { id: "z6Mk", delegate: false, branches: {}, tags: {}, ...props };
|
||||
}
|
||||
|
||||
test("sortedTags lists the newest first, then by name descending", () => {
|
||||
const sorted = sortedTags({
|
||||
"v1.0": tag(1),
|
||||
"v1.2": tag(2),
|
||||
"v1.1": tag(2),
|
||||
});
|
||||
|
||||
expect(sorted.map(([name]) => name)).toEqual(["v1.2", "v1.1", "v1.0"]);
|
||||
});
|
||||
|
||||
test("sortRemotes puts delegates first, then aliased peers by alias, then by NID", () => {
|
||||
const sorted = sortRemotes([
|
||||
{ id: "b", delegate: false },
|
||||
{ id: "z", alias: "Bob", delegate: false },
|
||||
{ id: "a", delegate: false },
|
||||
{ id: "y", alias: "alice", delegate: false },
|
||||
{ id: "d", delegate: true },
|
||||
{ id: "x", alias: "zed", delegate: true },
|
||||
]);
|
||||
|
||||
expect(sorted.map(r => r.id)).toEqual(["x", "d", "y", "z", "a", "b"]);
|
||||
});
|
||||
|
||||
describe("selectedRefType", () => {
|
||||
const canonical = { branches: { main: "o" }, tags: { "v1.0": tag(1) } };
|
||||
|
||||
test.each([
|
||||
[undefined, "branch"],
|
||||
["main", "branch"],
|
||||
["v1.0", "tag"],
|
||||
["default", "branch"],
|
||||
["abc123", undefined],
|
||||
])("canonical %j is a %j", (revision, expected) => {
|
||||
expect(selectedRefType(revision, undefined, canonical, "default")).toBe(
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
test.each([
|
||||
["feature", "branch"],
|
||||
["v2.0", "tag"],
|
||||
["main", undefined],
|
||||
["default", undefined],
|
||||
])("a peer's %j is a %j", (revision, expected) => {
|
||||
const peer = remote({
|
||||
branches: { feature: "o" },
|
||||
tags: { "v2.0": tag(1) },
|
||||
});
|
||||
|
||||
expect(selectedRefType(revision, peer, canonical, "default")).toBe(
|
||||
expected,
|
||||
);
|
||||
});
|
||||
|
||||
test("refs that haven't loaded yet only know the default branch", () => {
|
||||
expect(selectedRefType("default", undefined, undefined, "default")).toBe(
|
||||
"branch",
|
||||
);
|
||||
expect(selectedRefType("main", undefined, undefined, "default")).toBe(
|
||||
undefined,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user