"use client";
import { useMemo } from "react";
import { Badge, Box, Group, Stack, Text, Tooltip } from "@mantine/core";
import { IconCircleFilled } from "@tabler/icons-react";
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import { clientApi } from "@homarr/api/client";
import { useI18n } from "@homarr/translation/client";
import type { WidgetComponentProps } from "../definition";
dayjs.extend(relativeTime);
const EMPTY_VALUE = "โ";
const formatBitsPerSecond = (bps: number): string => {
const units = ["b/s", "Kb/s", "Mb/s", "Gb/s"];
let value = bps;
let unitIndex = 0;
while (value >= 1000 && unitIndex < units.length - 1) {
value /= 1000;
unitIndex += 1;
}
return `${value.toFixed(value >= 100 || unitIndex === 0 ? 0 : 1)} ${units[unitIndex]}`;
};
const formatUptime = (seconds: number): string => {
const days = Math.floor(seconds / 86400);
const hours = Math.floor((seconds % 86400) / 3600);
return days > 0 ? `${days}d ${hours}h` : `${hours}h`;
};
export default function UnifiOsWidget({ integrationIds, options }: WidgetComponentProps<"unifiOs">) {
const t = useI18n();
const { data: results = [] } = clientApi.widget.unifiOs.summary.useQuery({ integrationIds });
const summary = useMemo(() => results[0]?.summary ?? null, [results]);
if (summary === null) {
return (
{t("widget.unifiOs.error.noData")}
);
}
const wanColor = summary.wan.state === "online" ? "green" : summary.wan.state === "offline" ? "red" : "gray";
return (
{/* The site name is not decorative: a console can host several sites and
the widget only shows one. Naming it makes that choice visible. */}
{summary.site.name}
{t("widget.unifiOs.card.internet")}
{summary.wan.throughput === null
? EMPTY_VALUE
: `โ${formatBitsPerSecond(summary.wan.throughput.txBps)} โ${formatBitsPerSecond(summary.wan.throughput.rxBps)}`}
{options.showIsp && (
{summary.wan.isp === null ? (
<>
{t("widget.unifiOs.card.isp")}
{EMPTY_VALUE}
>
) : (
<>
{summary.wan.isp.name}
{summary.wan.isp.externalIp !== null ? ` ยท ${summary.wan.isp.externalIp}` : ""}
{t("widget.unifiOs.card.latency", { value: `${summary.wan.isp.latencyMs}` })}
{/* The cloud metrics behind this badge lag ~27 minutes: it must
never look live, hence the relative-time badge instead of a
bare number. */}
{dayjs(summary.wan.isp.measuredAt).fromNow()}
>
)}
)}
{t("widget.unifiOs.card.clients")}
{summary.clients === null
? EMPTY_VALUE
: t("widget.unifiOs.card.clientBreakdown", {
total: `${summary.clients.total}`,
wired: `${summary.clients.wired}`,
wireless: `${summary.clients.wireless}`,
})}
{options.showDevices && (
{t("widget.unifiOs.card.devices")}
0 ? "red" : undefined}>
{summary.devices === null
? EMPTY_VALUE
: t("widget.unifiOs.card.devicesOnline", {
online: `${summary.devices.online}`,
total: `${summary.devices.total}`,
})}
)}
{options.showFirmwareUpdates && (summary.devices === null || summary.devices.firmwareUpdatable > 0) && (
{t("widget.unifiOs.card.firmware")}
{summary.devices === null
? EMPTY_VALUE
: t("widget.unifiOs.card.firmwarePending", { count: `${summary.devices.firmwareUpdatable}` })}
)}
{t("widget.unifiOs.card.version", { version: summary.site.applicationVersion })}
{(summary.devices === null || summary.gateway !== null) && (
{t("widget.unifiOs.card.gatewayUptime", {
uptime: summary.gateway?.uptimeSec != null ? formatUptime(summary.gateway.uptimeSec) : EMPTY_VALUE,
})}
)}
);
}