feat!: release v2 (#6545)
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com> Co-authored-by: arturict <arturict@users.noreply.github.com> Co-authored-by: Thomas <thomas@ajnart.fr> Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com> Co-authored-by: Danura30082 <danura30082+github@gmail.com> Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com> Co-authored-by: ajnart <thomas@ajnart.fr> Co-authored-by: ajnart <thomas@ajnart.dev> Co-authored-by: chenjun <chenjun@foxmail.com> Co-authored-by: Wikibear <wikibear@users.noreply.github.com> Co-authored-by: Bergasha <bergasha@gmail.com> Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com> Co-authored-by: Lucas Araujo <lucas.lra@gmail.com> Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com> Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social> Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com> Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com> Co-authored-by: thrawnx <tight.bulb9384@fastmail.com> Co-authored-by: Chris <58918656+nordada@users.noreply.github.com> Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com> Co-authored-by: Yanis <naamaneyanis@gmail.com> Co-authored-by: Wikibear <me@sej.de> Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
This commit is contained in:
3286 files changed
+284386
-139176
No files matched your search
@@ -0,0 +1,20 @@
|
||||
.chartSurface {
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
.chartSurface :global(.mantine-ScrollArea-viewport),
|
||||
.chartSurface :global(.recharts-wrapper),
|
||||
.chartSurface :global(.recharts-surface) {
|
||||
background-color: transparent !important;
|
||||
}
|
||||
|
||||
.listSurface {
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
padding: 4px 6px;
|
||||
border: 1px solid rgb(from var(--mantine-color-secondaryColor-filled) r g b / calc(var(--opacity, 1) * 0.32));
|
||||
border-radius: var(--mantine-radius-sm);
|
||||
background-color: transparent;
|
||||
}
|
||||
@@ -1,16 +1,82 @@
|
||||
"use client";
|
||||
|
||||
import { Stack, Text } from "@mantine/core";
|
||||
import { getQueryKey } from "@trpc/react-query";
|
||||
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import { useWidgetRuntimeQueries } from "../runtime-hooks";
|
||||
import { UmamiContent } from "./umami-content";
|
||||
|
||||
export default function UmamiWidget({ options, integrationIds }: WidgetComponentProps<"umami">) {
|
||||
if (!options.websiteId || options.websiteId.trim() === "") {
|
||||
return <NoWebsiteConfigured />;
|
||||
export default function UmamiWidget({
|
||||
options,
|
||||
integrationIds,
|
||||
width,
|
||||
height,
|
||||
displayMode = "compact",
|
||||
displayScale = 1,
|
||||
widgetRuntimeRef,
|
||||
}: WidgetComponentProps<"umami">) {
|
||||
const integrationId = integrationIds[0] ?? "";
|
||||
let responsiveWidth = width;
|
||||
let responsiveHeight = height;
|
||||
if (displayMode !== "advanced") {
|
||||
responsiveWidth *= displayScale;
|
||||
responsiveHeight *= displayScale;
|
||||
}
|
||||
const commonTopInput = {
|
||||
integrationId,
|
||||
websiteId: options.websiteId,
|
||||
timeFrame: options.timeFrame,
|
||||
limit: options.topCount,
|
||||
};
|
||||
const hasWebsite = Boolean(options.websiteId?.trim());
|
||||
useWidgetRuntimeQueries(
|
||||
widgetRuntimeRef,
|
||||
hasWebsite
|
||||
? [
|
||||
getQueryKey(
|
||||
clientApi.widget.umami.getVisitorStats,
|
||||
{
|
||||
integrationIds,
|
||||
websiteId: options.websiteId,
|
||||
timeFrame: options.timeFrame,
|
||||
eventName: options.eventName || undefined,
|
||||
},
|
||||
"query",
|
||||
),
|
||||
getQueryKey(
|
||||
clientApi.widget.umami.getActiveVisitors,
|
||||
{ integrationId, websiteId: options.websiteId },
|
||||
"query",
|
||||
),
|
||||
...(options.viewMode === "events" && options.eventNames.length > 0
|
||||
? [
|
||||
getQueryKey(
|
||||
clientApi.widget.umami.getMultiEventTimeSeries,
|
||||
{
|
||||
integrationId,
|
||||
websiteId: options.websiteId,
|
||||
timeFrame: options.timeFrame,
|
||||
eventNames: [...options.eventNames].toSorted(),
|
||||
},
|
||||
"query",
|
||||
),
|
||||
]
|
||||
: []),
|
||||
...(displayMode === "advanced" || options.viewMode === "topPages"
|
||||
? [getQueryKey(clientApi.widget.umami.getTopPages, commonTopInput, "query")]
|
||||
: []),
|
||||
...(displayMode === "advanced" || options.viewMode === "topReferrers"
|
||||
? [getQueryKey(clientApi.widget.umami.getTopReferrers, commonTopInput, "query")]
|
||||
: []),
|
||||
]
|
||||
: [],
|
||||
);
|
||||
|
||||
if (!hasWebsite) return <NoWebsiteConfigured />;
|
||||
|
||||
return (
|
||||
<UmamiContent
|
||||
@@ -23,12 +89,15 @@ export default function UmamiWidget({ options, integrationIds }: WidgetComponent
|
||||
chartType={options.chartType}
|
||||
viewMode={options.viewMode}
|
||||
topCount={options.topCount}
|
||||
width={responsiveWidth}
|
||||
height={responsiveHeight}
|
||||
displayMode={displayMode}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function NoWebsiteConfigured() {
|
||||
const t = useScopedI18n("widget.umami");
|
||||
const t = useI18n("widget.umami");
|
||||
return (
|
||||
<Stack align="center" justify="center" h="100%">
|
||||
<Text c="dimmed" size="sm">
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
import { IconChartBar, IconServerOff } from "@tabler/icons-react";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createWidgetDefinition } from "../definition";
|
||||
import { getWidgetIntegrationConfig } from "@homarr/definitions";
|
||||
|
||||
import { createWidgetDefinition, matchesWidgetRuntimeQuery } from "../definition";
|
||||
import { optionsBuilder } from "../options";
|
||||
|
||||
export const timeFrameValues = ["today", "24h", "7d", "30d", "month", "lastMonth"] as const;
|
||||
|
||||
export const { definition, componentLoader } = createWidgetDefinition("umami", {
|
||||
icon: IconChartBar,
|
||||
supportsAdvancedFocus: true,
|
||||
queryKeys: [
|
||||
[["widget", "umami", "getVisitorStats"]],
|
||||
[["widget", "umami", "getActiveVisitors"]],
|
||||
[["widget", "umami", "getMultiEventTimeSeries"]],
|
||||
[["widget", "umami", "getTopPages"]],
|
||||
[["widget", "umami", "getTopReferrers"]],
|
||||
],
|
||||
queryMatcher: matchesWidgetRuntimeQuery,
|
||||
refetchInterval: null,
|
||||
supportedIntegrations: ["umami"],
|
||||
...getWidgetIntegrationConfig("umami"),
|
||||
createOptions() {
|
||||
return optionsBuilder.from(
|
||||
(factory) => ({
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
"use client";
|
||||
|
||||
import { BarChart, LineChart } from "@mantine/charts";
|
||||
import { Box, Group, Stack, Text, useMantineColorScheme } from "@mantine/core";
|
||||
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 { useScopedI18n } from "@homarr/translation/client";
|
||||
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, umamiQueryOptions } 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[];
|
||||
@@ -23,6 +27,9 @@ interface UmamiContentProps {
|
||||
chartType: string;
|
||||
viewMode: string;
|
||||
topCount: number;
|
||||
width: number;
|
||||
height: number;
|
||||
displayMode: "compact" | "advanced";
|
||||
}
|
||||
|
||||
export function UmamiContent({
|
||||
@@ -35,32 +42,36 @@ export function UmamiContent({
|
||||
chartType,
|
||||
viewMode,
|
||||
topCount,
|
||||
width,
|
||||
height,
|
||||
displayMode,
|
||||
}: UmamiContentProps) {
|
||||
const t = useScopedI18n("widget.umami");
|
||||
const { colorScheme } = useMantineColorScheme();
|
||||
const tickColor = colorScheme === "dark" ? "#c1c2c5" : "#495057";
|
||||
const t = useI18n("widget.umami");
|
||||
const locale = useCurrentIntlLocale();
|
||||
const tickColor = "var(--mantine-color-dimmed)";
|
||||
|
||||
const { data: results = [] } = clientApi.widget.umami.getVisitorStats.useQuery(
|
||||
const statsQuery = clientApi.widget.umami.getVisitorStats.useQuery(
|
||||
{ integrationIds, websiteId, timeFrame, eventName },
|
||||
umamiQueryOptions,
|
||||
);
|
||||
const results = getUsableWidgetQueryData(statsQuery);
|
||||
|
||||
const activeVisitorsInput = { integrationId: integrationIds[0] ?? "", websiteId };
|
||||
const { data: activeVisitors = 0 } = clientApi.widget.umami.getActiveVisitors.useQuery(
|
||||
activeVisitorsInput,
|
||||
umamiQueryOptions,
|
||||
);
|
||||
const activeVisitorsQuery = clientApi.widget.umami.getActiveVisitors.useQuery(activeVisitorsInput, umamiQueryOptions);
|
||||
const activeVisitors = activeVisitorsQuery.data;
|
||||
|
||||
const { data: multiEventSeries } = clientApi.widget.umami.getMultiEventTimeSeries.useQuery(
|
||||
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];
|
||||
const firstResult = results?.[0];
|
||||
if (isInitialWidgetQueryPending(statsQuery)) return <WidgetQueryLoadingState />;
|
||||
if (!firstResult) {
|
||||
return (
|
||||
<Stack align="center" justify="center" h="100%">
|
||||
@@ -74,7 +85,7 @@ export function UmamiContent({
|
||||
const { visitorStats } = firstResult;
|
||||
|
||||
const chartData = visitorStats.dataPoints.map((point: UmamiVisitorStats["dataPoints"][number]) => ({
|
||||
label: formatXLabel(point.timestamp, timeFrame),
|
||||
label: formatXLabel(point.timestamp, timeFrame, locale),
|
||||
visitors: point.visitors,
|
||||
...(point.events !== undefined ? { events: point.events } : {}),
|
||||
}));
|
||||
@@ -87,9 +98,110 @@ export function UmamiContent({
|
||||
|
||||
const isOverlay = chartStyle === "overlay" && hasEventSeries;
|
||||
const overlayBarSize = 16;
|
||||
const { isDense, showXAxis, showSecondaryStats, showDetailedStats, stackAdvancedContent } = getUmamiLayout({
|
||||
width,
|
||||
height,
|
||||
displayMode,
|
||||
});
|
||||
|
||||
const selectedView =
|
||||
viewMode === "events" ? (
|
||||
<UmamiEventsContent
|
||||
series={multiEventSeries ?? []}
|
||||
timeFrame={timeFrame}
|
||||
hasSelectedEvents={eventNames.length > 0}
|
||||
chartType={chartType}
|
||||
showXAxis={showXAxis}
|
||||
/>
|
||||
) : viewMode === "topPages" ? (
|
||||
<UmamiTopPagesContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
) : viewMode === "topReferrers" ? (
|
||||
<UmamiTopReferrersContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
) : chartType === "sparkline" ? (
|
||||
<LineChart
|
||||
h="100%"
|
||||
data={chartData}
|
||||
dataKey="label"
|
||||
series={series}
|
||||
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={series}
|
||||
tickLine="none"
|
||||
gridAxis="none"
|
||||
withLegend={false}
|
||||
withTooltip
|
||||
withXAxis={showXAxis}
|
||||
withYAxis={false}
|
||||
barProps={isOverlay ? { barSize: overlayBarSize, radius: 2 } : { radius: 2 }}
|
||||
barChartProps={isOverlay ? { barGap: -overlayBarSize } : undefined}
|
||||
xAxisProps={{ tick: { fontSize: 9, fill: tickColor }, interval: "preserveStartEnd" }}
|
||||
/>
|
||||
);
|
||||
|
||||
const advancedLists = (
|
||||
<SimpleGrid cols={viewMode === "topPages" || viewMode === "topReferrers" ? 1 : 2} spacing="xs" mih={180}>
|
||||
{viewMode !== "topPages" && (
|
||||
<UmamiTopPagesContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
)}
|
||||
{viewMode !== "topReferrers" && (
|
||||
<UmamiTopReferrersContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
)}
|
||||
</SimpleGrid>
|
||||
);
|
||||
|
||||
const advancedContent = stackAdvancedContent ? (
|
||||
<ScrollArea className={classes.chartSurface} h="100%">
|
||||
<Stack gap="md">
|
||||
<Box h={Math.max(280, Math.min(420, height - 160))}>{selectedView}</Box>
|
||||
{advancedLists}
|
||||
</Stack>
|
||||
</ScrollArea>
|
||||
) : (
|
||||
<SimpleGrid cols={2} spacing="md" h="100%" style={{ gridTemplateRows: "minmax(0, 1fr)" }}>
|
||||
<Box className={classes.chartSurface} h="100%" mih={260}>
|
||||
{selectedView}
|
||||
</Box>
|
||||
<ScrollArea className={classes.chartSurface} h="100%">
|
||||
{advancedLists}
|
||||
</ScrollArea>
|
||||
</SimpleGrid>
|
||||
);
|
||||
|
||||
return (
|
||||
<Stack gap={4} p="xs" h="100%">
|
||||
<Stack gap={isDense ? 1 : 4} p={isDense ? 2 : "xs"} h="100%" pos="relative">
|
||||
<Group justify="space-between" align="baseline" wrap="nowrap">
|
||||
<Text size="xs" c="dimmed" truncate="end" style={{ maxWidth: "55%" }}>
|
||||
{visitorStats.domain} ({formatTimeFrameLabel(timeFrame, t)})
|
||||
@@ -100,8 +212,8 @@ export function UmamiContent({
|
||||
<Group gap={4} align="baseline">
|
||||
<Text size="lg" fw={700} lh={1}>
|
||||
{viewMode === "events"
|
||||
? (multiEventTotal ?? 0).toLocaleString()
|
||||
: (visitorStats.eventCount ?? 0).toLocaleString()}
|
||||
? (multiEventTotal ?? 0).toLocaleString(locale)
|
||||
: (visitorStats.eventCount ?? 0).toLocaleString(locale)}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{t("event.label")}
|
||||
@@ -112,7 +224,7 @@ export function UmamiContent({
|
||||
</Text>
|
||||
<Group gap={4} align="baseline">
|
||||
<Text size="lg" fw={700} lh={1}>
|
||||
{visitorStats.totalVisitors.toLocaleString()}
|
||||
{visitorStats.totalVisitors.toLocaleString(locale)}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{t("visitors")}
|
||||
@@ -122,7 +234,7 @@ export function UmamiContent({
|
||||
) : (
|
||||
<Group gap={4} align="baseline">
|
||||
<Text size="xl" fw={700} lh={1}>
|
||||
{visitorStats.totalVisitors.toLocaleString()}
|
||||
{visitorStats.totalVisitors.toLocaleString(locale)}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{t("visitors")}
|
||||
@@ -130,85 +242,33 @@ export function UmamiContent({
|
||||
</Group>
|
||||
)}
|
||||
</Group>
|
||||
<Group wrap="wrap" style={{ columnGap: 12, rowGap: 4 }}>
|
||||
<Text size="xs" c="green">
|
||||
● {activeVisitors.toLocaleString()} {t("active")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.totalPageviews.toLocaleString()} {t("pageviews")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.totalVisits.toLocaleString()} {t("visits")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.bounceRate}% {t("bounceRate")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{formatDuration(visitorStats.avgDuration * 1000)} {t("avgDuration")}
|
||||
</Text>
|
||||
</Group>
|
||||
<Box mt={4} style={{ flex: 1, minHeight: 0 }}>
|
||||
{viewMode === "events" ? (
|
||||
<UmamiEventsContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
eventNames={eventNames}
|
||||
chartType={chartType}
|
||||
/>
|
||||
) : viewMode === "topPages" ? (
|
||||
<UmamiTopPagesContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
) : viewMode === "topReferrers" ? (
|
||||
<UmamiTopReferrersContent
|
||||
integrationIds={integrationIds}
|
||||
websiteId={websiteId}
|
||||
timeFrame={timeFrame}
|
||||
limit={topCount}
|
||||
/>
|
||||
) : chartType === "sparkline" ? (
|
||||
<LineChart
|
||||
h="100%"
|
||||
data={chartData}
|
||||
dataKey="label"
|
||||
series={series}
|
||||
withDots={false}
|
||||
curveType="monotone"
|
||||
tickLine="none"
|
||||
gridAxis="none"
|
||||
withLegend={false}
|
||||
withTooltip
|
||||
withXAxis
|
||||
withYAxis={false}
|
||||
xAxisProps={{
|
||||
tick: { fontSize: 9, fill: tickColor },
|
||||
interval: "preserveStartEnd",
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<BarChart
|
||||
h="100%"
|
||||
data={chartData}
|
||||
dataKey="label"
|
||||
series={series}
|
||||
tickLine="none"
|
||||
gridAxis="none"
|
||||
withLegend={false}
|
||||
withTooltip
|
||||
withXAxis
|
||||
withYAxis={false}
|
||||
barProps={isOverlay ? { barSize: overlayBarSize, radius: 2 } : { radius: 2 }}
|
||||
barChartProps={isOverlay ? { barGap: -overlayBarSize } : undefined}
|
||||
xAxisProps={{
|
||||
tick: { fontSize: 9, fill: tickColor },
|
||||
interval: "preserveStartEnd",
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{showSecondaryStats && (
|
||||
<Group wrap="wrap" style={{ columnGap: 12, rowGap: 4 }}>
|
||||
{activeVisitors !== undefined && (
|
||||
<Text size="xs" c="green">
|
||||
● {activeVisitors.toLocaleString(locale)} {t("active")}
|
||||
</Text>
|
||||
)}
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.totalPageviews.toLocaleString(locale)} {t("pageviews")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.totalVisits.toLocaleString(locale)} {t("visits")}
|
||||
</Text>
|
||||
{showDetailedStats && (
|
||||
<>
|
||||
<Text size="xs" c="dimmed">
|
||||
{visitorStats.bounceRate.toLocaleString(locale)}% {t("bounceRate")}
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed">
|
||||
{formatDuration(visitorStats.avgDuration * 1000)} {t("avgDuration")}
|
||||
</Text>
|
||||
</>
|
||||
)}
|
||||
</Group>
|
||||
)}
|
||||
<Box className={classes.chartSurface} mt={4} style={{ flex: 1, minHeight: 0 }}>
|
||||
{displayMode === "advanced" ? advancedContent : selectedView}
|
||||
</Box>
|
||||
</Stack>
|
||||
);
|
||||
|
||||
@@ -1,39 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { BarChart, LineChart } from "@mantine/charts";
|
||||
import { Box, Group, Stack, Text, useMantineColorScheme } from "@mantine/core";
|
||||
import { Box, Group, Stack, Text } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import type { UmamiEventSeries } from "@homarr/integrations/types";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { EVENT_COLORS, formatXLabel, umamiQueryOptions } from "./umami-utils";
|
||||
import { EVENT_COLORS, formatXLabel } from "./umami-utils";
|
||||
|
||||
interface UmamiEventsContentProps {
|
||||
integrationIds: string[];
|
||||
websiteId: string;
|
||||
series: UmamiEventSeries[];
|
||||
timeFrame: string;
|
||||
eventNames: string[];
|
||||
hasSelectedEvents: boolean;
|
||||
chartType: string;
|
||||
showXAxis: boolean;
|
||||
}
|
||||
|
||||
export function UmamiEventsContent({
|
||||
integrationIds,
|
||||
websiteId,
|
||||
series,
|
||||
timeFrame,
|
||||
eventNames,
|
||||
hasSelectedEvents,
|
||||
chartType,
|
||||
showXAxis,
|
||||
}: UmamiEventsContentProps) {
|
||||
const t = useScopedI18n("widget.umami");
|
||||
const { colorScheme } = useMantineColorScheme();
|
||||
const tickColor = colorScheme === "dark" ? "#c1c2c5" : "#495057";
|
||||
const t = useI18n("widget.umami");
|
||||
const locale = useCurrentIntlLocale();
|
||||
const tickColor = "var(--mantine-color-dimmed)";
|
||||
|
||||
const { data: series = [] } = clientApi.widget.umami.getMultiEventTimeSeries.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, eventNames: [...eventNames].toSorted() },
|
||||
umamiQueryOptions,
|
||||
);
|
||||
|
||||
if (eventNames.length === 0) {
|
||||
if (!hasSelectedEvents) {
|
||||
return (
|
||||
<Stack align="center" justify="center" h="100%">
|
||||
<Text c="dimmed" size="sm">
|
||||
@@ -57,7 +51,7 @@ export function UmamiEventsContent({
|
||||
);
|
||||
|
||||
const chartData = allTimestamps.map((timestamp) => {
|
||||
const row: Record<string, string | number> = { label: formatXLabel(timestamp, timeFrame) };
|
||||
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;
|
||||
}
|
||||
@@ -104,7 +98,7 @@ export function UmamiEventsContent({
|
||||
gridAxis="none"
|
||||
withLegend={false}
|
||||
withTooltip
|
||||
withXAxis
|
||||
withXAxis={showXAxis}
|
||||
withYAxis={false}
|
||||
xAxisProps={{
|
||||
tick: { fontSize: 9, fill: tickColor },
|
||||
@@ -121,7 +115,7 @@ export function UmamiEventsContent({
|
||||
gridAxis="none"
|
||||
withLegend={false}
|
||||
withTooltip
|
||||
withXAxis
|
||||
withXAxis={showXAxis}
|
||||
withYAxis={false}
|
||||
barProps={{ radius: 2 }}
|
||||
xAxisProps={{
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getUmamiLayout } from "./umami-layout";
|
||||
|
||||
describe("Umami layout", () => {
|
||||
test.each([
|
||||
{ width: 260, height: 119, property: "isDense" as const, expected: true },
|
||||
{ width: 260, height: 120, property: "isDense" as const, expected: false },
|
||||
{ width: 100, height: 80, property: "showXAxis" as const, expected: false },
|
||||
{ width: 100, height: 80, property: "showSecondaryStats" as const, expected: false },
|
||||
{ width: 100, height: 80, property: "showDetailedStats" as const, expected: false },
|
||||
])("sets $property to $expected at $width x $height", ({ width, height, property, expected }) => {
|
||||
expect(getUmamiLayout({ width, height, displayMode: "compact" })[property]).toBe(expected);
|
||||
});
|
||||
|
||||
test("keeps advanced details visible and changes columns at 900px", () => {
|
||||
expect(getUmamiLayout({ width: 899, height: 80, displayMode: "advanced" })).toEqual({
|
||||
isDense: false,
|
||||
showXAxis: true,
|
||||
showSecondaryStats: true,
|
||||
showDetailedStats: true,
|
||||
stackAdvancedContent: true,
|
||||
});
|
||||
expect(getUmamiLayout({ width: 900, height: 80, displayMode: "advanced" }).stackAdvancedContent).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
interface UmamiLayoutInput {
|
||||
width: number;
|
||||
height: number;
|
||||
displayMode: "compact" | "advanced";
|
||||
}
|
||||
|
||||
export const getUmamiLayout = ({ width, height, displayMode }: UmamiLayoutInput) => {
|
||||
const isAdvanced = displayMode === "advanced";
|
||||
return {
|
||||
isDense: !isAdvanced && height < 120,
|
||||
showXAxis: isAdvanced || (width >= 220 && height >= 150),
|
||||
showSecondaryStats: isAdvanced || (width >= 220 && height >= 180),
|
||||
showDetailedStats: isAdvanced || (width >= 360 && height >= 240),
|
||||
stackAdvancedContent: isAdvanced && width < 900,
|
||||
};
|
||||
};
|
||||
@@ -4,9 +4,11 @@ import { Group, ScrollArea, Stack, Text } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import type { UmamiMetricItem } from "@homarr/integrations/types";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||
import classes from "./component.module.css";
|
||||
|
||||
import { umamiQueryOptions } from "./umami-utils";
|
||||
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||
|
||||
interface UmamiTopListProps {
|
||||
integrationIds: string[];
|
||||
@@ -16,27 +18,27 @@ interface UmamiTopListProps {
|
||||
}
|
||||
|
||||
export function UmamiTopPagesContent({ integrationIds, websiteId, timeFrame, limit }: UmamiTopListProps) {
|
||||
const t = useScopedI18n("widget.umami");
|
||||
const { data = [] } = clientApi.widget.umami.getTopPages.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, limit },
|
||||
umamiQueryOptions,
|
||||
);
|
||||
return <UmamiTopList items={data} heading={t("option.viewMode.option.topPages")} emptyLabel={t("topPages.direct")} />;
|
||||
const t = useI18n("widget.umami");
|
||||
const data =
|
||||
getUsableWidgetQueryData(
|
||||
clientApi.widget.umami.getTopPages.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, limit },
|
||||
umamiQueryOptions,
|
||||
),
|
||||
) ?? [];
|
||||
return <UmamiTopList items={data} heading={t("option.viewMode.option.topPages")} emptyLabel={t("top.direct")} />;
|
||||
}
|
||||
|
||||
export function UmamiTopReferrersContent({ integrationIds, websiteId, timeFrame, limit }: UmamiTopListProps) {
|
||||
const t = useScopedI18n("widget.umami");
|
||||
const { data = [] } = clientApi.widget.umami.getTopReferrers.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, limit },
|
||||
umamiQueryOptions,
|
||||
);
|
||||
return (
|
||||
<UmamiTopList
|
||||
items={data}
|
||||
heading={t("option.viewMode.option.topReferrers")}
|
||||
emptyLabel={t("topReferrers.direct")}
|
||||
/>
|
||||
);
|
||||
const t = useI18n("widget.umami");
|
||||
const data =
|
||||
getUsableWidgetQueryData(
|
||||
clientApi.widget.umami.getTopReferrers.useQuery(
|
||||
{ integrationId: integrationIds[0] ?? "", websiteId, timeFrame, limit },
|
||||
umamiQueryOptions,
|
||||
),
|
||||
) ?? [];
|
||||
return <UmamiTopList items={data} heading={t("option.viewMode.option.topReferrers")} emptyLabel={t("top.direct")} />;
|
||||
}
|
||||
|
||||
function UmamiTopList({
|
||||
@@ -48,8 +50,10 @@ function UmamiTopList({
|
||||
heading: string;
|
||||
emptyLabel: string;
|
||||
}) {
|
||||
const locale = useCurrentIntlLocale();
|
||||
|
||||
return (
|
||||
<Stack gap={2} h="100%">
|
||||
<Stack className={classes.listSurface} gap={2} h="100%">
|
||||
<Text size="xs" c="dimmed" fw={500}>
|
||||
{heading}
|
||||
</Text>
|
||||
@@ -64,7 +68,7 @@ function UmamiTopList({
|
||||
{item.x || emptyLabel}
|
||||
</Text>
|
||||
<Text size="xs" fw={600} flex="0 0 auto">
|
||||
{item.y.toLocaleString()}
|
||||
{item.y.toLocaleString(locale)}
|
||||
</Text>
|
||||
</Group>
|
||||
))}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { useScopedI18n } from "@homarr/translation/client";
|
||||
import type { useI18n } from "@homarr/translation/client";
|
||||
|
||||
import { timeFrameValues } from "./index";
|
||||
|
||||
@@ -6,17 +6,17 @@ export const umamiQueryOptions = { refetchInterval: 60_000 } as const;
|
||||
|
||||
export const EVENT_COLORS = ["blue.5", "orange.5", "green.5", "red.5", "violet.5", "teal.5", "yellow.5", "pink.5"];
|
||||
|
||||
export function formatXLabel(timestamp: string, timeFrame: string): string {
|
||||
export function formatXLabel(timestamp: string, timeFrame: string, locale: string): string {
|
||||
const date = new Date(timestamp);
|
||||
switch (timeFrame) {
|
||||
case "today":
|
||||
case "24h":
|
||||
return `${date.getHours().toString().padStart(2, "0")}:00`;
|
||||
return date.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit", hourCycle: "h23" });
|
||||
case "7d":
|
||||
case "30d":
|
||||
case "month":
|
||||
case "lastMonth":
|
||||
return date.toLocaleDateString(undefined, { month: "short", day: "numeric" });
|
||||
return date.toLocaleDateString(locale, { month: "short", day: "numeric" });
|
||||
default:
|
||||
return timestamp;
|
||||
}
|
||||
@@ -24,10 +24,7 @@ export function formatXLabel(timestamp: string, timeFrame: string): string {
|
||||
|
||||
export type TimeFrame = (typeof timeFrameValues)[number];
|
||||
|
||||
export function formatTimeFrameLabel(
|
||||
timeFrame: TimeFrame,
|
||||
t: ReturnType<typeof useScopedI18n<"widget.umami">>,
|
||||
): string {
|
||||
export function formatTimeFrameLabel(timeFrame: TimeFrame, t: ReturnType<typeof useI18n<"widget.umami">>): string {
|
||||
if (timeFrameValues.includes(timeFrame)) {
|
||||
return t(`option.timeFrame.option.${timeFrame}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user