From 00d8391a02de2f166a21ae63608fea6b075d7b4c Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Tue, 23 Apr 2024 13:47:51 +1200 Subject: [PATCH] fix(published-ports): fix published port link and into a new component [EE-6592] (#11657) --- .../ImageStatus/PublishedPortLink.tsx | 48 ++++++++++++++++ .../ContainersDatatable/columns/ports.tsx | 55 +++---------------- .../ServicesDatatable/columns/ports.tsx | 30 +++------- 3 files changed, 65 insertions(+), 68 deletions(-) create mode 100644 app/react/docker/components/ImageStatus/PublishedPortLink.tsx diff --git a/app/react/docker/components/ImageStatus/PublishedPortLink.tsx b/app/react/docker/components/ImageStatus/PublishedPortLink.tsx new file mode 100644 index 000000000..413aa1e10 --- /dev/null +++ b/app/react/docker/components/ImageStatus/PublishedPortLink.tsx @@ -0,0 +1,48 @@ +import { ExternalLink } from 'lucide-react'; + +import { Icon } from '@@/Icon'; + +type Props = { + hostURL?: string; + hostPort?: string | number; + containerPort?: string | number; +}; + +export function PublishedPortLink({ hostURL, hostPort, containerPort }: Props) { + return ( + + + {hostPort}:{containerPort} + + ); +} + +function generateContainerURL( + hostURL?: string, + hostPort?: string | number, + containerPort?: string | number +) { + const url = stripTrailingSlash(hostURL?.toLowerCase()); + + if (!url.startsWith('http://') && !url.startsWith('https://')) { + if (String(containerPort).endsWith('443')) { + return `https://${url}:${hostPort}`; + } + + return `http://${url}:${hostPort}`; + } + + return `${url}:${hostPort}`; +} + +function stripTrailingSlash(url?: string) { + if (!url) { + return ''; + } + return url.endsWith('/') ? url.slice(0, -1) : url; +} diff --git a/app/react/docker/containers/ListView/ContainersDatatable/columns/ports.tsx b/app/react/docker/containers/ListView/ContainersDatatable/columns/ports.tsx index 1c560f873..946789340 100644 --- a/app/react/docker/containers/ListView/ContainersDatatable/columns/ports.tsx +++ b/app/react/docker/containers/ListView/ContainersDatatable/columns/ports.tsx @@ -1,11 +1,8 @@ import _ from 'lodash'; -import { ExternalLink } from 'lucide-react'; import { CellContext } from '@tanstack/react-table'; import type { DockerContainer } from '@/react/docker/containers/types'; -import { getSchemeFromPort } from '@/react/common/network-utils'; - -import { Icon } from '@@/Icon'; +import { PublishedPortLink } from '@/react/docker/components/ImageStatus/PublishedPortLink'; import { useRowContext } from '../RowContext'; @@ -32,46 +29,12 @@ function Cell({ row }: CellContext) { return '-'; } - const publicURL = getPublicUrl(environment.PublicURL); - - return _.uniqBy(ports, 'public').map((port) => { - let url = publicURL || port.host || ''; - if (!url.startsWith('http')) { - const scheme = getSchemeFromPort(port.private); - url = `${scheme}://${url}`; - } - url = `${url}:${port.public}`; - - return ( - - - {port.public}:{port.private} - - ); - }); -} - -function getPublicUrl(url?: string): string { - if (!url) { - return ''; - } - - // Add protocol if missing - const u = - url.startsWith('http://') || url.startsWith('https://') - ? url - : `http://${url}`; - - try { - const parsedUrl = new URL(u); - return `${parsedUrl.protocol}://${parsedUrl.hostname}`; - } catch (error) { - return ''; - } + return _.uniqBy(ports, 'public').map((port) => ( + + )); } diff --git a/app/react/docker/services/ListView/ServicesDatatable/columns/ports.tsx b/app/react/docker/services/ListView/ServicesDatatable/columns/ports.tsx index 3f57e3f8f..399433572 100644 --- a/app/react/docker/services/ListView/ServicesDatatable/columns/ports.tsx +++ b/app/react/docker/services/ListView/ServicesDatatable/columns/ports.tsx @@ -1,11 +1,8 @@ -import { ExternalLink } from 'lucide-react'; import { CellContext } from '@tanstack/react-table'; import { ServiceViewModel } from '@/docker/models/service'; import { useCurrentEnvironment } from '@/react/hooks/useCurrentEnvironment'; -import { getSchemeFromPort } from '@/react/common/network-utils'; - -import { Icon } from '@@/Icon'; +import { PublishedPortLink } from '@/react/docker/components/ImageStatus/PublishedPortLink'; import { columnHelper } from './helper'; @@ -37,24 +34,13 @@ function Cell({ return '-'; } - const { PublicURL: publicUrl } = environmentQuery.data; - return ports .filter((port) => port.PublishedPort) - .map((port) => { - const scheme = getSchemeFromPort(port.TargetPort); - - return ( - - - {port.PublishedPort}:{port.TargetPort} - - ); - }); + .map((port) => ( + + )); }