2026-08-17 14:17:24 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-17 17:21:18 +02:00
|
|
|
import { Globe, Timer } from "@phosphor-icons/react/ssr";
|
2026-08-17 14:17:24 +02:00
|
|
|
import type { Widget } from "@/lib/config/schema";
|
|
|
|
|
import { useWidgetSubscription } from "@/lib/ws/client";
|
2026-08-17 14:27:07 +02:00
|
|
|
import { SkeletonLines } from "@/components/widgets/Skeleton";
|
2026-08-17 17:21:18 +02:00
|
|
|
import { WidgetCard } from "@/components/widgets/WidgetCard";
|
|
|
|
|
import { StatusTag } from "@/components/widgets/StatusTag";
|
2026-08-17 14:17:24 +02:00
|
|
|
|
|
|
|
|
type HttpWidget = Extract<Widget, { type: "http" }>;
|
|
|
|
|
|
2026-08-17 17:21:18 +02:00
|
|
|
function statusTone(up: boolean | undefined, consecutiveFailures: number): "accent" | "down" | "degraded" | "neutral" {
|
|
|
|
|
if (up === undefined) return "neutral";
|
|
|
|
|
if (up) return "accent";
|
|
|
|
|
return consecutiveFailures > 2 ? "down" : "degraded";
|
2026-08-17 14:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function HttpWidget({ widget, widgetId }: { widget: HttpWidget; widgetId: string }) {
|
|
|
|
|
const result = useWidgetSubscription(widgetId);
|
|
|
|
|
const data = result?.type === "http" ? result.data : null;
|
|
|
|
|
const errorMessage = result?.type === "error" ? result.message : null;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-17 17:21:18 +02:00
|
|
|
<WidgetCard span={3}>
|
|
|
|
|
<div className="flex items-center gap-2.5">
|
|
|
|
|
<Globe size={18} className="text-accent" aria-hidden />
|
|
|
|
|
<span className="flex-1 text-[15px] font-medium text-fg">{widget.name}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<StatusTag tone={statusTone(data?.up, data?.consecutiveFailures ?? 0)}>
|
|
|
|
|
{data ? (data.up ? `${data.statusCode ?? "up"}` : "down") : "…"}
|
|
|
|
|
</StatusTag>
|
2026-08-17 14:17:24 +02:00
|
|
|
</div>
|
|
|
|
|
{errorMessage && <span className="text-xs text-status-down">{errorMessage}</span>}
|
2026-08-17 14:27:07 +02:00
|
|
|
{!result && !errorMessage && <SkeletonLines />}
|
2026-08-17 14:17:24 +02:00
|
|
|
{data && (
|
2026-08-17 17:21:18 +02:00
|
|
|
<div className="flex items-center gap-1.5 text-[11px] text-fg-muted">
|
|
|
|
|
<Timer size={12} aria-hidden />
|
|
|
|
|
<span className="font-mono tabular-nums text-fg">{data.latencyMs}ms</span> latency
|
|
|
|
|
{!data.up && data.error && <span className="text-status-down">{data.error}</span>}
|
|
|
|
|
</div>
|
2026-08-17 14:17:24 +02:00
|
|
|
)}
|
2026-08-17 17:21:18 +02:00
|
|
|
</WidgetCard>
|
2026-08-17 14:17:24 +02:00
|
|
|
);
|
|
|
|
|
}
|