From 4df98100889170ae6e83482512a8ffa1df0b2c45 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 14 Jun 2023 10:18:09 +0200 Subject: [PATCH] Rename hostnamePort to hostAndPort Signed-off-by: Sebastian Martinez --- httpd-client/index.ts | 2 +- src/App/Header/Connect.svelte | 4 +-- src/App/Header/Search.svelte | 7 +++-- src/App/Header/SearchResultsModal.svelte | 4 +-- src/lib/router.ts | 38 ++++++++++++------------ src/lib/utils.ts | 26 +++++++++------- src/views/home/Index.svelte | 4 +-- src/views/projects/Header.svelte | 8 ++--- src/views/projects/Issue.svelte | 2 +- src/views/projects/View.svelte | 6 ++-- src/views/projects/router.ts | 24 +++++++-------- src/views/seeds/View.svelte | 7 ++--- src/views/seeds/router.ts | 6 ++-- src/views/session/Index.svelte | 5 +++- tests/unit/router.test.ts | 10 +++---- 15 files changed, 78 insertions(+), 75 deletions(-) diff --git a/httpd-client/index.ts b/httpd-client/index.ts index 77ce6811c..3543a2723 100644 --- a/httpd-client/index.ts +++ b/httpd-client/index.ts @@ -125,7 +125,7 @@ export class HttpdClient { }`; } - public get hostnamePort(): string { + public get hostAndPort(): string { return `${this.#baseUrl.hostname}:${this.#baseUrl.port}`; } diff --git a/src/App/Header/Connect.svelte b/src/App/Header/Connect.svelte index 5c52415f7..f4b997e84 100644 --- a/src/App/Header/Connect.svelte +++ b/src/App/Header/Connect.svelte @@ -120,7 +120,7 @@ route={{ resource: "seeds", params: { - hostnamePort: httpd.api.hostnamePort, + hostAndPort: httpd.api.hostAndPort, projectPageIndex: 0, }, }}> @@ -152,7 +152,7 @@ route={{ resource: "seeds", params: { - hostnamePort: httpd.api.hostnamePort, + hostAndPort: httpd.api.hostAndPort, projectPageIndex: 0, }, }}> diff --git a/src/App/Header/Search.svelte b/src/App/Header/Search.svelte index d96d4180f..0a3b2c131 100644 --- a/src/App/Header/Search.svelte +++ b/src/App/Header/Search.svelte @@ -6,7 +6,7 @@ import * as modal from "@app/lib/modal"; import * as router from "@app/lib/router"; import { searchPlaceholder } from "@app/lib/shared"; - import { unreachable } from "@app/lib/utils"; + import { getHostAndPort, unreachable } from "@app/lib/utils"; import Icon from "@app/components/Icon.svelte"; import SearchResultsModal from "@app/App/Header/SearchResultsModal.svelte"; @@ -47,13 +47,14 @@ } else if (searchResult.type === "projects") { input = ""; if (searchResult.results.length === 1) { + const { project, baseUrl } = searchResult.results[0]; void router.push({ resource: "projects", params: { view: { resource: "tree" }, - id: searchResult.results[0].project.id, + id: project.id, peer: undefined, - hostnamePort: searchResult.results[0].baseUrl.hostname, + hostAndPort: getHostAndPort(baseUrl), hash: undefined, search: undefined, }, diff --git a/src/App/Header/SearchResultsModal.svelte b/src/App/Header/SearchResultsModal.svelte index f058e0d13..432557797 100644 --- a/src/App/Header/SearchResultsModal.svelte +++ b/src/App/Header/SearchResultsModal.svelte @@ -2,7 +2,7 @@ import type { ProjectBaseUrl } from "@app/lib/search"; import * as modal from "@app/lib/modal"; - import { formatRepositoryId } from "@app/lib/utils"; + import { formatRepositoryId, getHostAndPort } from "@app/lib/utils"; import Link from "@app/components/Link.svelte"; import Modal from "@app/components/Modal.svelte"; @@ -40,7 +40,7 @@ resource: "projects", params: { view: { resource: "tree" }, - hostnamePort: result.baseUrl.hostname, + hostAndPort: getHostAndPort(result.baseUrl), id: result.project.id, }, }}> diff --git a/src/lib/router.ts b/src/lib/router.ts index 91d60379d..a37ebbfe8 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -124,8 +124,8 @@ function pathToRoute(url: URL): Route | null { const resource = segments.shift(); switch (resource) { case "seeds": { - const hostnamePort = segments.shift(); - if (hostnamePort) { + const hostAndPort = segments.shift(); + if (hostAndPort) { const id = segments.shift(); if (id) { // Allows project paths with or without trailing slash @@ -139,17 +139,17 @@ function pathToRoute(url: URL): Route | null { view: { resource: "tree" }, id, peer: undefined, - hostnamePort, + hostAndPort, }, }; } - const params = resolveProjectRoute(url, hostnamePort, id, segments); + const params = resolveProjectRoute(url, hostAndPort, id, segments); if (params) { return { resource: "projects", params: { ...params, - hostnamePort, + hostAndPort, id, }, }; @@ -158,7 +158,7 @@ function pathToRoute(url: URL): Route | null { } return { resource: "seeds", - params: { hostnamePort, projectPageIndex: 0 }, + params: { hostAndPort, projectPageIndex: 0 }, }; } return null; @@ -192,11 +192,11 @@ export function routeToPath(route: Route) { } else if (route.resource === "session") { return `/session?id=${route.params.id}&sig=${route.params.signature}&pk=${route.params.publicKey}`; } else if (route.resource === "seeds") { - return `/seeds/${route.params.hostnamePort}`; + return `/seeds/${route.params.hostAndPort}`; } else if (route.resource === "loadError") { return ""; } else if (route.resource === "projects") { - const hostnamePortPrefix = `/seeds/${route.params.hostnamePort}`; + const hostAndPortPrefix = `/seeds/${route.params.hostAndPort}`; const content = `/${route.params.view.resource}`; let peer = ""; @@ -230,31 +230,31 @@ export function routeToPath(route: Route) { if (route.params.view.resource === "tree") { if (suffix) { - return `${hostnamePortPrefix}/${route.params.id}${peer}/tree${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/tree${suffix}`; } - return `${hostnamePortPrefix}/${route.params.id}${peer}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}`; } else if (route.params.view.resource === "commits") { - return `${hostnamePortPrefix}/${route.params.id}${peer}/commits${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/commits${suffix}`; } else if (route.params.view.resource === "history") { - return `${hostnamePortPrefix}/${route.params.id}${peer}/history${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/history${suffix}`; } else if ( route.params.view.resource === "issues" && route.params.view.params?.view.resource === "new" ) { - return `${hostnamePortPrefix}/${route.params.id}${peer}/issues/new${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/issues/new${suffix}`; } else if (route.params.view.resource === "issues") { - return `${hostnamePortPrefix}/${route.params.id}${peer}/issues${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/issues${suffix}`; } else if (route.params.view.resource === "issue") { - return `${hostnamePortPrefix}/${route.params.id}${peer}/issues/${route.params.view.params.issue}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/issues/${route.params.view.params.issue}`; } else if (route.params.view.resource === "patches") { - return `${hostnamePortPrefix}/${route.params.id}${peer}/patches${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/patches${suffix}`; } else if (route.params.view.resource === "patch") { if (route.params.view.params.revision) { - return `${hostnamePortPrefix}/${route.params.id}${peer}/patches/${route.params.view.params.patch}/${route.params.view.params.revision}${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/patches/${route.params.view.params.patch}/${route.params.view.params.revision}${suffix}`; } - return `${hostnamePortPrefix}/${route.params.id}${peer}/patches/${route.params.view.params.patch}${suffix}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}/patches/${route.params.view.params.patch}${suffix}`; } else { - return `${hostnamePortPrefix}/${route.params.id}${peer}${content}`; + return `${hostAndPortPrefix}/${route.params.id}${peer}${content}`; } } else if (route.resource === "booting") { return ""; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index ce1cfd72f..4af7b7ed8 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -244,18 +244,18 @@ export function twemoji( }); } -export function extractBaseUrl(hostnamePort: string): BaseUrl { +export function extractBaseUrl(hostAndPort: string): BaseUrl { if ( - hostnamePort === "radicle.local" || - hostnamePort === "radicle.local:8080" || - hostnamePort === "0.0.0.0" || - hostnamePort === "0.0.0.0:8080" || - hostnamePort === "127.0.0.1" || - hostnamePort === "127.0.0.1:8080" + hostAndPort === "radicle.local" || + hostAndPort === "radicle.local:8080" || + hostAndPort === "0.0.0.0" || + hostAndPort === "0.0.0.0:8080" || + hostAndPort === "127.0.0.1" || + hostAndPort === "127.0.0.1:8080" ) { return { hostname: "127.0.0.1", port: 8080, scheme: "http" }; - } else if (hostnamePort.includes(":")) { - const [hostname, port] = hostnamePort.split(":"); + } else if (hostAndPort.includes(":")) { + const [hostname, port] = hostAndPort.split(":"); return { hostname, port: Number(port), @@ -263,13 +263,19 @@ export function extractBaseUrl(hostnamePort: string): BaseUrl { }; } else { return { - hostname: hostnamePort, + hostname: hostAndPort, port: config.seeds.defaultHttpdPort, scheme: config.seeds.defaultHttpdScheme, }; } } +export function getHostAndPort(baseUrl: BaseUrl): string { + return baseUrl.port === config.seeds.defaultHttpdPort + ? baseUrl.hostname + : `${baseUrl.hostname}:${baseUrl.port}`; +} + export function createAddRemoveArrays( currentArray: string[], newArray: string[], diff --git a/src/views/home/Index.svelte b/src/views/home/Index.svelte index 11e032cb3..4c3d5aaef 100644 --- a/src/views/home/Index.svelte +++ b/src/views/home/Index.svelte @@ -1,7 +1,7 @@