|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Link from "next/link"; |
| 4 | +import { Check, Copy, ExternalLink } from "lucide-react"; |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | +import { useTranslations } from "next-intl"; |
| 7 | +import { getStoredToken } from "@/lib/use-auth"; |
| 8 | +import { |
| 9 | + buildMcpClientSnippets, |
| 10 | + MCP_CLIENT_REGISTRY, |
| 11 | + type McpClientId, |
| 12 | + type McpConnectLocale, |
| 13 | + type McpConnectMode, |
| 14 | +} from "@/lib/mcp/connect-snippets"; |
| 15 | + |
| 16 | +interface McpConnectClientProps { |
| 17 | + locale: McpConnectLocale; |
| 18 | +} |
| 19 | + |
| 20 | +export function McpConnectClient({ locale }: McpConnectClientProps) { |
| 21 | + const t = useTranslations("mcpConnect"); |
| 22 | + const [mode, setMode] = useState<McpConnectMode>("publish"); |
| 23 | + const [clientId, setClientId] = useState<McpClientId>("claude-code"); |
| 24 | + const [token, setToken] = useState<string | null>(null); |
| 25 | + const [copyStatus, setCopyStatus] = useState<{ |
| 26 | + id: string; |
| 27 | + state: "copied" | "failed"; |
| 28 | + } | null>(null); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const storedToken = getStoredToken(); |
| 32 | + Promise.resolve().then(() => setToken(storedToken)); |
| 33 | + }, []); |
| 34 | + |
| 35 | + const blocks = buildMcpClientSnippets(clientId, { |
| 36 | + token, |
| 37 | + mode, |
| 38 | + locale, |
| 39 | + }); |
| 40 | + |
| 41 | + async function copy(value: string, id: string) { |
| 42 | + try { |
| 43 | + await navigator.clipboard.writeText(value); |
| 44 | + setCopyStatus({ id, state: "copied" }); |
| 45 | + } catch { |
| 46 | + setCopyStatus({ id, state: "failed" }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + function copyLabel(id: string) { |
| 51 | + if (copyStatus?.id !== id) return t("copy.copy"); |
| 52 | + return t(`copy.${copyStatus.state}`); |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="mx-auto max-w-6xl px-6 lg:px-8"> |
| 57 | + <header className="mb-10 border-t-4 border-[var(--foreground)] pt-6"> |
| 58 | + <p className="font-mono text-[10px] uppercase tracking-[0.3em] text-neutral-500"> |
| 59 | + {t("eyebrow")} |
| 60 | + </p> |
| 61 | + <h1 className="mt-2 font-serif text-4xl font-black uppercase tracking-tight text-[var(--foreground)] md:text-6xl"> |
| 62 | + {t("title")} |
| 63 | + </h1> |
| 64 | + <p className="mt-4 max-w-3xl text-sm leading-relaxed text-neutral-600 dark:text-neutral-400 md:text-base"> |
| 65 | + {t("intro")} |
| 66 | + </p> |
| 67 | + <p className="mt-3 max-w-3xl font-mono text-xs leading-relaxed text-neutral-500"> |
| 68 | + {t("privacy")} |
| 69 | + </p> |
| 70 | + </header> |
| 71 | + |
| 72 | + <section className="mb-8 border border-[var(--foreground)] p-4 md:p-6"> |
| 73 | + <h2 className="mb-3 font-mono text-xs font-bold uppercase tracking-widest"> |
| 74 | + {t("mode.label")} |
| 75 | + </h2> |
| 76 | + <div className="grid grid-cols-1 gap-2 sm:grid-cols-2"> |
| 77 | + {(["search", "publish"] as const).map((candidate) => ( |
| 78 | + <button |
| 79 | + key={candidate} |
| 80 | + type="button" |
| 81 | + aria-pressed={mode === candidate} |
| 82 | + onClick={() => setMode(candidate)} |
| 83 | + className={`border px-4 py-3 text-left font-sans text-sm font-bold transition-colors ${ |
| 84 | + mode === candidate |
| 85 | + ? "border-[#CC0000] bg-[#CC0000] text-white" |
| 86 | + : "border-[var(--foreground)] hover:text-[#CC0000]" |
| 87 | + }`} |
| 88 | + > |
| 89 | + {t(`mode.${candidate}`)} |
| 90 | + </button> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + </section> |
| 94 | + |
| 95 | + {token ? ( |
| 96 | + <section className="mb-8 flex flex-col gap-3 border border-emerald-700 bg-emerald-50 p-4 text-emerald-950 dark:bg-emerald-950/30 dark:text-emerald-100 sm:flex-row sm:items-center sm:justify-between"> |
| 97 | + <div> |
| 98 | + <div className="font-sans text-sm font-bold"> |
| 99 | + {t("auth.tokenReady")} |
| 100 | + </div> |
| 101 | + <div className="mt-1 font-mono text-xs opacity-70"> |
| 102 | + ••••••••{token.slice(-6)} |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + <button |
| 106 | + type="button" |
| 107 | + onClick={() => copy(token, "satoken")} |
| 108 | + className="inline-flex items-center justify-center gap-2 border border-current px-4 py-2 font-mono text-xs font-bold uppercase tracking-wider hover:bg-emerald-900 hover:text-white" |
| 109 | + > |
| 110 | + {copyStatus?.id === "satoken" && copyStatus.state === "copied" ? ( |
| 111 | + <Check className="size-4" aria-hidden="true" /> |
| 112 | + ) : ( |
| 113 | + <Copy className="size-4" aria-hidden="true" /> |
| 114 | + )} |
| 115 | + {copyStatus?.id === "satoken" |
| 116 | + ? t(`copy.${copyStatus.state}`) |
| 117 | + : t("auth.copyToken")} |
| 118 | + </button> |
| 119 | + </section> |
| 120 | + ) : ( |
| 121 | + <section className="mb-8 border border-amber-700 bg-amber-50 p-4 text-sm leading-relaxed text-amber-950 dark:bg-amber-950/30 dark:text-amber-100"> |
| 122 | + {t("auth.loginHint")}{" "} |
| 123 | + <Link |
| 124 | + href="/login" |
| 125 | + className="font-bold underline underline-offset-4" |
| 126 | + > |
| 127 | + {t("auth.loginLink")} |
| 128 | + </Link> |
| 129 | + </section> |
| 130 | + )} |
| 131 | + |
| 132 | + <div className="grid gap-8 lg:grid-cols-[15rem_minmax(0,1fr)]"> |
| 133 | + <section aria-label={t("clients.label")}> |
| 134 | + <h2 className="mb-3 font-mono text-xs font-bold uppercase tracking-widest"> |
| 135 | + {t("clients.label")} |
| 136 | + </h2> |
| 137 | + <div |
| 138 | + role="tablist" |
| 139 | + aria-orientation="vertical" |
| 140 | + className="grid grid-cols-2 border-l border-t border-[var(--foreground)] sm:grid-cols-3 lg:grid-cols-1" |
| 141 | + > |
| 142 | + {MCP_CLIENT_REGISTRY.map((client) => ( |
| 143 | + <button |
| 144 | + key={client.id} |
| 145 | + id={`mcp-client-${client.id}`} |
| 146 | + type="button" |
| 147 | + role="tab" |
| 148 | + aria-selected={clientId === client.id} |
| 149 | + aria-controls="mcp-client-panel" |
| 150 | + onClick={() => setClientId(client.id)} |
| 151 | + className={`border-b border-r border-[var(--foreground)] px-3 py-3 text-left font-sans text-xs font-bold transition-colors ${ |
| 152 | + clientId === client.id |
| 153 | + ? "bg-[var(--foreground)] text-[var(--background)]" |
| 154 | + : "hover:text-[#CC0000]" |
| 155 | + }`} |
| 156 | + > |
| 157 | + {t(`clients.${client.id}`)} |
| 158 | + </button> |
| 159 | + ))} |
| 160 | + </div> |
| 161 | + </section> |
| 162 | + |
| 163 | + <section |
| 164 | + id="mcp-client-panel" |
| 165 | + role="tabpanel" |
| 166 | + aria-labelledby={`mcp-client-${clientId}`} |
| 167 | + className="min-w-0" |
| 168 | + > |
| 169 | + <div className="mb-4 border-b-2 border-[var(--foreground)] pb-3"> |
| 170 | + <h2 className="font-serif text-2xl font-black"> |
| 171 | + {t(`clients.${clientId}`)} |
| 172 | + </h2> |
| 173 | + </div> |
| 174 | + |
| 175 | + <div className="space-y-5"> |
| 176 | + {blocks.map((block) => { |
| 177 | + if (block.kind === "code") { |
| 178 | + const copyId = `${clientId}-${mode}-${block.id}`; |
| 179 | + return ( |
| 180 | + <article key={block.id}> |
| 181 | + <div className="mb-2 flex flex-wrap items-baseline justify-between gap-2"> |
| 182 | + <h3 className="font-mono text-xs font-bold uppercase tracking-widest"> |
| 183 | + {t(`blocks.${block.title}`)} |
| 184 | + </h3> |
| 185 | + {block.detail ? ( |
| 186 | + <span className="break-all font-mono text-[11px] text-neutral-500"> |
| 187 | + {block.detail} |
| 188 | + </span> |
| 189 | + ) : null} |
| 190 | + </div> |
| 191 | + <div className="relative border border-[var(--foreground)] bg-neutral-950 text-neutral-100"> |
| 192 | + <pre className="overflow-x-auto whitespace-pre-wrap break-words p-4 pr-28 font-mono text-xs leading-6"> |
| 193 | + <code>{block.content}</code> |
| 194 | + </pre> |
| 195 | + <button |
| 196 | + type="button" |
| 197 | + onClick={() => copy(block.content, copyId)} |
| 198 | + className="absolute right-2 top-2 inline-flex items-center gap-1.5 border border-neutral-600 bg-neutral-950 px-2.5 py-1.5 font-mono text-[10px] uppercase tracking-wider hover:border-white" |
| 199 | + aria-label={`${copyLabel(copyId)}: ${t( |
| 200 | + `blocks.${block.title}`, |
| 201 | + )}`} |
| 202 | + > |
| 203 | + {copyStatus?.id === copyId && |
| 204 | + copyStatus.state === "copied" ? ( |
| 205 | + <Check className="size-3.5" aria-hidden="true" /> |
| 206 | + ) : ( |
| 207 | + <Copy className="size-3.5" aria-hidden="true" /> |
| 208 | + )} |
| 209 | + {copyLabel(copyId)} |
| 210 | + </button> |
| 211 | + </div> |
| 212 | + </article> |
| 213 | + ); |
| 214 | + } |
| 215 | + |
| 216 | + if (block.kind === "link") { |
| 217 | + return ( |
| 218 | + <article key={block.id}> |
| 219 | + <h3 className="mb-2 font-mono text-xs font-bold uppercase tracking-widest"> |
| 220 | + {t(`blocks.${block.title}`)} |
| 221 | + </h3> |
| 222 | + <a |
| 223 | + href={block.href} |
| 224 | + target="_blank" |
| 225 | + rel="noopener noreferrer" |
| 226 | + className="flex items-center justify-between gap-3 border border-[var(--foreground)] p-4 text-sm font-bold hover:text-[#CC0000]" |
| 227 | + > |
| 228 | + <span>{t(`messages.${block.messageKey}`)}</span> |
| 229 | + <ExternalLink |
| 230 | + className="size-4 shrink-0" |
| 231 | + aria-hidden="true" |
| 232 | + /> |
| 233 | + </a> |
| 234 | + </article> |
| 235 | + ); |
| 236 | + } |
| 237 | + |
| 238 | + return ( |
| 239 | + <p |
| 240 | + key={block.id} |
| 241 | + className={`border p-4 text-sm leading-relaxed ${ |
| 242 | + block.tone === "notice" |
| 243 | + ? "border-amber-700 bg-amber-50 text-amber-950 dark:bg-amber-950/30 dark:text-amber-100" |
| 244 | + : "border-[var(--foreground)]" |
| 245 | + }`} |
| 246 | + > |
| 247 | + {t(`messages.${block.messageKey}`)} |
| 248 | + </p> |
| 249 | + ); |
| 250 | + })} |
| 251 | + </div> |
| 252 | + </section> |
| 253 | + </div> |
| 254 | + </div> |
| 255 | + ); |
| 256 | +} |
0 commit comments