"use client"; import { useMemo, useState } from "react"; import { SegmentedControl, SimpleGrid, Stack, Text } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import { useScopedI18n } from "@homarr/translation/client"; import type { WidgetComponentProps } from "../definition"; import { WidgetEmptyState } from "../common/empty-state"; import { AveragesSection } from "./averages"; import { combineSpeedtestDashboards } from "./helpers"; import { LatestResultSection } from "./latest-result"; import { RecentResultsSection } from "./recent-results"; export default function SpeedtestTrackerWidget({ options, integrationIds, width, height, displayMode = "compact", }: WidgetComponentProps<"speedtestTracker">) { const t = useScopedI18n("widget.speedtestTracker"); const { data: dashboardData = [] } = clientApi.widget.speedtestTracker.getDashboard.useQuery({ integrationIds }); const [advancedRange, setAdvancedRange] = useState("24"); const combined = useMemo( () => combineSpeedtestDashboards(dashboardData.map((item) => item.dashboard)), [dashboardData], ); const rangeHours = displayMode === "advanced" ? Number(advancedRange) : 12; const cutoff = rangeHours > 0 ? Date.now() - rangeHours * 60 * 60 * 1000 : 0; const recentFiltered = combined.recentResults.filter((result) => result.created_at.getTime() > cutoff); const hasStatSection = (options.showLatestResult && combined.latestResult !== null) || (options.showStats && combined.stats !== null); const hasChart = options.showRecentResults && recentFiltered.length > 0; const noSectionsEnabled = !options.showLatestResult && !options.showStats && !options.showRecentResults; const advancedSectionWidth = width >= 720 ? width / 2 : width; const latest = options.showLatestResult && combined.latestResult && ( ); const averages = options.showStats && combined.stats && ( ); if (displayMode === "advanced") { return ( {hasStatSection && ( = 720 ? 2 : 1} spacing="md"> {latest} {averages} )} {hasChart && (
)} {noSectionsEnabled && ( {t("noSectionsEnabled")} )} {!noSectionsEnabled && !hasStatSection && !hasChart && }
); } return ( {hasStatSection && ( {latest &&
{latest}
} {averages && height >= 170 &&
{averages}
}
)} {hasChart && (
)} {noSectionsEnabled && ( {t("noSectionsEnabled")} )} {!noSectionsEnabled && !hasStatSection && !hasChart && }
); }