Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

6.0 KB
Raw Blame
"use client";

import React from "react";
import { Badge, Box, Card, Flex, Group, Text, VisuallyHidden } from "@mantine/core";
import {
  IconArrowsExchange,
  IconCircleCheckFilled,
  IconCircleXFilled,
  IconMapPin,
  IconQuestionMark,
  IconShieldCheck,
  IconShieldX,
} from "@tabler/icons-react";

import type { RouterOutputs } from "@homarr/api";
import { useRequiredBoard } from "@homarr/boards/context";
import { useI18n } from "@homarr/translation/client";

import { getStatusColor, RUNNING_STATUS } from "./helpers";

export type VpnInfo = RouterOutputs["widget"]["vpn"]["getSummaries"][number]["summary"];

const neutralBorderColor = "rgb(from var(--mantine-color-default-border) r g b / calc(var(--opacity, 1) * 0.45))";

export function VpnIntegrationCard({
  vpn,
  integrationName,
  variant = "single",
  dense = false,
}: {
  vpn: VpnInfo;
  integrationName?: string;
  variant?: "single" | "list";
  dense?: boolean;
}) {
  const board = useRequiredBoard();
  const compact = variant === "list";

  const content = vpn ? (
    <Flex direction="row" w="100%" align="center" gap="xs">
      <VpnStatusColumn vpnStatus={vpn.vpnStatus} dnsStatus={vpn.dnsStatus} compact={compact} dense={dense} />
      <VpnInfoColumn
        integrationName={compact ? integrationName : undefined}
        publicIp={vpn.publicIp}
        city={vpn.city}
        country={vpn.country}
        provider={vpn.vpnProvider?.provider}
        protocol={vpn.vpnProvider?.protocol}
        compact={compact}
        dense={dense}
      />
    </Flex>
  ) : (
    <VpnUnavailableContent compact={compact} dense={dense} integrationName={compact ? integrationName : undefined} />
  );

  if (compact) {
    return (
      <Card
        withBorder
        radius={board.itemRadius}
        p={dense ? 6 : "xs"}
        w="100%"
        bg="transparent"
        style={{ borderColor: neutralBorderColor }}
      >
        {content}
      </Card>
    );
  }

  return <Box w="100%">{content}</Box>;
}

function VpnUnavailableContent({
  compact,
  dense,
  integrationName,
}: {
  compact: boolean;
  dense: boolean;
  integrationName?: string;
}) {
  const t = useI18n("widget.vpn");

  return (
    <Flex direction="row" w="100%" align="center" gap="xs">
      {/* An unavailable VPN is red; unknown DNS status stays neutral. */}
      <VpnStatusColumn vpnStatus="" dnsStatus="" compact={compact} dense={dense} />
      <Flex gap={2} direction="column" w="100%" align="flex-start" style={{ minWidth: 0 }}>
        {integrationName ? (
          <Text fw={600} size="xs" lh={1.2} c="dimmed" lineClamp={1}>
            {integrationName}
          </Text>
        ) : null}
        <Text fw={700} size={dense ? "sm" : compact ? "md" : "lg"} lh={1.2} c="red" lineClamp={2}>
          {t("serviceUnavailable")}
        </Text>
      </Flex>
    </Flex>
  );
}

function VpnStatusColumn({
  vpnStatus,
  dnsStatus,
  compact,
  dense,
}: {
  vpnStatus: string;
  dnsStatus?: string;
  compact: boolean;
  dense: boolean;
}) {
  const t = useI18n("widget.vpn");
  // Connected shows a shield with a check; anything else (stopped or unavailable) shows a shield with a cross.
  const ShieldIcon = vpnStatus === RUNNING_STATUS ? IconShieldCheck : IconShieldX;
  const statusLabel = t(
    vpnStatus === RUNNING_STATUS ? "status.running" : vpnStatus ? "status.notRunning" : "status.unavailable",
  );

  return (
    <Flex
      gap={4}
      direction="column"
      w={compact ? "28%" : "30%"}
      miw={dense ? 44 : compact ? 56 : undefined}
      align="center"
    >
      <ShieldIcon
        aria-label={t("status.vpn", { status: statusLabel })}
        stroke={2}
        color={getStatusColor(vpnStatus)}
        size={dense ? 32 : compact ? 44 : 52}
      />
      <DnsStatusBadge status={dnsStatus} />
    </Flex>
  );
}

function VpnInfoColumn({
  integrationName,
  publicIp,
  city,
  country,
  provider,
  protocol,
  compact,
  dense,
}: {
  integrationName?: string;
  publicIp: string;
  city?: string;
  country?: string;
  provider?: string;
  protocol?: string;
  compact: boolean;
  dense: boolean;
}) {
  return (
    <Flex gap={2} direction="column" w="100%" align="flex-start" style={{ minWidth: 0 }}>
      {integrationName ? (
        <Text fw={600} size="xs" lh={1.2} c="dimmed" lineClamp={1}>
          {integrationName}
        </Text>
      ) : null}
      <Text fw={700} size={dense ? "md" : compact ? "xl" : "2xl"} lh={1.1} lineClamp={1}>
        {publicIp || "—"}
      </Text>
      {!dense && (city || country) && (
        <Group gap={4} justify="flex-start" wrap="nowrap">
          <IconMapPin aria-hidden size="var(--mantine-font-size-xs)" />
          <Text fs="italic" fw={500} size="sm" lh={1.2} lineClamp={1}>
            {[city, country].filter(Boolean).join(", ")}
          </Text>
        </Group>
      )}
      {!dense && (provider || protocol) && <VpnProviderDetails provider={provider} protocol={protocol} />}
    </Flex>
  );
}

function VpnProviderDetails({ provider, protocol }: { provider?: string; protocol?: string }) {
  return (
    <Group gap={6} justify="flex-start" wrap="nowrap" style={{ minWidth: 0 }}>
      <Text fz="xs" lh={1.2} tt="capitalize" lineClamp={1}>
        {provider}
      </Text>
      <IconArrowsExchange stroke={1} size="var(--mantine-font-size-xs)" style={{ flexShrink: 0 }} />
      <Text fz="xs" lh={1.2} tt="capitalize" lineClamp={1}>
        {protocol}
      </Text>
    </Group>
  );
}

function DnsStatusBadge({ status }: { status?: string }) {
  const t = useI18n("widget.vpn");
  let StatusIcon = IconQuestionMark;
  let color = "gray";
  let statusLabel = t("status.unavailable");
  if (status) {
    color = getStatusColor(status);
    StatusIcon = IconCircleXFilled;
    statusLabel = t("status.notRunning");
    if (status === RUNNING_STATUS) {
      StatusIcon = IconCircleCheckFilled;
      statusLabel = t("status.running");
    }
  }
  return (
    <Badge
      variant="outline"
      color={color}
      size="xs"
      radius="xl"
      leftSection={<StatusIcon aria-hidden size="var(--mantine-font-size-xs)" />}
    >
      DNS
      <VisuallyHidden> {statusLabel}</VisuallyHidden>
    </Badge>
  );
}