54 lines
1.7 KiB
React
Raw Normal View History

import { useTranslation } from "react-i18next";
import useSWR from "swr";
2024-11-27 17:01:47 -08:00
export default function Ping({ groupName, serviceName, style }) {
const { t } = useTranslation();
2024-11-27 17:01:47 -08:00
const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ groupName, serviceName }).toString()}`, {
refreshInterval: 30000,
});
let colorClass = "text-black/20 dark:text-white/40 opacity-20";
let backgroundClass = "bg-theme-500/10 dark:bg-theme-900/50 px-1.5 py-0.5";
let statusTitle = t("ping.ping");
let statusText = "";
2023-09-27 13:24:10 +02:00
2023-09-27 22:07:38 +02:00
if (error) {
colorClass = "text-rose-500";
statusText = t("ping.error");
statusTitle += ` ${t("ping.error")}`;
2023-09-27 22:07:38 +02:00
} else if (!data) {
statusText = t("ping.ping");
statusTitle += ` ${t("ping.not_available")}`;
} else if (!data.alive) {
colorClass = "text-rose-500/80";
statusTitle += ` ${t("ping.down")}`;
statusText = t("ping.down");
} else if (data.alive) {
const ping = t("common.ms", { value: data.time, style: "unit", unit: "millisecond", maximumFractionDigits: 0 });
statusTitle += ` ${t("ping.up")} (${ping})`;
colorClass = "text-emerald-500/80";
2023-09-27 22:07:38 +02:00
if (style === "basic") {
statusText = t("ping.up");
2023-09-27 22:07:38 +02:00
} else {
statusText = ping;
colorClass += " lowercase";
2023-09-27 22:07:38 +02:00
}
2023-09-27 13:24:10 +02:00
}
if (style === "dot") {
backgroundClass = "p-4";
colorClass = colorClass.replace(/text-/g, "bg-").replace(/\/\d\d/g, "");
}
2023-04-14 10:32:12 -07:00
return (
<div
className={`w-auto text-center rounded-b-[3px] overflow-hidden ping-status ${backgroundClass}`}
title={statusTitle}
>
{style !== "dot" && <div className={`font-bold uppercase text-[8px] ${colorClass}`}>{statusText}</div>}
{style === "dot" && <div className={`rounded-full h-3 w-3 ${colorClass}`} />}
2023-04-14 10:32:12 -07:00
</div>
);
}