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.

7.0 KB
Raw Blame
import type { PropsWithChildren } from "react";
import { Badge, Center, Divider, Flex, Group, List, Popover, RingProgress, Stack, Text } from "@mantine/core";
import {
  IconArrowNarrowDown,
  IconArrowNarrowUp,
  IconBrain,
  IconClockHour3,
  IconCpu,
  IconDatabase,
  IconDeviceLaptop,
  IconHeartBolt,
  IconNetwork,
  IconQuestionMark,
  IconServer,
} from "@tabler/icons-react";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";

import { capitalize } from "@homarr/common";
import type { ComputeResource, Resource, StorageResource } from "@homarr/integrations/types";
import { useByteFormatter } from "@homarr/settings";
import { useI18n } from "@homarr/translation/client";
import { zoomCompensatedSize } from "@homarr/ui";

dayjs.extend(duration);

interface ResourcePopoverProps {
  item: Resource;
}

export const ResourcePopover = ({ item, children }: PropsWithChildren<ResourcePopoverProps>) => {
  return (
    <Popover
      withArrow
      withinPortal
      radius="lg"
      shadow="sm"
      transitionProps={{
        transition: "pop",
      }}
    >
      {children}
      <Popover.Dropdown>
        <ResourceTypeEntryDetails item={item} />
      </Popover.Dropdown>
    </Popover>
  );
};

export const ResourceTypeEntryDetails = ({ item }: { item: Resource }) => {
  const t = useI18n("widget.healthMonitoring.cluster.popover");
  return (
    <Stack gap={0}>
      <Group wrap="nowrap" align="start" justify="apart">
        <Group wrap="nowrap" align="center">
          <ResourceIcon type={item.type} size={35} />
          <Stack gap={0}>
            <Text fw={700} size="md">
              {item.name}
            </Text>
            <Text c={item.isRunning ? "green" : "yellow"}>{capitalize(item.status)}</Text>
          </Stack>
        </Group>
        <Group align="end">
          {item.type === "node" && <RightSection label={t("rightSection.node")} value={item.node} />}
          {item.type === "lxc" && <RightSection label={t("rightSection.vmId")} value={item.vmId} />}
          {item.type === "qemu" && <RightSection label={t("rightSection.vmId")} value={item.vmId} />}
          {item.type === "storage" && <RightSection label={t("rightSection.plugin")} value={item.storagePlugin} />}
        </Group>
      </Group>
      <Divider mt={0} mb="xs" />
      {item.type !== "storage" && <ComputeResourceDetails item={item} />}
      {item.type === "storage" && <StorageResourceDetails item={item} />}
    </Stack>
  );
};

interface RightSectionProps {
  label: string;
  value: string | number;
}

const RightSection = ({ label, value }: RightSectionProps) => {
  return (
    <Stack align="end" gap={0}>
      <Text fw={200} size="sm">
        {label}
      </Text>
      <Text c="dimmed" size="xs">
        {value}
      </Text>
    </Stack>
  );
};

const ComputeResourceDetails = ({ item }: { item: ComputeResource }) => {
  const t = useI18n("widget.healthMonitoring.cluster.popover.detail");
  const { formatBytesPair } = useByteFormatter();
  const memory = formatBytesPair(item.memory.used, item.memory.total);
  const storage = formatBytesPair(item.storage.used, item.storage.total);
  return (
    <List>
      <List.Item icon={<IconCpu size="var(--mantine-font-size-md)" />}>
        {t("cpu")} - {item.cpu.cores}
      </List.Item>
      <List.Item icon={<IconBrain size="var(--mantine-font-size-md)" />}>
        {t("memory")} - {memory.used} / {memory.total}
      </List.Item>
      <List.Item icon={<IconDatabase size="var(--mantine-font-size-md)" />}>
        {t("storage")} - {storage.used} / {storage.total}
      </List.Item>
      <List.Item icon={<IconClockHour3 size="var(--mantine-font-size-md)" />}>
        {t("uptime")} - {dayjs(dayjs().add(-item.uptime, "seconds")).fromNow(true)}
      </List.Item>
      {item.haState && (
        <List.Item icon={<IconHeartBolt size="var(--mantine-font-size-md)" />}>
          {t("haState")} - {capitalize(item.haState)}
        </List.Item>
      )}
      <NetStats item={item} />
      <DiskStats item={item} />
    </List>
  );
};

const StorageResourceDetails = ({ item }: { item: StorageResource }) => {
  const t = useI18n("widget.healthMonitoring.cluster.popover.detail");
  const { formatBytesPair } = useByteFormatter();
  const storagePercent = item.total ? (item.used / item.total) * 100 : 0;
  const storage = formatBytesPair(item.used, item.total);
  return (
    <Stack gap={0}>
      <Center>
        <RingProgress
          roundCaps
          size={100}
          thickness={10}
          label={<Text ta="center">{storagePercent.toFixed(1)}%</Text>}
          sections={[{ value: storagePercent, color: storagePercent > 75 ? "orange" : "green" }]}
        />
        <Group align="center" gap={0}>
          <Text>
            {t("storage")} - {storage.used} / {storage.total}
          </Text>
        </Group>
      </Center>
      <Flex gap="sm" mt={0} justify="end">
        <StorageType item={item} />
      </Flex>
    </Stack>
  );
};

const DiskStats = ({ item }: { item: ComputeResource }) => {
  const { formatBytes } = useByteFormatter();
  if (!item.storage.read || !item.storage.write) {
    return null;
  }
  return (
    <List.Item icon={<IconDatabase size="var(--mantine-font-size-md)" />}>
      <Group gap="sm">
        <Group gap={0}>
          <Text>{formatBytes(item.storage.write)}</Text>
          <IconArrowNarrowDown size="var(--mantine-font-size-sm)" />
        </Group>
        <Group gap={0}>
          <Text>{formatBytes(item.storage.read)}</Text>
          <IconArrowNarrowUp size="var(--mantine-font-size-sm)" />
        </Group>
      </Group>
    </List.Item>
  );
};

const NetStats = ({ item }: { item: ComputeResource }) => {
  const { formatBytes } = useByteFormatter();
  if (!item.network.in || !item.network.out) {
    return null;
  }
  return (
    <List.Item icon={<IconNetwork size="var(--mantine-font-size-md)" />}>
      <Group gap="sm">
        <Group gap={0}>
          <Text>{formatBytes(item.network.in)}</Text>
          <IconArrowNarrowDown size="var(--mantine-font-size-sm)" />
        </Group>
        <Group gap={0}>
          <Text>{formatBytes(item.network.out)}</Text>
          <IconArrowNarrowUp size="var(--mantine-font-size-sm)" />
        </Group>
      </Group>
    </List.Item>
  );
};

const StorageType = ({ item }: { item: StorageResource }) => {
  const t = useI18n("widget.healthMonitoring.cluster.popover.detail.storageType");
  if (item.isShared) {
    return <Badge color="blue">{t("shared")}</Badge>;
  } else {
    return <Badge>{t("local")}</Badge>;
  }
};

const ResourceIcon = ({ type, size }: { type: Resource["type"]; size: number }) => {
  const style = zoomCompensatedSize(size);
  switch (type) {
    case "node":
      return <IconServer style={style} />;
    case "lxc":
      return <IconDeviceLaptop style={style} />;
    case "qemu":
      return <IconDeviceLaptop style={style} />;
    case "storage":
      return <IconDatabase style={style} />;
    default:
      console.error(`Unknown resource type: ${type as string}`);
      return <IconQuestionMark style={style} />;
  }
};