"use client"; import { BarChart, LineChart } from "@mantine/charts"; import { Box, Group, Stack, Text } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import type { UmamiEventSeries } from "@homarr/integrations/types"; import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client"; import { EVENT_COLORS, formatXLabel, umamiQueryOptions } from "./umami-utils"; import { getUsableWidgetQueryData } from "../common/query-state"; interface UmamiEventsContentProps { integrationIds: string[]; websiteId: string; timeFrame: string; eventNames: string[]; chartType: string; showXAxis: boolean; } export function UmamiEventsContent({ integrationIds, websiteId, timeFrame, eventNames, chartType, showXAxis, }: UmamiEventsContentProps) { const t = useScopedI18n("widget.umami"); const locale = useCurrentIntlLocale(); const tickColor = "var(--mantine-color-dimmed)"; const series = getUsableWidgetQueryData( 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, 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 ( {chartSeries.length > 1 && ( {chartSeries.map((serie) => ( {serie.name} ))} )} {chartType === "sparkline" ? ( ) : ( )} ); }