sodabase 0.0.0-stage → 0.1.2
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.
- package/README.md +51 -2
- package/dist/api/client.js +291 -0
- package/dist/api/fakeClient.js +154 -0
- package/dist/api/gotrueAuth.js +180 -0
- package/dist/api/types.js +15 -0
- package/dist/app.js +98 -0
- package/dist/cli.js +83 -0
- package/dist/config/tokenStore.js +33 -0
- package/dist/ui.js +768 -0
- package/package.json +54 -4
package/dist/ui.js
ADDED
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Text, useApp, useInput } from "ink";
|
|
3
|
+
import { createContext, useContext, useEffect, useRef, useState } from "react";
|
|
4
|
+
import { totalIssues } from "./api/types.js";
|
|
5
|
+
import { saveCreds } from "./config/tokenStore.js";
|
|
6
|
+
// ── utils ─────────────────────────────────────────────────────────────────────
|
|
7
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
8
|
+
function useSpinner() {
|
|
9
|
+
const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
10
|
+
const [i, setI] = useState(0);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const t = setInterval(() => setI((x) => (x + 1) % frames.length), 90);
|
|
13
|
+
return () => clearInterval(t);
|
|
14
|
+
}, []);
|
|
15
|
+
return frames[i] ?? "⠋";
|
|
16
|
+
}
|
|
17
|
+
const Ctx = createContext(null);
|
|
18
|
+
export function WizardProvider({ value, children }) {
|
|
19
|
+
return _jsx(Ctx.Provider, { value: value, children: children });
|
|
20
|
+
}
|
|
21
|
+
function useWizard() {
|
|
22
|
+
const w = useContext(Ctx);
|
|
23
|
+
if (!w)
|
|
24
|
+
throw new Error("WizardProvider missing");
|
|
25
|
+
return w;
|
|
26
|
+
}
|
|
27
|
+
// ── presentational bits ─────────────────────────────────────────────────────────
|
|
28
|
+
export function BrandBar() {
|
|
29
|
+
return (_jsx(Box, { children: _jsxs(Text, { backgroundColor: "green", color: "black", children: [" Sodabase Wizard · ", _jsx(Text, { bold: true, children: "setup" }), " "] }) }));
|
|
30
|
+
}
|
|
31
|
+
function Eyebrow({ children }) {
|
|
32
|
+
return (_jsxs(Text, { color: "green", bold: true, children: ["\u25E7 ", children] }));
|
|
33
|
+
}
|
|
34
|
+
// ── Phase: detect + GoTrue sign-in (one screen) ──────────────────────────────────
|
|
35
|
+
/** The detection facts block, with labels aligned in a left column (Directory / Framework / …). */
|
|
36
|
+
function ProjectFacts({ project }) {
|
|
37
|
+
const pad = (s) => s.padEnd(10);
|
|
38
|
+
// Empty framework/projectRef = detection missed; name the real reason so we never claim a present
|
|
39
|
+
// file is absent (the file may exist but lack the Supabase dep / project_id).
|
|
40
|
+
const frameworkMiss = project.hasPackageJson ? "no Supabase dependency in package.json" : "package.json not found";
|
|
41
|
+
const refMiss = project.hasConfigToml ? "no project_id in supabase/config.toml" : "supabase/config.toml not found";
|
|
42
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: pad("Directory") }), _jsx(Text, { color: "green", children: "\u2714 " }), project.directory] }), _jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: pad("Framework") }), project.framework ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: "green", children: "\u2714 " }), project.framework] })) : (_jsxs(Text, { dimColor: true, children: ["\u2013 ", frameworkMiss] }))] }), _jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: pad("Supabase") }), project.projectRef ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: "green", children: "\u2714 " }), "project ref ", _jsx(Text, { dimColor: true, children: project.projectRef })] })) : (_jsxs(Text, { dimColor: true, children: ["\u2013 ", refMiss] }))] })] }));
|
|
43
|
+
}
|
|
44
|
+
const PROVIDERS = [
|
|
45
|
+
{ id: "github", label: "GitHub" },
|
|
46
|
+
{ id: "google", label: "Google" },
|
|
47
|
+
{ id: "email", label: "Email (one-time code)" },
|
|
48
|
+
];
|
|
49
|
+
/** Turn a raw GoTrue/transport error into a human message + an actionable hint. */
|
|
50
|
+
function friendlySignInError(raw) {
|
|
51
|
+
if (/multiple accounts with the same email/i.test(raw))
|
|
52
|
+
return {
|
|
53
|
+
text: "This email has more than one Sodabase account, so that provider can't pick one.",
|
|
54
|
+
hint: "Try a different provider (press r), or ask support to merge the duplicate accounts.",
|
|
55
|
+
};
|
|
56
|
+
if (/timed out|redirect/i.test(raw))
|
|
57
|
+
return { text: raw, hint: "Check that http://127.0.0.1:8976/callback is in the Supabase Auth redirect allowlist." };
|
|
58
|
+
if (/anon key/i.test(raw))
|
|
59
|
+
return { text: raw, hint: "Set SODABASE_SUPABASE_ANON_KEY to the project's publishable key." };
|
|
60
|
+
if (/rate limit|too many|for security purposes/i.test(raw))
|
|
61
|
+
return { text: "Too many sign-in attempts for now.", hint: "Wait a minute, then press r to request a fresh code." };
|
|
62
|
+
if (/expired|invalid|incorrect|otp|token/i.test(raw))
|
|
63
|
+
return { text: "That code is invalid or has expired.", hint: "Press r to go back and request a new code." };
|
|
64
|
+
return { text: raw };
|
|
65
|
+
}
|
|
66
|
+
export function SignIn({ onDone }) {
|
|
67
|
+
const { services, signedIn, demo, interactive } = useWizard();
|
|
68
|
+
const { exit } = useApp();
|
|
69
|
+
const spin = useSpinner();
|
|
70
|
+
const [stage, setStage] = useState(interactive ? "pick" : "signing");
|
|
71
|
+
const [cursor, setCursor] = useState(0);
|
|
72
|
+
const [chosen, setChosen] = useState(undefined);
|
|
73
|
+
const [email, setEmail] = useState("");
|
|
74
|
+
const [code, setCode] = useState("");
|
|
75
|
+
const [err, setErr] = useState(null);
|
|
76
|
+
const [project, setProject] = useState(null);
|
|
77
|
+
const mounted = useRef(true);
|
|
78
|
+
useEffect(() => () => void (mounted.current = false), []);
|
|
79
|
+
// Finish sign-in the same way for every path: set the bearer, persist (never in --demo), advance.
|
|
80
|
+
function complete(session) {
|
|
81
|
+
if (!mounted.current)
|
|
82
|
+
return;
|
|
83
|
+
services.setBearer(session.accessToken);
|
|
84
|
+
if (!demo) {
|
|
85
|
+
// Never persist the fake --demo session to the real creds file.
|
|
86
|
+
try {
|
|
87
|
+
saveCreds({
|
|
88
|
+
accessToken: session.accessToken,
|
|
89
|
+
...(session.refreshToken ? { refreshToken: session.refreshToken } : {}),
|
|
90
|
+
...(session.expiresAt ? { expiresAt: session.expiresAt } : {}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
/* non-fatal */
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
onDone({});
|
|
98
|
+
}
|
|
99
|
+
function fail(e) {
|
|
100
|
+
if (!mounted.current)
|
|
101
|
+
return;
|
|
102
|
+
setErr(friendlySignInError(e instanceof Error ? e.message : String(e)));
|
|
103
|
+
setStage("error");
|
|
104
|
+
}
|
|
105
|
+
async function go(provider) {
|
|
106
|
+
setChosen(provider);
|
|
107
|
+
setErr(null);
|
|
108
|
+
setStage("signing");
|
|
109
|
+
try {
|
|
110
|
+
complete(await services.signIn(provider));
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
fail(e);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async function startEmail() {
|
|
117
|
+
setErr(null);
|
|
118
|
+
setStage("sending");
|
|
119
|
+
try {
|
|
120
|
+
await services.emailOtpStart(email.trim());
|
|
121
|
+
if (!mounted.current)
|
|
122
|
+
return;
|
|
123
|
+
setCode("");
|
|
124
|
+
setStage("code");
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
fail(e);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async function verifyEmail() {
|
|
131
|
+
setErr(null);
|
|
132
|
+
setStage("verifying");
|
|
133
|
+
try {
|
|
134
|
+
complete(await services.emailOtpVerify(email.trim(), code.trim()));
|
|
135
|
+
}
|
|
136
|
+
catch (e) {
|
|
137
|
+
fail(e);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
useEffect(() => {
|
|
141
|
+
if (signedIn) {
|
|
142
|
+
onDone({}); // a valid session is already loaded — skip detect + sign-in
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// Detect the project in the background; the sign-in picker is usable while the facts fill in.
|
|
146
|
+
let on = true;
|
|
147
|
+
void (async () => {
|
|
148
|
+
try {
|
|
149
|
+
const p = await services.detectProject();
|
|
150
|
+
if (on)
|
|
151
|
+
setProject(p);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
/* detection is best-effort — the picker still works without the facts */
|
|
155
|
+
}
|
|
156
|
+
})();
|
|
157
|
+
if (!interactive)
|
|
158
|
+
void go(); // non-interactive: env/default provider, no picker
|
|
159
|
+
return () => void (on = false);
|
|
160
|
+
}, []);
|
|
161
|
+
useInput((input, key) => {
|
|
162
|
+
// Escape steps back through the text-entry stages (so it never destroys a half-typed sign-in);
|
|
163
|
+
// from the picker or a transient stage it quits. SignIn owns escape entirely — App's global
|
|
164
|
+
// handler defers to the sign-in screen.
|
|
165
|
+
if (key.escape) {
|
|
166
|
+
if (stage === "email") {
|
|
167
|
+
setErr(null);
|
|
168
|
+
setStage("pick");
|
|
169
|
+
}
|
|
170
|
+
else if (stage === "code") {
|
|
171
|
+
setErr(null);
|
|
172
|
+
setCode("");
|
|
173
|
+
setStage("email");
|
|
174
|
+
}
|
|
175
|
+
else if (stage === "error") {
|
|
176
|
+
setErr(null);
|
|
177
|
+
setCode("");
|
|
178
|
+
setStage("pick");
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
exit();
|
|
182
|
+
process.exit(130); // user abort
|
|
183
|
+
}
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (stage === "pick") {
|
|
187
|
+
if (key.upArrow)
|
|
188
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
189
|
+
else if (key.downArrow)
|
|
190
|
+
setCursor((c) => Math.min(PROVIDERS.length - 1, c + 1));
|
|
191
|
+
else if (key.return) {
|
|
192
|
+
const id = PROVIDERS[cursor]?.id;
|
|
193
|
+
if (id === "email") {
|
|
194
|
+
setErr(null);
|
|
195
|
+
setEmail("");
|
|
196
|
+
setStage("email");
|
|
197
|
+
}
|
|
198
|
+
else
|
|
199
|
+
void go(id);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
else if (stage === "email") {
|
|
203
|
+
// Hand-rolled controlled text input (no ink-text-input dep): printable chars + backspace + enter.
|
|
204
|
+
if (key.return) {
|
|
205
|
+
if (email.trim())
|
|
206
|
+
void startEmail();
|
|
207
|
+
}
|
|
208
|
+
else if (key.backspace || key.delete)
|
|
209
|
+
setEmail((e) => e.slice(0, -1));
|
|
210
|
+
else if (input && !key.ctrl && !key.meta && !key.tab)
|
|
211
|
+
setEmail((e) => e + input);
|
|
212
|
+
}
|
|
213
|
+
else if (stage === "code") {
|
|
214
|
+
// Same hand-rolled input, digits-only. Supabase's email OTP length is configurable (6–10), so cap at
|
|
215
|
+
// the max (10), not 6 — a project set to 8-digit codes must still be fully enterable.
|
|
216
|
+
if (key.return) {
|
|
217
|
+
if (code.trim())
|
|
218
|
+
void verifyEmail();
|
|
219
|
+
}
|
|
220
|
+
else if (key.backspace || key.delete)
|
|
221
|
+
setCode((c) => c.slice(0, -1));
|
|
222
|
+
else if (input && /^[0-9]+$/.test(input))
|
|
223
|
+
setCode((c) => (c + input).slice(0, 10));
|
|
224
|
+
}
|
|
225
|
+
else if (stage === "error") {
|
|
226
|
+
if (input === "r" || key.return) {
|
|
227
|
+
setErr(null);
|
|
228
|
+
setCode("");
|
|
229
|
+
setStage("pick");
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
// Active in every stage so escape is handled even during the transient sending/verifying/signing
|
|
234
|
+
// states (where it quits); the per-stage branches above guard the non-escape keys.
|
|
235
|
+
{ isActive: interactive });
|
|
236
|
+
const providerLabel = PROVIDERS.find((p) => p.id === chosen)?.label ?? chosen ?? "your provider";
|
|
237
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: signedIn ? "Signed in" : "Project detected" }), signedIn ? (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: "\u2714" }), " Already signed in ", _jsx(Text, { dimColor: true, children: "(using your saved session)" })] }) })) : (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, flexDirection: "column", children: _jsx(Text, { children: "We'll set up data-quality monitoring for your Supabase project \u2014 watching freshness, volume and quality drift, without touching a line of your code." }) }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: !project ? (_jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Detecting Supabase project\u2026"] })) : (_jsx(ProjectFacts, { project: project })) }), stage === "pick" ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "How do you want to sign in?" }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: PROVIDERS.map((p, i) => {
|
|
238
|
+
const here = i === cursor;
|
|
239
|
+
return (_jsxs(Text, { color: here ? "green" : undefined, children: [here ? "❯ " : " ", p.label] }, p.id));
|
|
240
|
+
}) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 enter to continue \u00B7 GitHub/Google open your browser \u00B7 esc to quit" }) })] })) : stage === "email" ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "Enter your email \u2014 we'll send you a sign-in code." }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: ["Email: ", _jsx(Text, { color: "green", children: email }), _jsx(Text, { inverse: true, children: " " })] }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "type to edit \u00B7 backspace to delete \u00B7 enter to send \u00B7 esc to go back" }) })] })) : stage === "sending" ? (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Sending a sign-in code to ", email, "\u2026"] }) })) : stage === "code" ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { dimColor: true, children: ["We emailed a sign-in code to ", email, ". Enter it below."] }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: ["Code: ", _jsx(Text, { color: "green", children: code }), _jsx(Text, { inverse: true, children: " " })] }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "enter to verify \u00B7 backspace to delete \u00B7 esc to go back" }) })] })) : stage === "verifying" ? (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Verifying your code\u2026"] }) })) : stage === "error" && err ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { color: "red", children: err.text }), err.hint && _jsx(Text, { dimColor: true, children: err.hint }), interactive && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "press r to start over \u00B7 esc to go back" }) }))] })) : (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Opening your browser to sign in with ", providerLabel, "\u2026"] }) }))] }))] }));
|
|
241
|
+
}
|
|
242
|
+
// ── Phase: connect (multi-step: authorize → list → pick project) ─────────────────
|
|
243
|
+
/** Short status tags shown next to a project in the picker: "watching" (already set up in this workspace) and
|
|
244
|
+
* a paused/transitional Supabase status (its DB is offline, so it can't be watched until resumed). */
|
|
245
|
+
function projectTags(p) {
|
|
246
|
+
const tags = [];
|
|
247
|
+
if (p.watching)
|
|
248
|
+
tags.push({ label: "watching", color: "green" });
|
|
249
|
+
if (p.status && p.status !== "ACTIVE_HEALTHY") {
|
|
250
|
+
tags.push({ label: p.status === "INACTIVE" ? "paused" : p.status.toLowerCase().replace(/_/g, " "), color: "yellow" });
|
|
251
|
+
}
|
|
252
|
+
return tags;
|
|
253
|
+
}
|
|
254
|
+
/** Can we set up monitoring on this project now? Only a known non-healthy status (paused/transitional — its DB
|
|
255
|
+
* is offline) blocks; an empty/unknown status is left to the backend. Keeps us off the "watch an offline DB →
|
|
256
|
+
* deferred failure" path, including the single-project auto-select that skips the picker. */
|
|
257
|
+
function isWatchable(p) {
|
|
258
|
+
return !p.status || p.status === "ACTIVE_HEALTHY";
|
|
259
|
+
}
|
|
260
|
+
const issuesLabel = (n) => `${n} ${n === 1 ? "issue" : "issues"}`;
|
|
261
|
+
export function Connect({ onDone }) {
|
|
262
|
+
const { services, interactive } = useWizard();
|
|
263
|
+
const spin = useSpinner();
|
|
264
|
+
const [connectionId, setConnectionId] = useState(null);
|
|
265
|
+
const [alreadyConnected, setAlreadyConnected] = useState(false);
|
|
266
|
+
const [projects, setProjects] = useState(null);
|
|
267
|
+
const [cursor, setCursor] = useState(0);
|
|
268
|
+
const [chosen, setChosen] = useState(null);
|
|
269
|
+
const [error, setError] = useState(null);
|
|
270
|
+
useEffect(() => {
|
|
271
|
+
let on = true;
|
|
272
|
+
// Abort the authorize poll when we leave this screen (escape back to the hub) so an abandoned
|
|
273
|
+
// connect doesn't keep polling for up to 5 minutes — and a re-entry can't start a second,
|
|
274
|
+
// competing poll loop against the same handback.
|
|
275
|
+
const controller = new AbortController();
|
|
276
|
+
(async () => {
|
|
277
|
+
try {
|
|
278
|
+
// connect() opens the browser, polls the hosted handback, and self-completes with our own
|
|
279
|
+
// Bearer — no browser web session needed (fixes the 401/403).
|
|
280
|
+
const { connectionId: cid, alreadyConnected: ac } = await services.connect(controller.signal);
|
|
281
|
+
if (!on)
|
|
282
|
+
return;
|
|
283
|
+
setConnectionId(cid);
|
|
284
|
+
setAlreadyConnected(!!ac);
|
|
285
|
+
const projs = await services.listProjects(cid);
|
|
286
|
+
if (!on)
|
|
287
|
+
return;
|
|
288
|
+
setProjects(projs);
|
|
289
|
+
// No projects in the connected org → don't advance with an empty projectRef (which onboards nothing and
|
|
290
|
+
// fails deep). Say so plainly.
|
|
291
|
+
if (projs.length === 0) {
|
|
292
|
+
setError("This Supabase organization has no projects. Create one in Supabase, then run setup again.");
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (projs.length === 1 || !interactive)
|
|
296
|
+
finish(cid, projs[0]);
|
|
297
|
+
}
|
|
298
|
+
catch (e) {
|
|
299
|
+
if (on && !controller.signal.aborted)
|
|
300
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
301
|
+
}
|
|
302
|
+
})();
|
|
303
|
+
return () => {
|
|
304
|
+
on = false;
|
|
305
|
+
controller.abort();
|
|
306
|
+
};
|
|
307
|
+
}, []);
|
|
308
|
+
function finish(connId, project) {
|
|
309
|
+
// Don't send an offline (paused/transitional) project into onboarding — the DB is unreachable, so it would
|
|
310
|
+
// fail deep in the job. Surface it here, up front and recoverable, instead. Also covers the single-project
|
|
311
|
+
// auto-select path, which skips the picker (and its tag) entirely.
|
|
312
|
+
if (project && !isWatchable(project)) {
|
|
313
|
+
setError(`"${project.name}" is paused in Supabase — resume it there, then run setup again.`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const ref = project?.ref ?? "";
|
|
317
|
+
setChosen(ref);
|
|
318
|
+
// alreadyWatching routes the hub: an already-watched project skips provisioning and goes straight to
|
|
319
|
+
// adjusting its tables; a new one is provisioned first. Set it explicitly (not just when true) so a
|
|
320
|
+
// later pick of a NEW project resets the flag left over from a previous "manage" round.
|
|
321
|
+
onDone({ connectionId: connId, projectRef: ref, alreadyWatching: !!project?.watching });
|
|
322
|
+
}
|
|
323
|
+
useInput((_i, key) => {
|
|
324
|
+
if (!projects || chosen)
|
|
325
|
+
return;
|
|
326
|
+
if (key.upArrow)
|
|
327
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
328
|
+
else if (key.downArrow)
|
|
329
|
+
setCursor((c) => Math.min(projects.length - 1, c + 1));
|
|
330
|
+
else if (key.return && connectionId)
|
|
331
|
+
finish(connectionId, projects[cursor]);
|
|
332
|
+
}, { isActive: interactive && !!projects && !chosen });
|
|
333
|
+
const picking = interactive && projects && projects.length > 1 && !chosen;
|
|
334
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "Authorize your database" }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: error ? (_jsx(Text, { color: "red", children: error })) : !connectionId ? (_jsxs(_Fragment, { children: [_jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Opening your browser to authorize\u2026"] }), _jsx(Text, { dimColor: true, children: "Read-only \u2014 no connection string to paste. Approve on Supabase, then return here." })] })) : !projects ? (_jsxs(Text, { children: [_jsx(Text, { color: "green", children: spin }), " Loading your projects\u2026"] })) : picking ? (_jsxs(_Fragment, { children: [_jsx(Text, { children: "Pick the project to watch:" }), projects.map((p, i) => (_jsxs(Text, { color: i === cursor ? "green" : undefined, children: [i === cursor ? "❯ " : " ", p.name, " ", _jsxs(Text, { dimColor: true, children: ["(", p.ref, ")"] }), projectTags(p).map((t) => (_jsxs(Text, { color: t.color, children: [" ", "\u00B7 ", t.label] }, t.label)))] }, p.ref))), _jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 enter select \u00B7 green = watching \u00B7 yellow = offline \u00B7 esc to go back" })] })) : (_jsxs(_Fragment, { children: [alreadyConnected && (_jsxs(Text, { dimColor: true, children: [_jsx(Text, { color: "green", children: "\u2713" }), " Already connected \u2014 continuing on your existing connection."] })), _jsxs(Text, { children: [_jsx(Text, { color: "green", children: "\u2714" }), " Connected \u00B7 project ", _jsx(Text, { bold: true, children: chosen ?? projects[0]?.ref ?? "—" }), _jsx(Text, { color: "green", children: " 🔒 read-only, audited" })] })] })) })] }));
|
|
335
|
+
}
|
|
336
|
+
// ── Phase: the server-side onboarding job (two-panel) ─────────────────────────────
|
|
337
|
+
// Human labels for the backend onboarding job's step names (machine keys). Unknown names fall back to the raw
|
|
338
|
+
// key, so a new backend step still shows (just un-prettified) rather than disappearing.
|
|
339
|
+
const STEP_LABELS = {
|
|
340
|
+
provision: "Provision read-only role",
|
|
341
|
+
map_schema: "Map schema & monitors",
|
|
342
|
+
grant: "Grant read access",
|
|
343
|
+
activate: "Activate monitors",
|
|
344
|
+
scan: "Day-1 quality scan queued",
|
|
345
|
+
onboard: "Set up monitoring", // legacy single-step (jobs enqueued before step enrichment)
|
|
346
|
+
timeout: "Timed out",
|
|
347
|
+
};
|
|
348
|
+
const stepLabel = (name) => STEP_LABELS[name] ?? name;
|
|
349
|
+
export function AgentView({ ctx, onDone }) {
|
|
350
|
+
const { services, fast } = useWizard();
|
|
351
|
+
const [steps, setSteps] = useState([]);
|
|
352
|
+
const [logs, setLogs] = useState(["Planning the trust setup."]);
|
|
353
|
+
const [error, setError] = useState(null);
|
|
354
|
+
useEffect(() => {
|
|
355
|
+
let on = true;
|
|
356
|
+
(async () => {
|
|
357
|
+
try {
|
|
358
|
+
const { id } = await services.enqueueOnboarding(ctx.connectionId ?? "", ctx.projectRef ?? "");
|
|
359
|
+
while (on) {
|
|
360
|
+
const st = await services.jobStatus(id);
|
|
361
|
+
if (!on)
|
|
362
|
+
return;
|
|
363
|
+
setSteps(st.steps);
|
|
364
|
+
const last = st.steps.filter((s) => s.status === "done" || s.status === "skipped").at(-1);
|
|
365
|
+
if (last?.detail)
|
|
366
|
+
setLogs((l) => [...l, `${stepLabel(last.name)} — ${last.detail}`].slice(-4));
|
|
367
|
+
if (st.status === "done") {
|
|
368
|
+
await sleep(fast ? 80 : 500);
|
|
369
|
+
if (on)
|
|
370
|
+
onDone({});
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (st.status === "failed")
|
|
374
|
+
return setError(st.error ?? "onboarding failed");
|
|
375
|
+
await sleep(fast ? 90 : 650);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
catch (e) {
|
|
379
|
+
if (on)
|
|
380
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
381
|
+
}
|
|
382
|
+
})();
|
|
383
|
+
return () => {
|
|
384
|
+
on = false;
|
|
385
|
+
};
|
|
386
|
+
}, []);
|
|
387
|
+
const done = steps.filter((s) => s.status === "done").length;
|
|
388
|
+
const total = steps.filter((s) => s.status !== "skipped").length || steps.length;
|
|
389
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "Setting up \u2014 agent at work" }), error ? (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: "red", children: error }) })) : (_jsxs(_Fragment, { children: [_jsxs(Box, { marginTop: 1, flexDirection: "row", children: [_jsxs(Box, { flexDirection: "column", width: 40, children: [_jsx(Text, { bold: true, children: "Learn" }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "Sodabase is setting up your monitors" }), _jsx(Text, { dimColor: true, children: "and baselines on the server \u2014 the same" }), _jsx(Text, { dimColor: true, children: "job the web onboarding runs. This panel" }), _jsx(Text, { dimColor: true, children: "just renders it; the CLI owns no" }), _jsx(Text, { dimColor: true, children: "orchestration." })] })] }), _jsxs(Box, { flexDirection: "column", flexGrow: 1, children: [_jsx(Text, { bold: true, children: "Tasks" }), steps.map((s) => {
|
|
390
|
+
const mk = s.status === "done" ? "✔" : s.status === "running" ? "▶" : s.status === "skipped" ? "—" : "□";
|
|
391
|
+
const color = s.status === "done" || s.status === "running" ? "green" : undefined;
|
|
392
|
+
return (_jsxs(Text, { color: color, dimColor: s.status === "pending" || s.status === "queued" || s.status === "skipped", children: [" ", mk, " ", stepLabel(s.name)] }, s.name));
|
|
393
|
+
}), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [_jsx(Text, { color: "green", children: "Progress" }), " ", _jsx(Text, { bold: true, children: `${done}/${total}` })] }) })] })] }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "Tail logs" }), logs.map((l, i) => (_jsxs(Text, { dimColor: true, children: [" ", l] }, i)))] })] }))] }));
|
|
394
|
+
}
|
|
395
|
+
// ── Phase: Scope (onboard-then-adjust; monitor ids) ──────────────────────────────
|
|
396
|
+
export function ScopeAdjust({ onDone }) {
|
|
397
|
+
const { services, fast, interactive } = useWizard();
|
|
398
|
+
const [proposals, setProposals] = useState(null);
|
|
399
|
+
const [sel, setSel] = useState([]);
|
|
400
|
+
const [cursor, setCursor] = useState(0);
|
|
401
|
+
const [committed, setCommitted] = useState(false);
|
|
402
|
+
const [error, setError] = useState(null);
|
|
403
|
+
useEffect(() => {
|
|
404
|
+
let on = true;
|
|
405
|
+
(async () => {
|
|
406
|
+
try {
|
|
407
|
+
const list = await services.proposals();
|
|
408
|
+
if (!on)
|
|
409
|
+
return;
|
|
410
|
+
setProposals(list.proposals);
|
|
411
|
+
setSel(list.proposals.map(() => true)); // all proposed monitors on by default
|
|
412
|
+
// Nothing to adjust (already set up, or discovery proposed none) → don't make the user stare at an empty
|
|
413
|
+
// list: show the empty message briefly, then advance. Applies in interactive mode too.
|
|
414
|
+
if (list.proposals.length === 0) {
|
|
415
|
+
await sleep(fast ? 300 : 900);
|
|
416
|
+
if (on)
|
|
417
|
+
await commit(list.proposals, []);
|
|
418
|
+
}
|
|
419
|
+
else if (!interactive) {
|
|
420
|
+
await sleep(fast ? 300 : 1200);
|
|
421
|
+
if (on)
|
|
422
|
+
await commit(list.proposals, list.proposals.map(() => true));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
catch (e) {
|
|
426
|
+
if (on)
|
|
427
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
428
|
+
}
|
|
429
|
+
})();
|
|
430
|
+
return () => {
|
|
431
|
+
on = false;
|
|
432
|
+
};
|
|
433
|
+
}, []);
|
|
434
|
+
async function commit(p, s) {
|
|
435
|
+
try {
|
|
436
|
+
if (p.length === 0) {
|
|
437
|
+
// Nothing proposed → no confirm/decline calls to make; just move on.
|
|
438
|
+
setCommitted(true);
|
|
439
|
+
onDone({ selected: 0 });
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
const confirm = p.filter((_, i) => s[i]).map((x) => x.id);
|
|
443
|
+
const decline = p.filter((_, i) => !s[i]).map((x) => x.id);
|
|
444
|
+
await services.confirmProposals(confirm.length === p.length ? null : confirm);
|
|
445
|
+
if (decline.length)
|
|
446
|
+
await services.declineProposals(decline);
|
|
447
|
+
setCommitted(true);
|
|
448
|
+
onDone({ selected: confirm.length });
|
|
449
|
+
}
|
|
450
|
+
catch (e) {
|
|
451
|
+
// A network/401 here must surface, not become an unhandled rejection that kills the Ink app.
|
|
452
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
useInput((_i, key) => {
|
|
456
|
+
if (!proposals || committed)
|
|
457
|
+
return;
|
|
458
|
+
if (key.upArrow)
|
|
459
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
460
|
+
else if (key.downArrow)
|
|
461
|
+
setCursor((c) => Math.min(proposals.length - 1, c + 1));
|
|
462
|
+
else if (_i === " ")
|
|
463
|
+
setSel((s) => s.map((v, i) => (i === cursor ? !v : v)));
|
|
464
|
+
else if (key.return)
|
|
465
|
+
void commit(proposals, sel);
|
|
466
|
+
}, { isActive: interactive && !!proposals && !committed });
|
|
467
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "What matters most" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Monitoring what discovery proposed. Adjust below \u2014 unpicked ones won't run." }) }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: error ? (_jsx(Text, { color: "red", children: error })) : !proposals ? (_jsx(Text, { dimColor: true, children: "loading proposals\u2026" })) : proposals.length === 0 ? (_jsx(Text, { dimColor: true, children: "Nothing to adjust \u2014 these are already set up." })) : (proposals.map((p, i) => {
|
|
468
|
+
const on = sel[i];
|
|
469
|
+
const here = interactive && i === cursor;
|
|
470
|
+
return (_jsxs(Text, { color: on ? "green" : undefined, dimColor: !on, children: [here ? "❯ " : " ", on ? "[x]" : "[ ]", " ", p.dataset, " ", _jsxs(Text, { dimColor: true, children: ["\u00B7 ", p.type] })] }, p.id));
|
|
471
|
+
})) }), _jsx(Box, { marginTop: 1, children: interactive && !committed && proposals && proposals.length > 0 ? (_jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 space toggle \u00B7 enter confirm \u00B7 esc to go back" })) : (_jsx(Text, { dimColor: true, children: committed ? "saved" : "accepting recommended…" })) })] }));
|
|
472
|
+
}
|
|
473
|
+
// ── Phase: consented in-flow MCP install ─────────────────────────────────────────
|
|
474
|
+
// These ids are forwarded verbatim to BOTH `add-mcp -a` and `skills -a` (see client.ts).
|
|
475
|
+
// Keep every id in this list valid for both CLIs — all three are today; an id only one of
|
|
476
|
+
// them knows would make that tool's install fail (non-fatal, but all-or-nothing for skills).
|
|
477
|
+
const AGENTS = ["claude-code", "cursor", "codex"];
|
|
478
|
+
export function McpInstall({ onDone }) {
|
|
479
|
+
const { services, fast, interactive } = useWizard();
|
|
480
|
+
const [sel, setSel] = useState([true, false, false]);
|
|
481
|
+
const [cursor, setCursor] = useState(0);
|
|
482
|
+
const [result, setResult] = useState(null);
|
|
483
|
+
const [skillResult, setSkillResult] = useState(null);
|
|
484
|
+
const [skillError, setSkillError] = useState(null);
|
|
485
|
+
// The agents the MCP install actually wrote, so the skill-error hold can advance with the right context.
|
|
486
|
+
const [installedAgents, setInstalledAgents] = useState([]);
|
|
487
|
+
const [busy, setBusy] = useState(false);
|
|
488
|
+
const [error, setError] = useState(null);
|
|
489
|
+
// MCP install is reached only from the hub menu (interactive). Back-navigation can unmount this
|
|
490
|
+
// mid-install, so guard every post-await state update.
|
|
491
|
+
const mounted = useRef(true);
|
|
492
|
+
useEffect(() => () => void (mounted.current = false), []);
|
|
493
|
+
async function run(choices) {
|
|
494
|
+
if (choices.length === 0) {
|
|
495
|
+
onDone({ installed: [] });
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
setBusy(true);
|
|
499
|
+
try {
|
|
500
|
+
const r = await services.installMcp(choices);
|
|
501
|
+
if (!mounted.current)
|
|
502
|
+
return;
|
|
503
|
+
setResult(r);
|
|
504
|
+
setInstalledAgents(choices);
|
|
505
|
+
// Also install the trust-advisor skill to the same agents. Non-fatal: the MCP (the main
|
|
506
|
+
// thing) is already in place, so a skill failure shouldn't block onboarding — we surface it
|
|
507
|
+
// with a manual fallback instead.
|
|
508
|
+
let skillFailed = false;
|
|
509
|
+
try {
|
|
510
|
+
const s = await services.installSkill(choices);
|
|
511
|
+
if (mounted.current)
|
|
512
|
+
setSkillResult(s);
|
|
513
|
+
}
|
|
514
|
+
catch (e) {
|
|
515
|
+
if (mounted.current)
|
|
516
|
+
setSkillError(e instanceof Error ? e.message : String(e));
|
|
517
|
+
skillFailed = true;
|
|
518
|
+
}
|
|
519
|
+
await sleep(fast ? 300 : 900);
|
|
520
|
+
if (!mounted.current)
|
|
521
|
+
return;
|
|
522
|
+
// Hold on a skill failure so the manual fallback is readable/actionable (enter advances);
|
|
523
|
+
// otherwise finish.
|
|
524
|
+
if (skillFailed)
|
|
525
|
+
setBusy(false);
|
|
526
|
+
else
|
|
527
|
+
onDone({ installed: choices });
|
|
528
|
+
}
|
|
529
|
+
catch (e) {
|
|
530
|
+
// Don't silently swallow it — the user should know the MCP didn't install, and how to add it by hand.
|
|
531
|
+
if (!mounted.current)
|
|
532
|
+
return;
|
|
533
|
+
setBusy(false);
|
|
534
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
useInput((input, key) => {
|
|
538
|
+
if (busy)
|
|
539
|
+
return;
|
|
540
|
+
if (skillError) {
|
|
541
|
+
// Holding after a non-fatal skill failure: the MCP already landed, so advance on enter.
|
|
542
|
+
if (key.return)
|
|
543
|
+
onDone({ installed: installedAgents });
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (error) {
|
|
547
|
+
if (key.return)
|
|
548
|
+
onDone({ installed: [] });
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (key.upArrow)
|
|
552
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
553
|
+
else if (key.downArrow)
|
|
554
|
+
setCursor((c) => Math.min(AGENTS.length - 1, c + 1));
|
|
555
|
+
else if (input === " ")
|
|
556
|
+
setSel((s) => s.map((v, i) => (i === cursor ? !v : v)));
|
|
557
|
+
else if (input === "n")
|
|
558
|
+
void run([]);
|
|
559
|
+
else if (key.return)
|
|
560
|
+
void run(AGENTS.filter((_, i) => sel[i]));
|
|
561
|
+
}, { isActive: interactive && !busy && (!result || skillError !== null) });
|
|
562
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "Set up your coding agent (optional)" }), error ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { color: "yellow", children: ["Couldn't auto-install the MCP: ", error] }), _jsx(Text, { dimColor: true, children: "Add it yourself (Claude Code):" }), _jsxs(Text, { color: "cyan", children: [" ", "claude mcp add --transport http sodabase https://sodabase.io/mcp"] }), interactive && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "enter to continue" }) }))] })) : result ? (_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsxs(Text, { dimColor: true, children: ["Ran: ", result.command] }), result.wrote.map((w) => (_jsxs(Text, { children: [" ", _jsx(Text, { color: "green", children: "\u2714" }), " ", w.agent, " ", _jsxs(Text, { dimColor: true, children: ["\u2192 ", w.path] })] }, w.agent))), skillResult && (_jsxs(Text, { children: [" ", _jsx(Text, { color: "green", children: "\u2714" }), " trust-advisor skill ", _jsx(Text, { dimColor: true, children: "\u2192 sodabase/agent-skills" })] })), skillError && (_jsxs(Box, { flexDirection: "column", children: [_jsxs(Text, { color: "yellow", children: ["Couldn't install the trust-advisor skill: ", skillError] }), _jsx(Text, { dimColor: true, children: "Add it yourself:" }), _jsxs(Text, { color: "cyan", children: [" ", "npx skills add sodabase/agent-skills"] })] })), _jsx(Text, { color: "green", children: "Ready next time you restart your agent." }), skillError && interactive && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "enter to continue" }) }))] })) : (_jsxs(_Fragment, { children: [_jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "Install the Sodabase MCP + trust-advisor skill so your agent can ask \"is this data safe to trust?\"" }), _jsx(Text, { dimColor: true, children: "Explicit \u2014 we show the exact change and only write on your confirm." })] }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: AGENTS.map((a, i) => {
|
|
563
|
+
const on = sel[i];
|
|
564
|
+
const here = interactive && i === cursor;
|
|
565
|
+
return (_jsxs(Text, { color: on ? "green" : undefined, dimColor: !on, children: [here ? "❯ " : " ", on ? "[x]" : "[ ]", " ", a] }, a));
|
|
566
|
+
}) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: interactive ? "↑↓ move · space toggle · enter install · n skip · esc to go back" : "installing for claude-code…" }) })] }))] }));
|
|
567
|
+
}
|
|
568
|
+
// ── Phase: manage projects (the hub's "Manage projects" entry) ───────────────────
|
|
569
|
+
// A picker over the projects already being watched; selecting one drills into its watched tables.
|
|
570
|
+
// A final row onboards a brand-new project (the Supabase authorize + pick flow).
|
|
571
|
+
export function ManageProjects({ onDone }) {
|
|
572
|
+
const { services, interactive } = useWizard();
|
|
573
|
+
const [projects, setProjects] = useState(null);
|
|
574
|
+
const [cursor, setCursor] = useState(0);
|
|
575
|
+
const [error, setError] = useState(null);
|
|
576
|
+
useEffect(() => {
|
|
577
|
+
let on = true;
|
|
578
|
+
(async () => {
|
|
579
|
+
try {
|
|
580
|
+
const p = await services.dashboardProjects();
|
|
581
|
+
if (on)
|
|
582
|
+
setProjects(p);
|
|
583
|
+
}
|
|
584
|
+
catch (e) {
|
|
585
|
+
if (on)
|
|
586
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
587
|
+
}
|
|
588
|
+
})();
|
|
589
|
+
return () => {
|
|
590
|
+
on = false;
|
|
591
|
+
};
|
|
592
|
+
}, []);
|
|
593
|
+
const onboardRow = projects?.length ?? 0; // the "+ onboard a new project" row sits after the projects
|
|
594
|
+
useInput((_i, key) => {
|
|
595
|
+
if (!projects)
|
|
596
|
+
return;
|
|
597
|
+
if (key.upArrow)
|
|
598
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
599
|
+
else if (key.downArrow)
|
|
600
|
+
setCursor((c) => Math.min(onboardRow, c + 1));
|
|
601
|
+
else if (key.return) {
|
|
602
|
+
const p = projects[cursor];
|
|
603
|
+
if (cursor < projects.length && p)
|
|
604
|
+
onDone({ projectRef: p.ref }, { to: "managetables" });
|
|
605
|
+
else
|
|
606
|
+
onDone({}, { to: "connect" }); // onboard a new project
|
|
607
|
+
}
|
|
608
|
+
}, { isActive: interactive && !!projects });
|
|
609
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "Manage projects" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Pick a project to manage its tables, or onboard a new one." }) }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: error ? (_jsx(Text, { color: "red", children: error })) : !projects ? (_jsx(Text, { dimColor: true, children: "loading your projects\u2026" })) : (_jsxs(_Fragment, { children: [projects.map((p, i) => {
|
|
610
|
+
const here = i === cursor;
|
|
611
|
+
const n = totalIssues(p);
|
|
612
|
+
return (_jsxs(Text, { color: here ? "green" : undefined, children: [here ? "❯ " : " ", p.name, " ", _jsxs(Text, { color: n > 0 ? "yellow" : "green", children: ["\u00B7 ", issuesLabel(n)] })] }, p.ref));
|
|
613
|
+
}), _jsxs(Text, { color: cursor === onboardRow ? "green" : undefined, children: [cursor === onboardRow ? "❯ " : " ", "+ Onboard a new project"] })] })) }), projects && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 enter select \u00B7 esc to go back" }) }))] }));
|
|
614
|
+
}
|
|
615
|
+
// ── Phase: manage a project's table monitoring ───────────────────────────────────
|
|
616
|
+
// Lists the selected project's tables with their current watch state (one row per table, watched or
|
|
617
|
+
// not), each toggleable. Toggles commit immediately via the per-table watch endpoint (toggleDataset).
|
|
618
|
+
export function ManageTables({ ctx }) {
|
|
619
|
+
const { services, interactive } = useWizard();
|
|
620
|
+
const projectRef = ctx.projectRef ?? "";
|
|
621
|
+
const [tables, setTables] = useState(null);
|
|
622
|
+
const [cursor, setCursor] = useState(0);
|
|
623
|
+
const [error, setError] = useState(null);
|
|
624
|
+
const mounted = useRef(true);
|
|
625
|
+
useEffect(() => () => void (mounted.current = false), []);
|
|
626
|
+
// Latest tables[] for toggle() to read without a stale closure; the in-flight set serializes
|
|
627
|
+
// writes per row so a rapid double-press can't race or double-fire.
|
|
628
|
+
const tablesRef = useRef(tables);
|
|
629
|
+
tablesRef.current = tables;
|
|
630
|
+
const inFlight = useRef(new Set());
|
|
631
|
+
useEffect(() => {
|
|
632
|
+
if (!projectRef) {
|
|
633
|
+
// Only reachable via ManageProjects, which always passes a ref — guard anyway so we never hit
|
|
634
|
+
// a malformed /projects//datasets URL.
|
|
635
|
+
setError("No project selected.");
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
let on = true;
|
|
639
|
+
(async () => {
|
|
640
|
+
try {
|
|
641
|
+
const rows = await services.projectDatasets(projectRef);
|
|
642
|
+
if (on)
|
|
643
|
+
setTables(rows);
|
|
644
|
+
}
|
|
645
|
+
catch (e) {
|
|
646
|
+
if (on)
|
|
647
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
648
|
+
}
|
|
649
|
+
})();
|
|
650
|
+
return () => {
|
|
651
|
+
on = false;
|
|
652
|
+
};
|
|
653
|
+
}, [projectRef]);
|
|
654
|
+
// Optimistic toggle + immediate save against the real per-table watch endpoint; revert the row if
|
|
655
|
+
// the call fails or the server reports the read grant expired (needsReauth).
|
|
656
|
+
function toggle(i) {
|
|
657
|
+
const t = tablesRef.current?.[i];
|
|
658
|
+
if (!t || inFlight.current.has(t.id))
|
|
659
|
+
return; // one in-flight write per row
|
|
660
|
+
const next = !t.monitored;
|
|
661
|
+
const apply = (monitored) => setTables((rows) => rows?.map((row, j) => (j === i ? { ...row, monitored } : row)) ?? rows);
|
|
662
|
+
apply(next);
|
|
663
|
+
setError(null);
|
|
664
|
+
inFlight.current.add(t.id);
|
|
665
|
+
void (async () => {
|
|
666
|
+
try {
|
|
667
|
+
const r = await services.toggleDataset(projectRef, t.id, next);
|
|
668
|
+
if (!mounted.current)
|
|
669
|
+
return;
|
|
670
|
+
if (next && r.needsReauth) {
|
|
671
|
+
// Only an ENABLE needs the read grant; a dead grant means this watch didn't take effect.
|
|
672
|
+
apply(false);
|
|
673
|
+
setError("Monitoring can't resume — your read-only access expired. Reconnect the project, then retry.");
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
apply(r.monitored); // reconcile with server truth
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
catch (e) {
|
|
680
|
+
if (!mounted.current)
|
|
681
|
+
return;
|
|
682
|
+
apply(!next);
|
|
683
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
684
|
+
}
|
|
685
|
+
finally {
|
|
686
|
+
inFlight.current.delete(t.id);
|
|
687
|
+
}
|
|
688
|
+
})();
|
|
689
|
+
}
|
|
690
|
+
useInput((input, key) => {
|
|
691
|
+
if (!tables || tables.length === 0)
|
|
692
|
+
return;
|
|
693
|
+
if (key.upArrow)
|
|
694
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
695
|
+
else if (key.downArrow)
|
|
696
|
+
setCursor((c) => Math.min(tables.length - 1, c + 1));
|
|
697
|
+
else if (input === " " || key.return)
|
|
698
|
+
toggle(cursor);
|
|
699
|
+
}, { isActive: interactive && !!tables });
|
|
700
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: "Table monitoring" }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { dimColor: true, children: "Toggle a table to watch or unwatch it \u2014 changes save as you go." }), projectRef && (_jsxs(Text, { dimColor: true, children: ["Project ", _jsx(Text, { color: "green", children: projectRef })] }))] }), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [error && _jsx(Text, { color: "red", children: error }), !tables ? (_jsx(Text, { dimColor: true, children: "loading this project's tables\u2026" })) : tables.length === 0 ? (_jsx(Text, { dimColor: true, children: "No tables discovered for this project yet." })) : (tables.map((t, i) => {
|
|
701
|
+
const here = i === cursor;
|
|
702
|
+
return (_jsxs(Text, { color: t.monitored ? "green" : undefined, dimColor: !t.monitored, children: [here ? "❯ " : " ", t.monitored ? "[x]" : "[ ]", " ", t.name] }, t.id));
|
|
703
|
+
}))] }), tables && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 space/enter toggle watch \u00B7 esc to go back" }) }))] }));
|
|
704
|
+
}
|
|
705
|
+
// ── Phase: home (trust report + hub menu) ────────────────────────────────────────
|
|
706
|
+
// The post-sign-in landing. Shows a trust report built ONLY from real backend data (no invented
|
|
707
|
+
// metrics) and an action menu; each action returns here when it finishes.
|
|
708
|
+
const HOME_MENU = [
|
|
709
|
+
{ id: "manage", to: "manageprojects", label: "Manage projects", hint: "pick a project · onboard a new one" },
|
|
710
|
+
{ id: "mcp", to: "mcp", label: "Install MCP for your agent", hint: "check_trust in Claude, Cursor & more" },
|
|
711
|
+
];
|
|
712
|
+
/** A trust-report row with the label in a left-aligned dim column (matching the detect screen). */
|
|
713
|
+
function StatRow({ label, children }) {
|
|
714
|
+
return (_jsxs(Text, { children: [_jsx(Text, { dimColor: true, children: label.padEnd(16) }), children] }));
|
|
715
|
+
}
|
|
716
|
+
const plural = (n, s) => `${n} ${s}${n === 1 ? "" : "s"}`;
|
|
717
|
+
export function Home({ ctx, onDone }) {
|
|
718
|
+
const { exit } = useApp();
|
|
719
|
+
const { services, fast, interactive } = useWizard();
|
|
720
|
+
const [projects, setProjects] = useState(null);
|
|
721
|
+
const [cursor, setCursor] = useState(0);
|
|
722
|
+
useEffect(() => {
|
|
723
|
+
let on = true;
|
|
724
|
+
(async () => {
|
|
725
|
+
try {
|
|
726
|
+
const p = await services.dashboardProjects();
|
|
727
|
+
if (on)
|
|
728
|
+
setProjects(p);
|
|
729
|
+
}
|
|
730
|
+
catch {
|
|
731
|
+
if (on)
|
|
732
|
+
setProjects([]); // no projects yet / offline — render the empty-state + menu
|
|
733
|
+
}
|
|
734
|
+
})();
|
|
735
|
+
return () => {
|
|
736
|
+
on = false;
|
|
737
|
+
};
|
|
738
|
+
}, []);
|
|
739
|
+
// Non-interactive (demo/CI): the hub is terminal — there's no menu to drive, so show the report
|
|
740
|
+
// and exit after a readable pause.
|
|
741
|
+
useEffect(() => {
|
|
742
|
+
if (interactive)
|
|
743
|
+
return;
|
|
744
|
+
const t = setTimeout(() => exit(), fast ? 400 : 3200);
|
|
745
|
+
return () => clearTimeout(t);
|
|
746
|
+
}, [interactive]);
|
|
747
|
+
useInput((_i, key) => {
|
|
748
|
+
if (key.upArrow)
|
|
749
|
+
setCursor((c) => Math.max(0, c - 1));
|
|
750
|
+
else if (key.downArrow)
|
|
751
|
+
setCursor((c) => Math.min(HOME_MENU.length - 1, c + 1));
|
|
752
|
+
else if (key.return) {
|
|
753
|
+
const item = HOME_MENU[cursor];
|
|
754
|
+
if (item)
|
|
755
|
+
onDone({}, { to: item.to });
|
|
756
|
+
}
|
|
757
|
+
}, { isActive: interactive && projects !== null });
|
|
758
|
+
const list = projects ?? [];
|
|
759
|
+
const watching = list.length > 0;
|
|
760
|
+
const tables = list.reduce((a, p) => a + p.watched, 0);
|
|
761
|
+
const findings = list.reduce((a, p) => a + p.dataQualityIssues, 0);
|
|
762
|
+
const signals = list.reduce((a, p) => a + p.openIssues, 0);
|
|
763
|
+
const healthy = list.filter((p) => totalIssues(p) === 0).length;
|
|
764
|
+
return (_jsxs(Box, { flexDirection: "column", paddingX: 2, marginTop: 1, children: [_jsx(Eyebrow, { children: watching ? "Sodabase is watching" : "Welcome to Sodabase" }), _jsx(Box, { marginTop: 1, flexDirection: "column", children: projects === null ? (_jsx(Text, { dimColor: true, children: "loading your trust report\u2026" })) : watching ? (_jsxs(_Fragment, { children: [_jsxs(StatRow, { label: "Tables watched", children: [_jsx(Text, { color: "green", bold: true, children: tables }), " ", "across ", plural(list.length, "project")] }), _jsxs(StatRow, { label: "Day-1 findings", children: [_jsx(Text, { color: findings > 0 ? "yellow" : "green", bold: true, children: findings }), " ", "to review", healthy > 0 && _jsxs(Text, { dimColor: true, children: [" \u00B7 ", plural(healthy, "project"), " healthy"] })] }), _jsxs(StatRow, { label: "Open signals", children: [_jsx(Text, { color: signals > 0 ? "yellow" : "green", bold: true, children: signals }), " ", _jsx(Text, { dimColor: true, children: "freshness & volume anomalies" })] }), ctx.installed && ctx.installed.length > 0 && (_jsxs(StatRow, { label: "MCP", children: [_jsx(Text, { color: "green", children: "\u2714" }), " installed for ", ctx.installed.join(", ")] }))] })) : (_jsxs(_Fragment, { children: [_jsx(Text, { children: "You're not watching any projects yet." }), _jsx(Text, { dimColor: true, children: "Pick one below \u2014 Sodabase sets up read-only monitoring, no code changes." })] })) }), interactive && projects !== null && (_jsxs(_Fragment, { children: [_jsx(Box, { marginTop: 1, flexDirection: "column", children: HOME_MENU.map((m, i) => {
|
|
765
|
+
const here = i === cursor;
|
|
766
|
+
return (_jsxs(Text, { color: here ? "green" : undefined, children: [here ? "❯ " : " ", m.label, " ", _jsxs(Text, { dimColor: true, children: ["\u00B7 ", m.hint] })] }, m.id));
|
|
767
|
+
}) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "\u2191\u2193 move \u00B7 enter select \u00B7 esc to quit" }) })] }))] }));
|
|
768
|
+
}
|