"use client";
import { BarChart, LineChart } from "@mantine/charts";
import { Box, Group, ScrollArea, SimpleGrid, Stack, Text, useMantineColorScheme } from "@mantine/core";
import { clientApi } from "@homarr/api/client";
import { formatDuration } from "@homarr/common";
import type { UmamiVisitorStats } from "@homarr/integrations/types";
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
import { UmamiEventsContent } from "./umami-events-content";
import { UmamiTopPagesContent, UmamiTopReferrersContent } from "./umami-top-list";
import { formatTimeFrameLabel, formatXLabel, umamiQueryOptions } from "./umami-utils";
import type { TimeFrame } from "./umami-utils";
interface UmamiContentProps {
integrationIds: string[];
websiteId: string;
timeFrame: TimeFrame;
eventName?: string;
eventNames: string[];
chartStyle: string;
chartType: string;
viewMode: string;
topCount: number;
width: number;
height: number;
displayMode: "compact" | "advanced";
}
export function UmamiContent({
integrationIds,
websiteId,
timeFrame,
eventName,
eventNames,
chartStyle,
chartType,
viewMode,
topCount,
width,
height,
displayMode,
}: UmamiContentProps) {
const t = useScopedI18n("widget.umami");
const tCommon = useScopedI18n("common");
const locale = useCurrentIntlLocale();
const { colorScheme } = useMantineColorScheme();
const tickColor = colorScheme === "dark" ? "#c1c2c5" : "#495057";
const { data: results = [], isPending: isStatsPending } = clientApi.widget.umami.getVisitorStats.useQuery(
{ integrationIds, websiteId, timeFrame, eventName },
umamiQueryOptions,
);
const activeVisitorsInput = { integrationId: integrationIds[0] ?? "", websiteId };
const { data: activeVisitors = 0 } = clientApi.widget.umami.getActiveVisitors.useQuery(
activeVisitorsInput,
umamiQueryOptions,
);
const { data: multiEventSeries } = clientApi.widget.umami.getMultiEventTimeSeries.useQuery(
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, eventNames: [...eventNames].toSorted() },
{ enabled: viewMode === "events" && eventNames.length > 0, ...umamiQueryOptions },
);
const multiEventTotal = multiEventSeries
? multiEventSeries.flatMap(({ dataPoints }) => dataPoints).reduce((sum, { y }) => sum + y, 0)
: undefined;
const firstResult = results[0];
if (isStatsPending) {
return (
{tCommon("action.loading")}
);
}
if (!firstResult) {
return (
{t("error.noData")}
);
}
const { visitorStats } = firstResult;
const chartData = visitorStats.dataPoints.map((point: UmamiVisitorStats["dataPoints"][number]) => ({
label: formatXLabel(point.timestamp, timeFrame),
visitors: point.visitors,
...(point.events !== undefined ? { events: point.events } : {}),
}));
const hasEventSeries = visitorStats.dataPoints.some(({ events }) => events !== undefined);
const series = [
{ name: "visitors", color: "blue.5" },
...(hasEventSeries ? [{ name: "events", color: "orange.5" }] : []),
];
const isOverlay = chartStyle === "overlay" && hasEventSeries;
const overlayBarSize = 16;
const isAdvanced = displayMode === "advanced";
const isDense = !isAdvanced && height < 120;
const showXAxis = isAdvanced || height >= 140;
const showSecondaryStats = isAdvanced || height >= 96;
const showDetailedStats = isAdvanced || (width >= 260 && height >= 150);
const selectedView =
viewMode === "events" ? (
) : viewMode === "topPages" ? (
) : viewMode === "topReferrers" ? (
) : chartType === "sparkline" ? (
) : (
);
const advancedLists = (
{viewMode !== "topPages" && (
)}
{viewMode !== "topReferrers" && (
)}
);
const advancedContent =
width < 900 ? (
{selectedView}
{advancedLists}
) : (
{selectedView}
{advancedLists}
);
return (
{visitorStats.domain} ({formatTimeFrameLabel(timeFrame, t)})
{(eventName && viewMode === "chart" && visitorStats.eventCount !== undefined) ||
(viewMode === "events" && multiEventTotal !== undefined) ? (
{viewMode === "events"
? (multiEventTotal ?? 0).toLocaleString(locale)
: (visitorStats.eventCount ?? 0).toLocaleString(locale)}
{t("event.label")}
/
{visitorStats.totalVisitors.toLocaleString(locale)}
{t("visitors")}
) : (
{visitorStats.totalVisitors.toLocaleString(locale)}
{t("visitors")}
)}
{showSecondaryStats && (
● {activeVisitors.toLocaleString(locale)} {t("active")}
{visitorStats.totalPageviews.toLocaleString(locale)} {t("pageviews")}
{visitorStats.totalVisits.toLocaleString(locale)} {t("visits")}
{showDetailedStats && (
<>
{visitorStats.bounceRate.toLocaleString(locale)}% {t("bounceRate")}
{formatDuration(visitorStats.avgDuration * 1000)} {t("avgDuration")}
>
)}
)}
{displayMode === "advanced" ? advancedContent : selectedView}
);
}