@blackbelt-technology/pi-dashboard-gmail-plugin 0.9.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/package.json +3 -3
- package/src/bridge/attachments.ts +4 -1
- package/src/bridge/gmail-api.ts +12 -4
- package/src/bridge/lease-client.ts +9 -2
- package/src/client/GmailSettings.tsx +258 -133
- package/src/i18n.ts +19 -3
- package/src/server/accounts.ts +13 -2
- package/src/server/google-oauth.ts +4 -1
- package/src/server/lease.ts +4 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blackbelt-technology/pi-dashboard-gmail-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Multi-account Gmail for pi agents — guided Google OAuth setup, per-account permission levels, server-side token leasing and account-scoped Gmail tools with confirmed writes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -61,8 +61,8 @@
|
|
|
61
61
|
"react-dom": ">=18.0.0"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@blackbelt-technology/dashboard-plugin-runtime": "^0.
|
|
65
|
-
"@blackbelt-technology/pi-dashboard-shared": "^0.
|
|
64
|
+
"@blackbelt-technology/dashboard-plugin-runtime": "^0.10.0",
|
|
65
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.10.0",
|
|
66
66
|
"@fastify/rate-limit": "^11.2.0",
|
|
67
67
|
"oauth4webapi": "^3.8.8"
|
|
68
68
|
},
|
|
@@ -17,7 +17,9 @@ import { isPathInside } from "@blackbelt-technology/pi-dashboard-shared/path-con
|
|
|
17
17
|
export const MAX_ATTACH_BYTES = 20 * 1024 * 1024;
|
|
18
18
|
|
|
19
19
|
class PathRefusedError extends Error {
|
|
20
|
-
|
|
20
|
+
readonly code: "path_refused" | "exists" | "too_large";
|
|
21
|
+
|
|
22
|
+
constructor(code: "path_refused" | "exists" | "too_large") {
|
|
21
23
|
super(
|
|
22
24
|
code === "exists"
|
|
23
25
|
? "path_refused: target already exists (attachments never overwrite)"
|
|
@@ -25,6 +27,7 @@ class PathRefusedError extends Error {
|
|
|
25
27
|
? "path_refused: file exceeds the 20 MiB attachment limit"
|
|
26
28
|
: "path_refused: path must stay inside the session working directory",
|
|
27
29
|
);
|
|
30
|
+
this.code = code;
|
|
28
31
|
this.name = "PathRefusedError";
|
|
29
32
|
}
|
|
30
33
|
}
|
package/src/bridge/gmail-api.ts
CHANGED
|
@@ -34,11 +34,19 @@ export interface GmailMessage {
|
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export class GmailApi {
|
|
37
|
+
private readonly base: string;
|
|
38
|
+
private readonly token: string;
|
|
39
|
+
private readonly fetchImpl: typeof fetch;
|
|
40
|
+
|
|
37
41
|
constructor(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
) {
|
|
42
|
+
base: string,
|
|
43
|
+
token: string,
|
|
44
|
+
fetchImpl: typeof fetch = fetch,
|
|
45
|
+
) {
|
|
46
|
+
this.base = base;
|
|
47
|
+
this.token = token;
|
|
48
|
+
this.fetchImpl = fetchImpl;
|
|
49
|
+
}
|
|
42
50
|
|
|
43
51
|
private async call<T>(method: string, pathAndQuery: string, body?: unknown): Promise<T> {
|
|
44
52
|
let res: Response;
|
|
@@ -15,11 +15,14 @@ import { isTier, type Op, tierAllows } from "../shared/scopes.js";
|
|
|
15
15
|
export type LaneRequest = (pluginId: string, type: string, payload?: unknown) => Promise<PluginLaneReply>;
|
|
16
16
|
|
|
17
17
|
export class GmailToolError extends Error {
|
|
18
|
+
readonly code: string;
|
|
19
|
+
|
|
18
20
|
constructor(
|
|
19
|
-
|
|
21
|
+
code: string,
|
|
20
22
|
message: string,
|
|
21
23
|
) {
|
|
22
24
|
super(message);
|
|
25
|
+
this.code = code;
|
|
23
26
|
this.name = "GmailToolError";
|
|
24
27
|
}
|
|
25
28
|
}
|
|
@@ -34,7 +37,11 @@ function laneError(error: string): GmailToolError {
|
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
export class LeaseClient {
|
|
37
|
-
|
|
40
|
+
private readonly request: LaneRequest;
|
|
41
|
+
|
|
42
|
+
constructor(request: LaneRequest = requestPluginServer) {
|
|
43
|
+
this.request = request;
|
|
44
|
+
}
|
|
38
45
|
|
|
39
46
|
async lease(account: string, op: Op): Promise<LeaseReply> {
|
|
40
47
|
const reply = await this.request(PLUGIN_ID, LEASE_TYPE, { account, op });
|
|
@@ -12,8 +12,24 @@
|
|
|
12
12
|
* overwrite it or re-mount the flow); every flow error rendered as a sentence
|
|
13
13
|
* (`errorKey` / `ERROR_EN`); Revoke via `ui:confirm-dialog`; alias feedback;
|
|
14
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.
|
|
15
21
|
*/
|
|
16
|
-
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";
|
|
17
33
|
import type React from "react";
|
|
18
34
|
import { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
19
35
|
import { validateClientJson } from "../shared/client-json.js";
|
|
@@ -41,16 +57,20 @@ async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
|
|
41
57
|
return body;
|
|
42
58
|
}
|
|
43
59
|
|
|
60
|
+
// Secondary button recipe (`ui-contract.md`); `tap-target` = 44 px touch, 32 px from sm.
|
|
44
61
|
const btn =
|
|
45
|
-
"focus-ring rounded border border-[var(--border-
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
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)]";
|
|
49
70
|
// Static strings: Tailwind cannot see interpolated class names.
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
const badgeReauth =
|
|
53
|
-
"rounded border border-[var(--severity-warning-border)] bg-[var(--severity-warning-bg)] px-1 text-[11px] text-[var(--severity-warning-fg)]";
|
|
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)]`;
|
|
54
74
|
|
|
55
75
|
/** Poll a flow every 500 ms until terminal. */
|
|
56
76
|
function useFlowPoll(flowId: string | undefined, onPending: (s: FlowStatus) => void, onDone: (err: string | null) => void) {
|
|
@@ -82,11 +102,24 @@ function useFlowPoll(flowId: string | undefined, onPending: (s: FlowStatus) => v
|
|
|
82
102
|
|
|
83
103
|
function Copyable({ value }: { value: string }) {
|
|
84
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");
|
|
85
118
|
return (
|
|
86
119
|
<div className="flex items-center gap-2">
|
|
87
|
-
<code className="flex-1 overflow-x-auto rounded bg-[var(--bg-tertiary)] px-2 py-1 text-[12px]">{value}</code>
|
|
88
|
-
<button type="button" className={btn} onClick={() => void
|
|
89
|
-
|
|
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>
|
|
90
123
|
</button>
|
|
91
124
|
</div>
|
|
92
125
|
);
|
|
@@ -94,12 +127,55 @@ function Copyable({ value }: { value: string }) {
|
|
|
94
127
|
|
|
95
128
|
function StepLink({ href, label }: { href: string; label: string }) {
|
|
96
129
|
return (
|
|
97
|
-
<a href={href} target="_blank" rel="noopener noreferrer" className="focus-ring text-
|
|
130
|
+
<a href={href} target="_blank" rel="noopener noreferrer" className="focus-ring text-xs text-[var(--accent-text)] underline">
|
|
98
131
|
{label}
|
|
99
132
|
</a>
|
|
100
133
|
);
|
|
101
134
|
}
|
|
102
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
|
+
|
|
103
179
|
export function SetupWizard({
|
|
104
180
|
state,
|
|
105
181
|
highlight,
|
|
@@ -135,8 +211,11 @@ export function SetupWizard({
|
|
|
135
211
|
}
|
|
136
212
|
};
|
|
137
213
|
|
|
138
|
-
|
|
139
|
-
|
|
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);
|
|
140
219
|
const uploadMsg: Record<string, string> = {
|
|
141
220
|
not_json: t("errNotJson", undefined, "That file is not valid JSON. Upload the client_secret_*.json you downloaded (step 5)."),
|
|
142
221
|
web_client: t("errWebClient", undefined, "This is a Web application client. Create a Desktop app client instead (step 4)."),
|
|
@@ -151,19 +230,17 @@ export function SetupWizard({
|
|
|
151
230
|
};
|
|
152
231
|
|
|
153
232
|
return (
|
|
154
|
-
<ol className="flex flex-col
|
|
155
|
-
<
|
|
156
|
-
<
|
|
157
|
-
|
|
158
|
-
{
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
/>
|
|
166
|
-
</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
|
+
/>
|
|
167
244
|
<div className={muted}>{t("withoutGcloud", undefined, "Without gcloud:")}</div>
|
|
168
245
|
<div className="flex gap-3">
|
|
169
246
|
<StepLink href={links.projectCreate} label={t("createProject", undefined, "Create project")} />
|
|
@@ -172,14 +249,12 @@ export function SetupWizard({
|
|
|
172
249
|
<div className={muted}>{t("withGcloud", undefined, "With gcloud (run it yourself):")}</div>
|
|
173
250
|
<Copyable value={cmdCreate as string} />
|
|
174
251
|
<Copyable value={cmdEnable as string} />
|
|
175
|
-
</
|
|
176
|
-
<
|
|
177
|
-
<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)}>
|
|
178
254
|
<StepLink href={links.branding} label={t("openBranding", undefined, "Open Branding")} />
|
|
179
255
|
<div className={muted}>{t("fallback2", undefined, "If the link moved: APIs & Services → OAuth consent screen.")}</div>
|
|
180
|
-
</
|
|
181
|
-
<
|
|
182
|
-
<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)}>
|
|
183
258
|
<StepLink href={links.audience} label={t("openAudience", undefined, "Open Audience")} />
|
|
184
259
|
<div className={muted}>
|
|
185
260
|
{t(
|
|
@@ -195,42 +270,46 @@ export function SetupWizard({
|
|
|
195
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.",
|
|
196
271
|
)}
|
|
197
272
|
</div>
|
|
198
|
-
</
|
|
199
|
-
<
|
|
200
|
-
<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)}>
|
|
201
275
|
<StepLink href={links.clientCreate} label={t("openClients", undefined, "Create client")} />
|
|
202
276
|
<div className={muted}>
|
|
203
277
|
{t("clientHelp", undefined, "Application type: Desktop app. Download the JSON. If the link moved: APIs & Services → Credentials.")}
|
|
204
278
|
</div>
|
|
205
|
-
</
|
|
206
|
-
<
|
|
207
|
-
<
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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
|
+
)}
|
|
218
303
|
/>
|
|
219
304
|
{state.client.configured && (
|
|
220
305
|
<div className={muted} data-testid="gmail-client-ok">
|
|
221
306
|
✓ {state.client.clientId}
|
|
222
307
|
</div>
|
|
223
308
|
)}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
{uploadMsg[uploadError.code] ?? uploadError.code}
|
|
227
|
-
</div>
|
|
228
|
-
)}
|
|
229
|
-
</li>
|
|
230
|
-
<li className={stepClass(6)} data-testid="gmail-step-6">
|
|
231
|
-
<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)}>
|
|
232
311
|
<div className={muted}>{t("step6Help", undefined, "Add your first account below — that is the test sign-in.")}</div>
|
|
233
|
-
</
|
|
312
|
+
</WizardStepItem>
|
|
234
313
|
</ol>
|
|
235
314
|
);
|
|
236
315
|
}
|
|
@@ -242,18 +321,10 @@ const TIER_DESC: Record<Tier, [key: string, en: string]> = {
|
|
|
242
321
|
send: ["tierSend", "read, draft, send, reply, labels, archive, trash"],
|
|
243
322
|
};
|
|
244
323
|
|
|
245
|
-
/**
|
|
246
|
-
function
|
|
324
|
+
/** Each level's own description, as a select option list. */
|
|
325
|
+
function useTierOptions(): { value: string; label: string }[] {
|
|
247
326
|
const t = useT();
|
|
248
|
-
return (
|
|
249
|
-
<>
|
|
250
|
-
{TIERS.map((tier) => (
|
|
251
|
-
<option key={tier} value={tier}>
|
|
252
|
-
{`${TIER_LABEL[tier]} — ${t(TIER_DESC[tier][0], undefined, TIER_DESC[tier][1])}`}
|
|
253
|
-
</option>
|
|
254
|
-
))}
|
|
255
|
-
</>
|
|
256
|
-
);
|
|
327
|
+
return TIERS.map((tier) => ({ value: tier, label: `${TIER_LABEL[tier]} — ${t(TIER_DESC[tier][0], undefined, TIER_DESC[tier][1])}` }));
|
|
257
328
|
}
|
|
258
329
|
|
|
259
330
|
/** Codes the user can report from Google's own error page (closed list, design D1). */
|
|
@@ -272,6 +343,7 @@ function AccountRow({
|
|
|
272
343
|
}) {
|
|
273
344
|
const t = useT();
|
|
274
345
|
const ConfirmDialog = useUiPrimitive("ui:confirm-dialog");
|
|
346
|
+
const tierOptions = useTierOptions();
|
|
275
347
|
const [alias, setAlias] = useState(acct.alias ?? "");
|
|
276
348
|
const [msg, setMsg] = useState<string | null>(null);
|
|
277
349
|
const [aliasMsg, setAliasMsg] = useState<{ ok: boolean; text: string } | null>(null);
|
|
@@ -317,21 +389,21 @@ function AccountRow({
|
|
|
317
389
|
});
|
|
318
390
|
return (
|
|
319
391
|
<li
|
|
320
|
-
className="flex flex-col gap-
|
|
392
|
+
className="flex flex-col gap-2 rounded-md border border-[var(--border-secondary)] p-2"
|
|
321
393
|
data-testid="gmail-account-row"
|
|
322
394
|
data-email={acct.email}
|
|
323
395
|
>
|
|
324
396
|
<div className="flex flex-wrap items-center gap-2">
|
|
325
|
-
<span className="font-
|
|
397
|
+
<span className="text-sm font-semibold text-[var(--text-primary)]" data-testid="gmail-account-email">
|
|
326
398
|
{acct.email}
|
|
327
399
|
</span>
|
|
328
400
|
{acct.status === "ok" ? (
|
|
329
401
|
<span className={badgeOk} data-testid="gmail-account-status" data-status="ok">
|
|
330
|
-
{t("statusOk", undefined, "
|
|
402
|
+
{t("statusOk", undefined, "Connected")}
|
|
331
403
|
</span>
|
|
332
404
|
) : (
|
|
333
405
|
<span className={badgeReauth} data-testid="gmail-account-status" data-status="reauth_required">
|
|
334
|
-
{t("statusReauth", undefined, "
|
|
406
|
+
{t("statusReauth", undefined, "Re-auth needed")}
|
|
335
407
|
</span>
|
|
336
408
|
)}
|
|
337
409
|
{acct.testingHint && (
|
|
@@ -340,9 +412,9 @@ function AccountRow({
|
|
|
340
412
|
</span>
|
|
341
413
|
)}
|
|
342
414
|
<input
|
|
343
|
-
aria-label={t("
|
|
415
|
+
aria-label={t("aliasFor", { email: acct.email }, `Alias for ${acct.email}`)}
|
|
344
416
|
data-testid="gmail-account-alias"
|
|
345
|
-
className={`${field} w-
|
|
417
|
+
className={`${field} w-28`}
|
|
346
418
|
value={alias}
|
|
347
419
|
placeholder={t("aliasPlaceholder", undefined, "alias")}
|
|
348
420
|
onChange={(e) => {
|
|
@@ -365,18 +437,24 @@ function AccountRow({
|
|
|
365
437
|
<label className={muted}>
|
|
366
438
|
{t("level", undefined, "Level")}{" "}
|
|
367
439
|
<select
|
|
440
|
+
aria-label={t("levelFor", { email: acct.email }, `Level for ${acct.email}`)}
|
|
368
441
|
data-testid="gmail-account-level"
|
|
369
442
|
value={acct.tier}
|
|
370
443
|
onChange={(e) => void setLevel(e.target.value as Tier)}
|
|
371
444
|
className={field}
|
|
372
445
|
>
|
|
373
|
-
|
|
446
|
+
{tierOptions.map((o) => (
|
|
447
|
+
<option key={o.value} value={o.value}>
|
|
448
|
+
{o.label}
|
|
449
|
+
</option>
|
|
450
|
+
))}
|
|
374
451
|
</select>
|
|
375
452
|
</label>
|
|
376
453
|
<button
|
|
377
454
|
type="button"
|
|
378
455
|
className={btn}
|
|
379
456
|
data-testid="gmail-account-reauth"
|
|
457
|
+
aria-label={t("reauthFor", { email: acct.email }, `Re-authenticate ${acct.email}`)}
|
|
380
458
|
onClick={() =>
|
|
381
459
|
void run(async () => {
|
|
382
460
|
// Force a fresh consent at the current level (status reset on persist).
|
|
@@ -387,7 +465,13 @@ function AccountRow({
|
|
|
387
465
|
>
|
|
388
466
|
{t("reauth", undefined, "Re-authenticate")}
|
|
389
467
|
</button>
|
|
390
|
-
<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
|
+
>
|
|
391
475
|
{t("revoke", undefined, "Revoke")}
|
|
392
476
|
</button>
|
|
393
477
|
{msg && (
|
|
@@ -415,10 +499,76 @@ function AccountRow({
|
|
|
415
499
|
);
|
|
416
500
|
}
|
|
417
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
|
+
|
|
418
569
|
export function GmailSettings() {
|
|
419
570
|
const t = useT();
|
|
420
571
|
const OAuthFlow = useUiPrimitive("ui:oauth-flow");
|
|
421
|
-
const newLevelId = useId();
|
|
422
572
|
const [state, setState] = useState<GmailState | null>(null);
|
|
423
573
|
const [loadError, setLoadError] = useState<string | null>(null);
|
|
424
574
|
const [flow, setFlow] = useState<FlowState | null>(null);
|
|
@@ -487,8 +637,19 @@ export function GmailSettings() {
|
|
|
487
637
|
}
|
|
488
638
|
};
|
|
489
639
|
|
|
490
|
-
if (loadError)
|
|
491
|
-
|
|
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
|
+
);
|
|
492
653
|
|
|
493
654
|
const step = errorStep(flowError);
|
|
494
655
|
const errKey = errorKey(flowError);
|
|
@@ -500,19 +661,21 @@ export function GmailSettings() {
|
|
|
500
661
|
: t("summaryClient", { clientId: client.clientId ?? "" }, `✓ ${client.clientId ?? ""}`);
|
|
501
662
|
return (
|
|
502
663
|
<section className="flex flex-col gap-3" data-testid="gmail-settings" aria-labelledby="gmail-settings-title">
|
|
503
|
-
<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)]">
|
|
504
665
|
{t("title", undefined, "Gmail")}
|
|
505
666
|
</h3>
|
|
506
|
-
<
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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
|
+
>
|
|
510
673
|
<SetupWizard state={state} highlight={step} onUploaded={() => void refresh()} />
|
|
511
|
-
</
|
|
674
|
+
</SettingsGroup>
|
|
512
675
|
|
|
513
676
|
<div className="flex flex-col gap-2" data-testid="gmail-accounts">
|
|
514
|
-
<div className="font-
|
|
515
|
-
<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">
|
|
516
679
|
{t(
|
|
517
680
|
"levelHelp",
|
|
518
681
|
undefined,
|
|
@@ -520,7 +683,7 @@ export function GmailSettings() {
|
|
|
520
683
|
)}
|
|
521
684
|
</p>
|
|
522
685
|
{state.accounts.length === 0 && <div className={muted}>{t("noAccounts", undefined, "No accounts connected.")}</div>}
|
|
523
|
-
<ul className="flex flex-col gap-2">
|
|
686
|
+
<ul className="m-0 flex list-none flex-col gap-2 p-0">
|
|
524
687
|
{state.accounts.map((a) => (
|
|
525
688
|
<AccountRow
|
|
526
689
|
key={a.sub}
|
|
@@ -543,7 +706,7 @@ export function GmailSettings() {
|
|
|
543
706
|
</ul>
|
|
544
707
|
{flow ? (
|
|
545
708
|
<div className="flex flex-col gap-2">
|
|
546
|
-
<p className={muted} data-testid="gmail-consent-hint">
|
|
709
|
+
<p className={`${muted} m-0`} data-testid="gmail-consent-hint">
|
|
547
710
|
{t(
|
|
548
711
|
"consentHint",
|
|
549
712
|
undefined,
|
|
@@ -559,11 +722,11 @@ export function GmailSettings() {
|
|
|
559
722
|
/>
|
|
560
723
|
{flow.phase === "waiting" && (
|
|
561
724
|
<details data-testid="gmail-google-error">
|
|
562
|
-
<summary className="focus-ring cursor-pointer text-[
|
|
725
|
+
<summary className="focus-ring cursor-pointer text-xs text-[var(--text-primary)]">
|
|
563
726
|
{t("googleErrorSummary", undefined, "Google showed an error instead of returning here?")}
|
|
564
727
|
</summary>
|
|
565
728
|
<div className={muted}>{t("googleErrorHelp", undefined, "Pick the error code Google displayed:")}</div>
|
|
566
|
-
<div className="flex flex-wrap gap-2">
|
|
729
|
+
<div className="mt-1 flex flex-wrap gap-2">
|
|
567
730
|
{REPORT_CODES.map((code) => (
|
|
568
731
|
<button
|
|
569
732
|
key={code}
|
|
@@ -580,47 +743,9 @@ export function GmailSettings() {
|
|
|
580
743
|
)}
|
|
581
744
|
</div>
|
|
582
745
|
) : (
|
|
583
|
-
<
|
|
584
|
-
<div className="flex items-center gap-2">
|
|
585
|
-
<label htmlFor={newLevelId} className={muted}>
|
|
586
|
-
{t("newLevel", undefined, "Level for the new account")}
|
|
587
|
-
</label>
|
|
588
|
-
<select
|
|
589
|
-
id={newLevelId}
|
|
590
|
-
data-testid="gmail-new-level"
|
|
591
|
-
value={newTier}
|
|
592
|
-
onChange={(e) => setNewTier(e.target.value as Tier)}
|
|
593
|
-
className={field}
|
|
594
|
-
>
|
|
595
|
-
<TierOptions />
|
|
596
|
-
</select>
|
|
597
|
-
<button
|
|
598
|
-
type="button"
|
|
599
|
-
className={btn}
|
|
600
|
-
data-testid="gmail-add-account"
|
|
601
|
-
disabled={!client.configured}
|
|
602
|
-
onClick={() => void addAccount()}
|
|
603
|
-
>
|
|
604
|
-
{t("addAccount", undefined, "Add account")}
|
|
605
|
-
</button>
|
|
606
|
-
</div>
|
|
607
|
-
<p className={muted} data-testid="gmail-cross-org-hint">
|
|
608
|
-
{t(
|
|
609
|
-
"crossOrgHint",
|
|
610
|
-
undefined,
|
|
611
|
-
"Accounts outside the project's Workspace organization need an External audience, with each account added as a test user (step 3).",
|
|
612
|
-
)}
|
|
613
|
-
</p>
|
|
614
|
-
</div>
|
|
615
|
-
)}
|
|
616
|
-
{flowError && (
|
|
617
|
-
<div role="alert" className={errorText} data-testid="gmail-flow-error" data-step={step ?? ""}>
|
|
618
|
-
{t(errKey, { clientId: client.clientId ?? "" }, ERROR_EN[errKey])}{" "}
|
|
619
|
-
<span className={muted} data-testid="gmail-flow-error-code">
|
|
620
|
-
({flowError})
|
|
621
|
-
</span>
|
|
622
|
-
</div>
|
|
746
|
+
<AddAccount configured={client.configured} newTier={newTier} onTier={setNewTier} onAdd={() => void addAccount()} />
|
|
623
747
|
)}
|
|
748
|
+
{flowError && <FlowErrorNotice code={flowError} step={step} errKey={errKey} clientId={client.clientId ?? ""} />}
|
|
624
749
|
{lastRevoke && (
|
|
625
750
|
<div role="status" className={muted} data-testid="gmail-revoke-result">
|
|
626
751
|
{lastRevoke}
|
package/src/i18n.ts
CHANGED
|
@@ -17,7 +17,9 @@ export const catalog = {
|
|
|
17
17
|
"“内部”仅允许项目所属 Workspace 组织的账户:只有当你要连接的每个账户都属于该组织时才选择它。否则选择“外部”,并将要连接的每个地址添加为测试用户。测试模式下令牌 7 天后过期;“发布应用”可获得更长期的令牌,但会显示未验证应用警告。",
|
|
18
18
|
clientHelp: "应用类型:桌面应用。下载 JSON。若链接已变更:API 和服务 → 凭据。",
|
|
19
19
|
consentHint: "在 Google 同意屏幕上勾选每一项权限(全选)。Gmail 权限可能默认未勾选。",
|
|
20
|
+
copied: "已复制",
|
|
20
21
|
copy: "复制",
|
|
22
|
+
copyFailed: "复制失败",
|
|
21
23
|
createProject: "创建项目",
|
|
22
24
|
crossOrgHint: "项目 Workspace 组织之外的账户需要“外部”目标对象,并将每个账户添加为测试用户(第 3 步)。",
|
|
23
25
|
enableGmail: "启用 Gmail API",
|
|
@@ -70,8 +72,14 @@ export const catalog = {
|
|
|
70
72
|
revokedOk: "{email} 已移除,并已在 Google 撤销访问权限。",
|
|
71
73
|
revokeManual: "{email} 已在本地移除,但无法连接 Google。请在 myaccount.google.com/permissions 撤销访问权限。",
|
|
72
74
|
setup: "Google Cloud 设置",
|
|
73
|
-
statusOk: "
|
|
75
|
+
statusOk: "已连接",
|
|
74
76
|
statusReauth: "需要重新验证",
|
|
77
|
+
stepDone: "已完成",
|
|
78
|
+
aliasFor: "{email} 的别名",
|
|
79
|
+
levelFor: "{email} 的级别",
|
|
80
|
+
revokeFor: "撤销 {email}",
|
|
81
|
+
reauthFor: "重新验证 {email}",
|
|
82
|
+
addAccountDisabled: "上传 OAuth 客户端 JSON(上面第 5 步)后即可使用。",
|
|
75
83
|
step1: "1. Google Cloud 项目 + Gmail API",
|
|
76
84
|
step2: "2. 品牌",
|
|
77
85
|
step3: "3. 目标对象 + 测试用户",
|
|
@@ -106,7 +114,9 @@ export const catalog = {
|
|
|
106
114
|
"Alkalmazástípus: Asztali alkalmazás. Töltsd le a JSON-t. Ha a link elköltözött: API-k és szolgáltatások → Hitelesítő adatok.",
|
|
107
115
|
consentHint:
|
|
108
116
|
"A Google hozzájárulási képernyőjén pipálj be minden engedélyt (Összes kijelölése). A Gmail-engedély kezdetben kipipálatlan lehet.",
|
|
117
|
+
copied: "Másolva",
|
|
109
118
|
copy: "Másolás",
|
|
119
|
+
copyFailed: "A másolás nem sikerült",
|
|
110
120
|
createProject: "Projekt létrehozása",
|
|
111
121
|
crossOrgHint:
|
|
112
122
|
"A projekt Workspace-szervezetén kívüli fiókokhoz Külső célközönség kell, minden fiókot tesztfelhasználóként felvéve (3. lépés).",
|
|
@@ -176,8 +186,14 @@ export const catalog = {
|
|
|
176
186
|
revokeManual:
|
|
177
187
|
"{email} helyben eltávolítva, de a Google nem volt elérhető. Vond vissza a hozzáférést a myaccount.google.com/permissions oldalon.",
|
|
178
188
|
setup: "Google Cloud beállítás",
|
|
179
|
-
statusOk: "
|
|
180
|
-
statusReauth: "
|
|
189
|
+
statusOk: "Csatlakoztatva",
|
|
190
|
+
statusReauth: "Újrahitelesítés szükséges",
|
|
191
|
+
stepDone: "kész",
|
|
192
|
+
aliasFor: "{email} aliasa",
|
|
193
|
+
levelFor: "{email} szintje",
|
|
194
|
+
revokeFor: "{email} visszavonása",
|
|
195
|
+
reauthFor: "{email} újrahitelesítése",
|
|
196
|
+
addAccountDisabled: "Töltse fel az OAuth-kliens JSON-t (fent az 5. lépés), hogy ez elérhető legyen.",
|
|
181
197
|
step1: "1. Google Cloud projekt + Gmail API",
|
|
182
198
|
step2: "2. Arculat",
|
|
183
199
|
step3: "3. Célközönség + tesztfelhasználók",
|
package/src/server/accounts.ts
CHANGED
|
@@ -48,8 +48,15 @@ export interface AccountSummary {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export class AccountError extends Error {
|
|
51
|
+
readonly code: | "not_found"
|
|
52
|
+
| "ambiguous"
|
|
53
|
+
| "alias_taken"
|
|
54
|
+
| "invalid_alias"
|
|
55
|
+
| "missing_refresh"
|
|
56
|
+
| "no_client";
|
|
57
|
+
|
|
51
58
|
constructor(
|
|
52
|
-
|
|
59
|
+
code:
|
|
53
60
|
| "not_found"
|
|
54
61
|
| "ambiguous"
|
|
55
62
|
| "alias_taken"
|
|
@@ -59,6 +66,7 @@ export class AccountError extends Error {
|
|
|
59
66
|
message: string,
|
|
60
67
|
) {
|
|
61
68
|
super(message);
|
|
69
|
+
this.code = code;
|
|
62
70
|
this.name = "AccountError";
|
|
63
71
|
}
|
|
64
72
|
}
|
|
@@ -88,10 +96,13 @@ function labels(accounts: AccountRecord[]): string {
|
|
|
88
96
|
}
|
|
89
97
|
|
|
90
98
|
export class AccountStore {
|
|
99
|
+
private readonly creds: PluginCredentials;
|
|
91
100
|
/** Serializes alias check+write: the store's `update()` is per-key, uniqueness is cross-key. */
|
|
92
101
|
private aliasChain: Promise<unknown> = Promise.resolve();
|
|
93
102
|
|
|
94
|
-
constructor(
|
|
103
|
+
constructor(creds: PluginCredentials) {
|
|
104
|
+
this.creds = creds;
|
|
105
|
+
}
|
|
95
106
|
|
|
96
107
|
async getClient(): Promise<ClientRecord | undefined> {
|
|
97
108
|
const rec = await this.creds.get(CLIENT_KEY);
|
|
@@ -26,8 +26,11 @@ import type { ClientRecord } from "./accounts.js";
|
|
|
26
26
|
|
|
27
27
|
/** Fixed, input-free error codes a flow can fail with (the wizard maps them to steps). */
|
|
28
28
|
export class GmailFlowError extends Error {
|
|
29
|
-
|
|
29
|
+
readonly code: string;
|
|
30
|
+
|
|
31
|
+
constructor(code: string) {
|
|
30
32
|
super(code);
|
|
33
|
+
this.code = code;
|
|
31
34
|
this.name = "GmailFlowError";
|
|
32
35
|
}
|
|
33
36
|
}
|
package/src/server/lease.ts
CHANGED
|
@@ -38,11 +38,14 @@ type LeaseErrorCode =
|
|
|
38
38
|
| "refresh_failed";
|
|
39
39
|
|
|
40
40
|
class LeaseError extends Error {
|
|
41
|
+
readonly code: LeaseErrorCode;
|
|
42
|
+
|
|
41
43
|
constructor(
|
|
42
|
-
|
|
44
|
+
code: LeaseErrorCode,
|
|
43
45
|
detail: string,
|
|
44
46
|
) {
|
|
45
47
|
super(`${code}: ${detail}`);
|
|
48
|
+
this.code = code;
|
|
46
49
|
this.name = "LeaseError";
|
|
47
50
|
}
|
|
48
51
|
}
|