Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/mcp/McpConnectLauncher.tsx
T

121 lines
5.2 KiB
TypeScript

// 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 = [
<ClaudeMark className="h-3.5 w-3.5" />,
<ChatGptMark className="text-icon-neutral-primary h-3.5 w-3.5" />,
<CopilotMark className="h-3.5 w-3.5" />,
<ClaudeCodeMark className="h-3.5 w-3.5" />,
<CodexMark className="h-3.5 w-3.5" />,
<GeminiMark className="h-3.5 w-3.5" />,
];
/** 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<McpConnectVM>();
const revokeFetcher = useFetcher<FetcherPayload>();
const [open, setOpen] = useState(false);
const [revokingClientId, setRevokingClientId] = useState<string | null>(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.
*/}
<button
type="button"
onClick={() => setOpen(true)}
// No `title`: the native tooltip would cover the hover animation with a
// grey box repeating the label that is already next to the marks.
aria-label={t("mcp.connect.open")}
className="hg-mcp-pill border-border-default bg-surface-neutral-primary hover:bg-surface-neutral-secondary focus-visible:shadow-focus text-text-neutral-secondary flex h-8 shrink-0 cursor-pointer items-center gap-2 rounded-md border border-solid pr-3 pl-2.5 outline-none"
data-ui-id="ui.mcp.connect.trigger"
>
<span className="hg-mcp-marks shrink-0" aria-hidden="true">
{/* Doubled so the conveyor loops seamlessly at -50%. */}
<span className="hg-mcp-marks-track">
{[...PILL_MARKS, ...PILL_MARKS].map((mark, index) => (
<span key={index} className="flex h-3.5 w-3.5 items-center justify-center">
{mark}
</span>
))}
</span>
</span>
<span className="text-body-small-medium-0 font-body-small-medium-0 hidden md:inline">{t("mcp.connect.open")}</span>
</button>
<McpConnectDialog open={open} onOpenChange={onOpenChange} vm={vm} onRevoke={onRevoke} revokingClientId={revokingClientId} />
</>
);
}