|
| 1 | +import { useCallback, useEffect, useState } from 'react'; |
| 2 | +import { Check, Copy, Download, Loader2, ShieldCheck, ShieldOff, AlertCircle } from 'lucide-react'; |
| 3 | +import { Button } from '@mcpmux/ui'; |
| 4 | +import { getGatewayStatus } from '@/lib/api/gateway'; |
| 5 | +import { |
| 6 | + generateWorkspaceConfigSnippet, |
| 7 | + getGatewayAuthDisabled, |
| 8 | + installWorkspaceMcpConfig, |
| 9 | + listWorkspaceInstallClients, |
| 10 | + setGatewayAuthDisabled, |
| 11 | + type WorkspaceInstallClient, |
| 12 | + type WorkspaceInstallResult, |
| 13 | +} from '@/lib/api/workspaceInstall'; |
| 14 | + |
| 15 | +/** Clients selected by default — the most common three. */ |
| 16 | +const DEFAULT_SELECTED = ['cursor', 'claude-code', 'vscode']; |
| 17 | + |
| 18 | +/** |
| 19 | + * "Connect apps to this folder" — writes (or extends) project-local MCP configs |
| 20 | + * inside `workspaceRoot`, injecting `X-Mcpmux-Workspace: <folder path>` so the |
| 21 | + * gateway routes those apps to this folder's binding deterministically, even |
| 22 | + * when the client doesn't report MCP roots. Also surfaces (and can flip) the |
| 23 | + * system-wide auth toggle inline, since disabling it makes the config a pure |
| 24 | + * URL + header with no access key. |
| 25 | + */ |
| 26 | +export function WorkspaceInstallPanel({ workspaceRoot }: { workspaceRoot: string }) { |
| 27 | + const [clients, setClients] = useState<WorkspaceInstallClient[]>([]); |
| 28 | + const [selected, setSelected] = useState<Set<string>>(() => new Set(DEFAULT_SELECTED)); |
| 29 | + const [mcpUrl, setMcpUrl] = useState<string | null>(null); |
| 30 | + const [authDisabled, setAuthDisabled] = useState<boolean | null>(null); |
| 31 | + const [installing, setInstalling] = useState(false); |
| 32 | + const [results, setResults] = useState<WorkspaceInstallResult[] | null>(null); |
| 33 | + const [copiedId, setCopiedId] = useState<string | null>(null); |
| 34 | + const [error, setError] = useState<string | null>(null); |
| 35 | + const [togglingAuth, setTogglingAuth] = useState(false); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + let cancelled = false; |
| 39 | + void (async () => { |
| 40 | + try { |
| 41 | + const [list, status, disabled] = await Promise.all([ |
| 42 | + listWorkspaceInstallClients(), |
| 43 | + getGatewayStatus().catch(() => ({ running: false, url: null as string | null })), |
| 44 | + getGatewayAuthDisabled().catch(() => false), |
| 45 | + ]); |
| 46 | + if (cancelled) return; |
| 47 | + setClients(list); |
| 48 | + setAuthDisabled(disabled); |
| 49 | + setMcpUrl(status.url ? `${status.url}/mcp` : null); |
| 50 | + } catch (e) { |
| 51 | + if (!cancelled) setError(e instanceof Error ? e.message : String(e)); |
| 52 | + } |
| 53 | + })(); |
| 54 | + return () => { |
| 55 | + cancelled = true; |
| 56 | + }; |
| 57 | + }, []); |
| 58 | + |
| 59 | + const toggleClient = (id: string) => { |
| 60 | + setSelected((prev) => { |
| 61 | + const next = new Set(prev); |
| 62 | + if (next.has(id)) next.delete(id); |
| 63 | + else next.add(id); |
| 64 | + return next; |
| 65 | + }); |
| 66 | + setResults(null); |
| 67 | + }; |
| 68 | + |
| 69 | + const handleCopy = useCallback( |
| 70 | + async (clientId: string) => { |
| 71 | + if (!mcpUrl) return; |
| 72 | + try { |
| 73 | + const snip = await generateWorkspaceConfigSnippet({ |
| 74 | + client: clientId, |
| 75 | + serverUrl: mcpUrl, |
| 76 | + workspaceRoot, |
| 77 | + }); |
| 78 | + await navigator.clipboard.writeText(snip.content); |
| 79 | + setCopiedId(clientId); |
| 80 | + setTimeout(() => setCopiedId((c) => (c === clientId ? null : c)), 1500); |
| 81 | + } catch (e) { |
| 82 | + setError(e instanceof Error ? e.message : String(e)); |
| 83 | + } |
| 84 | + }, |
| 85 | + [mcpUrl, workspaceRoot] |
| 86 | + ); |
| 87 | + |
| 88 | + const handleInstall = async () => { |
| 89 | + if (!mcpUrl || selected.size === 0) return; |
| 90 | + setInstalling(true); |
| 91 | + setError(null); |
| 92 | + setResults(null); |
| 93 | + try { |
| 94 | + const res = await installWorkspaceMcpConfig({ |
| 95 | + workspaceRoot, |
| 96 | + serverUrl: mcpUrl, |
| 97 | + clients: Array.from(selected), |
| 98 | + }); |
| 99 | + setResults(res); |
| 100 | + } catch (e) { |
| 101 | + setError(e instanceof Error ? e.message : String(e)); |
| 102 | + } finally { |
| 103 | + setInstalling(false); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + const handleDisableAuth = async () => { |
| 108 | + setTogglingAuth(true); |
| 109 | + try { |
| 110 | + const v = await setGatewayAuthDisabled(true); |
| 111 | + setAuthDisabled(v); |
| 112 | + } catch (e) { |
| 113 | + setError(e instanceof Error ? e.message : String(e)); |
| 114 | + } finally { |
| 115 | + setTogglingAuth(false); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + return ( |
| 120 | + <div className="space-y-4" data-testid="workspace-install-panel"> |
| 121 | + <p className="text-sm text-[rgb(var(--muted))]"> |
| 122 | + Drop McpMux into this folder's MCP config for the apps you use. Each gets a{' '} |
| 123 | + <code className="text-xs">X-Mcpmux-Workspace</code> header set to this path, so it routes |
| 124 | + here automatically — even apps that don't report the folder (like Cursor). |
| 125 | + </p> |
| 126 | + |
| 127 | + {/* Self-introductory auth nudge — disabling auth makes the written config |
| 128 | + a pure URL + header with no access key to manage. */} |
| 129 | + {authDisabled === false && ( |
| 130 | + <div |
| 131 | + className="flex items-start gap-2.5 rounded-lg border border-amber-200 bg-amber-50 p-3 text-xs dark:border-amber-800/60 dark:bg-amber-900/20" |
| 132 | + data-testid="workspace-install-auth-nudge" |
| 133 | + > |
| 134 | + <ShieldOff className="mt-0.5 h-4 w-4 flex-shrink-0 text-amber-600 dark:text-amber-400" /> |
| 135 | + <div className="min-w-0 flex-1"> |
| 136 | + <p className="font-medium text-amber-800 dark:text-amber-300"> |
| 137 | + Apps will need an access key to connect. |
| 138 | + </p> |
| 139 | + <p className="mt-0.5 text-amber-700 dark:text-amber-400"> |
| 140 | + For zero-config setup, disable system-wide authentication — apps then connect with |
| 141 | + just the URL and this workspace header. |
| 142 | + </p> |
| 143 | + <Button |
| 144 | + variant="secondary" |
| 145 | + size="sm" |
| 146 | + className="mt-2 h-7 text-xs" |
| 147 | + disabled={togglingAuth} |
| 148 | + onClick={handleDisableAuth} |
| 149 | + data-testid="workspace-install-disable-auth" |
| 150 | + > |
| 151 | + {togglingAuth ? ( |
| 152 | + <Loader2 className="mr-1.5 h-3 w-3 animate-spin" /> |
| 153 | + ) : ( |
| 154 | + <ShieldOff className="mr-1.5 h-3 w-3" /> |
| 155 | + )} |
| 156 | + Disable authentication |
| 157 | + </Button> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + )} |
| 161 | + {authDisabled === true && ( |
| 162 | + <div className="flex items-center gap-2 rounded-lg border border-emerald-200 bg-emerald-50 p-2.5 text-xs text-emerald-700 dark:border-emerald-800/60 dark:bg-emerald-900/20 dark:text-emerald-300"> |
| 163 | + <ShieldCheck className="h-4 w-4 flex-shrink-0" /> |
| 164 | + Authentication is off — apps connect with just the URL and workspace header. |
| 165 | + </div> |
| 166 | + )} |
| 167 | + |
| 168 | + {/* Client checklist with per-row copy. */} |
| 169 | + <div className="overflow-hidden rounded-lg border border-[rgb(var(--border))]"> |
| 170 | + {clients.map((c, i) => { |
| 171 | + const checked = selected.has(c.id); |
| 172 | + return ( |
| 173 | + <label |
| 174 | + key={c.id} |
| 175 | + className={`flex cursor-pointer items-center gap-3 px-3 py-2.5 transition-colors hover:bg-[rgb(var(--surface-hover))] ${ |
| 176 | + i > 0 ? 'border-t border-[rgb(var(--border-subtle))]' : '' |
| 177 | + }`} |
| 178 | + data-testid={`workspace-install-client-${c.id}`} |
| 179 | + > |
| 180 | + <input |
| 181 | + type="checkbox" |
| 182 | + checked={checked} |
| 183 | + onChange={() => toggleClient(c.id)} |
| 184 | + className="h-4 w-4 flex-shrink-0 accent-primary-500" |
| 185 | + /> |
| 186 | + <div className="min-w-0 flex-1"> |
| 187 | + <div className="text-sm font-medium text-[rgb(var(--foreground))]">{c.label}</div> |
| 188 | + <div className="truncate font-mono text-[11px] text-[rgb(var(--muted))]"> |
| 189 | + {c.config_path} |
| 190 | + </div> |
| 191 | + </div> |
| 192 | + <button |
| 193 | + type="button" |
| 194 | + title="Copy this client's config" |
| 195 | + disabled={!mcpUrl} |
| 196 | + onClick={(e) => { |
| 197 | + e.preventDefault(); |
| 198 | + void handleCopy(c.id); |
| 199 | + }} |
| 200 | + className="flex-shrink-0 rounded-md p-1.5 text-[rgb(var(--muted))] transition-colors hover:bg-[rgb(var(--surface))] hover:text-[rgb(var(--foreground))] disabled:opacity-40" |
| 201 | + data-testid={`workspace-install-copy-${c.id}`} |
| 202 | + > |
| 203 | + {copiedId === c.id ? ( |
| 204 | + <Check className="h-3.5 w-3.5 text-green-600" /> |
| 205 | + ) : ( |
| 206 | + <Copy className="h-3.5 w-3.5" /> |
| 207 | + )} |
| 208 | + </button> |
| 209 | + </label> |
| 210 | + ); |
| 211 | + })} |
| 212 | + </div> |
| 213 | + |
| 214 | + {error && ( |
| 215 | + <div className="flex items-start gap-2 rounded-lg border border-red-200 bg-red-50 p-2.5 text-xs text-red-600 dark:border-red-800 dark:bg-red-900/20 dark:text-red-400"> |
| 216 | + <AlertCircle className="mt-0.5 h-3.5 w-3.5 flex-shrink-0" /> |
| 217 | + <span>{error}</span> |
| 218 | + </div> |
| 219 | + )} |
| 220 | + |
| 221 | + {results && ( |
| 222 | + <div className="space-y-1.5" data-testid="workspace-install-results"> |
| 223 | + {results.map((r) => ( |
| 224 | + <div |
| 225 | + key={r.client} |
| 226 | + className="flex items-center gap-2 rounded-md border border-[rgb(var(--border-subtle))] bg-[rgb(var(--surface))] px-2.5 py-1.5 text-xs" |
| 227 | + > |
| 228 | + {r.action === 'error' ? ( |
| 229 | + <AlertCircle className="h-3.5 w-3.5 flex-shrink-0 text-red-500" /> |
| 230 | + ) : ( |
| 231 | + <Check className="h-3.5 w-3.5 flex-shrink-0 text-green-600" /> |
| 232 | + )} |
| 233 | + <span className="font-medium">{r.label}</span> |
| 234 | + <span className="text-[rgb(var(--muted))]"> |
| 235 | + {r.action === 'error' ? r.error : `${r.action} ${r.path}`} |
| 236 | + </span> |
| 237 | + </div> |
| 238 | + ))} |
| 239 | + </div> |
| 240 | + )} |
| 241 | + |
| 242 | + <Button |
| 243 | + variant="primary" |
| 244 | + size="sm" |
| 245 | + className="w-full" |
| 246 | + disabled={installing || selected.size === 0 || !mcpUrl} |
| 247 | + onClick={handleInstall} |
| 248 | + data-testid="workspace-install-button" |
| 249 | + > |
| 250 | + {installing ? ( |
| 251 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 252 | + ) : ( |
| 253 | + <Download className="mr-2 h-4 w-4" /> |
| 254 | + )} |
| 255 | + {mcpUrl |
| 256 | + ? `Install into ${selected.size} app${selected.size === 1 ? '' : 's'}` |
| 257 | + : 'Start the gateway to install'} |
| 258 | + </Button> |
| 259 | + </div> |
| 260 | + ); |
| 261 | +} |
0 commit comments