|
| 1 | +import React, { useState } from "react" |
| 2 | +import { VSCodeLink } from "@vscode/webview-ui-toolkit/react" |
| 3 | + |
| 4 | +import { vscode } from "@src/utils/vscode" |
| 5 | +import { useExtensionState } from "@src/context/ExtensionStateContext" |
| 6 | +import { useAppTranslation } from "@src/i18n/TranslationContext" |
| 7 | +import { Button } from "@src/components/ui" |
| 8 | + |
| 9 | +import { mcpMarketplaceCatalog, type McpMarketplaceItem } from "../../../../src/shared/mcpMarketplaceCatalog" |
| 10 | + |
| 11 | +const McpMarketplace = () => { |
| 12 | + const { mcpServers: servers } = useExtensionState() |
| 13 | + const { t } = useAppTranslation() |
| 14 | + |
| 15 | + const installedServerNames = new Set(servers.map((s) => s.name)) |
| 16 | + |
| 17 | + return ( |
| 18 | + <div style={{ marginTop: "15px" }}> |
| 19 | + <div |
| 20 | + style={{ |
| 21 | + fontWeight: 500, |
| 22 | + fontSize: "13px", |
| 23 | + color: "var(--vscode-foreground)", |
| 24 | + marginBottom: "6px", |
| 25 | + }}> |
| 26 | + {t("mcp:marketplace.title")} |
| 27 | + </div> |
| 28 | + <div |
| 29 | + style={{ |
| 30 | + fontSize: "12px", |
| 31 | + color: "var(--vscode-descriptionForeground)", |
| 32 | + marginBottom: "10px", |
| 33 | + }}> |
| 34 | + {t("mcp:marketplace.description")} |
| 35 | + </div> |
| 36 | + <div style={{ display: "flex", flexDirection: "column", gap: "8px" }}> |
| 37 | + {mcpMarketplaceCatalog.map((item) => ( |
| 38 | + <MarketplaceRow key={item.name} item={item} isInstalled={installedServerNames.has(item.name)} /> |
| 39 | + ))} |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +const MarketplaceRow = ({ item, isInstalled }: { item: McpMarketplaceItem; isInstalled: boolean }) => { |
| 46 | + const { t } = useAppTranslation() |
| 47 | + const [envValues, setEnvValues] = useState<Record<string, string>>(() => { |
| 48 | + const initial: Record<string, string> = {} |
| 49 | + if (item.setupEnvKeys) { |
| 50 | + for (const key of item.setupEnvKeys) { |
| 51 | + initial[key] = item.config.env?.[key] ?? "" |
| 52 | + } |
| 53 | + } |
| 54 | + return initial |
| 55 | + }) |
| 56 | + |
| 57 | + const handleInstall = () => { |
| 58 | + const config = { ...item.config } |
| 59 | + if (item.requiresSetup && item.setupEnvKeys) { |
| 60 | + config.env = { ...config.env } |
| 61 | + for (const key of item.setupEnvKeys) { |
| 62 | + if (envValues[key]) { |
| 63 | + config.env[key] = envValues[key] |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + vscode.postMessage({ |
| 68 | + type: "installMcpServer", |
| 69 | + serverName: item.name, |
| 70 | + config, |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <div |
| 76 | + className="rounded bg-vscode-textCodeBlock-background p-2" |
| 77 | + style={{ |
| 78 | + opacity: isInstalled ? 0.6 : 1, |
| 79 | + }}> |
| 80 | + <div className="flex items-center justify-between gap-2"> |
| 81 | + <div className="flex-1 min-w-0"> |
| 82 | + <div className="flex items-center gap-1.5"> |
| 83 | + <span className="codicon codicon-search text-xs" /> |
| 84 | + <span className="font-medium text-[13px] text-vscode-foreground">{item.displayName}</span> |
| 85 | + {item.requiresSetup && ( |
| 86 | + <span |
| 87 | + className="text-[10px] px-1 py-0.5 rounded" |
| 88 | + style={{ |
| 89 | + background: "var(--vscode-editorWarning-foreground)", |
| 90 | + color: "var(--vscode-editor-background)", |
| 91 | + }} |
| 92 | + title={t("mcp:marketplace.requiresSetupHint", { |
| 93 | + keys: item.setupEnvKeys?.join(", ") ?? "", |
| 94 | + })}> |
| 95 | + {t("mcp:marketplace.requiresSetup")} |
| 96 | + </span> |
| 97 | + )} |
| 98 | + </div> |
| 99 | + <div className="text-xs text-vscode-descriptionForeground mt-0.5">{item.description}</div> |
| 100 | + </div> |
| 101 | + <div className="flex items-center gap-2 shrink-0"> |
| 102 | + {item.url && ( |
| 103 | + <VSCodeLink href={item.url} style={{ fontSize: "11px" }}> |
| 104 | + {t("mcp:marketplace.learnMore")} |
| 105 | + </VSCodeLink> |
| 106 | + )} |
| 107 | + <Button |
| 108 | + variant="secondary" |
| 109 | + disabled={isInstalled} |
| 110 | + onClick={handleInstall} |
| 111 | + style={{ minWidth: "60px", fontSize: "12px" }}> |
| 112 | + {isInstalled ? ( |
| 113 | + <> |
| 114 | + <span className="codicon codicon-check mr-1" /> |
| 115 | + Added |
| 116 | + </> |
| 117 | + ) : ( |
| 118 | + <> |
| 119 | + <span className="codicon codicon-add mr-1" /> |
| 120 | + {t("mcp:marketplace.install")} |
| 121 | + </> |
| 122 | + )} |
| 123 | + </Button> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + {item.requiresSetup && item.setupEnvKeys && !isInstalled && ( |
| 127 | + <div className="mt-2 flex flex-col gap-1.5"> |
| 128 | + {item.setupEnvKeys.map((key) => ( |
| 129 | + <div key={key} className="flex items-center gap-2"> |
| 130 | + <label |
| 131 | + className="text-[11px] text-vscode-descriptionForeground shrink-0" |
| 132 | + style={{ minWidth: "90px" }}> |
| 133 | + {key}: |
| 134 | + </label> |
| 135 | + <input |
| 136 | + type="text" |
| 137 | + value={envValues[key] ?? ""} |
| 138 | + onChange={(e) => |
| 139 | + setEnvValues((prev) => ({ |
| 140 | + ...prev, |
| 141 | + [key]: e.target.value, |
| 142 | + })) |
| 143 | + } |
| 144 | + placeholder={item.config.env?.[key] ?? ""} |
| 145 | + className="flex-1 rounded px-1.5 py-0.5 text-xs" |
| 146 | + style={{ |
| 147 | + background: "var(--vscode-input-background)", |
| 148 | + color: "var(--vscode-input-foreground)", |
| 149 | + border: "1px solid var(--vscode-input-border, transparent)", |
| 150 | + }} |
| 151 | + /> |
| 152 | + </div> |
| 153 | + ))} |
| 154 | + </div> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + ) |
| 158 | +} |
| 159 | + |
| 160 | +export default McpMarketplace |
0 commit comments