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
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
2023-09-10 23:36:54 +02:00
|
|
|
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden ping-error">
|
2022-11-07 11:35:13 -08:00
|
|
|
<div className="text-[8px] font-bold text-rose-500 uppercase">{t("ping.error")}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
return (
|
2023-09-10 23:36:54 +02:00
|
|
|
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden ping-ping">
|
2022-11-07 11:35:13 -08:00
|
|
|
<div className="text-[8px] font-bold text-black/20 dark:text-white/40 uppercase">{t("ping.ping")}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-27 13:31:39 +02:00
|
|
|
let statusText = `HTTP status ${data.status}`;
|
|
|
|
let status;
|
2023-09-27 13:24:10 +02:00
|
|
|
|
2023-04-14 10:32:12 -07:00
|
|
|
if (data.status > 403) {
|
2023-09-27 13:24:10 +02:00
|
|
|
if (style === "basic") {
|
|
|
|
status = t("docker.offline")
|
|
|
|
} else if (style === "dot") {
|
|
|
|
status = "◉"
|
|
|
|
} else {
|
|
|
|
status = data.status
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:35:13 -08:00
|
|
|
return (
|
2023-09-10 23:36:54 +02:00
|
|
|
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden ping-status-invalid" title={statusText}>
|
2023-09-27 13:24:10 +02:00
|
|
|
<div className="text-[8px] font-bold text-rose-500/80 uppercase">{status}</div>
|
2022-11-07 11:35:13 -08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-09-27 13:24:10 +02:00
|
|
|
|
|
|
|
// Sucessful ping
|
2023-09-27 13:31:39 +02:00
|
|
|
const ping = t("common.ms", { value: data.latency, style: "unit", unit: "millisecond", maximumFractionDigits: 0 })
|
2023-09-27 13:24:10 +02:00
|
|
|
|
|
|
|
statusText += ` (${ping})`;
|
|
|
|
|
|
|
|
if (style === "basic") {
|
|
|
|
status = t("docker.running")
|
|
|
|
} else if (style === "dot") {
|
|
|
|
status = "◉"
|
|
|
|
} else {
|
|
|
|
status = ping
|
|
|
|
}
|
|
|
|
|
2023-04-14 10:32:12 -07:00
|
|
|
return (
|
2023-09-10 23:36:54 +02:00
|
|
|
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden ping-status-valid" title={statusText}>
|
2023-09-27 13:24:10 +02:00
|
|
|
<div className="text-[8px] font-bold text-emerald-500/80 uppercase">{status}</div>
|
2023-04-14 10:32:12 -07:00
|
|
|
</div>
|
|
|
|
);
|
2022-11-07 11:35:13 -08:00
|
|
|
}
|