2022-11-07 11:35:13 -08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-09-27 13:24:10 +02:00
|
|
|
export default function Ping({ group, service, style }) {
|
2022-11-07 11:35:13 -08:00
|
|
|
const { t } = useTranslation();
|
2023-06-11 09:50:41 -07:00
|
|
|
const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ group, service }).toString()}`, {
|
2022-11-07 11:35:13 -08:00
|
|
|
refreshInterval: 30000
|
|
|
|
});
|
|
|
|
|
2023-09-27 22:07:38 +02:00
|
|
|
let textSize = "text-[8px]";
|
|
|
|
let colorClass = ""
|
|
|
|
let backgroundClass = "bg-theme-500/10 dark:bg-theme-900/50";
|
|
|
|
let statusTitle = "HTTP status";
|
|
|
|
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 += " error"
|
|
|
|
} else if (!data) {
|
|
|
|
colorClass = "text-black/20 dark:text-white/40"
|
|
|
|
statusText = t("ping.ping")
|
|
|
|
statusTitle += " not available"
|
|
|
|
} else if (data.status > 403) {
|
|
|
|
colorClass = "text-rose-500/80"
|
|
|
|
statusTitle += ` ${data.status}`
|
2023-09-28 09:34:55 +02:00
|
|
|
|
2023-09-27 13:24:10 +02:00
|
|
|
if (style === "basic") {
|
2023-09-27 22:07:38 +02:00
|
|
|
statusText = t("docker.offline")
|
2023-09-27 13:24:10 +02:00
|
|
|
} else if (style === "dot") {
|
2023-09-27 22:07:38 +02:00
|
|
|
statusText = "◉"
|
2023-09-28 09:34:55 +02:00
|
|
|
textSize = "text-[14px]"
|
2023-09-27 22:07:38 +02:00
|
|
|
backgroundClass = ""
|
2023-09-27 13:24:10 +02:00
|
|
|
} else {
|
2023-09-27 22:07:38 +02:00
|
|
|
statusText = data.status
|
2023-09-27 13:24:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-09-27 22:07:38 +02:00
|
|
|
const ping = t("common.ms", { value: data.latency, style: "unit", unit: "millisecond", maximumFractionDigits: 0 })
|
|
|
|
statusTitle += ` ${data.status} (${ping})`;
|
|
|
|
colorClass = "text-emerald-500/80"
|
|
|
|
|
|
|
|
if (style === "basic") {
|
|
|
|
statusText = t("docker.running")
|
|
|
|
} else if (style === "dot") {
|
|
|
|
statusText = "◉"
|
2023-09-28 09:34:55 +02:00
|
|
|
textSize = "text-[14px]"
|
2023-09-27 22:07:38 +02:00
|
|
|
backgroundClass = ""
|
|
|
|
} else {
|
|
|
|
statusText = ping
|
|
|
|
}
|
2023-09-27 13:24:10 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 10:32:12 -07:00
|
|
|
return (
|
2023-09-27 22:07:38 +02:00
|
|
|
<div className={`w-auto px-1.5 py-0.5 text-center rounded-b-[3px] overflow-hidden ping-status-invalid ${backgroundClass}`} title={statusTitle}>
|
|
|
|
<div className={`font-bold uppercase ${textSize} ${colorClass}`}>{statusText}</div>
|
2023-04-14 10:32:12 -07:00
|
|
|
</div>
|
|
|
|
);
|
2022-11-07 11:35:13 -08:00
|
|
|
}
|