From 48e1f2f56c736876489316732223083fb880fabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C5=ABdolfs=20O=C5=A1i=C5=86=C5=A1?= Date: Mon, 16 Nov 2026 11:50:46 +0100 Subject: [PATCH] desktop: Respect $host in `publicExplorer` template So users can choose whether to pick the first seed from `preferredSeeds` or hard-code a different seed in their config like this: `https://radicle.network/nodes/oak.radicle.garden/$rid$path`. --- desktop/src/lib/radUri.ts | 19 +++++++++-- desktop/src/lib/utils.ts | 12 +------ desktop/tests/unit/radUri.test.ts | 55 ++++++++++++++++++++++++++++++- desktop/tests/unit/utils.test.ts | 16 +++++++++ 4 files changed, 87 insertions(+), 15 deletions(-) diff --git a/desktop/src/lib/radUri.ts b/desktop/src/lib/radUri.ts index 855d4a9ff..de03512f8 100644 --- a/desktop/src/lib/radUri.ts +++ b/desktop/src/lib/radUri.ts @@ -348,13 +348,22 @@ function explorerResourcePath(uri: RadUri): string | undefined { } } +// Fills the `publicExplorer` template the way heartwood's `ExplorerUrl` does: +// `$host` becomes `host`, `$rid` the repo and `$path` the resource. A template +// without `$host` names its own node. User pages have no `$rid`, so they hang +// off whatever precedes `$rid` in the template. export function explorerUrl( reference: RadReference, - base: string, + template: string, host: string, ): string | undefined { - const node = `${base.replace(/\/+$/, "")}/nodes/${host}`; + // Replacer functions keep `$&` and friends in a value literal, as heartwood's + // `str::replace` does. + const url = template.replaceAll("$host", () => host); if (reference.type === "did") { + const rid = url.indexOf("$rid"); + if (rid === -1) return undefined; + const node = url.slice(0, rid).replace(/\/+$/, ""); return `${node}/users/${formatDid(reference.node)}`; } @@ -366,7 +375,11 @@ export function explorerUrl( ? `#${reference.uri.fragment}` : ""; - return `${node}/rad:${reference.uri.repo}${path}${fragment}`; + return ( + url + .replaceAll("$rid", () => `rad:${reference.uri.repo}`) + .replaceAll("$path", () => path) + fragment + ); } function decodeSegment(segment: string): string { diff --git a/desktop/src/lib/utils.ts b/desktop/src/lib/utils.ts index 417b98fb3..283c27795 100644 --- a/desktop/src/lib/utils.ts +++ b/desktop/src/lib/utils.ts @@ -359,17 +359,7 @@ export function explorerLink( reference: RadReference, config: Config, ): string | undefined { - return explorerUrl(reference, explorerBase(config), explorerSeed(config)); -} - -function explorerBase(config: Config): string { - const nodes = config.publicExplorer.indexOf("/nodes/$host"); - if (nodes !== -1) return config.publicExplorer.slice(0, nodes); - try { - return new URL(config.publicExplorer).origin; - } catch { - return config.publicExplorer; - } + return explorerUrl(reference, config.publicExplorer, explorerSeed(config)); } function explorerSeed(config: Config): string { diff --git a/desktop/tests/unit/radUri.test.ts b/desktop/tests/unit/radUri.test.ts index 8305e34df..9206751e2 100644 --- a/desktop/tests/unit/radUri.test.ts +++ b/desktop/tests/unit/radUri.test.ts @@ -1,9 +1,23 @@ import { describe, expect, test } from "vitest"; import type { RadUri } from "@app/lib/radUri"; + +// Most cases care about the path, not the template, so they build the +// explorer's default `/nodes/$host/$rid$path` shape from a base URL. +function explorerUrl( + reference: Parameters[0], + base: string, + host: string, +) { + return explorerUrlFromTemplate( + reference, + `${base.replace(/\/+$/, "")}/nodes/$host/$rid$path`, + host, + ); +} import { cobListType, - explorerUrl, + explorerUrl as explorerUrlFromTemplate, filePath, fileReference, formatRadUri, @@ -395,6 +409,45 @@ describe("explorer URLs", () => { }); }); +describe("explorer templates", () => { + const did = { type: "did", node: node01 } as const; + const patch = parseReference(`rad:${repo00}/cob/${patchType}/${oid}`)!; + + test("keeps a host written into the template", () => { + const template = + "https://radicle.network/nodes/oak.radicle.garden/$rid$path"; + const node = "https://radicle.network/nodes/oak.radicle.garden"; + expect(explorerUrlFromTemplate(patch, template, "seed")).toBe( + `${node}/rad:${repo00}/patches/${oid}`, + ); + expect(explorerUrlFromTemplate(did, template, "seed")).toBe( + `${node}/users/did:key:${node01}`, + ); + }); + + test("fills a template of another shape", () => { + const template = "https://example.com/$host/repos/$rid$path"; + expect(explorerUrlFromTemplate(patch, template, "seed")).toBe( + `https://example.com/seed/repos/rad:${repo00}/patches/${oid}`, + ); + expect(explorerUrlFromTemplate(did, template, "seed")).toBe( + `https://example.com/seed/repos/users/did:key:${node01}`, + ); + }); + + test("has no user page without $rid", () => { + expect( + explorerUrlFromTemplate(did, "https://example.com/$host/$path", "seed"), + ).toBeUndefined(); + }); + + test("keeps replacement patterns in the host literal", () => { + expect( + explorerUrlFromTemplate(patch, "https://x/nodes/$host/$rid$path", "a$&b"), + ).toBe(`https://x/nodes/a$&b/rad:${repo00}/patches/${oid}`); + }); +}); + describe("explorer URL edge cases", () => { const base = "https://app.radicle.xyz"; const node = `${base}/nodes/seed.radicle.xyz`; diff --git a/desktop/tests/unit/utils.test.ts b/desktop/tests/unit/utils.test.ts index e2af74140..ed3d2cf3e 100644 --- a/desktop/tests/unit/utils.test.ts +++ b/desktop/tests/unit/utils.test.ts @@ -261,6 +261,22 @@ describe("explorer links", () => { ); }); + test("keeps a host written into the template", () => { + const publicExplorer = + "https://radicle.network/nodes/oak.radicle.garden/$rid$path"; + const preferredSeeds = [`${nid}@seed1.oak.radicle.garden:8776`]; + const node = "https://radicle.network/nodes/oak.radicle.garden"; + expect(explorerLink(repo, config({ publicExplorer, preferredSeeds }))).toBe( + `${node}/${rid}`, + ); + expect( + explorerLink( + { type: "did", node: nid }, + config({ publicExplorer, preferredSeeds }), + ), + ).toBe(`${node}/users/did:key:${nid}`); + }); + test("explorerHost returns the host of the template", () => { expect(explorerHost(config({}))).toBe("radicle.network"); });