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.

3.7 KB
Raw Blame
"use client";

import { BarChart, LineChart } from "@mantine/charts";
import { Box, Group, Stack, Text } from "@mantine/core";

import type { UmamiEventSeries } from "@homarr/integrations/types";
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";

import { EVENT_COLORS, formatXLabel } from "./umami-utils";

interface UmamiEventsContentProps {
  series: UmamiEventSeries[];
  timeFrame: string;
  hasSelectedEvents: boolean;
  chartType: string;
  showXAxis: boolean;
}

export function UmamiEventsContent({
  series,
  timeFrame,
  hasSelectedEvents,
  chartType,
  showXAxis,
}: UmamiEventsContentProps) {
  const t = useI18n("widget.umami");
  const locale = useCurrentIntlLocale();
  const tickColor = "var(--mantine-color-dimmed)";

  if (!hasSelectedEvents) {
    return (
      <Stack align="center" justify="center" h="100%">
        <Text c="dimmed" size="sm">
          {t("events.selectPrompt")}
        </Text>
      </Stack>
    );
  }

  // Collect all unique timestamps across all series
  const allTimestamps = Array.from(
    new Set(series.flatMap(({ dataPoints }) => dataPoints.map(({ x: xPoint }) => xPoint))),
  ).toSorted();

  // Build per-event lookup by timestamp string
  const byEvent = new Map(
    series.map((serie: UmamiEventSeries) => [
      serie.eventName,
      new Map(serie.dataPoints.map((point) => [point.x, point.y])),
    ]),
  );

  const chartData = allTimestamps.map((timestamp) => {
    const row: Record<string, string | number> = { label: formatXLabel(timestamp, timeFrame, locale) };
    for (const serie of series) {
      row[serie.eventName] = byEvent.get(serie.eventName)?.get(timestamp) ?? 0;
    }
    return row;
  });

  const chartSeries = series.map((serie: UmamiEventSeries, index: number) => ({
    name: serie.eventName,
    color: EVENT_COLORS[index % EVENT_COLORS.length] ?? "blue.5",
  }));

  return (
    <Stack gap={4} h="100%">
      {chartSeries.length > 1 && (
        <Group gap={12} justify="center" wrap="wrap" style={{ flexShrink: 0, rowGap: 2 }}>
          {chartSeries.map((serie) => (
            <Group key={serie.name} gap={4} align="center" wrap="nowrap">
              <Box
                w={8}
                h={8}
                style={{
                  borderRadius: "50%",
                  backgroundColor: `var(--mantine-color-${serie.color.replace(".", "-")})`,
                  flexShrink: 0,
                }}
              />
              <Text size="xs" c="dimmed" truncate>
                {serie.name}
              </Text>
            </Group>
          ))}
        </Group>
      )}
      <Box style={{ flex: 1, minHeight: 0 }}>
        {chartType === "sparkline" ? (
          <LineChart
            h="100%"
            data={chartData}
            dataKey="label"
            series={chartSeries}
            withDots={false}
            curveType="monotone"
            tickLine="none"
            gridAxis="none"
            withLegend={false}
            withTooltip
            withXAxis={showXAxis}
            withYAxis={false}
            xAxisProps={{
              tick: { fontSize: 9, fill: tickColor },
              interval: "preserveStartEnd",
            }}
          />
        ) : (
          <BarChart
            h="100%"
            data={chartData}
            dataKey="label"
            series={chartSeries}
            tickLine="none"
            gridAxis="none"
            withLegend={false}
            withTooltip
            withXAxis={showXAxis}
            withYAxis={false}
            barProps={{ radius: 2 }}
            xAxisProps={{
              tick: { fontSize: 9, fill: tickColor },
              interval: "preserveStartEnd",
            }}
          />
        )}
      </Box>
    </Stack>
  );
}