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.

2.4 KB
Raw Blame
export type StatusBadgeSize = 'sm' | 'md';

export interface StatusBadgeProps {
  isEnabled: boolean;
  disabled?: boolean;
  size?: StatusBadgeSize;
  onToggle?: () => void;
  labelEnabled?: string;
  labelDisabled?: string;
  titleEnabled?: string;
  titleDisabled?: string;
  titleWhenDisabled?: string;
  /** What the toggle switches, for a page with many alike On/Off badges. */
  ariaLabel?: string;
}

const STATUS_BADGE_BASE_CLASS =
  'inline-flex min-h-11 items-center justify-center rounded-md text-xs font-medium transition-colors focus:outline-hidden focus-visible:ring-2 focus-visible:ring-offset-1 focus-visible:ring-blue-400 sm:min-h-0';
const STATUS_BADGE_PADDING_BY_SIZE: Record<StatusBadgeSize, string> = {
  sm: 'px-2 py-0.5',
  md: 'px-2.5 py-1',
};
const STATUS_BADGE_ENABLED_CLASS =
  'bg-blue-50 text-blue-700 hover:bg-blue-100 dark:bg-blue-500 dark:text-blue-300 dark:hover:bg-blue-500';
const STATUS_BADGE_DISABLED_STATE_CLASS = 'text-muted hover:bg-surface-hover';
const STATUS_BADGE_INTERACTION_DISABLED_CLASS =
  'opacity-60 cursor-not-allowed hover:bg-transparent dark:hover:bg-transparent';

export function resolveStatusBadgeSize(size: StatusBadgeSize | undefined): StatusBadgeSize {
  return size ?? 'sm';
}

export function getStatusBadgeClass(
  size: StatusBadgeSize,
  isEnabled: boolean,
  disabled: boolean,
): string {
  return [
    STATUS_BADGE_BASE_CLASS,
    STATUS_BADGE_PADDING_BY_SIZE[size],
    isEnabled ? STATUS_BADGE_ENABLED_CLASS : STATUS_BADGE_DISABLED_STATE_CLASS,
    disabled ? STATUS_BADGE_INTERACTION_DISABLED_CLASS : '',
  ]
    .join(' ')
    .trim();
}

export function getStatusBadgeLabel(props: StatusBadgeProps): string {
  return props.isEnabled ? (props.labelEnabled ?? 'Enabled') : (props.labelDisabled ?? 'Disabled');
}

/**
 * A badge's accessible name: what it switches, then the visible state word,
 * so voice control can still say what is on screen (WCAG 2.5.3).
 */
export function getStatusBadgeAriaLabel(props: StatusBadgeProps): string | undefined {
  const context = props.ariaLabel?.trim();
  return context ? `${context}: ${getStatusBadgeLabel(props)}` : undefined;
}

export function getStatusBadgeTitle(props: StatusBadgeProps, disabled: boolean): string {
  if (disabled) {
    return props.titleWhenDisabled ?? props.titleDisabled ?? props.titleEnabled ?? '';
  }
  return props.isEnabled ? (props.titleEnabled ?? '') : (props.titleDisabled ?? '');
}