@1agh/maude 2.1.1 → 2.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.
Files changed (70) hide show
  1. package/apps/studio/actions/agents.ts +18 -42
  2. package/apps/studio/actions/verbs.ts +49 -0
  3. package/apps/studio/actions.manifest.json +103 -19
  4. package/apps/studio/ai/create-claim.ts +1 -1
  5. package/apps/studio/bin/_asset-file.mjs +117 -0
  6. package/apps/studio/bin/_canvas-file.mjs +146 -0
  7. package/apps/studio/bin/_footage.mjs +123 -0
  8. package/apps/studio/bin/_pdf-playwright.mjs +14 -0
  9. package/apps/studio/bin/_png-playwright.mjs +15 -1
  10. package/apps/studio/bin/_pw-launch.mjs +98 -0
  11. package/apps/studio/bin/_retry-save.mjs +49 -0
  12. package/apps/studio/bin/_sync.mjs +145 -2
  13. package/apps/studio/bin/asset-rename.sh +12 -0
  14. package/apps/studio/bin/asset-trash.sh +12 -0
  15. package/apps/studio/bin/canvas-move.sh +12 -0
  16. package/apps/studio/bin/canvas-new.sh +12 -0
  17. package/apps/studio/bin/footage.sh +12 -0
  18. package/apps/studio/bin/generate.sh +7 -0
  19. package/apps/studio/bin/retry-save.sh +13 -0
  20. package/apps/studio/bridge/canvas-mode-store.ts +11 -2
  21. package/apps/studio/bridge/dgn-protocol.ts +8 -3
  22. package/apps/studio/bridge/occlusion.ts +45 -0
  23. package/apps/studio/canvas-theme-pill.tsx +169 -26
  24. package/apps/studio/client/export-center.jsx +32 -3
  25. package/apps/studio/client/home/HomeAccountBanner.jsx +61 -0
  26. package/apps/studio/client/home/HomeAccountsChip.jsx +404 -0
  27. package/apps/studio/client/home/HomeConnection.jsx +4 -3
  28. package/apps/studio/client/home/ProjectRows.jsx +514 -0
  29. package/apps/studio/client/home/home-accounts-model.js +148 -99
  30. package/apps/studio/client/home/home-api.js +4 -0
  31. package/apps/studio/client/home/home-app.jsx +127 -432
  32. package/apps/studio/client/home/home-icons.jsx +8 -0
  33. package/apps/studio/client/home/home-model.js +109 -0
  34. package/apps/studio/client/home/home.css +447 -111
  35. package/apps/studio/client/home/use-account-fix.js +123 -0
  36. package/apps/studio/client/v2/assets/generate-ask.jsx +3 -1
  37. package/apps/studio/client/v2/export/done-toast.jsx +2 -5
  38. package/apps/studio/client/v2/export/export-sheet.jsx +9 -2
  39. package/apps/studio/client/v2/export/export.css +9 -0
  40. package/apps/studio/client/v2/export/exports-model.js +16 -3
  41. package/apps/studio/client/v2/export/file-rows.jsx +81 -5
  42. package/apps/studio/client/v2/export/native-save.js +33 -0
  43. package/apps/studio/client/v2/export/sections/print-copy.js +3 -0
  44. package/apps/studio/client/v2/export/sections/print.jsx +37 -20
  45. package/apps/studio/client/v2/shell/occlusion-insets.js +37 -9
  46. package/apps/studio/client/v2/shell/use-occluded-insets.js +13 -3
  47. package/apps/studio/collab/room.ts +21 -7
  48. package/apps/studio/dist/client.bundle.js +692 -692
  49. package/apps/studio/dist/comment-mount.js +2 -2
  50. package/apps/studio/dist/styles.css +1 -1
  51. package/apps/studio/exporters/ghostscript.ts +78 -1
  52. package/apps/studio/exporters/pdf-fonts.ts +31 -4
  53. package/apps/studio/generation/jobs.ts +6 -0
  54. package/apps/studio/http.ts +6 -2
  55. package/apps/studio/routes/agent.ts +6 -2
  56. package/apps/studio/routes/project-undo.ts +10 -0
  57. package/apps/studio/server.ts +25 -0
  58. package/apps/studio/shell/cli-yield.ts +48 -11
  59. package/apps/studio/whats-new.json +18 -0
  60. package/cli/commands/design.mjs +74 -16
  61. package/cli/lib/bash-guard.mjs +4 -4
  62. package/cli/lib/confine-read.mjs +2 -0
  63. package/cli/lib/design-help.gen.mjs +15 -2
  64. package/cli/lib/design-hook.mjs +1 -1
  65. package/cli/lib/desktop-locate.mjs +36 -8
  66. package/cli/lib/studio-locate.mjs +8 -2
  67. package/cli/lib/sub-verbs.mjs +13 -1
  68. package/package.json +8 -8
  69. package/apps/studio/client/home/HomeAccounts.jsx +0 -262
  70. package/apps/studio/client/home/HomeAccountsRow.jsx +0 -221
@@ -37,7 +37,7 @@
37
37
  import { createHash, createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
38
38
  import { lstatSync, readFileSync, realpathSync } from 'node:fs';
39
39
  import { createConnection } from 'node:net';
40
- import { homedir } from 'node:os';
40
+ import { homedir, userInfo } from 'node:os';
41
41
  import { dirname, isAbsolute, join, resolve } from 'node:path';
42
42
  import { fileURLToPath } from 'node:url';
43
43
 
@@ -115,8 +115,26 @@ function real(p) {
115
115
 
116
116
  const underTest = (e) => !!e && (!!e.NODE_TEST_CONTEXT || e.NODE_ENV === 'test');
117
117
 
118
+ /**
119
+ * #218 — the home the app's files sit under. `$HOME` when it is an absolute path (a throwaway-HOME
120
+ * harness keeps its own); anything else is not a home — an AI chat's shell once assigned a prompt
121
+ * fragment to a variable named HOME, every helper then found no app and sent no studio key — so the
122
+ * account's own home (the passwd entry, which ignores `$HOME`) stands in. Never throws.
123
+ */
124
+ export function homeOf(env = process.env) {
125
+ const h = env.HOME;
126
+ if (h && isAbsolute(h)) return h;
127
+ try {
128
+ const u = userInfo().homedir;
129
+ if (u && isAbsolute(u)) return u;
130
+ } catch {
131
+ /* no passwd entry: the platform default below */
132
+ }
133
+ return homedir();
134
+ }
135
+
118
136
  /** The registry the app writes: Tauri's `app_data_dir()` + `servers.json` (cli-yield.ts registryPath). */
119
- export function registryPath(env = process.env, platform = process.platform, home = homedir()) {
137
+ export function registryPath(env = process.env, platform = process.platform, home = homeOf(env)) {
120
138
  const override = env[REGISTRY_FILE_ENV];
121
139
  if (override && isAbsolute(override)) return override;
122
140
  // Under a test runner (node --test, bun test) only an explicit override counts: a suite that runs
@@ -129,7 +147,7 @@ export function registryPath(env = process.env, platform = process.platform, hom
129
147
  export function appDataRegistryPath(
130
148
  env = process.env,
131
149
  platform = process.platform,
132
- home = homedir()
150
+ home = homeOf(env)
133
151
  ) {
134
152
  if (platform === 'darwin')
135
153
  return join(home, 'Library', 'Application Support', APP_IDENTIFIER, 'servers.json');
@@ -324,9 +342,9 @@ const LAUNCH_DOMAIN = 'maude-launch/1';
324
342
  const HELPER_LAUNCH_DOMAIN = 'maude-launch-helper/1';
325
343
 
326
344
  /** apps/studio/shell/key-file.ts `serverKeyDir`, verbatim: `<state>/maude/servers`. */
327
- export function serverKeyDir(env = process.env, home = homedir()) {
345
+ export function serverKeyDir(env = process.env, home = homeOf(env)) {
328
346
  const s = env.XDG_STATE_HOME;
329
- const state = s && isAbsolute(s) ? s : join(env.HOME || home, '.local', 'state');
347
+ const state = s && isAbsolute(s) ? s : join(home, '.local', 'state');
330
348
  return join(state, 'maude', 'servers');
331
349
  }
332
350
 
@@ -632,6 +650,13 @@ exit: 0 ok · 10 not the app's (use the project's own server; no key) · 11 the
632
650
  it did not answer the check (do not use _server.json) · 4 refused (message on stderr) · 2 usage
633
651
  `;
634
652
 
653
+ /**
654
+ * How long a caller that is not latency-bound (the bash helpers' `port` / `key` / `launch-url`, an
655
+ * export) waits for an app server to answer the attest check: 4 tries, 3 s each, 750 ms apart. A
656
+ * server busy with an export or a fresh boot answers late; only after the last try is it unverified.
657
+ */
658
+ export const ATTEST_PATIENCE = Object.freeze({ attempts: 4, retryDelayMs: 750, timeoutMs: 3000 });
659
+
635
660
  function argsOf(argv) {
636
661
  const o = { mode: argv[0] };
637
662
  for (let i = 1; i < argv.length; i++) {
@@ -681,7 +706,7 @@ export async function main(
681
706
  }
682
707
  const root = resolve(a.root);
683
708
  if (a.mode === 'port') {
684
- const p = await probeDesktop(root, { env, attempts: 4, retryDelayMs: 750 });
709
+ const p = await probeDesktop(root, { env, ...ATTEST_PATIENCE });
685
710
  // `--ensure show` (/design:browse): the app shows the project itself — nothing on stdout
686
711
  if (a.ensure === 'show' && p.state !== 'no-app' && p.socket) {
687
712
  const r = await ensureDesktop(root, { env, purpose: 'show', log });
@@ -726,8 +751,11 @@ export async function main(
726
751
  err.write(`desktop-locate: ${a.mode} needs --port N\n${USAGE}`);
727
752
  return 2;
728
753
  }
729
- // the app's attested server, else a terminal-started one's key file (sec-loopback-bearer §7.5)
730
- const r = await serverKeyFor(root, Number(a.port), { env });
754
+ // the app's attested server, else a terminal-started one's key file (sec-loopback-bearer §7.5).
755
+ // fix-bulk B1 — the same patience as `port` mode: a busy app server (an export, a fresh boot)
756
+ // answers the check late, and one 1.5 s try read as "did not attest" (exit 11). Still only an
757
+ // attested port gets its key; a wrong answer stays 11 after the last try.
758
+ const r = await serverKeyFor(root, Number(a.port), { env, ...ATTEST_PATIENCE });
731
759
  if (r.unverified) return 11;
732
760
  if (a.mode === 'launch-url') {
733
761
  // a browser opens the studio with a one-time token (a server that needs no key: the plain URL)
@@ -55,8 +55,14 @@ export async function probeStudio(
55
55
  root,
56
56
  { timeoutMs = 300, attempts = 1, retryDelayMs = 250 } = {}
57
57
  ) {
58
- const app = await probeDesktop(root, { timeoutMs: Math.max(timeoutMs, 300) });
59
- if (app.state === 'unverified') return { state: 'none' };
58
+ // fix-bulk B1 — the app's attest check gets the same tries as `/_health` below: a busy app server
59
+ // (an export, a fresh boot) answers late, and one try read as "no studio" for every verb
60
+ const app = await probeDesktop(root, {
61
+ timeoutMs: Math.max(timeoutMs, 300),
62
+ attempts,
63
+ retryDelayMs,
64
+ });
65
+ if (app.state === 'unverified') return { state: 'none', unverified: true };
60
66
  if (app.state === 'running') {
61
67
  const base = `http://127.0.0.1:${app.port}`;
62
68
  let health = null;
@@ -47,7 +47,19 @@ export const SUB_VERBS = {
47
47
  // S5.4 (V2-1.15 §5.10): the AI runs — `list` reads, `stop` stops one of your own runs.
48
48
  runs: { list: NATIVE, stop: NATIVE },
49
49
  // S11.11: `status` reads; `resync` re-compares every file with the cloud copy (shared → prompt).
50
- sync: { status: NATIVE, resync: NATIVE },
50
+ // fix-bulk 2026-11-10 follow-up: Sync details' buttons. All prompt (verbs.ts: the answers are shared,
51
+ // `compare` / `cancel-transfer` are held at the tier their recipe had).
52
+ sync: {
53
+ status: NATIVE,
54
+ resync: NATIVE,
55
+ compare: NATIVE,
56
+ 'keep-mine': NATIVE,
57
+ 'use-theirs': NATIVE,
58
+ 'keep-both': NATIVE,
59
+ 'publish-ai-edit': NATIVE,
60
+ 'discard-ai-edit': NATIVE,
61
+ 'cancel-transfer': NATIVE,
62
+ },
51
63
  // S7.6c (V2-1.10 §5.7): a video's captions — `list` reads, `add` / `export` write in the project.
52
64
  captions: { add: NATIVE, list: NATIVE, export: NATIVE },
53
65
  // S10.3 (S10 §2.4): the design-system reads; the helper (ds/ds-verb.ts) refuses a write flag
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1agh/maude",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Marketplace of Claude Code plugins by Michal Dovrtěl: `design` (canvas-first design iteration) + `flow` (generic agentic workflow loop with .ai second brain). Ships the `maude` CLI (with `mdcc` legacy alias) to scaffold workspace, run the design dev server, and manage configs.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -72,13 +72,13 @@
72
72
  "test:e2e:desktop:parity:cloud": "pnpm --filter @maude/desktop-e2e e2e:parity:cloud"
73
73
  },
74
74
  "optionalDependencies": {
75
- "@1agh/maude-darwin-arm64": "2.1.1",
76
- "@1agh/maude-darwin-x64": "2.1.1",
77
- "@1agh/maude-linux-arm64": "2.1.1",
78
- "@1agh/maude-linux-arm64-musl": "2.1.1",
79
- "@1agh/maude-linux-x64": "2.1.1",
80
- "@1agh/maude-linux-x64-musl": "2.1.1",
81
- "@1agh/maude-win32-x64": "2.1.1"
75
+ "@1agh/maude-darwin-arm64": "2.1.2",
76
+ "@1agh/maude-darwin-x64": "2.1.2",
77
+ "@1agh/maude-linux-arm64": "2.1.2",
78
+ "@1agh/maude-linux-arm64-musl": "2.1.2",
79
+ "@1agh/maude-linux-x64": "2.1.2",
80
+ "@1agh/maude-linux-x64-musl": "2.1.2",
81
+ "@1agh/maude-win32-x64": "2.1.2"
82
82
  },
83
83
  "files": [
84
84
  "cli",
@@ -1,262 +0,0 @@
1
- // client/home/HomeAccounts.jsx — 2.0.5 W4b: Home's avatar and the card it opens.
2
- //
3
- // Every profile on this Mac by its label, with the accounts it is signed in with (Maude Cloud,
4
- // team servers, GitHub): Sign out per account where Home has a path (asks first), Sign in with
5
- // GitHub for a profile without it, and add / switch at the foot. What each row means and why a
6
- // row has no button: home-accounts-model.js. The card fetches the team-server sign-ins when it
7
- // opens (`GET /_home/accounts`) and after every sign-out.
8
-
9
- import { useCallback, useEffect, useRef, useState } from 'react';
10
-
11
- import { cloudSignOut, getAccounts, hubSignOut, shellInvoke, shellListen } from './home-api.js';
12
- import { homeAccounts } from './home-accounts-model.js';
13
- import { githubSignIn } from '../v2/onboarding/github-home.js';
14
-
15
- const KIND_TITLE = { cloud: 'Maude Cloud', team: 'Team server', github: 'GitHub' };
16
-
17
- /** The team-server sign-ins of `ids`, re-read when `open` turns on or `gen` moves. */
18
- function useHubProfiles(ids, open, gen) {
19
- const [hubProfiles, setHubProfiles] = useState([]);
20
- const key = ids.join(',');
21
- // biome-ignore lint/correctness/useExhaustiveDependencies: `key` stands for `ids`; `gen` re-reads
22
- useEffect(() => {
23
- if (!open || !key) return undefined;
24
- let live = true;
25
- getAccounts(key.split(',')).then((r) => {
26
- if (live && r.ok && Array.isArray(r.json?.profiles)) setHubProfiles(r.json.profiles);
27
- });
28
- return () => {
29
- live = false;
30
- };
31
- }, [open, key, gen]);
32
- return hubProfiles;
33
- }
34
-
35
- /**
36
- * @param shell / clouds / native — Home's data
37
- * @param onAddCloud () => void Home's Maude Cloud sign-in (a fresh profile when one is signed in)
38
- * @param onAddTeam () => void the Advanced › team server sheet
39
- * @param onChanged () => Promise reload Home's data after a sign-out / sign-in
40
- */
41
- export function HomeAccounts({ shell, clouds, native, onAddCloud, onAddTeam, onChanged }) {
42
- const [open, setOpen] = useState(false);
43
- const [gen, setGen] = useState(0);
44
- const [asking, setAsking] = useState(null);
45
- const [busy, setBusy] = useState(false);
46
- const [error, setError] = useState(null);
47
- const [ghCode, setGhCode] = useState(null);
48
- const wrap = useRef(null);
49
- const ids = (shell?.profiles ?? []).map((p) => p?.id).filter((id) => typeof id === 'string');
50
- const hubProfiles = useHubProfiles(ids, open, gen);
51
- const model = homeAccounts({ native, shell, clouds, hubProfiles });
52
-
53
- const close = useCallback(() => {
54
- setOpen(false);
55
- setAsking(null);
56
- setError(null);
57
- }, []);
58
- useEffect(() => {
59
- if (!open) return undefined;
60
- const onKey = (e) => e.key === 'Escape' && close();
61
- const onDown = (e) => wrap.current && !wrap.current.contains(e.target) && close();
62
- window.addEventListener('keydown', onKey);
63
- window.addEventListener('pointerdown', onDown);
64
- return () => {
65
- window.removeEventListener('keydown', onKey);
66
- window.removeEventListener('pointerdown', onDown);
67
- };
68
- }, [open, close]);
69
- // the GitHub device code arrives as a shell message while a sign-in from this card runs
70
- useEffect(() => {
71
- if (!open || !native) return undefined;
72
- let off = () => {};
73
- shellListen('github://device-code', (p) => setGhCode(p?.user_code ?? null)).then((u) => {
74
- off = u;
75
- });
76
- return () => off();
77
- }, [open, native]);
78
-
79
- if (!model.face) return null;
80
-
81
- const signOut = async () => {
82
- const spec = asking;
83
- setBusy(true);
84
- setError(null);
85
- const r =
86
- spec.path === 'cloud-signout'
87
- ? await cloudSignOut(spec.profile)
88
- : await hubSignOut(spec.profile, spec.url);
89
- setBusy(false);
90
- setAsking(null);
91
- if (!r.ok || !r.json?.ok) {
92
- setError(r.json?.error || 'Couldn’t sign out. Nothing changed — try again.');
93
- return;
94
- }
95
- setGen((g) => g + 1);
96
- await onChanged?.();
97
- };
98
- const ghSignIn = async (profile) => {
99
- setError(null);
100
- setBusy(true);
101
- try {
102
- await githubSignIn(profile, shellInvoke);
103
- await onChanged?.();
104
- } catch (e) {
105
- setError(typeof e === 'string' ? e : 'GitHub sign-in didn’t finish. Try again.');
106
- } finally {
107
- setGhCode(null);
108
- setBusy(false);
109
- }
110
- };
111
-
112
- return (
113
- <span className="home-top-acct" ref={wrap}>
114
- <button
115
- type="button"
116
- className="home-top-acct-btn"
117
- data-testid="home-top-face"
118
- aria-label={model.face.title}
119
- title={model.face.title}
120
- aria-haspopup="dialog"
121
- aria-expanded={open}
122
- onClick={() => (open ? close() : setOpen(true))}
123
- >
124
- <span
125
- className={`home-face home-face--md home-face--${model.face.tone}`}
126
- aria-hidden="true"
127
- >
128
- {model.face.initial}
129
- </span>
130
- </button>
131
- {open ? (
132
- <div
133
- className="home-accts"
134
- role="dialog"
135
- aria-label="Accounts on this Mac"
136
- data-testid="home-accounts"
137
- >
138
- <p className="home-accts-h">Accounts on this Mac</p>
139
- {model.profiles.map((p) => (
140
- <section
141
- key={p.id}
142
- className="home-accts-p"
143
- data-testid={`home-accounts-profile-${p.id}`}
144
- >
145
- <h3 className="home-accts-ph">
146
- <span className={`home-acctf home-acctf--${p.face.tone}`} aria-hidden="true">
147
- {p.face.initial}
148
- </span>
149
- {p.label}
150
- </h3>
151
- {p.accounts.length ? (
152
- <ul className="home-accts-list">
153
- {p.accounts.map((a) => (
154
- <li
155
- key={a.key}
156
- className="home-accts-row"
157
- data-testid={`home-account-${p.id}-${a.kind}`}
158
- >
159
- <span className="home-accts-t">
160
- <strong>
161
- {a.kind === 'team'
162
- ? `${KIND_TITLE.team} · ${a.host}`
163
- : KIND_TITLE[a.kind]}
164
- </strong>
165
- {a.who ? <span>{a.who}</span> : null}
166
- </span>
167
- {a.signOut ? (
168
- <button
169
- type="button"
170
- className="btn btn--ghost btn--sm"
171
- data-testid={`home-account-signout-${p.id}-${a.kind}`}
172
- onClick={() => setAsking(a.signOut)}
173
- disabled={busy}
174
- >
175
- Sign out…
176
- </button>
177
- ) : null}
178
- </li>
179
- ))}
180
- </ul>
181
- ) : (
182
- <p className="home-accts-none">Not signed in to anything yet.</p>
183
- )}
184
- {p.githubSignIn ? (
185
- <button
186
- type="button"
187
- className="home-accts-act"
188
- data-testid={`home-account-github-signin-${p.id}`}
189
- onClick={() => ghSignIn(p.id)}
190
- disabled={busy}
191
- >
192
- Sign in with GitHub…
193
- </button>
194
- ) : null}
195
- </section>
196
- ))}
197
- {ghCode ? (
198
- <p className="home-accts-note" data-testid="home-accounts-github-code">
199
- Enter <strong>{ghCode}</strong> on the GitHub page that opened.
200
- </p>
201
- ) : null}
202
- {error ? (
203
- <p className="home-accts-note" role="alert" data-testid="home-accounts-error">
204
- {error}
205
- </p>
206
- ) : null}
207
- {asking ? (
208
- <div className="home-accts-ask" role="alertdialog" data-testid="home-accounts-confirm">
209
- <strong>{asking.confirm.title}</strong>
210
- <span>{asking.confirm.body}</span>
211
- <span className="home-accts-btns">
212
- <button
213
- type="button"
214
- className="btn btn--sm"
215
- data-testid="home-accounts-signout-cancel"
216
- onClick={() => setAsking(null)}
217
- >
218
- {asking.confirm.cancel}
219
- </button>
220
- <button
221
- type="button"
222
- className="btn btn--sm btn--danger"
223
- data-testid="home-accounts-signout-confirm"
224
- disabled={busy}
225
- onClick={signOut}
226
- >
227
- {asking.confirm.primary}
228
- </button>
229
- </span>
230
- </div>
231
- ) : null}
232
- {model.add ? (
233
- <div className="home-accts-foot">
234
- <button
235
- type="button"
236
- className="home-accts-act"
237
- data-testid="home-accounts-add-cloud"
238
- onClick={() => {
239
- close();
240
- onAddCloud?.();
241
- }}
242
- >
243
- {model.add.cloud}
244
- </button>
245
- <button
246
- type="button"
247
- className="home-accts-act"
248
- data-testid="home-accounts-add-team"
249
- onClick={() => {
250
- close();
251
- onAddTeam?.();
252
- }}
253
- >
254
- {model.add.team}
255
- </button>
256
- </div>
257
- ) : null}
258
- </div>
259
- ) : null}
260
- </span>
261
- );
262
- }
@@ -1,221 +0,0 @@
1
- // client/home/HomeAccountsRow.jsx — 2.1.1 B4 (artboard 14): "Accounts on this Mac", the row on top of
2
- // Home. One card per account (a Maude Cloud account by its e-mail, your server by its host) with where
3
- // the person is signed in and whether that sign-in works; a sign-in that stopped working (or is
4
- // missing) is fixed from its own card — Maude Cloud through Home's own sign-in, your server through the
5
- // team-server sign-in with the address already filled in. Cards come from `homeAccountCards`
6
- // (home-accounts-model.js); a server that does not answer has no button (nothing for the person to fix).
7
-
8
- import { useEffect, useRef, useState } from 'react';
9
-
10
- import { AcctFace, StateWord } from './HomeConnection.jsx';
11
- import {
12
- hubSignInCancel,
13
- hubSignInPoll,
14
- hubSignInStart,
15
- openTeamServerSignIn,
16
- } from './home-api.js';
17
- import { slugOf } from './home-accounts-model.js';
18
- import { CONN_TONE } from './home-where-model.js';
19
-
20
- const POLL_MS = 1200;
21
- const SIGNIN_WORDS = {
22
- unreachable: 'Couldn’t reach that server. Check the address, or whether it’s online.',
23
- noBrowser: 'The browser didn’t open. Nothing changed — try again.',
24
- refused: 'That sign-in page can’t be opened from here. Nothing changed.',
25
- };
26
-
27
- /**
28
- * @param {{ cards: object[], native: boolean, onCloudSignIn: (card: object) => unknown,
29
- * onAddCloud: () => void, onAddTeam: () => void, onChanged?: () => unknown,
30
- * pollMs?: number }} props
31
- */
32
- export function HomeAccountsRow({
33
- cards,
34
- native,
35
- onCloudSignIn,
36
- onAddCloud,
37
- onAddTeam,
38
- onChanged,
39
- pollMs = POLL_MS,
40
- }) {
41
- const [adding, setAdding] = useState(false);
42
- const [busy, setBusy] = useState(null); // the card key whose sign-in waits on the browser
43
- const [error, setError] = useState(null);
44
- const attempt = useRef(null); // { id, stop }
45
- const wrap = useRef(null);
46
-
47
- // leaving Home (or a new attempt) ends the one that waits
48
- useEffect(
49
- () => () => {
50
- const a = attempt.current;
51
- attempt.current = null;
52
- if (a) {
53
- a.stop();
54
- void hubSignInCancel(a.id);
55
- }
56
- },
57
- []
58
- );
59
- useEffect(() => {
60
- if (!adding) return undefined;
61
- const onKey = (e) => e.key === 'Escape' && setAdding(false);
62
- const onDown = (e) => wrap.current && !wrap.current.contains(e.target) && setAdding(false);
63
- window.addEventListener('keydown', onKey);
64
- window.addEventListener('pointerdown', onDown);
65
- return () => {
66
- window.removeEventListener('keydown', onKey);
67
- window.removeEventListener('pointerdown', onDown);
68
- };
69
- }, [adding]);
70
-
71
- if (!cards.length && !native) return null;
72
-
73
- const waitForBrowser = (id) =>
74
- new Promise((resolve) => {
75
- let timer = null;
76
- const stop = () => {
77
- clearTimeout(timer);
78
- resolve({ ok: false, cancelled: true });
79
- };
80
- attempt.current = { id, stop };
81
- const tick = async () => {
82
- const p = await hubSignInPoll(id);
83
- if (attempt.current?.id !== id) return;
84
- if (p.json?.pending) {
85
- timer = setTimeout(tick, pollMs);
86
- return;
87
- }
88
- attempt.current = null;
89
- resolve(
90
- p.ok && p.json?.ok
91
- ? { ok: true }
92
- : { ok: false, error: p.json?.error || SIGNIN_WORDS.unreachable }
93
- );
94
- };
95
- timer = setTimeout(tick, 0);
96
- });
97
-
98
- const fixServer = async (card) => {
99
- setError(null);
100
- setBusy(card.key);
101
- const s = await hubSignInStart(card.fix.profile, card.fix.url);
102
- if (!s.ok || !s.json?.ok) {
103
- setBusy(null);
104
- setError(s.json?.error || SIGNIN_WORDS.unreachable);
105
- return;
106
- }
107
- const id = s.json.id;
108
- const opened = await openTeamServerSignIn(s.json.signInUrl);
109
- if (opened !== 'ok') {
110
- void hubSignInCancel(id);
111
- setBusy(null);
112
- setError(opened === 'refused' ? SIGNIN_WORDS.refused : SIGNIN_WORDS.noBrowser);
113
- return;
114
- }
115
- const done = await waitForBrowser(id);
116
- setBusy(null);
117
- if (done.cancelled) return;
118
- if (!done.ok) {
119
- setError(done.error);
120
- return;
121
- }
122
- await onChanged?.();
123
- };
124
-
125
- const fix = (card) => (card.fix.path === 'cloud-signin' ? onCloudSignIn(card) : fixServer(card));
126
-
127
- return (
128
- <section
129
- className="home-sec home-acr"
130
- aria-labelledby="home-h-accounts"
131
- data-testid="home-accounts-row"
132
- >
133
- <h2 className="home-h" id="home-h-accounts">
134
- Accounts on this Mac
135
- {native ? (
136
- <span className="home-h-right home-acr-add" ref={wrap}>
137
- <button
138
- type="button"
139
- className="btn btn--ghost btn--sm"
140
- data-testid="home-accounts-row-add"
141
- aria-haspopup="menu"
142
- aria-expanded={adding}
143
- onClick={() => setAdding((o) => !o)}
144
- >
145
- Add an account…
146
- </button>
147
- {adding ? (
148
- <span className="island home-acr-menu" role="menu">
149
- <button
150
- type="button"
151
- role="menuitem"
152
- className="row-item"
153
- data-testid="home-accounts-row-add-cloud"
154
- onClick={() => {
155
- setAdding(false);
156
- onAddCloud?.();
157
- }}
158
- >
159
- Add a Maude Cloud account…
160
- </button>
161
- <button
162
- type="button"
163
- role="menuitem"
164
- className="row-item"
165
- data-testid="home-accounts-row-add-team"
166
- onClick={() => {
167
- setAdding(false);
168
- onAddTeam?.();
169
- }}
170
- >
171
- Connect to a team server…
172
- </button>
173
- </span>
174
- ) : null}
175
- </span>
176
- ) : null}
177
- </h2>
178
- <div className="home-acr-cards">
179
- {cards.map((c) => {
180
- const id = slugOf(c);
181
- const bad = c.state === 'rejected' || c.state === 'missing';
182
- return (
183
- <span
184
- key={c.key}
185
- className={`home-acc${bad ? ' home-acc--bad' : ''}`}
186
- data-kind={c.kind}
187
- data-state={c.state}
188
- data-testid={`home-account-card-${id}`}
189
- >
190
- <AcctFace face={c.face} state={c.state} size="lg" />
191
- <span className="home-acc-t">
192
- <strong>{c.title}</strong>
193
- <span>{c.sub}</span>
194
- <StateWord tone={CONN_TONE[c.state]} testid={`home-account-state-${id}`}>
195
- {c.word}
196
- </StateWord>
197
- </span>
198
- {c.fix ? (
199
- <button
200
- type="button"
201
- className="btn btn--primary btn--sm"
202
- data-testid={`home-account-signin-${id}`}
203
- disabled={!native || busy === c.key}
204
- title={native ? undefined : 'Sign in from the Maude app on your Mac.'}
205
- onClick={() => fix(c)}
206
- >
207
- {busy === c.key ? 'Waiting for the browser…' : c.fix.label}
208
- </button>
209
- ) : null}
210
- </span>
211
- );
212
- })}
213
- </div>
214
- {error ? (
215
- <p className="home-acct-note" role="alert" data-testid="home-accounts-row-error">
216
- {error}
217
- </p>
218
- ) : null}
219
- </section>
220
- );
221
- }