"use client"; import { useMemo } from "react"; import { Stack, Text } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import type { SpeedtestTrackerDashboardData } from "@homarr/integrations/types"; import { useScopedI18n } from "@homarr/translation/client"; import type { WidgetComponentProps } from "../definition"; import { WidgetEmptyState } from "../common/empty-state"; import { AveragesSection } from "./averages"; import { mergeStats } from "./helpers"; import { LatestResultSection } from "./latest-result"; import { RecentResultsSection } from "./recent-results"; export default function SpeedtestTrackerWidget({ options, integrationIds }: WidgetComponentProps<"speedtestTracker">) { const t = useScopedI18n("widget.speedtestTracker"); const { data: dashboardData = [] } = clientApi.widget.speedtestTracker.getDashboard.useQuery({ integrationIds }); const combined = useMemo( () => dashboardData.reduce( (acc, item) => ({ latestResult: item.dashboard.latestResult ?? acc.latestResult, stats: mergeStats(acc.stats, item.dashboard.stats), recentResults: [...acc.recentResults, ...item.dashboard.recentResults], }), { latestResult: null, stats: null, recentResults: [] }, ), [dashboardData], ); const twelveHoursAgo = Date.now() - 12 * 60 * 60 * 1000; const recentFiltered = combined.recentResults.filter((result) => result.created_at.getTime() > twelveHoursAgo); 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; return ( {hasStatSection && ( {options.showLatestResult && combined.latestResult && (
)} {options.showStats && combined.stats && (
)}
)} {hasChart && (
)} {noSectionsEnabled && ( {t("noSectionsEnabled")} )} {!noSectionsEnabled && !hasStatSection && !hasChart && }
); }