@skillcdn/console 0.5.0 → 0.5.1

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 (345) hide show
  1. package/README.md +68 -11
  2. package/SKILLCDN.md +3 -3
  3. package/dist/agent-page.d.ts.map +1 -1
  4. package/dist/agent-page.js +3 -2
  5. package/dist/agent-page.js.map +1 -1
  6. package/dist/api.d.ts +7 -6
  7. package/dist/api.d.ts.map +1 -1
  8. package/dist/api.js +6 -5
  9. package/dist/api.js.map +1 -1
  10. package/dist/charts.d.ts +103 -0
  11. package/dist/charts.d.ts.map +1 -0
  12. package/dist/charts.js +182 -0
  13. package/dist/charts.js.map +1 -0
  14. package/dist/cli/cli.d.ts +26 -2
  15. package/dist/cli/cli.d.ts.map +1 -1
  16. package/dist/cli/cli.js +1304 -383
  17. package/dist/cli/cli.js.map +1 -1
  18. package/dist/cli/command-words.d.ts +20 -0
  19. package/dist/cli/command-words.d.ts.map +1 -0
  20. package/dist/cli/command-words.js +51 -0
  21. package/dist/cli/command-words.js.map +1 -0
  22. package/dist/cli/credentials.d.ts +54 -5
  23. package/dist/cli/credentials.d.ts.map +1 -1
  24. package/dist/cli/credentials.js +126 -20
  25. package/dist/cli/credentials.js.map +1 -1
  26. package/dist/cli/directory.d.ts +19 -4
  27. package/dist/cli/directory.d.ts.map +1 -1
  28. package/dist/cli/directory.js +16 -8
  29. package/dist/cli/directory.js.map +1 -1
  30. package/dist/cli/format.d.ts +65 -8
  31. package/dist/cli/format.d.ts.map +1 -1
  32. package/dist/cli/format.js +242 -37
  33. package/dist/cli/format.js.map +1 -1
  34. package/dist/cli/help.d.ts +13 -4
  35. package/dist/cli/help.d.ts.map +1 -1
  36. package/dist/cli/help.js +92 -54
  37. package/dist/cli/help.js.map +1 -1
  38. package/dist/cli/main.js +76 -2
  39. package/dist/cli/main.js.map +1 -1
  40. package/dist/cli/pack.d.ts +69 -60
  41. package/dist/cli/pack.d.ts.map +1 -1
  42. package/dist/cli/pack.js +241 -270
  43. package/dist/cli/pack.js.map +1 -1
  44. package/dist/cli/plugin-check.d.ts +42 -0
  45. package/dist/cli/plugin-check.d.ts.map +1 -1
  46. package/dist/cli/plugin-check.js +221 -36
  47. package/dist/cli/plugin-check.js.map +1 -1
  48. package/dist/cli/plugin-cli.d.ts +6 -1
  49. package/dist/cli/plugin-cli.d.ts.map +1 -1
  50. package/dist/cli/plugin-cli.js +21 -4
  51. package/dist/cli/plugin-cli.js.map +1 -1
  52. package/dist/cli/plugin-main.js +1 -0
  53. package/dist/cli/plugin-main.js.map +1 -1
  54. package/dist/cli/serve.d.ts +33 -1
  55. package/dist/cli/serve.d.ts.map +1 -1
  56. package/dist/cli/serve.js +252 -3
  57. package/dist/cli/serve.js.map +1 -1
  58. package/dist/client.d.ts +48 -9
  59. package/dist/client.d.ts.map +1 -1
  60. package/dist/client.js +72 -80
  61. package/dist/client.js.map +1 -1
  62. package/dist/components/agent-page.d.ts +64 -1
  63. package/dist/components/agent-page.d.ts.map +1 -1
  64. package/dist/components/agent-page.js +244 -96
  65. package/dist/components/agent-page.js.map +1 -1
  66. package/dist/components/charts.d.ts +157 -0
  67. package/dist/components/charts.d.ts.map +1 -0
  68. package/dist/components/charts.js +182 -0
  69. package/dist/components/charts.js.map +1 -0
  70. package/dist/components/desk-records.d.ts +102 -0
  71. package/dist/components/desk-records.d.ts.map +1 -0
  72. package/dist/components/desk-records.js +221 -0
  73. package/dist/components/desk-records.js.map +1 -0
  74. package/dist/components/desk.d.ts +85 -0
  75. package/dist/components/desk.d.ts.map +1 -0
  76. package/dist/components/desk.js +135 -0
  77. package/dist/components/desk.js.map +1 -0
  78. package/dist/components/event-feed.d.ts.map +1 -1
  79. package/dist/components/event-feed.js +4 -1
  80. package/dist/components/event-feed.js.map +1 -1
  81. package/dist/components/home.d.ts.map +1 -1
  82. package/dist/components/home.js +4 -2
  83. package/dist/components/home.js.map +1 -1
  84. package/dist/components/plugin-settings.d.ts.map +1 -1
  85. package/dist/components/plugin-settings.js +54 -3
  86. package/dist/components/plugin-settings.js.map +1 -1
  87. package/dist/components/projects.d.ts.map +1 -1
  88. package/dist/components/projects.js +27 -7
  89. package/dist/components/projects.js.map +1 -1
  90. package/dist/components/rules.d.ts +8 -10
  91. package/dist/components/rules.d.ts.map +1 -1
  92. package/dist/components/rules.js +35 -13
  93. package/dist/components/rules.js.map +1 -1
  94. package/dist/components/setup.d.ts +9 -3
  95. package/dist/components/setup.d.ts.map +1 -1
  96. package/dist/components/setup.js +10 -4
  97. package/dist/components/setup.js.map +1 -1
  98. package/dist/console.css +677 -7
  99. package/dist/console.d.ts +5 -0
  100. package/dist/console.d.ts.map +1 -1
  101. package/dist/console.js +14 -8
  102. package/dist/console.js.map +1 -1
  103. package/dist/data.d.ts.map +1 -1
  104. package/dist/data.js +32 -33
  105. package/dist/data.js.map +1 -1
  106. package/dist/desk-page.d.ts +38 -0
  107. package/dist/desk-page.d.ts.map +1 -0
  108. package/dist/desk-page.js +184 -0
  109. package/dist/desk-page.js.map +1 -0
  110. package/dist/desk-records-page.d.ts +18 -0
  111. package/dist/desk-records-page.d.ts.map +1 -0
  112. package/dist/desk-records-page.js +130 -0
  113. package/dist/desk-records-page.js.map +1 -0
  114. package/dist/desk-records.d.ts +247 -0
  115. package/dist/desk-records.d.ts.map +1 -0
  116. package/dist/desk-records.js +543 -0
  117. package/dist/desk-records.js.map +1 -0
  118. package/dist/desk.d.ts +122 -0
  119. package/dist/desk.d.ts.map +1 -0
  120. package/dist/desk.js +211 -0
  121. package/dist/desk.js.map +1 -0
  122. package/dist/feed-stream.d.ts +39 -0
  123. package/dist/feed-stream.d.ts.map +1 -0
  124. package/dist/feed-stream.js +117 -0
  125. package/dist/feed-stream.js.map +1 -0
  126. package/dist/home-tiles.d.ts +10 -3
  127. package/dist/home-tiles.d.ts.map +1 -1
  128. package/dist/home-tiles.js +54 -31
  129. package/dist/home-tiles.js.map +1 -1
  130. package/dist/i18n/en.d.ts +180 -5
  131. package/dist/i18n/en.d.ts.map +1 -1
  132. package/dist/i18n/en.js +184 -7
  133. package/dist/i18n/en.js.map +1 -1
  134. package/dist/i18n/ko.d.ts.map +1 -1
  135. package/dist/i18n/ko.js +169 -6
  136. package/dist/i18n/ko.js.map +1 -1
  137. package/dist/index.d.ts +8 -0
  138. package/dist/index.d.ts.map +1 -1
  139. package/dist/index.js +8 -0
  140. package/dist/index.js.map +1 -1
  141. package/dist/limits.d.ts +11 -0
  142. package/dist/limits.d.ts.map +1 -1
  143. package/dist/limits.js +11 -0
  144. package/dist/limits.js.map +1 -1
  145. package/dist/onboarding.d.ts +6 -0
  146. package/dist/onboarding.d.ts.map +1 -1
  147. package/dist/onboarding.js +3 -3
  148. package/dist/onboarding.js.map +1 -1
  149. package/dist/plugin/context.d.ts +42 -11
  150. package/dist/plugin/context.d.ts.map +1 -1
  151. package/dist/plugin/limits.d.ts +2 -0
  152. package/dist/plugin/limits.d.ts.map +1 -1
  153. package/dist/plugin/limits.js +2 -0
  154. package/dist/plugin/limits.js.map +1 -1
  155. package/dist/plugin/manifest.d.ts +13 -0
  156. package/dist/plugin/manifest.d.ts.map +1 -1
  157. package/dist/plugin/manifest.js +30 -3
  158. package/dist/plugin/manifest.js.map +1 -1
  159. package/dist/plugin/rest-schemas.d.ts +55 -0
  160. package/dist/plugin/rest-schemas.d.ts.map +1 -1
  161. package/dist/plugin/rest-schemas.js +37 -0
  162. package/dist/plugin/rest-schemas.js.map +1 -1
  163. package/dist/plugin/sandbox.d.ts.map +1 -1
  164. package/dist/plugin/sandbox.js +4 -1
  165. package/dist/plugin/sandbox.js.map +1 -1
  166. package/dist/plugin/testing.d.ts +6 -2
  167. package/dist/plugin/testing.d.ts.map +1 -1
  168. package/dist/plugin/testing.js +52 -10
  169. package/dist/plugin/testing.js.map +1 -1
  170. package/dist/plugin.d.ts +1 -1
  171. package/dist/plugin.d.ts.map +1 -1
  172. package/dist/plugin.js +1 -1
  173. package/dist/plugin.js.map +1 -1
  174. package/dist/records.d.ts +283 -0
  175. package/dist/records.d.ts.map +1 -0
  176. package/dist/records.js +543 -0
  177. package/dist/records.js.map +1 -0
  178. package/dist/routes.d.ts +3 -1
  179. package/dist/routes.d.ts.map +1 -1
  180. package/dist/routes.js +3 -1
  181. package/dist/routes.js.map +1 -1
  182. package/dist/rules.d.ts +30 -11
  183. package/dist/rules.d.ts.map +1 -1
  184. package/dist/rules.js +98 -45
  185. package/dist/rules.js.map +1 -1
  186. package/dist/runner/acp.d.ts +1 -1
  187. package/dist/runner/acp.d.ts.map +1 -1
  188. package/dist/runner/acp.js +31 -20
  189. package/dist/runner/acp.js.map +1 -1
  190. package/dist/runner/claude.d.ts +1 -1
  191. package/dist/runner/claude.d.ts.map +1 -1
  192. package/dist/runner/claude.js +85 -15
  193. package/dist/runner/claude.js.map +1 -1
  194. package/dist/runner/codex.d.ts +1 -1
  195. package/dist/runner/codex.d.ts.map +1 -1
  196. package/dist/runner/codex.js +19 -16
  197. package/dist/runner/codex.js.map +1 -1
  198. package/dist/runner/desk.d.ts +43 -2
  199. package/dist/runner/desk.d.ts.map +1 -1
  200. package/dist/runner/desk.js +166 -10
  201. package/dist/runner/desk.js.map +1 -1
  202. package/dist/runner/process.d.ts.map +1 -1
  203. package/dist/runner/process.js +11 -4
  204. package/dist/runner/process.js.map +1 -1
  205. package/dist/runner/redact.d.ts +39 -4
  206. package/dist/runner/redact.d.ts.map +1 -1
  207. package/dist/runner/redact.js +81 -5
  208. package/dist/runner/redact.js.map +1 -1
  209. package/dist/runner/runner.d.ts +15 -1
  210. package/dist/runner/runner.d.ts.map +1 -1
  211. package/dist/runner/runner.js +231 -33
  212. package/dist/runner/runner.js.map +1 -1
  213. package/dist/runner/task-scheduler.d.ts +20 -0
  214. package/dist/runner/task-scheduler.d.ts.map +1 -1
  215. package/dist/runner/task-scheduler.js +34 -4
  216. package/dist/runner/task-scheduler.js.map +1 -1
  217. package/dist/runner/types.d.ts +36 -1
  218. package/dist/runner/types.d.ts.map +1 -1
  219. package/dist/runner/types.js +17 -0
  220. package/dist/runner/types.js.map +1 -1
  221. package/dist/runner.d.ts +6 -6
  222. package/dist/runner.d.ts.map +1 -1
  223. package/dist/runner.js +5 -5
  224. package/dist/runner.js.map +1 -1
  225. package/dist/schemas.d.ts +509 -5
  226. package/dist/schemas.d.ts.map +1 -1
  227. package/dist/schemas.js +76 -16
  228. package/dist/schemas.js.map +1 -1
  229. package/dist/services.d.ts +22 -5
  230. package/dist/services.d.ts.map +1 -1
  231. package/dist/services.js +35 -15
  232. package/dist/services.js.map +1 -1
  233. package/dist/setup-guide.d.ts +3 -1
  234. package/dist/setup-guide.d.ts.map +1 -1
  235. package/dist/setup-guide.js +7 -4
  236. package/dist/setup-guide.js.map +1 -1
  237. package/dist/vocabulary.d.ts +14 -3
  238. package/dist/vocabulary.d.ts.map +1 -1
  239. package/dist/vocabulary.js +13 -3
  240. package/dist/vocabulary.js.map +1 -1
  241. package/docs/specs/cli.md +64 -38
  242. package/docs/specs/plugins.md +27 -20
  243. package/docs/specs/records.md +113 -0
  244. package/docs/specs/rest.md +20 -13
  245. package/docs/specs/runner.md +23 -17
  246. package/package.json +7 -2
  247. package/skills/building-a-console/SKILL.md +4 -4
  248. package/skills/building-a-plugin/SKILL.md +6 -6
  249. package/skills/deploying-a-console/SKILL.md +4 -4
  250. package/skills/deploying-a-console/references/resources.md +41 -4
  251. package/skills/deploying-a-console/references/running-it-again.md +3 -2
  252. package/skills/keeping-a-desk/SKILL.md +133 -0
  253. package/skills/keeping-a-desk/collections.md +96 -0
  254. package/skills/planning-the-board/SKILL.md +14 -18
  255. package/skills/planning-the-board/references/a-task.md +2 -2
  256. package/skills/planning-the-board/references/project-rules.md +13 -14
  257. package/skills/planning-the-board/references/setting-up.md +15 -13
  258. package/skills/running-an-agent/SKILL.md +10 -9
  259. package/skills/working-the-board/SKILL.md +62 -44
  260. package/skills/working-the-board/references/terms.md +9 -5
  261. package/skills/working-the-board/references/the-pack.md +12 -10
  262. package/skills/working-the-board/references/the-sentence.md +2 -2
  263. package/src/agent-page.tsx +3 -1
  264. package/src/api.ts +77 -2
  265. package/src/charts.ts +250 -0
  266. package/src/cli/cli.ts +1698 -476
  267. package/src/cli/command-words.ts +56 -0
  268. package/src/cli/credentials.ts +181 -20
  269. package/src/cli/directory.ts +28 -10
  270. package/src/cli/format.ts +301 -46
  271. package/src/cli/help.ts +102 -54
  272. package/src/cli/main.ts +72 -2
  273. package/src/cli/pack.ts +333 -372
  274. package/src/cli/plugin-check.ts +275 -34
  275. package/src/cli/plugin-cli.ts +28 -4
  276. package/src/cli/plugin-main.ts +1 -0
  277. package/src/cli/serve.ts +295 -2
  278. package/src/client.ts +192 -177
  279. package/src/components/agent-page.tsx +489 -243
  280. package/src/components/charts.tsx +680 -0
  281. package/src/components/desk-records.tsx +724 -0
  282. package/src/components/desk.tsx +471 -0
  283. package/src/components/event-feed.tsx +5 -1
  284. package/src/components/home.tsx +12 -0
  285. package/src/components/plugin-settings.tsx +171 -12
  286. package/src/components/projects.tsx +29 -12
  287. package/src/components/rules.tsx +94 -34
  288. package/src/components/setup.tsx +18 -3
  289. package/src/console.tsx +17 -9
  290. package/src/data.ts +37 -34
  291. package/src/desk-page.tsx +332 -0
  292. package/src/desk-records-page.tsx +256 -0
  293. package/src/desk-records.ts +808 -0
  294. package/src/desk.ts +329 -0
  295. package/src/feed-stream.ts +144 -0
  296. package/src/home-tiles.ts +55 -35
  297. package/src/i18n/en.ts +196 -9
  298. package/src/i18n/ko.ts +178 -8
  299. package/src/index.ts +118 -0
  300. package/src/limits.ts +11 -0
  301. package/src/onboarding.ts +12 -4
  302. package/src/plugin/context.ts +55 -12
  303. package/src/plugin/limits.ts +2 -0
  304. package/src/plugin/manifest.ts +38 -2
  305. package/src/plugin/rest-schemas.ts +41 -0
  306. package/src/plugin/sandbox.ts +4 -1
  307. package/src/plugin/testing.ts +63 -12
  308. package/src/plugin.ts +2 -0
  309. package/src/records.ts +702 -0
  310. package/src/routes.ts +3 -1
  311. package/src/rules.ts +108 -47
  312. package/src/runner/acp.ts +47 -29
  313. package/src/runner/claude.ts +113 -23
  314. package/src/runner/codex.ts +35 -25
  315. package/src/runner/desk.ts +198 -12
  316. package/src/runner/process.ts +11 -4
  317. package/src/runner/redact.ts +92 -6
  318. package/src/runner/runner.ts +275 -34
  319. package/src/runner/task-scheduler.ts +36 -4
  320. package/src/runner/types.ts +47 -1
  321. package/src/runner.ts +27 -2
  322. package/src/schemas.ts +83 -15
  323. package/src/services.ts +49 -14
  324. package/src/setup-guide.ts +6 -2
  325. package/src/styles/README.md +11 -1
  326. package/src/styles/console.css +9 -0
  327. package/src/styles/pages/desk.css +126 -0
  328. package/src/styles/pages/documents.css +5 -0
  329. package/src/styles/parts/bars.css +80 -0
  330. package/src/styles/parts/chart.css +173 -0
  331. package/src/styles/parts/figures.css +65 -0
  332. package/src/styles/parts/legend.css +48 -0
  333. package/src/styles/parts/series.css +32 -0
  334. package/src/styles/parts/share.css +25 -0
  335. package/src/styles/parts/spark.css +23 -0
  336. package/src/styles/parts/stat.css +69 -0
  337. package/src/styles/tokens.css +13 -7
  338. package/src/vocabulary.ts +16 -3
  339. package/template/desk-web/README.md +8 -0
  340. package/template/desk-web/index.html +13 -0
  341. package/template/desk-web/package.json +25 -0
  342. package/template/desk-web/src/main.tsx +24 -0
  343. package/template/desk-web/src/page.tsx +51 -0
  344. package/template/desk-web/tsconfig.json +16 -0
  345. package/template/desk-web/vite.config.ts +15 -0
package/src/data.ts CHANGED
@@ -4,7 +4,6 @@ import {
4
4
  type CommentInput,
5
5
  type ConsoleClient,
6
6
  type DocumentFilter,
7
- EVENT_KINDS,
8
7
  type FileUpload,
9
8
  LIST_LIMIT,
10
9
  type ProjectClient,
@@ -73,8 +72,8 @@ import {
73
72
  type RestWorkspace,
74
73
  type RestWorkspacePackConnect,
75
74
  type RestWorkspacePatch,
76
- restEventSchema,
77
75
  } from "./api.js";
76
+ import { followFeed, whileFeedSettles } from "./feed-stream.js";
78
77
 
79
78
  // What the console shows, and how it stays current: the workspace (who is signed in, the
80
79
  // projects, the people, the tokens) is loaded once; the project the page is on is loaded whole,
@@ -325,7 +324,11 @@ async function readFeed(
325
324
  key: string,
326
325
  signal: AbortSignal,
327
326
  ): Promise<RestEvent[]> {
328
- const result = await client.project(key).events(0, { latest: true }, signal);
327
+ // Asked again where the console cannot say yet where the feed stands: never begun from nothing.
328
+ const result = await whileFeedSettles(
329
+ () => client.project(key).events(0, { latest: true }, signal),
330
+ { signal },
331
+ );
329
332
  return result.items.slice(-FEED_KEEP);
330
333
  }
331
334
 
@@ -608,7 +611,7 @@ export function useConsoleData(client: ConsoleClient, projectKey: string | undef
608
611
  }
609
612
  const controller = new AbortController();
610
613
  const { signal } = controller;
611
- let source: EventSource | undefined;
614
+ let unfollow: (() => void) | undefined;
612
615
  let reloadTimer: ReturnType<typeof setTimeout> | undefined;
613
616
  const scope = client.project(projectKey);
614
617
  const run = async () => {
@@ -653,42 +656,38 @@ export function useConsoleData(client: ConsoleClient, projectKey: string | undef
653
656
  });
654
657
  }
655
658
  });
656
- if (typeof EventSource === "undefined") {
659
+ // The stream begins after the feed read, and is opened again from the last event that
660
+ // arrived when the browser gives it up, while the console restarts say (`feed-stream.ts`).
661
+ if (signal.aborted) {
657
662
  return;
658
663
  }
659
- source = new EventSource(scope.eventStreamUrl(lastEvent.current));
660
- source.onopen = () => setLive(true);
661
- source.onerror = () => setLive(false);
662
- const arrived = (message: MessageEvent) => {
663
- const parsed = restEventSchema.safeParse(JSON.parse(String(message.data)));
664
- if (!parsed.success || parsed.data.id <= lastEvent.current) {
665
- return;
666
- }
667
- lastEvent.current = parsed.data.id;
668
- const event = parsed.data;
669
- setEvents((current) => [...current, event].slice(-FEED_KEEP));
670
- clearTimeout(reloadTimer);
671
- reloadTimer = setTimeout(() => {
672
- refreshProject(signal).catch(() => undefined);
673
- if (event.kind.startsWith("project.")) {
674
- refreshWorkspace(signal).catch(() => undefined);
675
- }
676
- // A decision raised or answered here may be one of the person's own: the count beside
677
- // My decisions follows it.
678
- if (event.kind.startsWith("decision.")) {
679
- refreshMe(signal).catch(() => undefined);
680
- }
681
- }, RELOAD_DELAY_MS);
682
- };
683
- for (const kind of EVENT_KINDS) {
684
- source.addEventListener(kind, arrived);
685
- }
664
+ unfollow = followFeed({
665
+ url: (after) => scope.eventStreamUrl(after),
666
+ after: lastEvent.current,
667
+ onLive: setLive,
668
+ onEvent: (event) => {
669
+ lastEvent.current = event.id;
670
+ setEvents((current) => [...current, event].slice(-FEED_KEEP));
671
+ clearTimeout(reloadTimer);
672
+ reloadTimer = setTimeout(() => {
673
+ refreshProject(signal).catch(() => undefined);
674
+ if (event.kind.startsWith("project.")) {
675
+ refreshWorkspace(signal).catch(() => undefined);
676
+ }
677
+ // A decision raised or answered here may be one of the person's own: the count
678
+ // beside My decisions follows it.
679
+ if (event.kind.startsWith("decision.")) {
680
+ refreshMe(signal).catch(() => undefined);
681
+ }
682
+ }, RELOAD_DELAY_MS);
683
+ },
684
+ });
686
685
  };
687
686
  void run();
688
687
  return () => {
689
688
  controller.abort();
690
689
  clearTimeout(reloadTimer);
691
- source?.close();
690
+ unfollow?.();
692
691
  setLive(false);
693
692
  };
694
693
  }, [client, projectKey, signedIn, generation, refreshProject, refreshWorkspace, refreshMe]);
@@ -964,7 +963,11 @@ export function useConsoleData(client: ConsoleClient, projectKey: string | undef
964
963
  const history: RestEvent[] = [];
965
964
  let after = 0;
966
965
  for (let page = 0; page < FEED_MAX_PAGES; page += 1) {
967
- const result = await scope().events(after, { task: taskId }, signal);
966
+ const from = after;
967
+ const result = await whileFeedSettles(
968
+ () => scope().events(from, { task: taskId }, signal),
969
+ { signal },
970
+ );
968
971
  history.push(...result.items);
969
972
  after = result.items.at(-1)?.id ?? after;
970
973
  if (!result.more) {
@@ -0,0 +1,332 @@
1
+ import { useEffect, useMemo, useState } from "react";
2
+ import type { RestWorkBrief } from "./api.js";
3
+ import type { ConsoleClient } from "./client.js";
4
+ import {
5
+ DESK_VIEWS,
6
+ DeskArchive,
7
+ DeskFiguresView,
8
+ DeskStanding,
9
+ type DeskView,
10
+ DeskViews,
11
+ } from "./components/desk.js";
12
+ import { Callout, EmptyState, Spinner } from "./components/ui.js";
13
+ import type { CustomPage } from "./console.js";
14
+ import type { ConsoleData } from "./data.js";
15
+ import { deviceLanguage, firstDayOfWeek, today as todayOf } from "./dates.js";
16
+ import {
17
+ DESK_RUNS,
18
+ DESK_TASKS,
19
+ type DeskHistory,
20
+ type DeskProject,
21
+ type DeskRun,
22
+ figuresOf,
23
+ readHistory,
24
+ readOpenRuns,
25
+ withHistory,
26
+ } from "./desk.js";
27
+ import type { RecordsReader } from "./desk-records.js";
28
+ import { DeskRecords } from "./desk-records-page.js";
29
+ import { DEFAULT_LANGUAGES, useLanguage, useWords } from "./i18n/index.js";
30
+ import { customPageHref, decisionHref, type Navigation, taskHref } from "./router.js";
31
+ import { errorWords } from "./use-action.js";
32
+
33
+ // The page of a desk (ADR-0116, ADR-0117): one page of a console of a member's own, the
34
+ // member's own extension of the console's pages. It comes with the records' view, what the
35
+ // member made and what became of it, read from the desk's own files; beside it stand three
36
+ // views of the member's work on the board, read through the client when one is looked at: what
37
+ // stands now from the plate and the open runs, the figures and the archive from a bounded page
38
+ // of each project's closed tasks and ended runs. The address says which view, and what it
39
+ // shows (ADR-0010).
40
+
41
+ /** What the address of a desk's page says: its view, the records' where it names none, and the words the board's archive is found by. */
42
+ export function deskViewOf(search: string): { readonly view: DeskView; readonly words: string } {
43
+ const params = new URLSearchParams(search);
44
+ const named = params.get("view");
45
+ return {
46
+ view: DESK_VIEWS.find((view) => view === named) ?? "records",
47
+ words: (params.get("q") ?? "").trim(),
48
+ };
49
+ }
50
+
51
+ /** Where a view of a desk's page is, from the page's own address; the board's archive with the words it is found by. */
52
+ export function deskHref(base: string, view: DeskView, words = ""): string {
53
+ const params = new URLSearchParams();
54
+ if (view !== "records") {
55
+ params.set("view", view);
56
+ }
57
+ if (view === "archive" && words.length > 0) {
58
+ params.set("q", words);
59
+ }
60
+ const query = params.toString();
61
+ return query.length === 0 ? base : `${base}?${query}`;
62
+ }
63
+
64
+ export interface DeskPageProps {
65
+ readonly client: ConsoleClient;
66
+ /** The workspace as the console keeps it current: who looks, and the projects they may see. */
67
+ readonly data: Pick<ConsoleData, "me" | "projects">;
68
+ /** The query of the address the page is at. */
69
+ readonly search: string;
70
+ /** The page's own address, without a query. */
71
+ readonly href: string;
72
+ readonly navigation: Navigation;
73
+ /** What reads the desk's records; the page's own `fetch` at `/records` where left out. */
74
+ readonly records?: RecordsReader | undefined;
75
+ }
76
+
77
+ /** What stands now, as one visit read it. */
78
+ interface Standing {
79
+ readonly work: RestWorkBrief;
80
+ readonly runs: readonly DeskRun[];
81
+ }
82
+
83
+ /** The closed work read so far for some words: the pages of it as one. */
84
+ interface Read {
85
+ readonly words: string;
86
+ readonly pages: number;
87
+ readonly history: DeskHistory;
88
+ }
89
+
90
+ export function DeskPage(props: DeskPageProps) {
91
+ const pack = useWords();
92
+ const words = pack.desk;
93
+ const { tag } = useLanguage();
94
+ const { client, data, search, href, navigation } = props;
95
+ const { view, words: query } = deskViewOf(search);
96
+ const personId = data.me?.person?.id;
97
+ const weekStart = data.me?.workspace.weekStart;
98
+ // The projects by what a row says of each: a list read again that says the same is the same
99
+ // list here, so that nothing is read from the console a second time for it.
100
+ const listed = JSON.stringify(
101
+ data.projects.map((project) => [
102
+ project.key,
103
+ project.name,
104
+ project.color,
105
+ project.symbol,
106
+ project.icon ?? null,
107
+ ]),
108
+ );
109
+ // biome-ignore lint/correctness/useExhaustiveDependencies: the list is held by what it says, not by the array that holds it
110
+ const projects = useMemo<readonly DeskProject[]>(
111
+ () =>
112
+ data.projects.map((project) => ({
113
+ key: project.key,
114
+ name: project.name,
115
+ color: project.color,
116
+ symbol: project.symbol,
117
+ icon: project.icon ?? null,
118
+ })),
119
+ [listed],
120
+ );
121
+ const [standing, setStanding] = useState<Standing | undefined>(undefined);
122
+ // Everything closed, which the figures count and the archive lists; and what words found.
123
+ const [whole, setWhole] = useState<Read | undefined>(undefined);
124
+ const [found, setFound] = useState<Read | undefined>(undefined);
125
+ const [busy, setBusy] = useState(false);
126
+ const [problem, setProblem] = useState<string | undefined>(undefined);
127
+ const couldNotLoad = words.couldNotLoad;
128
+
129
+ // What stands now is read each time the view is come to: the plate, then the open runs of
130
+ // the projects where a task of the plate says one is.
131
+ useEffect(() => {
132
+ if (view !== "now" || personId === undefined) {
133
+ return;
134
+ }
135
+ const controller = new AbortController();
136
+ setProblem(undefined);
137
+ client
138
+ .plate(controller.signal)
139
+ .then(async (work) => ({
140
+ work,
141
+ runs: await readOpenRuns(client, { tasks: work.tasks, personId }, controller.signal),
142
+ }))
143
+ .then((read) => {
144
+ if (!controller.signal.aborted) {
145
+ setStanding(read);
146
+ }
147
+ })
148
+ .catch((failure: unknown) => {
149
+ if (!controller.signal.aborted) {
150
+ setProblem(errorWords(failure, couldNotLoad));
151
+ }
152
+ });
153
+ return () => controller.abort();
154
+ }, [client, view, personId, couldNotLoad]);
155
+
156
+ // The closed work is read each time a view that shows it is come to, or its words change:
157
+ // the first page of each project, for the figures everything, for the archive what the
158
+ // words find.
159
+ const wanted = view === "archive" ? query : "";
160
+ const reads = view === "figures" || view === "archive";
161
+ useEffect(() => {
162
+ if (!reads || personId === undefined) {
163
+ return;
164
+ }
165
+ const controller = new AbortController();
166
+ setProblem(undefined);
167
+ setBusy(true);
168
+ readHistory(client, { projects, personId, words: wanted }, controller.signal)
169
+ .then((history) => {
170
+ if (!controller.signal.aborted) {
171
+ (wanted.length === 0 ? setWhole : setFound)({ words: wanted, pages: 1, history });
172
+ }
173
+ })
174
+ .catch((failure: unknown) => {
175
+ if (!controller.signal.aborted) {
176
+ setProblem(errorWords(failure, couldNotLoad));
177
+ }
178
+ })
179
+ .finally(() => {
180
+ if (!controller.signal.aborted) {
181
+ setBusy(false);
182
+ }
183
+ });
184
+ return () => controller.abort();
185
+ }, [client, reads, wanted, personId, projects, couldNotLoad]);
186
+
187
+ if (personId === undefined) {
188
+ return null;
189
+ }
190
+ const shown = wanted.length === 0 ? whole : found?.words === wanted ? found : undefined;
191
+ /** The next page of what is shown, joined to what was read. */
192
+ const onMore = () => {
193
+ if (shown === undefined || busy) {
194
+ return;
195
+ }
196
+ setBusy(true);
197
+ readHistory(client, { projects, personId, words: shown.words, page: shown.pages })
198
+ .then((history) => {
199
+ const next = {
200
+ words: shown.words,
201
+ pages: shown.pages + 1,
202
+ history: withHistory(shown.history, history),
203
+ };
204
+ (shown.words.length === 0 ? setWhole : setFound)(next);
205
+ })
206
+ .catch((failure: unknown) => setProblem(errorWords(failure, couldNotLoad)))
207
+ .finally(() => setBusy(false));
208
+ };
209
+ // A run that ended on a task still open has its title on the plate, where the plate was read.
210
+ const open = new Map((standing?.work.tasks ?? []).map(({ task }) => [task.id, task.title]));
211
+ const named = (runs: readonly DeskRun[]): readonly DeskRun[] =>
212
+ runs.map((run) =>
213
+ run.title === undefined ? { ...run, title: open.get(run.run.taskId) } : run,
214
+ );
215
+ const now = todayOf();
216
+ const loading = (
217
+ <div className="sc-loading">
218
+ <Spinner label={words.loading} />
219
+ </div>
220
+ );
221
+ const narrowed =
222
+ shown !== undefined && shown.history.of > shown.history.read ? (
223
+ <p className="sc-muted">{words.projectsRead(shown.history.read, shown.history.of)}</p>
224
+ ) : null;
225
+ return (
226
+ <>
227
+ <div className="sc-page-head">
228
+ <h1 className="sc-page-title">{words.title}</h1>
229
+ <p className="sc-page-lead">{words.lead}</p>
230
+ </div>
231
+ <DeskViews view={view} viewHref={(one) => deskHref(href, one)} />
232
+ {problem !== undefined && view !== "records" && <Callout tone="danger">{problem}</Callout>}
233
+ {view === "records" ? (
234
+ <DeskRecords
235
+ search={search}
236
+ href={href}
237
+ navigation={navigation}
238
+ reader={props.records}
239
+ weekStart={weekStart}
240
+ taskHref={taskHref}
241
+ />
242
+ ) : view === "now" ? (
243
+ standing === undefined ? (
244
+ problem === undefined && loading
245
+ ) : standing.work.tasks.length === 0 &&
246
+ standing.work.decisions.length === 0 &&
247
+ standing.runs.length === 0 ? (
248
+ <EmptyState title={words.now.empty.title} body={words.now.empty.body} />
249
+ ) : (
250
+ <DeskStanding
251
+ tasks={standing.work.tasks}
252
+ decisions={standing.work.decisions}
253
+ runs={standing.runs}
254
+ today={now}
255
+ taskHref={taskHref}
256
+ decisionHref={decisionHref}
257
+ />
258
+ )
259
+ ) : view === "figures" ? (
260
+ shown === undefined ? (
261
+ problem === undefined && loading
262
+ ) : (
263
+ <>
264
+ <DeskFiguresView
265
+ figures={figuresOf({
266
+ tasks: shown.history.tasks,
267
+ runs: shown.history.runs,
268
+ today: now,
269
+ firstDay: firstDayOfWeek(deviceLanguage(tag), weekStart),
270
+ })}
271
+ partial={
272
+ shown.history.more
273
+ ? { tasks: shown.pages * DESK_TASKS, runs: shown.pages * DESK_RUNS }
274
+ : undefined
275
+ }
276
+ />
277
+ {narrowed}
278
+ </>
279
+ )
280
+ ) : shown === undefined ? (
281
+ problem === undefined && loading
282
+ ) : (
283
+ <>
284
+ <DeskArchive
285
+ // Keyed by the words, so that the field says the words the address does.
286
+ key={shown.words}
287
+ tasks={shown.history.tasks}
288
+ runs={named(shown.history.runs)}
289
+ query={shown.words}
290
+ busy={busy}
291
+ onSearch={(next) => navigation.go(deskHref(href, "archive", next))}
292
+ more={shown.history.more}
293
+ onMore={onMore}
294
+ taskHref={taskHref}
295
+ />
296
+ {narrowed}
297
+ </>
298
+ )}
299
+ </>
300
+ );
301
+ }
302
+
303
+ /** The key a desk's page has unless a console names another: its address is `/pages/desk`. */
304
+ export const DESK_PAGE_KEY = "desk";
305
+
306
+ /**
307
+ * A desk's page for a console of one's own: `createConsole({ pages: [deskPage()] })` adds it as
308
+ * a tab among the workspace's, at `/pages/<key>`. `records` says what reads the desk's records,
309
+ * where that is not the page's own `fetch` at `/records`.
310
+ */
311
+ export function deskPage(
312
+ options: { readonly key?: string | undefined; readonly records?: RecordsReader | undefined } = {},
313
+ ): CustomPage {
314
+ const key = options.key ?? DESK_PAGE_KEY;
315
+ return {
316
+ key,
317
+ label: Object.fromEntries(
318
+ DEFAULT_LANGUAGES.map((language) => [language.tag, language.messages.desk.tab]),
319
+ ),
320
+ level: "workspace",
321
+ render: (context) => (
322
+ <DeskPage
323
+ client={context.client}
324
+ data={context.data}
325
+ search={context.search}
326
+ href={customPageHref(key)}
327
+ navigation={context.navigation}
328
+ records={options.records}
329
+ />
330
+ ),
331
+ };
332
+ }