64 lines
2.0 KiB
React
Raw Normal View History

import { useTranslation } from "react-i18next";
import useSWR from "swr";
2023-09-27 13:24:10 +02:00
export default function Ping({ group, service, style }) {
const { t } = useTranslation();
const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ group, service }).toString()}`, {
refreshInterval: 30000
});
if (error) {
return (
<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">
<div className="text-[8px] font-bold text-rose-500 uppercase">{t("ping.error")}</div>
</div>
);
}
if (!data) {
return (
<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">
<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
}
return (
<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>
</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 (
<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>
);
}