@hanzo/app 0.3.98 → 0.3.99

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 (2) hide show
  1. package/dist/lib/todo.js +98 -20
  2. package/package.json +1 -1
package/dist/lib/todo.js CHANGED
@@ -15,6 +15,79 @@ export const COLUMNS = [
15
15
  /** The priorities the store accepts, loudest first. */
16
16
  export const PRIORITIES = ['urgent', 'high', 'medium', 'low', 'none'];
17
17
  const STORAGE_KEY = 'hanzo_board_cards';
18
+ /**
19
+ * What `/v1/task/*` answers when a caller has no forge seat yet in a
20
+ * platform-made code workspace, or when the org has no forge namespace yet
21
+ * (cloud apps/task/source.go). Opening `/v1/git/workspace` provisions both
22
+ * idempotently; an API-key refusal ("a seat in a code workspace is a person's")
23
+ * does not match and surfaces untouched.
24
+ */
25
+ const UNSEATED = /no forge identity for this principal yet|no namespace on the forge yet/i;
26
+ const seatedByClient = new WeakMap();
27
+ function isUnseated(err) {
28
+ if (!err || typeof err !== 'object')
29
+ return false;
30
+ const e = err;
31
+ if (e.status !== 403)
32
+ return false;
33
+ const detail = typeof e.body === 'object' && e.body !== null && 'detail' in e.body
34
+ ? String(e.body.detail ?? '')
35
+ : typeof e.body === 'string'
36
+ ? e.body
37
+ : '';
38
+ const text = `${typeof e.message === 'string' ? e.message : ''} ${detail}`;
39
+ return UNSEATED.test(text);
40
+ }
41
+ function seatWorkspace(client) {
42
+ let pending = seatedByClient.get(client);
43
+ if (!pending) {
44
+ pending = client.http
45
+ .json({
46
+ method: 'POST',
47
+ path: '/v1/git/workspace',
48
+ body: {},
49
+ })
50
+ .catch((e) => {
51
+ seatedByClient.delete(client);
52
+ throw e;
53
+ });
54
+ seatedByClient.set(client, pending);
55
+ }
56
+ return pending;
57
+ }
58
+ /** Runs a `/v1/task/*` request, seating the caller on the forge once if 403 unseated. */
59
+ async function seated(client, run) {
60
+ try {
61
+ return await run();
62
+ }
63
+ catch (e) {
64
+ if (!isUnseated(e))
65
+ throw e;
66
+ await seatWorkspace(client);
67
+ return run();
68
+ }
69
+ }
70
+ function normalizeBoardKey(key) {
71
+ return key.trim().toLowerCase().replace(/_/g, '-');
72
+ }
73
+ function projectBoardKey(p) {
74
+ const repoUrl = p.repo?.url?.trim();
75
+ if (repoUrl) {
76
+ try {
77
+ const parsed = new URL(repoUrl);
78
+ if (/^git\./i.test(parsed.hostname)) {
79
+ const segs = parsed.pathname.replace(/\/+$/, '').split('/').filter(Boolean);
80
+ const last = segs[segs.length - 1]?.replace(/\.git$/i, '');
81
+ if (last)
82
+ return last;
83
+ }
84
+ }
85
+ catch { }
86
+ }
87
+ if (p.slug?.trim())
88
+ return p.slug.trim();
89
+ return (p.name || p.id || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
90
+ }
18
91
  export const DEFAULT_CARDS = [
19
92
  {
20
93
  id: 'hanzo-1',
@@ -144,11 +217,11 @@ export async function board(client, params = {}, signal) {
144
217
  query.label = params.label;
145
218
  if (params.scheduled !== undefined)
146
219
  query.scheduled = String(params.scheduled);
147
- const out = await client.http.json({
220
+ const out = await seated(client, () => client.http.json({
148
221
  path: '/v1/task/board',
149
222
  query,
150
223
  signal,
151
- });
224
+ }));
152
225
  const fetched = rows(out);
153
226
  // A SUCCESSFUL READ IS THE ANSWER, EMPTY OR NOT. An org whose board holds
154
227
  // no cards yet is a fact about the org — the empty state a reader is owed
@@ -173,15 +246,13 @@ export async function board(client, params = {}, signal) {
173
246
  export function targetOf(own, shown, keys) {
174
247
  return own || (shown && shown !== 'all' ? shown : '') || keys[0] || '';
175
248
  }
176
- /** The boards the server names (todo's own, then the forge's projects), added to `map` when one is given; with a fresh map, no stand-ins. */
249
+ /** The boards the server names (task's own, then the org's projects), added to `map` when one is given; with a fresh map, no stand-ins. */
177
250
  async function served(client, signal, map = new Map()) {
178
251
  const [todoPs, devPs] = await Promise.all([
179
- client.http
180
- .json({
252
+ seated(client, () => client.http.json({
181
253
  path: '/v1/task/projects',
182
254
  signal,
183
- })
184
- .catch(() => null),
255
+ })).catch(() => null),
185
256
  client.http
186
257
  .json({
187
258
  path: '/v1/project',
@@ -189,14 +260,23 @@ async function served(client, signal, map = new Map()) {
189
260
  })
190
261
  .catch(() => null),
191
262
  ]);
263
+ const seen = new Set(Array.from(map.keys(), normalizeBoardKey));
192
264
  for (const p of rows(todoPs)) {
193
- if (p?.key)
265
+ if (!p?.key)
266
+ continue;
267
+ const norm = normalizeBoardKey(p.key);
268
+ if (!map.has(p.key) && !seen.has(norm)) {
194
269
  map.set(p.key, { key: p.key, name: p.name || p.key });
270
+ seen.add(norm);
271
+ }
195
272
  }
196
273
  for (const p of Array.isArray(devPs) ? devPs : []) {
197
- const k = (p.name || p.id).toLowerCase().replace(/[^a-z0-9]+/g, '-');
198
- if (k && !map.has(k))
274
+ const k = projectBoardKey(p);
275
+ const norm = normalizeBoardKey(k);
276
+ if (k && !map.has(k) && !seen.has(norm)) {
199
277
  map.set(k, { key: k, name: p.name || k });
278
+ seen.add(norm);
279
+ }
200
280
  }
201
281
  return Array.from(map.values());
202
282
  }
@@ -215,10 +295,8 @@ export async function projects(client, signal) {
215
295
  if (!client)
216
296
  return fallback;
217
297
  try {
218
- const map = new Map();
219
- for (const f of fallback)
220
- map.set(f.key, f);
221
- return await served(client, signal, map);
298
+ const live = await served(client, signal);
299
+ return live.length > 0 ? live : fallback;
222
300
  }
223
301
  catch {
224
302
  return fallback;
@@ -246,12 +324,12 @@ export async function open(client, key, card, signal) {
246
324
  };
247
325
  if (client) {
248
326
  try {
249
- const created = await client.http.json({
327
+ const created = await seated(client, () => client.http.json({
250
328
  method: 'POST',
251
329
  path: `/v1/task/projects/${encodeURIComponent(key)}/issues`,
252
330
  body: card,
253
331
  signal,
254
- });
332
+ }));
255
333
  if (created?.id) {
256
334
  saveLocalCards([created, ...current]);
257
335
  return created;
@@ -286,12 +364,12 @@ export async function edit(client, key, num, patch, signal) {
286
364
  }
287
365
  if (client) {
288
366
  try {
289
- const serverUpdated = await client.http.json({
367
+ const serverUpdated = await seated(client, () => client.http.json({
290
368
  method: 'PATCH',
291
369
  path: `/v1/task/projects/${encodeURIComponent(key)}/issues/${num}`,
292
370
  body: patch,
293
371
  signal,
294
- });
372
+ }));
295
373
  if (serverUpdated?.id) {
296
374
  return serverUpdated;
297
375
  }
@@ -304,12 +382,12 @@ export async function edit(client, key, num, patch, signal) {
304
382
  export async function claim(client, key, num, signal) {
305
383
  if (client) {
306
384
  try {
307
- return await client.http.json({
385
+ return await seated(client, () => client.http.json({
308
386
  method: 'POST',
309
387
  path: `/v1/task/projects/${encodeURIComponent(key)}/issues/${num}/claim`,
310
388
  body: {},
311
389
  signal,
312
- });
390
+ }));
313
391
  }
314
392
  catch { }
315
393
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/app",
3
- "version": "0.3.98",
3
+ "version": "0.3.99",
4
4
  "description": "Hanzo's app: the shell and its apps (Home, Chat, Inbox, Contacts, Meet, Calendar, Drive, Board, Work, Bots), once, for every Hanzo surface.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",