178 lines
6.4 KiB
TypeScript
178 lines
6.4 KiB
TypeScript
"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 (
|
|
<Box h="100%" p="sm">
|
|
<Text c="dimmed" size="sm">
|
|
{t("widget.unifiOs.error.noData")}
|
|
</Text>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
const wanColor = summary.wan.state === "online" ? "green" : summary.wan.state === "offline" ? "red" : "gray";
|
|
|
|
return (
|
|
<Stack h="100%" p="sm" gap="xs" justify="space-between">
|
|
{/* The site name is not decorative: a console can host several sites and
|
|
the widget only shows one. Naming it makes that choice visible. */}
|
|
<Group gap={6} wrap="nowrap">
|
|
<IconCircleFilled size={8} color={`var(--mantine-color-${wanColor}-6)`} />
|
|
<Text size="xs" c="dimmed" tt="uppercase" fw={600} truncate>
|
|
{summary.site.name}
|
|
</Text>
|
|
</Group>
|
|
|
|
<Stack
|
|
gap={4}
|
|
p="xs"
|
|
style={{ borderRadius: "var(--mantine-radius-md)", background: "var(--mantine-color-default-hover)" }}
|
|
>
|
|
<Group justify="space-between" wrap="nowrap" gap="xs">
|
|
<Group gap={6} wrap="nowrap">
|
|
<IconCircleFilled size={10} color={`var(--mantine-color-${wanColor}-6)`} />
|
|
<Text fw={600} size="sm">
|
|
{t("widget.unifiOs.card.internet")}
|
|
</Text>
|
|
</Group>
|
|
<Text size="sm" ff="monospace">
|
|
{summary.wan.throughput === null
|
|
? EMPTY_VALUE
|
|
: `↑${formatBitsPerSecond(summary.wan.throughput.txBps)} ↓${formatBitsPerSecond(summary.wan.throughput.rxBps)}`}
|
|
</Text>
|
|
</Group>
|
|
|
|
{options.showIsp && (
|
|
<Group justify="space-between" wrap="nowrap" gap="xs">
|
|
{summary.wan.isp === null ? (
|
|
<Text size="xs" c="dimmed">
|
|
{EMPTY_VALUE}
|
|
</Text>
|
|
) : (
|
|
<>
|
|
<Text size="xs" c="dimmed" truncate>
|
|
{summary.wan.isp.name}
|
|
{summary.wan.isp.externalIp !== null ? ` · ${summary.wan.isp.externalIp}` : ""}
|
|
</Text>
|
|
<Group gap={6} wrap="nowrap">
|
|
<Text size="xs">{t("widget.unifiOs.card.latency", { value: `${summary.wan.isp.latencyMs}` })}</Text>
|
|
{/* The cloud metrics behind this badge lag ~27 minutes: it must
|
|
never look live, hence the relative-time badge instead of a
|
|
bare number. */}
|
|
<Tooltip
|
|
label={t("widget.unifiOs.card.measuredAt", {
|
|
time: dayjs(summary.wan.isp.measuredAt).format("HH:mm"),
|
|
})}
|
|
>
|
|
<Badge size="xs" variant="light" color="yellow">
|
|
{dayjs(summary.wan.isp.measuredAt).fromNow()}
|
|
</Badge>
|
|
</Tooltip>
|
|
</Group>
|
|
</>
|
|
)}
|
|
</Group>
|
|
)}
|
|
</Stack>
|
|
|
|
<Stack gap={2}>
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="sm" c="dimmed">
|
|
{t("widget.unifiOs.card.clients")}
|
|
</Text>
|
|
<Text size="sm" fw={600}>
|
|
{summary.clients === null
|
|
? EMPTY_VALUE
|
|
: t("widget.unifiOs.card.clientBreakdown", {
|
|
total: `${summary.clients.total}`,
|
|
wired: `${summary.clients.wired}`,
|
|
wireless: `${summary.clients.wireless}`,
|
|
})}
|
|
</Text>
|
|
</Group>
|
|
|
|
{options.showDevices && (
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="sm" c="dimmed">
|
|
{t("widget.unifiOs.card.devices")}
|
|
</Text>
|
|
<Text size="sm" fw={600} c={summary.devices !== null && summary.devices.offline > 0 ? "red" : undefined}>
|
|
{summary.devices === null
|
|
? EMPTY_VALUE
|
|
: t("widget.unifiOs.card.devicesOnline", {
|
|
online: `${summary.devices.online}`,
|
|
total: `${summary.devices.total}`,
|
|
})}
|
|
</Text>
|
|
</Group>
|
|
)}
|
|
|
|
{options.showFirmwareUpdates && (summary.devices === null || summary.devices.firmwareUpdatable > 0) && (
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="sm" c="dimmed">
|
|
{t("widget.unifiOs.card.firmware")}
|
|
</Text>
|
|
<Text size="sm" fw={600} c={summary.devices !== null ? "yellow" : undefined}>
|
|
{summary.devices === null
|
|
? EMPTY_VALUE
|
|
: t("widget.unifiOs.card.firmwarePending", { count: `${summary.devices.firmwareUpdatable}` })}
|
|
</Text>
|
|
</Group>
|
|
)}
|
|
</Stack>
|
|
|
|
<Group justify="space-between" wrap="nowrap">
|
|
<Text size="xs" c="dimmed" truncate>
|
|
{t("widget.unifiOs.card.version", { version: summary.site.applicationVersion })}
|
|
</Text>
|
|
<Text size="xs" c="dimmed">
|
|
{t("widget.unifiOs.card.gatewayUptime", {
|
|
uptime: summary.gateway?.uptimeSec != null ? formatUptime(summary.gateway.uptimeSec) : EMPTY_VALUE,
|
|
})}
|
|
</Text>
|
|
</Group>
|
|
</Stack>
|
|
);
|
|
}
|