mirror of https://github.com/portainer/portainer
fix(published-ports): fix published port link and into a new component [EE-6592] (#11657)
parent
4403503d82
commit
00d8391a02
|
@ -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 (
|
||||||
|
<a
|
||||||
|
className="image-tag"
|
||||||
|
href={generateContainerURL(hostURL, hostPort, containerPort)}
|
||||||
|
target="_blank"
|
||||||
|
rel="noreferrer"
|
||||||
|
>
|
||||||
|
<Icon icon={ExternalLink} />
|
||||||
|
{hostPort}:{containerPort}
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -1,11 +1,8 @@
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { ExternalLink } from 'lucide-react';
|
|
||||||
import { CellContext } from '@tanstack/react-table';
|
import { CellContext } from '@tanstack/react-table';
|
||||||
|
|
||||||
import type { DockerContainer } from '@/react/docker/containers/types';
|
import type { DockerContainer } from '@/react/docker/containers/types';
|
||||||
import { getSchemeFromPort } from '@/react/common/network-utils';
|
import { PublishedPortLink } from '@/react/docker/components/ImageStatus/PublishedPortLink';
|
||||||
|
|
||||||
import { Icon } from '@@/Icon';
|
|
||||||
|
|
||||||
import { useRowContext } from '../RowContext';
|
import { useRowContext } from '../RowContext';
|
||||||
|
|
||||||
|
@ -32,46 +29,12 @@ function Cell({ row }: CellContext<DockerContainer, string>) {
|
||||||
return '-';
|
return '-';
|
||||||
}
|
}
|
||||||
|
|
||||||
const publicURL = getPublicUrl(environment.PublicURL);
|
return _.uniqBy(ports, 'public').map((port) => (
|
||||||
|
<PublishedPortLink
|
||||||
return _.uniqBy(ports, 'public').map((port) => {
|
key={`${port.host}:${port.public}`}
|
||||||
let url = publicURL || port.host || '';
|
hostPort={port.public}
|
||||||
if (!url.startsWith('http')) {
|
containerPort={port.private}
|
||||||
const scheme = getSchemeFromPort(port.private);
|
hostURL={environment.PublicURL || port.host}
|
||||||
url = `${scheme}://${url}`;
|
/>
|
||||||
}
|
));
|
||||||
url = `${url}:${port.public}`;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<a
|
|
||||||
key={`${port.host}:${port.public}`}
|
|
||||||
className="image-tag"
|
|
||||||
href={url}
|
|
||||||
target="_blank"
|
|
||||||
rel="noreferrer"
|
|
||||||
>
|
|
||||||
<Icon icon={ExternalLink} />
|
|
||||||
{port.public}:{port.private}
|
|
||||||
</a>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
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 '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
import { ExternalLink } from 'lucide-react';
|
|
||||||
import { CellContext } from '@tanstack/react-table';
|
import { CellContext } from '@tanstack/react-table';
|
||||||
|
|
||||||
import { ServiceViewModel } from '@/docker/models/service';
|
import { ServiceViewModel } from '@/docker/models/service';
|
||||||
import { useCurrentEnvironment } from '@/react/hooks/useCurrentEnvironment';
|
import { useCurrentEnvironment } from '@/react/hooks/useCurrentEnvironment';
|
||||||
import { getSchemeFromPort } from '@/react/common/network-utils';
|
import { PublishedPortLink } from '@/react/docker/components/ImageStatus/PublishedPortLink';
|
||||||
|
|
||||||
import { Icon } from '@@/Icon';
|
|
||||||
|
|
||||||
import { columnHelper } from './helper';
|
import { columnHelper } from './helper';
|
||||||
|
|
||||||
|
@ -37,24 +34,13 @@ function Cell({
|
||||||
return '-';
|
return '-';
|
||||||
}
|
}
|
||||||
|
|
||||||
const { PublicURL: publicUrl } = environmentQuery.data;
|
|
||||||
|
|
||||||
return ports
|
return ports
|
||||||
.filter((port) => port.PublishedPort)
|
.filter((port) => port.PublishedPort)
|
||||||
.map((port) => {
|
.map((port) => (
|
||||||
const scheme = getSchemeFromPort(port.TargetPort);
|
<PublishedPortLink
|
||||||
|
hostPort={port.PublishedPort}
|
||||||
return (
|
containerPort={port.TargetPort}
|
||||||
<a
|
hostURL={environmentQuery.data.PublicURL}
|
||||||
key={`${publicUrl}:${port.PublishedPort}`}
|
/>
|
||||||
className="image-tag vertical-center"
|
));
|
||||||
href={`${scheme}://${publicUrl}:${port.PublishedPort}`}
|
|
||||||
target="_blank"
|
|
||||||
rel="noreferrer"
|
|
||||||
>
|
|
||||||
<Icon icon={ExternalLink} />
|
|
||||||
{port.PublishedPort}:{port.TargetPort}
|
|
||||||
</a>
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue