"use client"; export const dynamic = "force-dynamic"; import { useMemo } from "react"; import { useParams, useRouter } from "next/navigation"; import { SignedIn, SignedOut, useAuth } from "@/auth/clerk"; import { ApiError } from "@/api/mutator"; import { type gatewaysStatusApiV1GatewaysStatusGetResponse, type getGatewayApiV1GatewaysGatewayIdGetResponse, useGatewaysStatusApiV1GatewaysStatusGet, useGetGatewayApiV1GatewaysGatewayIdGet, } from "@/api/generated/gateways/gateways"; import { type listAgentsApiV1AgentsGetResponse, useListAgentsApiV1AgentsGet, } from "@/api/generated/agents/agents"; import { useOrganizationMembership } from "@/lib/use-organization-membership"; import { AdminOnlyNotice } from "@/components/auth/AdminOnlyNotice"; import { SignedOutPanel } from "@/components/auth/SignedOutPanel"; import { DashboardSidebar } from "@/components/organisms/DashboardSidebar"; import { DashboardShell } from "@/components/templates/DashboardShell"; import { Button } from "@/components/ui/button"; const formatTimestamp = (value?: string | null) => { if (!value) return "—"; const date = new Date(`${value}${value.endsWith("Z") ? "" : "Z"}`); if (Number.isNaN(date.getTime())) return "—"; return date.toLocaleString(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); }; const maskToken = (value?: string | null) => { if (!value) return "—"; if (value.length <= 8) return "••••"; return `••••${value.slice(-4)}`; }; export default function GatewayDetailPage() { const router = useRouter(); const params = useParams(); const { isSignedIn } = useAuth(); const gatewayIdParam = params?.gatewayId; const gatewayId = Array.isArray(gatewayIdParam) ? gatewayIdParam[0] : gatewayIdParam; const { isAdmin } = useOrganizationMembership(isSignedIn); const gatewayQuery = useGetGatewayApiV1GatewaysGatewayIdGet< getGatewayApiV1GatewaysGatewayIdGetResponse, ApiError >(gatewayId ?? "", { query: { enabled: Boolean(isSignedIn && isAdmin && gatewayId), refetchInterval: 30_000, }, }); const gateway = gatewayQuery.data?.status === 200 ? gatewayQuery.data.data : null; const agentsQuery = useListAgentsApiV1AgentsGet< listAgentsApiV1AgentsGetResponse, ApiError >(gatewayId ? { gateway_id: gatewayId } : undefined, { query: { enabled: Boolean(isSignedIn && isAdmin && gatewayId), refetchInterval: 15_000, }, }); const statusParams = gateway ? { gateway_url: gateway.url, gateway_token: gateway.token ?? undefined, gateway_main_session_key: gateway.main_session_key ?? undefined, } : undefined; const statusQuery = useGatewaysStatusApiV1GatewaysStatusGet< gatewaysStatusApiV1GatewaysStatusGetResponse, ApiError >(statusParams, { query: { enabled: Boolean(isSignedIn && isAdmin && statusParams), refetchInterval: 15_000, }, }); const agents = useMemo( () => agentsQuery.data?.status === 200 ? (agentsQuery.data.data.items ?? []) : [], [agentsQuery.data], ); const status = statusQuery.data?.status === 200 ? statusQuery.data.data : null; const isConnected = status?.connected ?? false; const title = useMemo( () => (gateway?.name ? gateway.name : "Gateway"), [gateway?.name], ); return (

{title}

Gateway configuration and connection details.

{isAdmin && gatewayId ? ( ) : null}
{!isAdmin ? ( ) : gatewayQuery.isLoading ? (
Loading gateway…
) : gatewayQuery.error ? (
{gatewayQuery.error.message}
) : gateway ? (

Connection

{statusQuery.isLoading ? "Checking" : isConnected ? "Online" : "Offline"}

Gateway URL

{gateway.url}

Token

{maskToken(gateway.token)}

Runtime

Main session key

{gateway.main_session_key}

Workspace root

{gateway.workspace_root}

Created

{formatTimestamp(gateway.created_at)}

Updated

{formatTimestamp(gateway.updated_at)}

Agents

{agentsQuery.isLoading ? ( Loading… ) : ( {agents.length} total )}
{agents.length === 0 && !agentsQuery.isLoading ? ( ) : ( agents.map((agent) => ( )) )}
Agent Status Last seen Updated
No agents assigned to this gateway.

{agent.name}

{agent.id}

{agent.status} {formatTimestamp(agent.last_seen_at ?? null)} {formatTimestamp(agent.updated_at)}
) : null}
); }