@blackbelt-technology/pi-dashboard-gmail-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.
- package/README.md +23 -8
- package/package.json +6 -6
- package/src/bridge/attachments.ts +4 -1
- package/src/bridge/gmail-api.ts +71 -5
- package/src/bridge/lease-client.ts +9 -2
- package/src/client/GmailSettings.tsx +457 -171
- package/src/client/wizard.ts +74 -1
- package/src/i18n.ts +119 -13
- package/src/server/accounts.ts +13 -2
- package/src/server/google-oauth.ts +4 -1
- package/src/server/lease.ts +4 -1
- package/src/server/routes.ts +45 -16
- package/src/shared/flow-codes.ts +36 -0
|
@@ -5,14 +5,37 @@
|
|
|
5
5
|
* (add / re-auth / level / alias / revoke, status badges, honest scope limit).
|
|
6
6
|
* Sign-in flows render through the host `ui:oauth-flow` primitive.
|
|
7
7
|
* See change: add-gmail-plugin.
|
|
8
|
+
*
|
|
9
|
+
* improve-gmail-settings-ux: declared theme tokens + `.focus-ring` on every
|
|
10
|
+
* control; consent hint + "Google showed an error?" disclosure while a sign-in
|
|
11
|
+
* waits (reported code latched via `reportedRef`, so neither poll callback can
|
|
12
|
+
* overwrite it or re-mount the flow); every flow error rendered as a sentence
|
|
13
|
+
* (`errorKey` / `ERROR_EN`); Revoke via `ui:confirm-dialog`; alias feedback;
|
|
14
|
+
* level descriptions; collapsed-summary project/client id.
|
|
15
|
+
*
|
|
16
|
+
* retro-ux-plugin-settings-forms: labelled inputs through the shared settings
|
|
17
|
+
* field family, numbered wizard step chips (`aria-current="step"`, done = check),
|
|
18
|
+
* "Connected" / "Re-auth needed" pills, per-account control names, one primary
|
|
19
|
+
* "Add account" button with a linked disabled reason, flow errors as a severity
|
|
20
|
+
* notice. Immediate-save model unchanged.
|
|
8
21
|
*/
|
|
9
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
copyText,
|
|
24
|
+
FieldShell,
|
|
25
|
+
oauthFlowClient,
|
|
26
|
+
SelectField,
|
|
27
|
+
SettingsGroup,
|
|
28
|
+
SettingsNotice,
|
|
29
|
+
TextField,
|
|
30
|
+
useT,
|
|
31
|
+
useUiPrimitive,
|
|
32
|
+
} from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
10
33
|
import type React from "react";
|
|
11
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
34
|
+
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
12
35
|
import { validateClientJson } from "../shared/client-json.js";
|
|
13
36
|
import type { AccountInfo } from "../shared/protocol.js";
|
|
14
37
|
import { TIERS, type Tier } from "../shared/scopes.js";
|
|
15
|
-
import { consoleLinks, errorStep, gcloudCommands, type WizardStep } from "./wizard.js";
|
|
38
|
+
import { consoleLinks, ERROR_EN, errorKey, errorStep, gcloudCommands, type WizardStep } from "./wizard.js";
|
|
16
39
|
|
|
17
40
|
const API = "/api/plugins/gmail";
|
|
18
41
|
|
|
@@ -34,9 +57,20 @@ async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
|
|
34
57
|
return body;
|
|
35
58
|
}
|
|
36
59
|
|
|
60
|
+
// Secondary button recipe (`ui-contract.md`); `tap-target` = 44 px touch, 32 px from sm.
|
|
37
61
|
const btn =
|
|
38
|
-
"rounded border border-[var(--border)] px-2
|
|
39
|
-
|
|
62
|
+
"focus-ring tap-target rounded-md border border-[var(--border-secondary)] bg-[var(--bg-tertiary)] px-2.5 text-xs font-semibold text-[var(--text-primary)] hover:bg-[var(--bg-hover)] disabled:opacity-50";
|
|
63
|
+
// The section's one primary action (white on the theme-invariant `--accent-solid`).
|
|
64
|
+
const primaryBtn =
|
|
65
|
+
"focus-ring tap-target rounded-md bg-[var(--accent-solid)] px-3 text-xs font-semibold text-white disabled:bg-[var(--bg-tertiary)] disabled:text-[var(--text-secondary)]";
|
|
66
|
+
const field =
|
|
67
|
+
"focus-ring rounded border border-[var(--border-secondary)] bg-[var(--bg-tertiary)] px-2 py-1 text-xs text-[var(--text-primary)]";
|
|
68
|
+
const muted = "text-xs text-[var(--text-secondary)]";
|
|
69
|
+
const errorText = "text-xs text-[var(--severity-error-fg)]";
|
|
70
|
+
// Static strings: Tailwind cannot see interpolated class names.
|
|
71
|
+
const pillBase = "inline-flex items-center rounded-full border px-1.5 py-px text-[11px] font-semibold";
|
|
72
|
+
const badgeOk = `${pillBase} border-[var(--severity-success-border)] bg-[var(--severity-success-bg)] text-[var(--severity-success-fg)]`;
|
|
73
|
+
const badgeReauth = `${pillBase} border-[var(--severity-warning-border)] bg-[var(--severity-warning-bg)] text-[var(--severity-warning-fg)]`;
|
|
40
74
|
|
|
41
75
|
/** Poll a flow every 500 ms until terminal. */
|
|
42
76
|
function useFlowPoll(flowId: string | undefined, onPending: (s: FlowStatus) => void, onDone: (err: string | null) => void) {
|
|
@@ -68,11 +102,24 @@ function useFlowPoll(flowId: string | undefined, onPending: (s: FlowStatus) => v
|
|
|
68
102
|
|
|
69
103
|
function Copyable({ value }: { value: string }) {
|
|
70
104
|
const t = useT();
|
|
105
|
+
const [result, setResult] = useState<"copied" | "failed" | null>(null);
|
|
106
|
+
const seq = useRef(0);
|
|
107
|
+
const copy = async (): Promise<void> => {
|
|
108
|
+
const n = ++seq.current;
|
|
109
|
+
const ok = await copyText(value);
|
|
110
|
+
if (n !== seq.current) return; // a later click superseded this one
|
|
111
|
+
setResult(ok ? "copied" : "failed");
|
|
112
|
+
setTimeout(() => {
|
|
113
|
+
if (n === seq.current) setResult(null);
|
|
114
|
+
}, 1500);
|
|
115
|
+
};
|
|
116
|
+
const label =
|
|
117
|
+
result === "copied" ? t("copied", undefined, "Copied") : result === "failed" ? t("copyFailed", undefined, "Copy failed") : t("copy", undefined, "Copy");
|
|
71
118
|
return (
|
|
72
119
|
<div className="flex items-center gap-2">
|
|
73
|
-
<code className="flex-1 overflow-x-auto rounded bg-[var(--bg-
|
|
74
|
-
<button type="button" className={btn} onClick={() => void
|
|
75
|
-
|
|
120
|
+
<code className="flex-1 overflow-x-auto rounded bg-[var(--bg-tertiary)] px-2 py-1 font-mono text-[12px] text-[var(--text-primary)]">{value}</code>
|
|
121
|
+
<button type="button" className={btn} onClick={() => void copy()}>
|
|
122
|
+
<span role="status">{label}</span>
|
|
76
123
|
</button>
|
|
77
124
|
</div>
|
|
78
125
|
);
|
|
@@ -80,12 +127,55 @@ function Copyable({ value }: { value: string }) {
|
|
|
80
127
|
|
|
81
128
|
function StepLink({ href, label }: { href: string; label: string }) {
|
|
82
129
|
return (
|
|
83
|
-
<a href={href} target="_blank" rel="noopener noreferrer" className="text-
|
|
130
|
+
<a href={href} target="_blank" rel="noopener noreferrer" className="focus-ring text-xs text-[var(--accent-text)] underline">
|
|
84
131
|
{label}
|
|
85
132
|
</a>
|
|
86
133
|
);
|
|
87
134
|
}
|
|
88
135
|
|
|
136
|
+
function WizardStepItem({
|
|
137
|
+
n,
|
|
138
|
+
title,
|
|
139
|
+
current,
|
|
140
|
+
done,
|
|
141
|
+
children,
|
|
142
|
+
}: {
|
|
143
|
+
n: WizardStep;
|
|
144
|
+
title: string;
|
|
145
|
+
current: boolean;
|
|
146
|
+
done: boolean;
|
|
147
|
+
children: React.ReactNode;
|
|
148
|
+
}) {
|
|
149
|
+
const t = useT();
|
|
150
|
+
return (
|
|
151
|
+
<li
|
|
152
|
+
className="grid grid-cols-[24px_1fr] gap-2 border-t border-[var(--border-subtle)] py-2 first:border-t-0"
|
|
153
|
+
data-testid={`gmail-step-${n}`}
|
|
154
|
+
aria-current={current ? "step" : undefined}
|
|
155
|
+
data-done={done ? "true" : undefined}
|
|
156
|
+
>
|
|
157
|
+
<span
|
|
158
|
+
className={`flex h-6 w-6 items-center justify-center rounded-full border text-[11px] font-bold ${
|
|
159
|
+
current
|
|
160
|
+
? "border-[var(--tint-blue-border)] bg-[var(--tint-blue-bg)] text-[var(--tint-blue-fg)]"
|
|
161
|
+
: done
|
|
162
|
+
? "border-[var(--severity-success-border)] bg-[var(--severity-success-bg)] text-[var(--severity-success-fg)]"
|
|
163
|
+
: "border-[var(--border-secondary)] bg-[var(--bg-tertiary)] text-[var(--text-secondary)]"
|
|
164
|
+
}`}
|
|
165
|
+
>
|
|
166
|
+
{done ? <span aria-hidden="true">✓</span> : n}
|
|
167
|
+
</span>
|
|
168
|
+
<div className="flex min-w-0 flex-col gap-1">
|
|
169
|
+
<div className="text-xs font-semibold text-[var(--text-primary)]">
|
|
170
|
+
{title.replace(/^\d+\.\s*/, "")}
|
|
171
|
+
{done ? <span className="sr-only"> — {t("stepDone", undefined, "done")}</span> : null}
|
|
172
|
+
</div>
|
|
173
|
+
{children}
|
|
174
|
+
</div>
|
|
175
|
+
</li>
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
89
179
|
export function SetupWizard({
|
|
90
180
|
state,
|
|
91
181
|
highlight,
|
|
@@ -121,8 +211,11 @@ export function SetupWizard({
|
|
|
121
211
|
}
|
|
122
212
|
};
|
|
123
213
|
|
|
124
|
-
|
|
125
|
-
|
|
214
|
+
// Current step: the highlighted one, else the next thing the user has to do.
|
|
215
|
+
const current: WizardStep | null =
|
|
216
|
+
focus ?? (!state.client.configured ? 1 : state.accounts.length === 0 ? 6 : null);
|
|
217
|
+
const isDone = (n: WizardStep) =>
|
|
218
|
+
n !== current && (state.client.configured ? n <= 5 || state.accounts.length > 0 : current !== null && n < current);
|
|
126
219
|
const uploadMsg: Record<string, string> = {
|
|
127
220
|
not_json: t("errNotJson", undefined, "That file is not valid JSON. Upload the client_secret_*.json you downloaded (step 5)."),
|
|
128
221
|
web_client: t("errWebClient", undefined, "This is a Web application client. Create a Desktop app client instead (step 4)."),
|
|
@@ -137,19 +230,17 @@ export function SetupWizard({
|
|
|
137
230
|
};
|
|
138
231
|
|
|
139
232
|
return (
|
|
140
|
-
<ol className="flex flex-col
|
|
141
|
-
<
|
|
142
|
-
<
|
|
143
|
-
|
|
144
|
-
{
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
/>
|
|
152
|
-
</label>
|
|
233
|
+
<ol className="m-0 flex list-none flex-col p-0" data-testid="gmail-wizard">
|
|
234
|
+
<WizardStepItem n={1} title={t("step1", undefined, "1. Google Cloud project + Gmail API")} current={current === 1} done={isDone(1)}>
|
|
235
|
+
<TextField
|
|
236
|
+
label={t("projectId", undefined, "Project id")}
|
|
237
|
+
hint={null}
|
|
238
|
+
mono
|
|
239
|
+
value={projectId}
|
|
240
|
+
onChange={setProjectId}
|
|
241
|
+
placeholder="my-gmail-agent"
|
|
242
|
+
testIds={{ control: "gmail-project-id" }}
|
|
243
|
+
/>
|
|
153
244
|
<div className={muted}>{t("withoutGcloud", undefined, "Without gcloud:")}</div>
|
|
154
245
|
<div className="flex gap-3">
|
|
155
246
|
<StepLink href={links.projectCreate} label={t("createProject", undefined, "Create project")} />
|
|
@@ -158,62 +249,86 @@ export function SetupWizard({
|
|
|
158
249
|
<div className={muted}>{t("withGcloud", undefined, "With gcloud (run it yourself):")}</div>
|
|
159
250
|
<Copyable value={cmdCreate as string} />
|
|
160
251
|
<Copyable value={cmdEnable as string} />
|
|
161
|
-
</
|
|
162
|
-
<
|
|
163
|
-
<div className="font-medium text-[13px]">{t("step2", undefined, "2. Branding")}</div>
|
|
252
|
+
</WizardStepItem>
|
|
253
|
+
<WizardStepItem n={2} title={t("step2", undefined, "2. Branding")} current={current === 2} done={isDone(2)}>
|
|
164
254
|
<StepLink href={links.branding} label={t("openBranding", undefined, "Open Branding")} />
|
|
165
255
|
<div className={muted}>{t("fallback2", undefined, "If the link moved: APIs & Services → OAuth consent screen.")}</div>
|
|
166
|
-
</
|
|
167
|
-
<
|
|
168
|
-
<div className="font-medium text-[13px]">{t("step3", undefined, "3. Audience + test users")}</div>
|
|
256
|
+
</WizardStepItem>
|
|
257
|
+
<WizardStepItem n={3} title={t("step3", undefined, "3. Audience + test users")} current={current === 3} done={isDone(3)}>
|
|
169
258
|
<StepLink href={links.audience} label={t("openAudience", undefined, "Open Audience")} />
|
|
170
259
|
<div className={muted}>
|
|
171
260
|
{t(
|
|
172
261
|
"audienceHelp",
|
|
173
262
|
undefined,
|
|
174
|
-
"Workspace
|
|
263
|
+
"Internal admits only accounts of the project's own Workspace organization: choose it only when every account you will connect belongs to that organization. Otherwise choose External and add every address you will connect as a test user. In Testing, tokens expire after 7 days; “Publish app” gives longer-lived tokens with an unverified-app warning.",
|
|
264
|
+
)}
|
|
265
|
+
</div>
|
|
266
|
+
<div className={muted}>
|
|
267
|
+
{t(
|
|
268
|
+
"audienceErrors",
|
|
269
|
+
undefined,
|
|
270
|
+
"Google error pages: org_internal = the account is outside the organization, switch to External; access_denied = add the account as a test user; admin_policy_enforced = the account's Workspace admin must trust this OAuth client.",
|
|
175
271
|
)}
|
|
176
272
|
</div>
|
|
177
|
-
</
|
|
178
|
-
<
|
|
179
|
-
<div className="font-medium text-[13px]">{t("step4", undefined, "4. Desktop OAuth client")}</div>
|
|
273
|
+
</WizardStepItem>
|
|
274
|
+
<WizardStepItem n={4} title={t("step4", undefined, "4. Desktop OAuth client")} current={current === 4} done={isDone(4)}>
|
|
180
275
|
<StepLink href={links.clientCreate} label={t("openClients", undefined, "Create client")} />
|
|
181
276
|
<div className={muted}>
|
|
182
277
|
{t("clientHelp", undefined, "Application type: Desktop app. Download the JSON. If the link moved: APIs & Services → Credentials.")}
|
|
183
278
|
</div>
|
|
184
|
-
</
|
|
185
|
-
<
|
|
186
|
-
<
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
279
|
+
</WizardStepItem>
|
|
280
|
+
<WizardStepItem n={5} title={t("step5", undefined, "5. Upload client_secret_*.json")} current={current === 5} done={isDone(5)}>
|
|
281
|
+
<FieldShell
|
|
282
|
+
label={t("uploadLabel", undefined, "Upload the OAuth client JSON")}
|
|
283
|
+
hint={null}
|
|
284
|
+
stacked
|
|
285
|
+
error={uploadError ? (uploadMsg[uploadError.code] ?? uploadError.code) : null}
|
|
286
|
+
testIds={{ error: "gmail-upload-error" }}
|
|
287
|
+
errorAttrs={uploadError ? { "data-step": String(uploadError.step) } : undefined}
|
|
288
|
+
control={({ id, describedBy, invalid }) => (
|
|
289
|
+
<input
|
|
290
|
+
id={id}
|
|
291
|
+
type="file"
|
|
292
|
+
className="focus-ring text-xs text-[var(--text-primary)]"
|
|
293
|
+
accept="application/json,.json"
|
|
294
|
+
data-testid="gmail-client-upload"
|
|
295
|
+
aria-describedby={describedBy}
|
|
296
|
+
aria-invalid={invalid || undefined}
|
|
297
|
+
onChange={(e) => {
|
|
298
|
+
const f = e.target.files?.[0];
|
|
299
|
+
if (f) void upload(f);
|
|
300
|
+
}}
|
|
301
|
+
/>
|
|
302
|
+
)}
|
|
196
303
|
/>
|
|
197
304
|
{state.client.configured && (
|
|
198
305
|
<div className={muted} data-testid="gmail-client-ok">
|
|
199
306
|
✓ {state.client.clientId}
|
|
200
307
|
</div>
|
|
201
308
|
)}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
{uploadMsg[uploadError.code] ?? uploadError.code}
|
|
205
|
-
</div>
|
|
206
|
-
)}
|
|
207
|
-
</li>
|
|
208
|
-
<li className={stepClass(6)} data-testid="gmail-step-6">
|
|
209
|
-
<div className="font-medium text-[13px]">{t("step6", undefined, "6. Test sign-in")}</div>
|
|
309
|
+
</WizardStepItem>
|
|
310
|
+
<WizardStepItem n={6} title={t("step6", undefined, "6. Test sign-in")} current={current === 6} done={isDone(6)}>
|
|
210
311
|
<div className={muted}>{t("step6Help", undefined, "Add your first account below — that is the test sign-in.")}</div>
|
|
211
|
-
</
|
|
312
|
+
</WizardStepItem>
|
|
212
313
|
</ol>
|
|
213
314
|
);
|
|
214
315
|
}
|
|
215
316
|
|
|
216
317
|
const TIER_LABEL: Record<Tier, string> = { readonly: "readonly", draft: "draft", send: "send" };
|
|
318
|
+
const TIER_DESC: Record<Tier, [key: string, en: string]> = {
|
|
319
|
+
readonly: ["tierReadonly", "read mail"],
|
|
320
|
+
draft: ["tierDraft", "read + create drafts"],
|
|
321
|
+
send: ["tierSend", "read, draft, send, reply, labels, archive, trash"],
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/** Each level's own description, as a select option list. */
|
|
325
|
+
function useTierOptions(): { value: string; label: string }[] {
|
|
326
|
+
const t = useT();
|
|
327
|
+
return TIERS.map((tier) => ({ value: tier, label: `${TIER_LABEL[tier]} — ${t(TIER_DESC[tier][0], undefined, TIER_DESC[tier][1])}` }));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Codes the user can report from Google's own error page (closed list, design D1). */
|
|
331
|
+
const REPORT_CODES = ["org_internal", "access_denied", "admin_policy_enforced", "other"] as const;
|
|
217
332
|
|
|
218
333
|
function AccountRow({
|
|
219
334
|
acct,
|
|
@@ -227,8 +342,12 @@ function AccountRow({
|
|
|
227
342
|
onRevoked: (email: string, remoteRevoked: boolean) => void;
|
|
228
343
|
}) {
|
|
229
344
|
const t = useT();
|
|
345
|
+
const ConfirmDialog = useUiPrimitive("ui:confirm-dialog");
|
|
346
|
+
const tierOptions = useTierOptions();
|
|
230
347
|
const [alias, setAlias] = useState(acct.alias ?? "");
|
|
231
348
|
const [msg, setMsg] = useState<string | null>(null);
|
|
349
|
+
const [aliasMsg, setAliasMsg] = useState<{ ok: boolean; text: string } | null>(null);
|
|
350
|
+
const [confirming, setConfirming] = useState(false);
|
|
232
351
|
const run = async (fn: () => Promise<void>) => {
|
|
233
352
|
setMsg(null);
|
|
234
353
|
try {
|
|
@@ -246,87 +365,207 @@ function AccountRow({
|
|
|
246
365
|
if (r.flowId) onFlow(r.flowId);
|
|
247
366
|
else onChanged();
|
|
248
367
|
});
|
|
368
|
+
const saveAlias = async () => {
|
|
369
|
+
try {
|
|
370
|
+
await api(`/accounts/${encodeURIComponent(acct.sub)}`, { method: "PATCH", body: JSON.stringify({ alias: alias || null }) });
|
|
371
|
+
setAliasMsg({ ok: true, text: t("aliasSaved", undefined, "Saved") });
|
|
372
|
+
onChanged();
|
|
373
|
+
} catch (err) {
|
|
374
|
+
const code = (err as { code?: string }).code ?? (err as Error).message;
|
|
375
|
+
const text =
|
|
376
|
+
code === "alias_taken"
|
|
377
|
+
? t("errAliasTaken", undefined, "That alias is already used by another account.")
|
|
378
|
+
: code === "invalid_alias"
|
|
379
|
+
? t("errInvalidAlias", undefined, "Aliases are 1–40 letters, digits, '.', '_' or '-'.")
|
|
380
|
+
: code;
|
|
381
|
+
setAliasMsg({ ok: false, text });
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
const revoke = () =>
|
|
385
|
+
run(async () => {
|
|
386
|
+
const r = await api<{ remoteRevoked: boolean }>(`/accounts/${encodeURIComponent(acct.sub)}`, { method: "DELETE" });
|
|
387
|
+
onRevoked(acct.email, r.remoteRevoked);
|
|
388
|
+
onChanged();
|
|
389
|
+
});
|
|
249
390
|
return (
|
|
250
|
-
<li
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
) : (
|
|
259
|
-
<span className="rounded bg-[var(--warning-bg)] px-1 text-[11px]" data-testid="gmail-account-status" data-status="reauth_required">
|
|
260
|
-
{t("statusReauth", undefined, "re-auth needed")}
|
|
391
|
+
<li
|
|
392
|
+
className="flex flex-col gap-2 rounded-md border border-[var(--border-secondary)] p-2"
|
|
393
|
+
data-testid="gmail-account-row"
|
|
394
|
+
data-email={acct.email}
|
|
395
|
+
>
|
|
396
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
397
|
+
<span className="text-sm font-semibold text-[var(--text-primary)]" data-testid="gmail-account-email">
|
|
398
|
+
{acct.email}
|
|
261
399
|
</span>
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
400
|
+
{acct.status === "ok" ? (
|
|
401
|
+
<span className={badgeOk} data-testid="gmail-account-status" data-status="ok">
|
|
402
|
+
{t("statusOk", undefined, "Connected")}
|
|
403
|
+
</span>
|
|
404
|
+
) : (
|
|
405
|
+
<span className={badgeReauth} data-testid="gmail-account-status" data-status="reauth_required">
|
|
406
|
+
{t("statusReauth", undefined, "Re-auth needed")}
|
|
407
|
+
</span>
|
|
408
|
+
)}
|
|
409
|
+
{acct.testingHint && (
|
|
410
|
+
<span className={muted} title={t("testingHint", undefined, "Testing-mode app: Google expires this grant after 7 days.")}>
|
|
411
|
+
{t("testingBadge", undefined, "testing: 7-day")}
|
|
412
|
+
</span>
|
|
413
|
+
)}
|
|
414
|
+
<input
|
|
415
|
+
aria-label={t("aliasFor", { email: acct.email }, `Alias for ${acct.email}`)}
|
|
416
|
+
data-testid="gmail-account-alias"
|
|
417
|
+
className={`${field} w-28`}
|
|
418
|
+
value={alias}
|
|
419
|
+
placeholder={t("aliasPlaceholder", undefined, "alias")}
|
|
420
|
+
onChange={(e) => {
|
|
421
|
+
setAlias(e.target.value);
|
|
422
|
+
setAliasMsg(null);
|
|
423
|
+
}}
|
|
424
|
+
onBlur={() => alias !== (acct.alias ?? "") && void saveAlias()}
|
|
425
|
+
/>
|
|
426
|
+
{aliasMsg && (
|
|
427
|
+
<span
|
|
428
|
+
role={aliasMsg.ok ? "status" : "alert"}
|
|
429
|
+
className={aliasMsg.ok ? muted : errorText}
|
|
430
|
+
data-testid="gmail-alias-feedback"
|
|
431
|
+
>
|
|
432
|
+
{aliasMsg.text}
|
|
433
|
+
</span>
|
|
434
|
+
)}
|
|
435
|
+
</div>
|
|
436
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
437
|
+
<label className={muted}>
|
|
438
|
+
{t("level", undefined, "Level")}{" "}
|
|
439
|
+
<select
|
|
440
|
+
aria-label={t("levelFor", { email: acct.email }, `Level for ${acct.email}`)}
|
|
441
|
+
data-testid="gmail-account-level"
|
|
442
|
+
value={acct.tier}
|
|
443
|
+
onChange={(e) => void setLevel(e.target.value as Tier)}
|
|
444
|
+
className={field}
|
|
445
|
+
>
|
|
446
|
+
{tierOptions.map((o) => (
|
|
447
|
+
<option key={o.value} value={o.value}>
|
|
448
|
+
{o.label}
|
|
449
|
+
</option>
|
|
450
|
+
))}
|
|
451
|
+
</select>
|
|
452
|
+
</label>
|
|
453
|
+
<button
|
|
454
|
+
type="button"
|
|
455
|
+
className={btn}
|
|
456
|
+
data-testid="gmail-account-reauth"
|
|
457
|
+
aria-label={t("reauthFor", { email: acct.email }, `Re-authenticate ${acct.email}`)}
|
|
458
|
+
onClick={() =>
|
|
459
|
+
void run(async () => {
|
|
460
|
+
// Force a fresh consent at the current level (status reset on persist).
|
|
461
|
+
const r = await api<{ flowId?: string }>(`/accounts/${encodeURIComponent(acct.sub)}/reauth`, { method: "POST" });
|
|
462
|
+
if (r.flowId) onFlow(r.flowId);
|
|
463
|
+
})
|
|
464
|
+
}
|
|
275
465
|
>
|
|
276
|
-
{
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
<button
|
|
308
|
-
type="button"
|
|
309
|
-
className={btn}
|
|
310
|
-
data-testid="gmail-account-revoke"
|
|
311
|
-
onClick={() =>
|
|
312
|
-
void run(async () => {
|
|
313
|
-
const r = await api<{ remoteRevoked: boolean }>(`/accounts/${encodeURIComponent(acct.sub)}`, { method: "DELETE" });
|
|
314
|
-
onRevoked(acct.email, r.remoteRevoked);
|
|
315
|
-
onChanged();
|
|
316
|
-
})
|
|
317
|
-
}
|
|
318
|
-
>
|
|
319
|
-
{t("revoke", undefined, "Revoke")}
|
|
320
|
-
</button>
|
|
321
|
-
{msg && (
|
|
322
|
-
<span role="alert" className="text-[12px] text-[var(--error)]">
|
|
323
|
-
{msg}
|
|
324
|
-
</span>
|
|
466
|
+
{t("reauth", undefined, "Re-authenticate")}
|
|
467
|
+
</button>
|
|
468
|
+
<button
|
|
469
|
+
type="button"
|
|
470
|
+
className={btn}
|
|
471
|
+
data-testid="gmail-account-revoke"
|
|
472
|
+
aria-label={t("revokeFor", { email: acct.email }, `Revoke ${acct.email}`)}
|
|
473
|
+
onClick={() => setConfirming(true)}
|
|
474
|
+
>
|
|
475
|
+
{t("revoke", undefined, "Revoke")}
|
|
476
|
+
</button>
|
|
477
|
+
{msg && (
|
|
478
|
+
<span role="alert" className={errorText}>
|
|
479
|
+
{msg}
|
|
480
|
+
</span>
|
|
481
|
+
)}
|
|
482
|
+
</div>
|
|
483
|
+
{confirming && (
|
|
484
|
+
<ConfirmDialog
|
|
485
|
+
message={t(
|
|
486
|
+
"revokeConfirm",
|
|
487
|
+
{ email: acct.email },
|
|
488
|
+
`Revoke ${acct.email}? The dashboard forgets the account and asks Google to revoke its access.`,
|
|
489
|
+
)}
|
|
490
|
+
confirmLabel={t("revoke", undefined, "Revoke")}
|
|
491
|
+
onConfirm={() => {
|
|
492
|
+
setConfirming(false);
|
|
493
|
+
void revoke();
|
|
494
|
+
}}
|
|
495
|
+
onCancel={() => setConfirming(false)}
|
|
496
|
+
/>
|
|
325
497
|
)}
|
|
326
498
|
</li>
|
|
327
499
|
);
|
|
328
500
|
}
|
|
329
501
|
|
|
502
|
+
function AddAccount({
|
|
503
|
+
configured,
|
|
504
|
+
newTier,
|
|
505
|
+
onTier,
|
|
506
|
+
onAdd,
|
|
507
|
+
}: {
|
|
508
|
+
configured: boolean;
|
|
509
|
+
newTier: Tier;
|
|
510
|
+
onTier: (t: Tier) => void;
|
|
511
|
+
onAdd: () => void;
|
|
512
|
+
}) {
|
|
513
|
+
const t = useT();
|
|
514
|
+
const addReasonId = useId();
|
|
515
|
+
const tierOptions = useTierOptions();
|
|
516
|
+
return (
|
|
517
|
+
<div className="flex flex-col gap-2">
|
|
518
|
+
<SelectField
|
|
519
|
+
label={t("newLevel", undefined, "Level for the new account")}
|
|
520
|
+
hint={null}
|
|
521
|
+
value={newTier}
|
|
522
|
+
options={tierOptions}
|
|
523
|
+
onChange={(v) => onTier(v as Tier)}
|
|
524
|
+
testIds={{ control: "gmail-new-level" }}
|
|
525
|
+
/>
|
|
526
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
527
|
+
<button
|
|
528
|
+
type="button"
|
|
529
|
+
className={primaryBtn}
|
|
530
|
+
data-testid="gmail-add-account"
|
|
531
|
+
disabled={!configured}
|
|
532
|
+
aria-describedby={configured ? undefined : addReasonId}
|
|
533
|
+
onClick={onAdd}
|
|
534
|
+
>
|
|
535
|
+
{t("addAccount", undefined, "Add account")}
|
|
536
|
+
</button>
|
|
537
|
+
{!configured && (
|
|
538
|
+
<span id={addReasonId} className={muted} data-testid="gmail-add-account-reason">
|
|
539
|
+
{t("addAccountDisabled", undefined, "Upload the OAuth client JSON (step 5 above) to enable this.")}
|
|
540
|
+
</span>
|
|
541
|
+
)}
|
|
542
|
+
</div>
|
|
543
|
+
<p className={`${muted} m-0`} data-testid="gmail-cross-org-hint">
|
|
544
|
+
{t(
|
|
545
|
+
"crossOrgHint",
|
|
546
|
+
undefined,
|
|
547
|
+
"Accounts outside the project's Workspace organization need an External audience, with each account added as a test user (step 3).",
|
|
548
|
+
)}
|
|
549
|
+
</p>
|
|
550
|
+
</div>
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** Failed sign-in as a severity notice; the sentence names the wizard step that fixes it. */
|
|
555
|
+
function FlowErrorNotice({ code, step, errKey, clientId }: { code: string; step: WizardStep | null; errKey: string; clientId: string }) {
|
|
556
|
+
const t = useT();
|
|
557
|
+
return (
|
|
558
|
+
<SettingsNotice tone="error">
|
|
559
|
+
<div data-testid="gmail-flow-error" data-step={step ?? ""}>
|
|
560
|
+
{t(errKey, { clientId }, ERROR_EN[errKey])}{" "}
|
|
561
|
+
<span className="text-[var(--severity-error-fg)]" data-testid="gmail-flow-error-code">
|
|
562
|
+
({code})
|
|
563
|
+
</span>
|
|
564
|
+
</div>
|
|
565
|
+
</SettingsNotice>
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
|
|
330
569
|
export function GmailSettings() {
|
|
331
570
|
const t = useT();
|
|
332
571
|
const OAuthFlow = useUiPrimitive("ui:oauth-flow");
|
|
@@ -336,6 +575,9 @@ export function GmailSettings() {
|
|
|
336
575
|
const [flowError, setFlowError] = useState<string | null>(null);
|
|
337
576
|
const [lastRevoke, setLastRevoke] = useState<string | null>(null);
|
|
338
577
|
const [newTier, setNewTier] = useState<Tier>("readonly");
|
|
578
|
+
// Latch (design D1): once the user reports a Google-page code, no late poll
|
|
579
|
+
// result may overwrite it or re-mount the flow view. Reset on a new flow.
|
|
580
|
+
const reportedRef = useRef<string | null>(null);
|
|
339
581
|
|
|
340
582
|
const refresh = useCallback(async () => {
|
|
341
583
|
try {
|
|
@@ -350,6 +592,7 @@ export function GmailSettings() {
|
|
|
350
592
|
}, [refresh]);
|
|
351
593
|
|
|
352
594
|
const startFlow = async (flowId: string) => {
|
|
595
|
+
reportedRef.current = null;
|
|
353
596
|
setFlowError(null);
|
|
354
597
|
try {
|
|
355
598
|
setFlow({ phase: "waiting", status: await oauthFlowClient.status(flowId) });
|
|
@@ -360,15 +603,29 @@ export function GmailSettings() {
|
|
|
360
603
|
};
|
|
361
604
|
useFlowPoll(
|
|
362
605
|
flow?.status?.flowId,
|
|
363
|
-
(status) =>
|
|
606
|
+
(status) => {
|
|
607
|
+
if (reportedRef.current) return;
|
|
608
|
+
setFlow({ phase: "waiting", status });
|
|
609
|
+
},
|
|
364
610
|
(err) => {
|
|
611
|
+
if (reportedRef.current) return;
|
|
365
612
|
setFlow(null);
|
|
366
613
|
setFlowError(err);
|
|
367
614
|
void refresh();
|
|
368
615
|
},
|
|
369
616
|
);
|
|
370
617
|
|
|
618
|
+
/** Order is load-bearing (design D1): latch, then clear, then cancel. */
|
|
619
|
+
const report = (code: string) => {
|
|
620
|
+
const flowId = flow?.status?.flowId;
|
|
621
|
+
reportedRef.current = code;
|
|
622
|
+
setFlow(null);
|
|
623
|
+
setFlowError(code);
|
|
624
|
+
if (flowId) void oauthFlowClient.cancel(flowId).catch(() => {});
|
|
625
|
+
};
|
|
626
|
+
|
|
371
627
|
const addAccount = async () => {
|
|
628
|
+
reportedRef.current = null;
|
|
372
629
|
setFlowError(null);
|
|
373
630
|
setFlow({ phase: "starting" });
|
|
374
631
|
try {
|
|
@@ -380,31 +637,53 @@ export function GmailSettings() {
|
|
|
380
637
|
}
|
|
381
638
|
};
|
|
382
639
|
|
|
383
|
-
if (loadError)
|
|
384
|
-
|
|
640
|
+
if (loadError) {
|
|
641
|
+
return (
|
|
642
|
+
<SettingsNotice tone="error" testId="gmail-load-error">
|
|
643
|
+
{loadError}
|
|
644
|
+
</SettingsNotice>
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
if (!state)
|
|
648
|
+
return (
|
|
649
|
+
<div data-testid="gmail-loading" className={muted}>
|
|
650
|
+
{t("loading", undefined, "Loading…")}
|
|
651
|
+
</div>
|
|
652
|
+
);
|
|
385
653
|
|
|
386
654
|
const step = errorStep(flowError);
|
|
655
|
+
const errKey = errorKey(flowError);
|
|
656
|
+
const { client } = state;
|
|
657
|
+
const summary = !client.configured
|
|
658
|
+
? t("summaryNotConfigured", undefined, "not configured")
|
|
659
|
+
: client.projectId
|
|
660
|
+
? t("summaryProject", { projectId: client.projectId }, `✓ project ${client.projectId}`)
|
|
661
|
+
: t("summaryClient", { clientId: client.clientId ?? "" }, `✓ ${client.clientId ?? ""}`);
|
|
387
662
|
return (
|
|
388
663
|
<section className="flex flex-col gap-3" data-testid="gmail-settings" aria-labelledby="gmail-settings-title">
|
|
389
|
-
<h3 id="gmail-settings-title" className="font-semibold text-[
|
|
664
|
+
<h3 id="gmail-settings-title" className="m-0 text-sm font-semibold text-[var(--text-primary)]">
|
|
390
665
|
{t("title", undefined, "Gmail")}
|
|
391
666
|
</h3>
|
|
392
|
-
<
|
|
393
|
-
|
|
667
|
+
<SettingsGroup
|
|
668
|
+
title={t("setup", undefined, "Google Cloud setup")}
|
|
669
|
+
meta={summary}
|
|
670
|
+
summaryTestId="gmail-setup-summary"
|
|
671
|
+
defaultOpen={!client.configured || state.accounts.length === 0 || step !== null}
|
|
672
|
+
>
|
|
394
673
|
<SetupWizard state={state} highlight={step} onUploaded={() => void refresh()} />
|
|
395
|
-
</
|
|
674
|
+
</SettingsGroup>
|
|
396
675
|
|
|
397
676
|
<div className="flex flex-col gap-2" data-testid="gmail-accounts">
|
|
398
|
-
<div className="font-
|
|
399
|
-
<p className={muted} data-testid="gmail-level-help">
|
|
677
|
+
<div className="text-sm font-semibold text-[var(--text-primary)]">{t("accounts", undefined, "Accounts")}</div>
|
|
678
|
+
<p className={`${muted} m-0`} data-testid="gmail-level-help">
|
|
400
679
|
{t(
|
|
401
680
|
"levelHelp",
|
|
402
681
|
undefined,
|
|
403
|
-
"Levels
|
|
682
|
+
"Levels are enforced by this plugin, not by Google: a token carries every scope Google granted (draft-level compose access could technically send), so a level limits what the agent's tools may do. Any session on this dashboard can use every account within its level.",
|
|
404
683
|
)}
|
|
405
684
|
</p>
|
|
406
685
|
{state.accounts.length === 0 && <div className={muted}>{t("noAccounts", undefined, "No accounts connected.")}</div>}
|
|
407
|
-
<ul className="flex flex-col gap-2">
|
|
686
|
+
<ul className="m-0 flex list-none flex-col gap-2 p-0">
|
|
408
687
|
{state.accounts.map((a) => (
|
|
409
688
|
<AccountRow
|
|
410
689
|
key={a.sub}
|
|
@@ -426,40 +705,47 @@ export function GmailSettings() {
|
|
|
426
705
|
))}
|
|
427
706
|
</ul>
|
|
428
707
|
{flow ? (
|
|
429
|
-
<
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
<
|
|
447
|
-
{
|
|
448
|
-
</
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
708
|
+
<div className="flex flex-col gap-2">
|
|
709
|
+
<p className={`${muted} m-0`} data-testid="gmail-consent-hint">
|
|
710
|
+
{t(
|
|
711
|
+
"consentHint",
|
|
712
|
+
undefined,
|
|
713
|
+
"On Google's consent screen tick every permission (Select all). The Gmail permission may start unticked.",
|
|
714
|
+
)}
|
|
715
|
+
</p>
|
|
716
|
+
<OAuthFlow
|
|
717
|
+
flow={flow}
|
|
718
|
+
onSendInput={(id, value) => oauthFlowClient.input(id, value)}
|
|
719
|
+
onCancel={(id) => {
|
|
720
|
+
void oauthFlowClient.cancel(id);
|
|
721
|
+
}}
|
|
722
|
+
/>
|
|
723
|
+
{flow.phase === "waiting" && (
|
|
724
|
+
<details data-testid="gmail-google-error">
|
|
725
|
+
<summary className="focus-ring cursor-pointer text-xs text-[var(--text-primary)]">
|
|
726
|
+
{t("googleErrorSummary", undefined, "Google showed an error instead of returning here?")}
|
|
727
|
+
</summary>
|
|
728
|
+
<div className={muted}>{t("googleErrorHelp", undefined, "Pick the error code Google displayed:")}</div>
|
|
729
|
+
<div className="mt-1 flex flex-wrap gap-2">
|
|
730
|
+
{REPORT_CODES.map((code) => (
|
|
731
|
+
<button
|
|
732
|
+
key={code}
|
|
733
|
+
type="button"
|
|
734
|
+
className={btn}
|
|
735
|
+
data-testid={`gmail-report-${code}`}
|
|
736
|
+
onClick={() => report(code)}
|
|
737
|
+
>
|
|
738
|
+
{code === "other" ? t("reportOther", undefined, "Something else") : code}
|
|
739
|
+
</button>
|
|
740
|
+
))}
|
|
741
|
+
</div>
|
|
742
|
+
</details>
|
|
743
|
+
)}
|
|
461
744
|
</div>
|
|
745
|
+
) : (
|
|
746
|
+
<AddAccount configured={client.configured} newTier={newTier} onTier={setNewTier} onAdd={() => void addAccount()} />
|
|
462
747
|
)}
|
|
748
|
+
{flowError && <FlowErrorNotice code={flowError} step={step} errKey={errKey} clientId={client.clientId ?? ""} />}
|
|
463
749
|
{lastRevoke && (
|
|
464
750
|
<div role="status" className={muted} data-testid="gmail-revoke-result">
|
|
465
751
|
{lastRevoke}
|