Files
back_end_oudelaa/oudelaa_dashboard/components/dashboard/connection-indicator.tsx
boutmoun123 1b753a419a
API Gateway CI / Build, lint, and test (push) Successful in 13m33s
API Gateway CI / Build API Gateway image (push) Successful in 5m43s
feat: sync marketplace dashboard and staging CI
2026-11-02 17:28:58 +03:00

85 lines
2.6 KiB
TypeScript

"use client";
import { Activity, RefreshCcw, Wifi, WifiOff } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { cn } from "@/lib/utils";
type ConnectionState = "checking" | "online" | "degraded" | "offline";
type ConnectionPayload = {
state: Exclude<ConnectionState, "checking">;
activeUpstream: string | null;
latencyMs: number | null;
checkedAt: string;
};
const stateStyles: Record<ConnectionState, string> = {
checking: "border-border bg-secondary/40 text-muted-foreground",
online: "border-emerald-500/25 bg-emerald-500/10 text-emerald-300",
degraded: "border-amber-500/25 bg-amber-500/10 text-amber-200",
offline: "border-rose-500/25 bg-rose-500/10 text-rose-200",
};
export function ConnectionIndicator() {
const [state, setState] = useState<ConnectionState>("checking");
const [payload, setPayload] = useState<ConnectionPayload | null>(null);
const check = useCallback(async () => {
setState((current) => (current === "offline" ? current : "checking"));
try {
const response = await fetch("/api/connection", {
cache: "no-store",
signal: AbortSignal.timeout(6_000),
});
const nextPayload = (await response.json()) as ConnectionPayload;
setPayload(nextPayload);
setState(nextPayload.state);
} catch {
setPayload(null);
setState("offline");
}
}, []);
useEffect(() => {
void check();
const interval = window.setInterval(() => void check(), 30_000);
return () => window.clearInterval(interval);
}, [check]);
const Icon =
state === "checking"
? RefreshCcw
: state === "offline"
? WifiOff
: state === "degraded"
? Activity
: Wifi;
const label =
state === "checking"
? "Checking API"
: state === "online"
? `API online${payload?.latencyMs !== null ? ` · ${payload?.latencyMs}ms` : ""}`
: state === "degraded"
? "API fallback"
: "API offline";
return (
<button
type="button"
onClick={() => void check()}
aria-label={`${label}. Activate to check again.`}
aria-live="polite"
aria-busy={state === "checking"}
title={payload?.activeUpstream ?? "Backend connection status"}
className={cn(
"inline-flex h-9 items-center gap-2 rounded-xl border px-3 text-xs font-semibold transition hover:-translate-y-0.5",
stateStyles[state],
)}
>
<Icon aria-hidden="true" className={cn("h-3.5 w-3.5", state === "checking" && "animate-spin")} />
<span className="hidden sm:inline">{label}</span>
</button>
);
}