"use client";
import { BarChart, LineChart } from "@mantine/charts";
import { Box, Group, Stack, Text, useMantineColorScheme } from "@mantine/core";
import { clientApi } from "@homarr/api/client";
import type { UmamiEventSeries } from "@homarr/integrations/types";
import { useScopedI18n } from "@homarr/translation/client";
import { EVENT_COLORS, formatXLabel, umamiQueryOptions } from "./umami-utils";
interface UmamiEventsContentProps {
integrationIds: string[];
websiteId: string;
timeFrame: string;
eventNames: string[];
chartType: string;
}
export function UmamiEventsContent({
integrationIds,
websiteId,
timeFrame,
eventNames,
chartType,
}: UmamiEventsContentProps) {
const t = useScopedI18n("widget.umami");
const { colorScheme } = useMantineColorScheme();
const tickColor = colorScheme === "dark" ? "#c1c2c5" : "#495057";
const { data: series = [] } = clientApi.widget.umami.getMultiEventTimeSeries.useQuery(
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, eventNames: [...eventNames].toSorted() },
umamiQueryOptions,
);
if (eventNames.length === 0) {
return (
{t("events.selectPrompt")}
);
}
// 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 = { label: formatXLabel(timestamp, timeFrame) };
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 (
{chartSeries.length > 1 && (
{chartSeries.map((serie) => (
{serie.name}
))}
)}
{chartType === "sparkline" ? (
) : (
)}
);
}