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.

4.0 KB
Raw Blame
import type { MantineColor, RingProgressProps } from "@mantine/core";
import { Card, Center, Group, RingProgress, ScrollArea, Stack, Text, Title, Tooltip } from "@mantine/core";
import { IconDatabaseHeart, IconFileDescription, IconHeartbeat, IconTransform } from "@tabler/icons-react";

import { useRequiredBoard } from "@homarr/boards/context";
import type { TdarrPieSegment, TdarrStatistics } from "@homarr/integrations";
import { useByteFormatter } from "@homarr/settings";
import { useI18n } from "@homarr/translation/client";
import type { TablerIcon } from "@homarr/ui";

const PIE_COLORS: MantineColor[] = ["cyan", "grape", "gray", "orange", "pink"];

interface StatisticsPanelProps {
  statistics: TdarrStatistics;
}

export function StatisticsPanel(props: StatisticsPanelProps) {
  const t = useI18n("widget.mediaTranscoding.panel.statistics");
  const tCommon = useI18n("widget.mediaTranscoding.panel.common");
  const { formatBytes } = useByteFormatter();

  const allLibs = props.statistics;

  // Check if Tdarr hs any Files
  if (!(allLibs.totalFileCount > 0)) {
    return (
      <Center style={{ flex: "1" }}>
        <Title order={6}>{tCommon("empty")}</Title>
      </Center>
    );
  }

  return (
    <ScrollArea h="100%">
      <Group wrap="wrap" justify="center" p={4} w="100%" gap="xs">
        <StatisticItem
          icon={IconTransform}
          label={tCommon("transcodes")}
          value={props.statistics.totalTranscodeCount}
        />
        <StatisticItem
          icon={IconHeartbeat}
          label={tCommon("healthChecks")}
          value={props.statistics.totalHealthCheckCount}
        />
        <StatisticItem icon={IconFileDescription} label={t("filesCount")} value={props.statistics.totalFileCount} />
        <StatisticItem
          icon={IconDatabaseHeart}
          label={t("savedSpace")}
          value={formatBytes(Math.floor(allLibs.totalSavedSpace))}
        />
      </Group>
      <Group justify="center" wrap="wrap" grow>
        <StatisticRingProgress items={allLibs.transcodeStatus} label={tCommon("transcodes")} />
        <StatisticRingProgress items={allLibs.healthCheckStatus} label={tCommon("healthChecks")} />
        <StatisticRingProgress items={allLibs.videoCodecs} label={t("videoCodecs")} />
        <StatisticRingProgress items={allLibs.videoContainers} label={t("videoContainers")} />
        <StatisticRingProgress items={allLibs.videoResolutions} label={t("videoResolutions")} />
      </Group>
    </ScrollArea>
  );
}

interface StatisticRingProgressProps {
  items: TdarrPieSegment[];
  label: string;
}

const StatisticRingProgress = ({ items, label }: StatisticRingProgressProps) => {
  return (
    <Stack align="center" gap={0} miw={60}>
      <Text size="10px" ta="center" style={{ whiteSpace: "nowrap" }}>
        {label}
      </Text>
      <RingProgress size={60} thickness={6} sections={toRingProgressSections(items)} />
    </Stack>
  );
};

export function toRingProgressSections(segments: TdarrPieSegment[]): RingProgressProps["sections"] {
  const total = segments.reduce((prev, curr) => prev + curr.value, 0);
  if (total <= 0) return [];
  return segments.map((segment, index) => ({
    value: (segment.value * 100) / total,
    tooltip: `${segment.name}: ${segment.value}`,
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    color: PIE_COLORS[index % PIE_COLORS.length]!, // Ensures a valid color in the case that index > PIE_COLORS.length
  }));
}

interface StatisticItemProps {
  icon: TablerIcon;
  value: string | number;
  label: string;
}

function StatisticItem(props: StatisticItemProps) {
  const board = useRequiredBoard();
  return (
    <Tooltip label={props.label}>
      <Card p={0} radius={board.itemRadius} miw={48} flex={1} bg="transparent">
        <Group justify="center" align="center" gap="xs" w="100%" wrap="nowrap">
          <props.icon size="var(--mantine-font-size-md)" style={{ flexShrink: 0 }} />
          <Text size="md">{props.value}</Text>
        </Group>
      </Card>
    </Tooltip>
  );
}