project-nomad/admin/inertia/lib/navigation.ts

27 lines
872 B
TypeScript

export function getServiceLink(ui_location: string): string {
// Check if the ui location is a valid URL
try {
const url = new URL(ui_location);
// If it is a valid URL, return it as is
return url.href;
} catch (e) {
// If it fails, it means it's not a valid URL
}
// Check if the ui location is a port number (strict: must be only digits)
const parsedPort = parseInt(ui_location, 10);
if (!isNaN(parsedPort) && String(parsedPort) === ui_location.trim()) {
// If it's a port number, return a link to the service on that port
return `http://${window.location.hostname}:${parsedPort}`;
}
const pathPattern = /^\/.+/;
if (pathPattern.test(ui_location)) {
// If it starts with a slash, treat it as a full path
return ui_location;
}
return `/${ui_location}`;
}