"use client"; import { BarChart, LineChart } from "@mantine/charts"; import { Box, Group, ScrollArea, SimpleGrid, Stack, Text } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import { formatDuration } from "@homarr/common"; import type { UmamiVisitorStats } from "@homarr/integrations/types"; import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client"; import classes from "./component.module.css"; import { UmamiEventsContent } from "./umami-events-content"; import { getUmamiLayout } from "./umami-layout"; import { UmamiTopPagesContent, UmamiTopReferrersContent } from "./umami-top-list"; import { formatTimeFrameLabel, formatXLabel, useUmamiQueryOptions } from "./umami-utils"; import type { TimeFrame } from "./umami-utils"; import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../common/query-state"; import { WidgetQueryLoadingState } from "../common/query-state-indicator"; 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 = useI18n("widget.umami"); const locale = useCurrentIntlLocale(); const umamiQueryOptions = useUmamiQueryOptions(); const tickColor = "var(--mantine-color-dimmed)"; const statsQuery = clientApi.widget.umami.getVisitorStats.useQuery( { integrationIds, websiteId, timeFrame, eventName }, umamiQueryOptions, ); const results = getUsableWidgetQueryData(statsQuery); const activeVisitorsInput = { integrationId: integrationIds[0] ?? "", websiteId }; const activeVisitorsQuery = clientApi.widget.umami.getActiveVisitors.useQuery(activeVisitorsInput, umamiQueryOptions); const activeVisitors = activeVisitorsQuery.data; const multiEventQuery = clientApi.widget.umami.getMultiEventTimeSeries.useQuery( { integrationId: integrationIds[0] ?? "", websiteId, timeFrame, eventNames: [...eventNames].toSorted() }, { enabled: viewMode === "events" && eventNames.length > 0, ...umamiQueryOptions }, ); const multiEventSeries = getUsableWidgetQueryData(multiEventQuery); const multiEventTotal = multiEventSeries ? multiEventSeries.flatMap(({ dataPoints }) => dataPoints).reduce((sum, { y }) => sum + y, 0) : undefined; const firstResult = results?.[0]; if (isInitialWidgetQueryPending(statsQuery)) return ; if (!firstResult) { return ( {t("error.noData")} ); } const { visitorStats } = firstResult; const chartData = visitorStats.dataPoints.map((point: UmamiVisitorStats["dataPoints"][number]) => ({ label: formatXLabel(point.timestamp, timeFrame, locale), 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 { isDense, showXAxis, showSecondaryStats, showDetailedStats, stackAdvancedContent } = getUmamiLayout({ width, height, displayMode, }); const selectedView = viewMode === "events" ? ( 0} chartType={chartType} showXAxis={showXAxis} /> ) : viewMode === "topPages" ? ( ) : viewMode === "topReferrers" ? ( ) : chartType === "sparkline" ? ( ) : ( ); const advancedLists = ( {viewMode !== "topPages" && ( )} {viewMode !== "topReferrers" && ( )} ); const advancedContent = stackAdvancedContent ? ( {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 !== undefined && ( ● {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} ); }