34 lines
1.3 KiB
TypeScript
34 lines
1.3 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import type { Widget } from "@/lib/config/schema";
|
||
|
|
import { useWidgetSubscription } from "@/lib/ws/client";
|
||
|
|
import { StatusDot } from "@/components/widgets/StatusDot";
|
||
|
|
import { formatUptime } from "@/lib/format";
|
||
|
|
|
||
|
|
type DatabaseWidget = Extract<Widget, { type: "database" }>;
|
||
|
|
|
||
|
|
const ENGINE_LABEL: Record<DatabaseWidget["engine"], string> = {
|
||
|
|
postgres: "PostgreSQL",
|
||
|
|
redis: "Redis",
|
||
|
|
};
|
||
|
|
|
||
|
|
export function DatabaseWidget({ widget, widgetId }: { widget: DatabaseWidget; widgetId: string }) {
|
||
|
|
const result = useWidgetSubscription(widgetId);
|
||
|
|
const data = result?.type === "database" ? result.data : null;
|
||
|
|
const errorMessage = result?.type === "error" ? result.message : null;
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="flex flex-col gap-2 rounded-[var(--radius-widget)] border border-border bg-surface p-4">
|
||
|
|
<div className="flex items-center justify-between gap-2">
|
||
|
|
<span className="text-sm font-medium text-fg">{widget.name}</span>
|
||
|
|
<StatusDot status={data?.status} health={data?.health} />
|
||
|
|
</div>
|
||
|
|
<span className="text-xs text-fg-muted">{ENGINE_LABEL[widget.engine]}</span>
|
||
|
|
{errorMessage && <span className="text-xs text-status-down">{errorMessage}</span>}
|
||
|
|
{data?.uptimeSeconds != null && (
|
||
|
|
<span className="text-xs text-fg-muted">Uptime: {formatUptime(data.uptimeSeconds)}</span>
|
||
|
|
)}
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|