// VD-MCP — topbar entry point for the MCP connector: the pill left of the // global search plus the dialog it opens. Container half of the pair; it owns // the fetchers and the `?connect=mcp` deep link, the dialog stays purely // presentational. import { useEffect, useState } from "react"; import { useFetcher, useSearchParams } from "react-router"; import { useTranslation } from "react-i18next"; import type { McpConnectVM } from "@hivegrid/view-models"; import { useRootData } from "~/utils/data/useRootData"; import { appPath } from "~/utils/app/CanonicalUrlUtils"; import { ChatGptMark, ClaudeCodeMark, ClaudeMark, CodexMark, CopilotMark, GeminiMark } from "./AiClientMarks"; import { McpConnectDialog } from "./McpConnectDialog"; // Every client the connect dialog has a guide for. At rest the first three sit // in the pill; on hover the whole set rides past (see `.hg-mcp-*` in globals.css). const PILL_MARKS = [ , , , , , , ]; /** Query param that opens the dialog from a link (docs, onboarding mail). */ export const MCP_CONNECT_SEARCH_PARAM = "connect"; export const MCP_CONNECT_SEARCH_VALUE = "mcp"; type FetcherPayload = McpConnectVM | { error: string } | undefined; function asVM(payload: FetcherPayload): McpConnectVM | null { return payload && "status" in payload ? payload : null; } export function McpConnectLauncher({ tenantSlug }: { tenantSlug: string }) { const { t } = useTranslation(); const rootData = useRootData(); const [searchParams, setSearchParams] = useSearchParams(); const loadFetcher = useFetcher(); const revokeFetcher = useFetcher(); const [open, setOpen] = useState(false); const [revokingClientId, setRevokingClientId] = useState(null); const href = appPath({ tenantSlug, path: "mcp/connect", mode: "host-aware" }); const deepLinked = searchParams.get(MCP_CONNECT_SEARCH_PARAM) === MCP_CONNECT_SEARCH_VALUE; useEffect(() => { if (deepLinked) setOpen(true); }, [deepLinked]); // Load once per opening; the revoke action returns a fresh VM, so there is no // second round-trip after cutting a connection. useEffect(() => { if (open && loadFetcher.state === "idle" && loadFetcher.data === undefined) { loadFetcher.load(href); } }, [open, href, loadFetcher]); useEffect(() => { if (revokeFetcher.state === "idle") setRevokingClientId(null); }, [revokeFetcher.state]); function onOpenChange(next: boolean) { setOpen(next); if (!next && deepLinked) { const params = new URLSearchParams(searchParams); params.delete(MCP_CONNECT_SEARCH_PARAM); setSearchParams(params, { replace: true, preventScrollReset: true }); } } function onRevoke(clientId: string) { const form = new FormData(); form.set("intent", "revoke"); form.set("clientId", clientId); if (rootData?.csrf) form.set("csrf", rootData.csrf); setRevokingClientId(clientId); revokeFetcher.submit(form, { method: "post", action: href, preventScrollReset: true }); } const loaded = asVM(revokeFetcher.data) ?? asVM(loadFetcher.data); // A payload that arrived but is not a VM means the route answered with an // error envelope — show plain text instead of an endless spinner (DoD E.24). const failed = loadFetcher.state === "idle" && loadFetcher.data !== undefined && asVM(loadFetcher.data) === null; const vm: McpConnectVM = loaded ?? (failed ? { status: "error", error: { messageKey: "mcp.connect.error" } } : { status: "loading" }); return ( <> {/* Box instead of a bare icon: the client marks say what this connects to faster than any label, and the label says what it is. Under md the label drops and the marks carry the button on their own. */} ); }