"use client"; export const dynamic = "force-dynamic"; import { useMemo, useState } from "react"; import Link from "next/link"; import { useAuth } from "@/auth/clerk"; import { useQueryClient } from "@tanstack/react-query"; import { GatewaysTable } from "@/components/gateways/GatewaysTable"; import { DashboardPageLayout } from "@/components/templates/DashboardPageLayout"; import { buttonVariants } from "@/components/ui/button"; import { ConfirmActionDialog } from "@/components/ui/confirm-action-dialog"; import { ApiError } from "@/api/mutator"; import { type listGatewaysApiV1GatewaysGetResponse, getListGatewaysApiV1GatewaysGetQueryKey, useDeleteGatewayApiV1GatewaysGatewayIdDelete, useListGatewaysApiV1GatewaysGet, } from "@/api/generated/gateways/gateways"; import { createOptimisticListDeleteMutation } from "@/lib/list-delete"; import { useOrganizationMembership } from "@/lib/use-organization-membership"; import type { GatewayRead } from "@/api/generated/model"; import { useUrlSorting } from "@/lib/use-url-sorting"; const GATEWAY_SORTABLE_COLUMNS = ["name", "workspace_root", "updated_at"]; export default function GatewaysPage() { const { isSignedIn } = useAuth(); const queryClient = useQueryClient(); const { sorting, onSortingChange } = useUrlSorting({ allowedColumnIds: GATEWAY_SORTABLE_COLUMNS, defaultSorting: [{ id: "name", desc: false }], paramPrefix: "gateways", }); const { isAdmin } = useOrganizationMembership(isSignedIn); const [deleteTarget, setDeleteTarget] = useState(null); const gatewaysKey = getListGatewaysApiV1GatewaysGetQueryKey(); const gatewaysQuery = useListGatewaysApiV1GatewaysGet< listGatewaysApiV1GatewaysGetResponse, ApiError >(undefined, { query: { enabled: Boolean(isSignedIn && isAdmin), refetchInterval: 30_000, refetchOnMount: "always", }, }); const gateways = useMemo( () => gatewaysQuery.data?.status === 200 ? (gatewaysQuery.data.data.items ?? []) : [], [gatewaysQuery.data], ); const deleteMutation = useDeleteGatewayApiV1GatewaysGatewayIdDelete< ApiError, { previous?: listGatewaysApiV1GatewaysGetResponse } >( { mutation: createOptimisticListDeleteMutation< GatewayRead, listGatewaysApiV1GatewaysGetResponse, { gatewayId: string } >({ queryClient, queryKey: gatewaysKey, getItemId: (gateway) => gateway.id, getDeleteId: ({ gatewayId }) => gatewayId, onSuccess: () => { setDeleteTarget(null); }, invalidateQueryKeys: [gatewaysKey], }), }, queryClient, ); const handleDelete = () => { if (!deleteTarget) return; deleteMutation.mutate({ gatewayId: deleteTarget.id }); }; return ( <> 0 ? ( Create gateway ) : null } isAdmin={isAdmin} adminOnlyMessage="Only organization owners and admins can access gateways." stickyHeader >
{gatewaysQuery.error ? (

{gatewaysQuery.error.message}

) : null}
setDeleteTarget(null)} title="Delete gateway?" description={ <> This removes the gateway connection from Mission Control. Boards using it will need a new gateway assigned. } errorMessage={deleteMutation.error?.message} errorStyle="text" cancelVariant="ghost" onConfirm={handleDelete} isConfirming={deleteMutation.isPending} /> ); }