import { useState } from "react"; import { aggregatorManagementUrl } from "@paperclipai/shared/aggregator-apps"; import { Label } from "@/components/ui/label"; import { ExternalLink } from "lucide-react"; import type { AggregatorAppCatalogEntry, AggregatorAppRoute } from "@paperclipai/shared/aggregator-app-catalog"; import { isToolConnectionAttentionHealth, type ToolConnection } from "@paperclipai/shared"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { AppLogo } from "./AppLogo"; import { remoteMcpProviders } from "@/features/connections/remote-mcp/providers"; import { ComposioAppSetup } from "./ComposioAppSetup"; export function aggregatorAppConnectHref(route: AggregatorAppRoute, connection?: ToolConnection) { const params = new URLSearchParams({ source: route.provider, targetToolkit: route.toolkit }); if (connection) params.set(connection.status === "draft" ? "resume" : "reconnect", connection.id); return `/apps/connect?${params}`; } export function AggregatorConnectDialog({ app, connections, initialProvider, onClose, onNavigate }: { app: AggregatorAppCatalogEntry; connections: Partial>; initialProvider?: AggregatorAppRoute["provider"]; onClose: () => void; onNavigate: (href: string) => void; }) { const [selectedAccountId, setSelectedAccountId] = useState(""); const [selected, setSelected] = useState(() => app.routes.find(route => route.provider === initialProvider) ?? (app.routes.length === 1 ? app.routes[0] : null)); const canChooseProvider = app.routes.length > 1; const existing = selected ? connections[selected.provider] ?? [] : []; const usableConnection = existing.find((connection) => connection.status === "active" && connection.enabled && !isToolConnectionAttentionHealth(connection.healthStatus)); const account = existing.find(connection => connection.id === selectedAccountId) ?? usableConnection ?? existing[0]; const managementUrl = selected ? aggregatorManagementUrl(selected.provider, typeof account?.config?.managementUrl === "string" ? account.config.managementUrl : null) : null; const provider = selected ? remoteMcpProviders[selected.provider] : null; function choose(route: AggregatorAppRoute) { const accounts = connections[route.provider] ?? []; if (accounts.length === 0) { onNavigate(aggregatorAppConnectHref(route)); onClose(); } else setSelected(route); } return { if (!open) onClose(); }}> {provider ? `Connect ${app.name} through ${provider.name}` : `Connect ${app.name}`} {selected?.provider === "composio" ? `Choose a saved Composio account or connect a new one to use ${app.name}.` : provider ? `Your ${provider.name} gateway is already saved. ${app.name} authorization is managed inside ${provider.name}.` : "Which service would you like to use? This service handles the connection and requests to this app."} {!selected ?
{[...app.routes].sort((a, b) => Number(Boolean(connections[b.provider]?.length)) - Number(Boolean(connections[a.provider]?.length))).map((route) => { const accounts = connections[route.provider] ?? []; const name = remoteMcpProviders[route.provider].name; const status = accounts.some((connection) => connection.status === "active" && connection.enabled && !isToolConnectionAttentionHealth(connection.healthStatus)) ? "Already connected" : accounts.some((connection) => connection.status === "draft") ? "Setup incomplete" : accounts.length ? "Needs attention" : "Set up a connection"; return ; })}
:
{selected.provider !== "composio" ?
: null} {selected.provider === "composio" ? connection.status === "active" && connection.enabled && !isToolConnectionAttentionHealth(connection.healthStatus))} onClose={onClose} onBack={canChooseProvider ? () => setSelected(null) : undefined} onConnectNew={() => { onNavigate(`${aggregatorAppConnectHref(selected)}&new=1`); onClose(); }} /> : null}
} {selected?.provider !== "composio" ? {selected ?
{managementUrl ? : null}
: null}
: null}
; }