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.

1.9 KB
Raw Blame
import { Box, Group, Stack, Text } from "@mantine/core";
import { IconNetwork } from "@tabler/icons-react";

import { useByteFormatter } from "@homarr/settings";
import { useI18n } from "@homarr/translation/client";

import type { LabelDisplayModeOption } from "..";
import { CommonChart } from "./common-chart";

export const CombinedNetworkTrafficChart = ({
  usageOverTime,
  hasShadow,
  labelDisplayMode,
  advanced = false,
}: {
  usageOverTime: {
    up: number;
    down: number;
  }[];
  hasShadow: boolean;
  labelDisplayMode: LabelDisplayModeOption;
  advanced?: boolean;
}) => {
  const chartData = usageOverTime.map((usage, index) => ({
    index,
    up: usage.up,
    down: usage.down,
  }));
  const t = useI18n("widget.systemResources.card");
  const { formatByteRate } = useByteFormatter();

  const tooltipLabel = (index: number) => {
    const point = usageOverTime[index];
    return (
      <Stack gap={2}>
        <Group gap={4} wrap="nowrap">
          <Box bg="orange.5" w={8} h={8} style={{ borderRadius: 99, flexShrink: 0 }} />
          <Text size="xs">
            {t("up")}: {formatByteRate(Math.round(point?.up ?? 0))}
          </Text>
        </Group>
        <Group gap={4} wrap="nowrap">
          <Box bg="yellow.5" w={8} h={8} style={{ borderRadius: 99, flexShrink: 0 }} />
          <Text size="xs">
            {t("down")}: {formatByteRate(Math.round(point?.down ?? 0))}
          </Text>
        </Group>
      </Stack>
    );
  };

  return (
    <CommonChart
      data={chartData}
      dataKey={"index"}
      series={[
        { name: "up", color: "orange.5" },
        { name: "down", color: "yellow.5" },
      ]}
      title={t("network")}
      icon={IconNetwork}
      yAxisProps={{ domain: [0, "dataMax"] }}
      chartType={hasShadow ? "area" : "line"}
      labelDisplayMode={labelDisplayMode}
      advanced={advanced}
      tooltipLabel={tooltipLabel}
    />
  );
};