Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

1.8 KB
Raw Blame
export interface WebInterfaceUrlFieldProps {
  metadataKind: 'guest' | 'agent' | 'docker' | 'docker-host';
  metadataId?: string;
  targetLabel?: string;
  title?: string;
  customUrl?: string;
  onCustomUrlChange?: (url: string) => void;
  embedded?: boolean;
  class?: string;
}

export function normalizeWebInterfaceUrl(value?: string | null): string {
  return (value || '').trim();
}

export type WebInterfaceUrlClassification =
  | { status: 'missing'; url: '' }
  | { status: 'invalid'; url: string; error: string }
  | { status: 'valid'; url: string };

export function classifyWebInterfaceUrl(value?: string | null): WebInterfaceUrlClassification {
  const url = normalizeWebInterfaceUrl(value);
  if (!url) {
    return { status: 'missing', url: '' };
  }

  let parsed: URL;
  try {
    parsed = new URL(url);
  } catch {
    return {
      status: 'invalid',
      url,
      error: 'Enter a valid URL (for example: https://198.51.100.100:8080).',
    };
  }

  if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
    return {
      status: 'invalid',
      url,
      error: 'URL must start with http:// or https://.',
    };
  }

  if (!parsed.hostname) {
    return {
      status: 'invalid',
      url,
      error: 'URL is missing a hostname or IP address.',
    };
  }

  return { status: 'valid', url };
}

export function validateWebInterfaceCustomUrl(value: string): string | null {
  const result = classifyWebInterfaceUrl(value);
  return result.status === 'invalid' ? result.error : null;
}

export function getWebInterfaceTargetLabel(
  metadataKind: WebInterfaceUrlFieldProps['metadataKind'],
  targetLabel?: string,
): string {
  const trimmed = normalizeWebInterfaceUrl(targetLabel);
  if (trimmed) return trimmed;
  return metadataKind === 'agent' ? 'agent' : 'workload';
}