2022-11-07 11:35:13 -08:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-06-11 09:50:41 -07:00
|
|
|
export default function Ping({ group, service }) {
|
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-06-11 09:50:41 -07:00
|
|
|
const statusText = `${service}: HTTP status ${data.status}`;
|
2022-11-07 11:35:13 -08:00
|
|
|
|
2023-04-14 10:32:12 -07:00
|
|
|
if (data.status > 403) {
|
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}>
|
2022-11-07 11:35:13 -08:00
|
|
|
<div className="text-[8px] font-bold text-rose-500/80">{data.status}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-05-09 16:43:53 -07:00
|
|
|
<div className="text-[8px] font-bold text-emerald-500/80">{t("common.ms", { value: data.latency, style: "unit", unit: "millisecond", maximumFractionDigits: 0 })}</div>
|
2023-04-14 10:32:12 -07:00
|
|
|
</div>
|
|
|
|
);
|
2022-11-07 11:35:13 -08:00
|
|
|
|
|
|
|
}
|