@blackbelt-technology/pi-dashboard-mcp-client-plugin 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,217 @@
1
+ /**
2
+ * ProjectSettings — the "Project settings…" panel of a Pi-global server row on
3
+ * the folder page. Writes only a project override (`enabled`, `exposure`) to
4
+ * `<cwd>/.pi/mcp.json`: Inherit / On / Off as a native radiogroup (arrow keys),
5
+ * an exposure select whose first option is "Inherit (<global value>)", and the
6
+ * override's `toolExposure` read-only (shown and preserved, never edited).
7
+ * Below 640px the panel is a bottom sheet; every action keeps a 44px hit area.
8
+ *
9
+ * See change: mcp-client-project-overrides (D9).
10
+ */
11
+ import { useT } from "@blackbelt-technology/dashboard-plugin-runtime";
12
+ import type React from "react";
13
+ import { useEffect, useRef, useState } from "react";
14
+ import { displayExposure, MCP_EXPOSURES } from "../core/pi-rules.js";
15
+ import type { EffectiveServerView, Scope } from "../core/types.js";
16
+ import { ApiError, scopeToWire, setProjectOverride } from "./api.js";
17
+ import { invalidateEffective } from "./hooks.js";
18
+
19
+ type EnabledChoice = "inherit" | "on" | "off";
20
+
21
+ export interface ProjectSettingsPanelProps {
22
+ /** The Pi-global row (carries `projectOverride` when the folder already overrides it). */
23
+ server: EffectiveServerView;
24
+ scope: Extract<Scope, { kind: "project" }>;
25
+ narrow: boolean;
26
+ onClose: () => void;
27
+ onSaved: () => void;
28
+ }
29
+
30
+ function enabledChoiceOf(raw: Record<string, unknown>): EnabledChoice {
31
+ if (raw.enabled === undefined) return "inherit";
32
+ return raw.enabled === false ? "off" : "on";
33
+ }
34
+
35
+ export function ProjectSettingsPanel({ server, scope, narrow, onClose, onSaved }: ProjectSettingsPanelProps): React.ReactElement {
36
+ const t = useT();
37
+ const { name } = server;
38
+ const raw = server.projectOverride?.raw ?? {};
39
+ const inheritedEnabled = server.projectOverride?.inherited.enabled ?? server.enabled;
40
+ const inheritedExposure = server.projectOverride?.inherited.exposure ?? server.exposure;
41
+
42
+ const initialEnabled = enabledChoiceOf(raw);
43
+ const initialExposure = raw.exposure === undefined ? "inherit" : displayExposure(raw.exposure);
44
+ const [enabled, setEnabled] = useState<EnabledChoice>(initialEnabled);
45
+ const [exposure, setExposure] = useState<string>(initialExposure);
46
+ const [saving, setSaving] = useState(false);
47
+ const [error, setError] = useState<string | null>(null);
48
+ const checkedRef = useRef<HTMLInputElement>(null);
49
+ // Focus goes to the CHECKED radio, not the first option.
50
+ useEffect(() => checkedRef.current?.focus(), []);
51
+
52
+ const toolExposure =
53
+ raw.toolExposure !== null && typeof raw.toolExposure === "object" && !Array.isArray(raw.toolExposure)
54
+ ? Object.entries(raw.toolExposure as Record<string, unknown>)
55
+ : [];
56
+
57
+ async function save(): Promise<void> {
58
+ setSaving(true);
59
+ setError(null);
60
+ try {
61
+ await setProjectOverride(
62
+ name,
63
+ {
64
+ enabled: enabled === "inherit" ? null : enabled === "on",
65
+ // An untouched exposure keeps the file's own literal (a legacy alias stays an alias).
66
+ exposure:
67
+ exposure === initialExposure ? ((raw.exposure as string | undefined) ?? null) : exposure === "inherit" ? null : exposure,
68
+ },
69
+ scopeToWire(scope),
70
+ );
71
+ invalidateEffective(scope.cwd);
72
+ onSaved();
73
+ onClose();
74
+ } catch (e) {
75
+ setError(e instanceof ApiError ? e.message : String(e));
76
+ } finally {
77
+ setSaving(false);
78
+ }
79
+ }
80
+
81
+ const enabledLabel = t("mcpProjectEnabledLabel", undefined, "Enabled in this folder");
82
+ const choices: Array<{ value: EnabledChoice; label: string }> = [
83
+ { value: "inherit", label: `Inherit (${inheritedEnabled ? "on" : "off"})` },
84
+ { value: "on", label: "On" },
85
+ { value: "off", label: "Off" },
86
+ ];
87
+
88
+ return (
89
+ <div className={`fixed inset-0 z-50 flex ${narrow ? "items-end" : "items-center"} justify-center`}>
90
+ <button
91
+ type="button"
92
+ aria-label={t("mcpProjectClose", undefined, "Close")}
93
+ tabIndex={-1}
94
+ onClick={onClose}
95
+ className="absolute inset-0 bg-[var(--bg-overlay,rgba(0,0,0,0.5))]"
96
+ />
97
+ <div
98
+ role="dialog"
99
+ aria-modal="true"
100
+ aria-labelledby={`mcp-project-panel-title-${name}`}
101
+ data-testid="mcp-folder-project-panel"
102
+ data-sheet={narrow ? "bottom" : "dialog"}
103
+ onKeyDown={(e) => {
104
+ if (e.key === "Escape") onClose();
105
+ }}
106
+ className={`relative w-full max-w-md bg-[var(--bg-secondary)] border border-[var(--border-secondary)] p-4 space-y-3 ${
107
+ narrow ? "rounded-t-xl" : "rounded-lg"
108
+ }`}
109
+ >
110
+ <div>
111
+ <h2 id={`mcp-project-panel-title-${name}`} className="text-sm font-semibold m-0 text-[var(--text-primary)]">
112
+ {t("mcpProjectSettingsTitle", { name }, `Project settings: ${name}`)}
113
+ </h2>
114
+ <p className="text-[11px] text-[var(--text-secondary)] m-0">
115
+ {t(
116
+ "mcpProjectSettingsHint",
117
+ undefined,
118
+ "Changes only this folder. Writes .pi/mcp.json; the Pi global entry and its credentials are kept.",
119
+ )}
120
+ </p>
121
+ </div>
122
+
123
+ <fieldset className="border-0 p-0 m-0">
124
+ <legend className="text-[12px] font-semibold text-[var(--text-primary)] mb-1">{enabledLabel}</legend>
125
+ <div role="radiogroup" aria-label={enabledLabel} className="flex gap-1">
126
+ {choices.map((c) => (
127
+ <label
128
+ key={c.value}
129
+ className={`inline-flex items-center gap-1.5 px-2 min-h-11 sm:min-h-0 sm:py-1 rounded border text-[12px] ${
130
+ enabled === c.value
131
+ ? "border-[var(--accent-primary,#60a5fa)] bg-[var(--accent-soft)] text-[var(--text-primary)]"
132
+ : "border-[var(--border-secondary)] text-[var(--text-secondary)]"
133
+ }`}
134
+ >
135
+ <input
136
+ ref={enabled === c.value ? checkedRef : undefined}
137
+ type="radio"
138
+ name={`mcp-project-enabled-${name}`}
139
+ value={c.value}
140
+ checked={enabled === c.value}
141
+ onChange={() => setEnabled(c.value)}
142
+ data-testid={`mcp-project-enabled-${c.value}`}
143
+ />
144
+ <span>{c.label}</span>
145
+ </label>
146
+ ))}
147
+ </div>
148
+ <p className="text-[11px] text-[var(--text-tertiary)] m-0 mt-1">
149
+ Inherit follows Pi global, including later changes there.
150
+ </p>
151
+ </fieldset>
152
+
153
+ <div>
154
+ <label htmlFor={`mcp-project-exposure-${name}`} className="block text-[12px] font-semibold text-[var(--text-primary)] mb-1">
155
+ Exposure in this folder
156
+ </label>
157
+ <select
158
+ id={`mcp-project-exposure-${name}`}
159
+ data-testid="mcp-project-exposure"
160
+ value={exposure}
161
+ onChange={(e) => setExposure(e.target.value)}
162
+ className="w-full min-h-11 sm:min-h-0 px-2 py-1 text-[12px] rounded border border-[var(--border-secondary)] bg-[var(--bg-tertiary,transparent)] text-[var(--text-primary)]"
163
+ >
164
+ <option value="inherit">{`Inherit (${inheritedExposure})`}</option>
165
+ {MCP_EXPOSURES.map((e) => (
166
+ <option key={e} value={e}>
167
+ {e}
168
+ </option>
169
+ ))}
170
+ </select>
171
+ </div>
172
+
173
+ {toolExposure.length > 0 && (
174
+ <div data-testid="mcp-project-tool-exposure">
175
+ <div className="text-[12px] font-semibold text-[var(--text-primary)]">Per-tool exposure (read-only)</div>
176
+ <p className="text-[11px] text-[var(--text-secondary)] m-0">
177
+ {toolExposure.map(([tool, v]) => (
178
+ <span key={tool} className="mr-2">
179
+ <code>{tool}</code> → {String(v)}
180
+ </span>
181
+ ))}
182
+ </p>
183
+ <p className="text-[11px] text-[var(--text-tertiary)] m-0">
184
+ Kept as is. Edit in <code>.pi/mcp.json</code> or pi's <code>/mcp</code>.
185
+ </p>
186
+ </div>
187
+ )}
188
+
189
+ {error && (
190
+ <p role="alert" data-testid="mcp-project-error" className="text-[11px] text-[var(--status-error,#f87171)] m-0">
191
+ {error}
192
+ </p>
193
+ )}
194
+
195
+ <div className="flex justify-end gap-2">
196
+ <button
197
+ type="button"
198
+ onClick={onClose}
199
+ data-testid="mcp-project-cancel"
200
+ className="px-3 py-1 min-h-11 min-w-11 sm:min-h-0 sm:min-w-0 rounded border border-[var(--border-secondary)] text-[12px] text-[var(--text-secondary)]"
201
+ >
202
+ Cancel
203
+ </button>
204
+ <button
205
+ type="button"
206
+ disabled={saving}
207
+ onClick={() => void save()}
208
+ data-testid="mcp-project-save"
209
+ className="px-3 py-1 min-h-11 min-w-11 sm:min-h-0 sm:min-w-0 rounded text-[12px] bg-[var(--accent-solid)] text-white disabled:opacity-50"
210
+ >
211
+ Save
212
+ </button>
213
+ </div>
214
+ </div>
215
+ </div>
216
+ );
217
+ }