pi-pocket 0.25.0 → 0.26.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.
Files changed (85) hide show
  1. package/AGENTS.md +2 -0
  2. package/README.md +5 -3
  3. package/docs/architecture.md +21 -0
  4. package/docs/design/design-mode.md +108 -0
  5. package/docs/design/notes-a.md +106 -0
  6. package/docs/design/notes-b.md +123 -0
  7. package/docs/design/notes-c.md +92 -0
  8. package/docs/design/notes-d.md +78 -0
  9. package/docs/design/release-notes-design.md +46 -0
  10. package/docs/extensions/browser-modules.md +2 -0
  11. package/docs/extensions/policies/api-stability.md +2 -0
  12. package/docs/extensions/reference/api-versions.md +1 -0
  13. package/docs/extensions/reference/host.md +1 -1
  14. package/docs/extensions/reference/limits.md +4 -0
  15. package/docs/extensions/reference/names.md +2 -2
  16. package/docs/extensions/reference/pocket.md +38 -0
  17. package/docs/features.md +19 -5
  18. package/docs/map.md +75 -54
  19. package/docs/release-notes.md +1 -1
  20. package/docs/self-editing.md +1 -1
  21. package/docs/systemd.md +1 -1
  22. package/npm-shrinkwrap.json +2 -2
  23. package/package.json +1 -1
  24. package/src/launcher/main.ts +20 -9
  25. package/src/server/app.ts +113 -4
  26. package/src/server/background.ts +3 -3
  27. package/src/server/cache-keeper.ts +605 -0
  28. package/src/server/compaction.ts +68 -15
  29. package/src/server/design/bundle.ts +65 -0
  30. package/src/server/design/export.ts +118 -0
  31. package/src/server/design/files.ts +49 -0
  32. package/src/server/design/guide.ts +56 -0
  33. package/src/server/design/index.ts +232 -0
  34. package/src/server/design/pptx.ts +304 -0
  35. package/src/server/design/render.ts +593 -0
  36. package/src/server/design/system.ts +248 -0
  37. package/src/server/docs.ts +55 -0
  38. package/src/server/extensions/artifacts.ts +202 -18
  39. package/src/server/extensions/design/canvas.js +304 -0
  40. package/src/server/extensions/design/comments.js +15 -0
  41. package/src/server/extensions/design/data.js +263 -0
  42. package/src/server/extensions/design/devices.js +70 -0
  43. package/src/server/extensions/design/docks.js +647 -0
  44. package/src/server/extensions/design/edits.js +37 -0
  45. package/src/server/extensions/design/index.js +326 -0
  46. package/src/server/extensions/design/messages.js +92 -0
  47. package/src/server/extensions/design/parts.js +160 -0
  48. package/src/server/extensions/design/state.js +146 -0
  49. package/src/server/extensions/design/style.css +1332 -0
  50. package/src/server/extensions/design/tweaks.js +161 -0
  51. package/src/server/extensions/design/views.js +375 -0
  52. package/src/server/extensions/design/workspace.js +723 -0
  53. package/src/server/extensions/design.ts +620 -0
  54. package/src/server/extensions/plan.ts +11 -9
  55. package/src/server/extensions/prompt.ts +11 -3
  56. package/src/server/extensions/subagents.ts +7 -1
  57. package/src/server/extensions/updates/index.js +16 -6
  58. package/src/server/extensions/updates.ts +2 -2
  59. package/src/server/http/artifact-assets.ts +216 -0
  60. package/src/server/http/artifact-inspect.ts +1281 -0
  61. package/src/server/http/artifacts.ts +89 -14
  62. package/src/server/http/conversation-routes.ts +46 -1
  63. package/src/server/http.ts +12 -4
  64. package/src/server/main.ts +4 -0
  65. package/src/server/npm-install.ts +82 -0
  66. package/src/server/npm-update.ts +100 -0
  67. package/src/server/reload.ts +5 -0
  68. package/src/server/room.ts +2 -0
  69. package/src/server/spend.ts +73 -9
  70. package/src/server/update-state.ts +257 -8
  71. package/src/server/updates.ts +268 -33
  72. package/src/server/zip.ts +163 -0
  73. package/types/pocket.d.ts +32 -0
  74. package/web/activity-fold.js +28 -0
  75. package/web/artifact-bridge.js +506 -82
  76. package/web/extension-ui.js +21 -1
  77. package/web/pip.js +46 -9
  78. package/web/sessions.js +1 -1
  79. package/web/sheets/appearance.js +10 -0
  80. package/web/sheets/viewers.js +26 -1
  81. package/web/style.css +11 -0
  82. package/web/taps.js +26 -0
  83. package/web/theme.js +6 -0
  84. package/web/transcript.js +19 -10
  85. package/src/server/http/public.ts +0 -30
@@ -0,0 +1,723 @@
1
+ // The workspace itself: the picker, the toolbar, the modes, and what fills the room under them.
2
+ import { openOf } from "./comments.js";
3
+ import { createCanvas, frameUrl } from "./canvas.js";
4
+ import { designsOf, latestOf, siblingsOf } from "./data.js";
5
+ import { clampSize, DEVICES, exportViewport, sizeOf } from "./devices.js";
6
+ import { createDocks } from "./docks.js";
7
+ import { changed } from "./tweaks.js";
8
+ import { createViews } from "./views.js";
9
+ import { useStore } from "./state.js";
10
+
11
+ /** The sheet's own width classes: below 560 px it stacks; from 900 px the side panel sits beside the canvas. */
12
+ const NARROW = 560;
13
+ const SIDE = 900;
14
+ const STACK = 560;
15
+
16
+ export function createWorkspace(env) {
17
+ const { pocket, store, data, parts, app } = env;
18
+ const { html, useEffect, useMemo, useRef, useState } = pocket;
19
+ const { Ico, Popover, Segmented, useSize, ago } = parts;
20
+ const canvas = createCanvas(env);
21
+ const docks = createDocks(env);
22
+ const views = createViews(env, canvas);
23
+
24
+ /** Parts of the app's own state this reads: the session's artifacts, who the person is. Redraws only when they change. */
25
+ function useApp() {
26
+ const [, draw] = useState(0);
27
+
28
+ useEffect(() => {
29
+ let last = [app.state.view.artifacts, app.state.me, app.state.conversationId];
30
+
31
+ return app.subscribe((state) => {
32
+ const next = [state.view.artifacts, state.me, state.conversationId];
33
+
34
+ if (next.some((each, index) => each !== last[index])) {
35
+ last = next;
36
+ draw((count) => count + 1);
37
+ }
38
+ });
39
+ }, []);
40
+
41
+ return app.state;
42
+ }
43
+
44
+ /** The designs of the session, with the server's list of their groups and kinds. */
45
+ function useDesigns(appState, s) {
46
+ const ours = appState.conversationId === s.conversation;
47
+
48
+ return useMemo(
49
+ () => (ours ? designsOf(appState.view, s.listed) : []),
50
+ [ours, appState.view.artifacts, s.listed],
51
+ );
52
+ }
53
+
54
+ const listNote = (design) =>
55
+ `${design.id} · v${latestOf(design)} · ${ago(design.versions.at(-1)?.createdAt ?? 0)}`;
56
+
57
+ // ─── Pop-ups ─────────────────────────────────────────────────────────────
58
+
59
+ function Picker({ s, designs, design, onClose }) {
60
+ const [query, setQuery] = useState("");
61
+ const words = query.trim().toLowerCase();
62
+ const shown = designs.filter(
63
+ (each) =>
64
+ words === "" ||
65
+ each.title.toLowerCase().includes(words) ||
66
+ each.id.includes(words) ||
67
+ (each.group ?? "").includes(words),
68
+ );
69
+ const rows = [];
70
+ const grouped = new Set();
71
+
72
+ for (const each of shown) {
73
+ if (each.group && !grouped.has(each.group)) {
74
+ grouped.add(each.group);
75
+ rows.push({ header: each.group });
76
+ rows.push(...shown.filter((other) => other.group === each.group));
77
+ } else if (!each.group) {
78
+ rows.push(each);
79
+ }
80
+ }
81
+
82
+ return html`<${Popover}
83
+ label="Designs"
84
+ onClose=${onClose}
85
+ wide=${true}
86
+ sheet=${env.sizeClass(s) === "narrow"}
87
+ >
88
+ ${
89
+ designs.length > 7 &&
90
+ html`<input
91
+ class="dsn-search"
92
+ type="search"
93
+ placeholder="Find a design"
94
+ value=${query}
95
+ onInput=${(event) => setQuery(event.currentTarget.value)}
96
+ />`
97
+ }
98
+ <ul class="dsn-list" aria-label="Designs">
99
+ ${rows.map((row) =>
100
+ row.header
101
+ ? html`<li class="dsn-list-head" key=${`g:${row.header}`}>
102
+ <${Ico} name="layers" size=${13} /> ${row.header}
103
+ </li>`
104
+ : html`<li key=${row.id}>
105
+ <button
106
+ type="button"
107
+ aria-current=${row.id === design?.id}
108
+ class=${`dsn-list-item ${row.id === design?.id ? "on" : ""} ${row.group ? "in-group" : ""}`}
109
+ onClick=${() => {
110
+ env.open(row.id);
111
+ onClose();
112
+ }}
113
+ >
114
+ <span class="dsn-list-main">
115
+ <strong>${row.title}</strong>
116
+ <span class="muted small mono">${listNote(row)}</span>
117
+ </span>
118
+ ${row.kind === "deck" && html`<span class="dsn-chip">deck</span>`}
119
+ ${row.id === design?.id && html`<${Ico} name="check" size=${15} />`}
120
+ </button>
121
+ </li>`,
122
+ )}
123
+ ${shown.length === 0 && html`<li class="muted small dsn-empty">Nothing by that name.</li>`}
124
+ </ul>
125
+ <//>`;
126
+ }
127
+
128
+ function CustomSize({ s }) {
129
+ return html`<div class="dsn-custom">
130
+ <label>
131
+ <span>W</span>
132
+ <input
133
+ type="number"
134
+ inputmode="numeric"
135
+ min="200"
136
+ max="4000"
137
+ value=${s.custom.w}
138
+ onChange=${(event) => env.setCustom({ w: clampSize(event.currentTarget.value, s.custom.w) })}
139
+ />
140
+ </label>
141
+ <button
142
+ type="button"
143
+ class="icon-button"
144
+ title="Swap width and height"
145
+ aria-label="Swap width and height"
146
+ onClick=${() => env.setCustom({ w: s.custom.h, h: s.custom.w })}
147
+ >
148
+ <${Ico} name="swap" size=${14} />
149
+ </button>
150
+ <label>
151
+ <span>H</span>
152
+ <input
153
+ type="number"
154
+ inputmode="numeric"
155
+ min="200"
156
+ max="4000"
157
+ value=${s.custom.h}
158
+ onChange=${(event) => env.setCustom({ h: clampSize(event.currentTarget.value, s.custom.h) })}
159
+ />
160
+ </label>
161
+ <span class="muted small">px</span>
162
+ </div>`;
163
+ }
164
+
165
+ function MenuItem({ icon, label, hint, onClick, disabled, href, on }) {
166
+ const inner = html`<${Ico} name=${icon} size=${16} /><span class="grow">${label}</span>${
167
+ hint && html`<span class="muted small">${hint}</span>`
168
+ }${on && html`<${Ico} name="check" size=${15} />`}`;
169
+
170
+ return href
171
+ ? html`<a
172
+ class="dsn-menu-item"
173
+ href=${href}
174
+ target="_blank"
175
+ rel="noopener"
176
+ onClick=${onClick}
177
+ >${inner}</a
178
+ >`
179
+ : html`<button
180
+ type="button"
181
+ class="dsn-menu-item"
182
+ disabled=${disabled}
183
+ onClick=${onClick}
184
+ >
185
+ ${inner}
186
+ </button>`;
187
+ }
188
+
189
+ function Menu({ s, device, size, design, shown, group, deck, canShare, canEdit, onClose }) {
190
+ const [busy, setBusy] = useState(null);
191
+
192
+ const run = async (format) => {
193
+ setBusy(format);
194
+
195
+ await data.download({
196
+ artifact: design.id,
197
+ version: shown,
198
+ format,
199
+ viewport: exportViewport(device, size),
200
+ slide: s.deck?.current,
201
+ });
202
+
203
+ setBusy(null);
204
+ onClose();
205
+ };
206
+
207
+ const exporting = (format, label, hint) =>
208
+ html`<${MenuItem}
209
+ icon="download"
210
+ label=${busy === format ? `Making the ${label}…` : label}
211
+ hint=${hint}
212
+ disabled=${busy !== null}
213
+ onClick=${() => run(format)}
214
+ />`;
215
+
216
+ return html`<${Popover}
217
+ label="More"
218
+ onClose=${onClose}
219
+ align="end"
220
+ sheet=${env.sizeClass(s) === "narrow"}
221
+ >
222
+ <div class="dsn-menu">
223
+ <div class="dsn-menu-label">Backdrop</div>
224
+ <${Segmented}
225
+ label="Backdrop"
226
+ value=${s.backdrop}
227
+ onChange=${env.setBackdrop}
228
+ options=${[
229
+ { value: "auto", label: "Theme" },
230
+ { value: "light", label: "Light", icon: "sun" },
231
+ { value: "dark", label: "Dark", icon: "moon" },
232
+ ]}
233
+ />
234
+ <${MenuItem}
235
+ icon="reload"
236
+ label="Load the page again"
237
+ onClick=${() => {
238
+ env.reloadFrame();
239
+ onClose();
240
+ }}
241
+ />
242
+ <${MenuItem}
243
+ icon="monitor"
244
+ label="Custom size…"
245
+ on=${device === "custom"}
246
+ onClick=${() => {
247
+ env.setDevice("custom");
248
+ onClose();
249
+ }}
250
+ />
251
+ <div class="dsn-menu-label">Design</div>
252
+ <${MenuItem}
253
+ icon="compare"
254
+ label="Compare versions"
255
+ disabled=${design.versions.length < 2}
256
+ onClick=${() => {
257
+ env.compare();
258
+ onClose();
259
+ }}
260
+ />
261
+ ${
262
+ group.length > 1 &&
263
+ html`<${MenuItem}
264
+ icon="grid"
265
+ label="Variations"
266
+ hint=${`${group.length}`}
267
+ onClick=${() => {
268
+ env.setView("grid");
269
+ onClose();
270
+ }}
271
+ />`
272
+ }
273
+ ${
274
+ deck &&
275
+ html`<${MenuItem}
276
+ icon="play"
277
+ label="Present"
278
+ onClick=${() => {
279
+ store.set({ present: true });
280
+ onClose();
281
+ }}
282
+ />`
283
+ }
284
+ <div class="dsn-menu-label">${canEdit ? "Export and share" : "Open"}</div>
285
+ ${canEdit && exporting("png", "PNG image", `v${shown}`)}
286
+ ${canEdit && exporting("pdf", "PDF", `v${shown}`)}
287
+ ${
288
+ canEdit &&
289
+ deck &&
290
+ exporting("pptx", "PowerPoint (PPTX)", `${s.deck?.slides ?? ""} slides`)
291
+ }
292
+ ${canEdit && exporting("html", "HTML bundle (zip)", "for code")}
293
+ ${
294
+ canShare &&
295
+ html`<${MenuItem}
296
+ icon="share"
297
+ label="Share a link…"
298
+ onClick=${() => {
299
+ onClose();
300
+ env.share(design.id, shown);
301
+ }}
302
+ />`
303
+ }
304
+ <${MenuItem}
305
+ icon="external"
306
+ label="Open in its own tab"
307
+ href=${frameUrl(s.conversation, design.id, shown)}
308
+ onClick=${onClose}
309
+ />
310
+ </div>
311
+ <//>`;
312
+ }
313
+
314
+ // ─── The toolbar ─────────────────────────────────────────────────────────
315
+
316
+ function Bar({
317
+ s,
318
+ device,
319
+ size,
320
+ designs,
321
+ design,
322
+ shown,
323
+ latest,
324
+ group,
325
+ deck,
326
+ canShare,
327
+ canEdit,
328
+ }) {
329
+ const [open, setOpen] = useState(null);
330
+ const narrow = env.sizeClass(s) === "narrow";
331
+ const devices = [
332
+ { value: "mobile", icon: DEVICES.mobile.icon, title: "Phone, 390 × 844" },
333
+ { value: "tablet", icon: DEVICES.tablet.icon, title: "Tablet, 820 × 1180" },
334
+ deck
335
+ ? { value: "slide", icon: "monitor", title: "Slide, 1280 × 720" }
336
+ : { value: "desktop", icon: DEVICES.desktop.icon, title: "Desktop, 1280 × 800" },
337
+ ...(narrow ? [] : [{ value: "custom", icon: "fit", title: "Custom size" }]),
338
+ ];
339
+ const open_ = openOf(s.notes[design.id]?.comments ?? []).length;
340
+ const edits = (s.edits[`${design.id}@${shown}`] ?? []).length;
341
+ const moved = changed(s.knobs, s.tweaks[design.id] ?? {}).length;
342
+ const tabs = [
343
+ { id: "view", label: "Preview", icon: "eye" },
344
+ {
345
+ id: "comment",
346
+ label: canEdit ? "Comment" : "Comments",
347
+ icon: "comment",
348
+ badge: open_,
349
+ },
350
+ ...(canEdit ? [{ id: "edit", label: "Edit text", icon: "text", badge: edits }] : []),
351
+ ...(canEdit ? [{ id: "tweaks", label: "Tweaks", icon: "sliders", badge: moved }] : []),
352
+ { id: "versions", label: "Versions", icon: "history", badge: design.versions.length },
353
+ ];
354
+ const active =
355
+ s.mode === "comment"
356
+ ? "comment"
357
+ : s.mode === "edit"
358
+ ? "edit"
359
+ : s.dock === "comments"
360
+ ? "comment"
361
+ : s.dock === "tweaks"
362
+ ? "tweaks"
363
+ : s.dock === "versions"
364
+ ? "versions"
365
+ : "view";
366
+
367
+ return html`<div class="dsn-bars">
368
+ <div class="dsn-bar">
369
+ <button
370
+ type="button"
371
+ class="dsn-picker"
372
+ aria-haspopup="dialog"
373
+ onClick=${() => setOpen("designs")}
374
+ >
375
+ <${Ico} name="design" size=${16} />
376
+ <span class="dsn-picker-title">${design.title}</span>
377
+ <span class=${`dsn-version-tag mono ${shown !== latest ? "old" : ""}`}
378
+ >v${shown}</span
379
+ >
380
+ <${Ico} name="down" size=${14} />
381
+ </button>
382
+ <${Segmented}
383
+ label="Size"
384
+ iconOnly=${true}
385
+ value=${device}
386
+ options=${devices.map((device) => ({ ...device, label: device.title }))}
387
+ onChange=${env.setDevice}
388
+ />
389
+ <button
390
+ type="button"
391
+ class="icon-button"
392
+ title="More"
393
+ aria-label="More"
394
+ aria-haspopup="dialog"
395
+ onClick=${() => setOpen("more")}
396
+ >
397
+ <${Ico} name="more" size=${18} />
398
+ </button>
399
+ </div>
400
+ ${device === "custom" && html`<${CustomSize} s=${s} />`}
401
+ <div class="dsn-tabs" role="group" aria-label="Mode">
402
+ ${tabs.map(
403
+ (tab) =>
404
+ html`<button
405
+ type="button"
406
+ key=${tab.id}
407
+ class=${`dsn-tab ${active === tab.id ? "on" : ""}`}
408
+ aria-pressed=${active === tab.id}
409
+ aria-label=${tab.label}
410
+ title=${tab.label}
411
+ onClick=${() => env.setTab(tab.id)}
412
+ >
413
+ <${Ico} name=${tab.icon} size=${16} />
414
+ <span class="dsn-tab-label">${tab.label}</span>
415
+ ${tab.badge > 0 && html`<span class="dsn-count">${tab.badge}</span>`}
416
+ </button>`,
417
+ )}
418
+ <span class="grow"></span>
419
+ ${
420
+ design.versions.length > 1 &&
421
+ html`<button
422
+ type="button"
423
+ class=${`icon-button ${s.view === "compare" ? "on" : ""}`}
424
+ title="Compare versions"
425
+ aria-label="Compare versions"
426
+ aria-pressed=${s.view === "compare"}
427
+ onClick=${() => (s.view === "compare" ? env.setView("canvas") : env.compare())}
428
+ >
429
+ <${Ico} name="compare" size=${16} />
430
+ </button>`
431
+ }
432
+ ${
433
+ group.length > 1 &&
434
+ html`<button
435
+ type="button"
436
+ class=${`icon-button ${s.view === "grid" ? "on" : ""}`}
437
+ title=${`Variations (${group.length})`}
438
+ aria-label=${`Variations, ${group.length}`}
439
+ aria-pressed=${s.view === "grid"}
440
+ onClick=${() => env.setView(s.view === "grid" ? "canvas" : "grid")}
441
+ >
442
+ <${Ico} name="grid" size=${16} />
443
+ </button>`
444
+ }
445
+ ${
446
+ deck &&
447
+ html`<button
448
+ type="button"
449
+ class="icon-button"
450
+ title="Present"
451
+ aria-label="Present"
452
+ onClick=${() => store.set({ present: true })}
453
+ >
454
+ <${Ico} name="play" size=${16} />
455
+ </button>`
456
+ }
457
+ </div>
458
+ ${
459
+ open === "designs" &&
460
+ html`<${Picker}
461
+ s=${s}
462
+ designs=${designs}
463
+ design=${design}
464
+ onClose=${() => setOpen(null)}
465
+ />`
466
+ }
467
+ ${
468
+ open === "more" &&
469
+ html`<${Menu}
470
+ s=${s}
471
+ device=${device}
472
+ size=${size}
473
+ design=${design}
474
+ shown=${shown}
475
+ group=${group}
476
+ deck=${deck}
477
+ canShare=${canShare}
478
+ canEdit=${canEdit}
479
+ onClose=${() => setOpen(null)}
480
+ />`
481
+ }
482
+ </div>`;
483
+ }
484
+
485
+ // ─── The whole ───────────────────────────────────────────────────────────
486
+
487
+ function Empty({ canEdit }) {
488
+ const ideas = [
489
+ "Design a landing page for a small coffee roaster. Make it a single page with a hero, three products, and a signup form.",
490
+ "Make a clickable prototype of a habit tracker app: today, history, and settings screens.",
491
+ "Make a 6-slide deck that pitches a weekend project, with a title, problem, solution, demo, and next steps.",
492
+ ];
493
+
494
+ return html`<div class="dsn-empty-state">
495
+ <${Ico} name="design" size=${34} />
496
+ <h3>No designs yet</h3>
497
+ <p class="muted">
498
+ Ask Pi to design something: a prototype, a screen, a one-pager, or a slide deck. It
499
+ shows up here, where you can point at things, comment, tweak, and compare versions.
500
+ </p>
501
+ ${
502
+ canEdit &&
503
+ html`<div class="dsn-ideas">
504
+ ${ideas.map(
505
+ (idea) =>
506
+ html`<button
507
+ type="button"
508
+ class="dsn-idea"
509
+ onClick=${() => env.sendToBox(idea)}
510
+ >
511
+ ${idea}
512
+ </button>`,
513
+ )}
514
+ </div>`
515
+ }
516
+ </div>`;
517
+ }
518
+
519
+ function Workspace() {
520
+ const s = useStore(pocket, store);
521
+ const appState = useApp();
522
+ const root = useRef(null);
523
+ const { w: width } = useSize(root);
524
+ const designs = useDesigns(appState, s);
525
+ const design = designs.find((each) => each.id === s.artifact) ?? null;
526
+ const me = appState.me;
527
+ const canEdit = me?.role !== undefined && me.role !== "viewer";
528
+ const canShare = canEdit && me?.sessions === undefined;
529
+ const seen = useRef(null);
530
+ const seenFor = useRef(null);
531
+ const lastLatest = useRef({ id: null, version: 0 });
532
+
533
+ // The width classes, for the bars, the dock, and the compare view.
534
+ useEffect(() => {
535
+ if (width > 0 && Math.abs(width - (store.get().width ?? 0)) >= 1) {
536
+ store.set({ width });
537
+ }
538
+ }, [width]);
539
+
540
+ // Which design is open: one asked for, else the one before, else the newest. A design that is new while the panel is
541
+ // open comes forward, unless the person is in the middle of something.
542
+ useEffect(() => {
543
+ if (s.conversation === null) {
544
+ return;
545
+ }
546
+
547
+ if (s.wanted) {
548
+ const found = env.find(designs, s.wanted.artifact);
549
+
550
+ if (found) {
551
+ env.open(found.id, s.wanted.version ?? null);
552
+ store.set({ wanted: null });
553
+
554
+ return;
555
+ }
556
+
557
+ if (s.listed !== null) {
558
+ store.set({ wanted: null });
559
+ pocket.notify("info", `No design matches “${s.wanted.artifact}”.`);
560
+ }
561
+
562
+ return;
563
+ }
564
+
565
+ const ids = designs.map((each) => each.id);
566
+
567
+ // The designs a session had when it opened are not new ones; only those after it are.
568
+ if (seenFor.current !== s.conversation) {
569
+ seenFor.current = s.conversation;
570
+ seen.current = null;
571
+ }
572
+
573
+ if (seen.current === null && designs.length > 0) {
574
+ seen.current = new Set(ids);
575
+ }
576
+
577
+ const added = seen.current === null ? [] : ids.filter((id) => !seen.current.has(id));
578
+
579
+ for (const id of added) {
580
+ seen.current.add(id);
581
+ }
582
+
583
+ if (design === null && designs.length > 0) {
584
+ env.open(designs[0].id);
585
+ } else if (
586
+ added.length > 0 &&
587
+ design !== null &&
588
+ s.mode === "view" &&
589
+ s.draft === null &&
590
+ s.view === "canvas" &&
591
+ !(s.edits[`${design.id}@${latestOf(design)}`]?.length > 0)
592
+ ) {
593
+ const fresh = designs.find((each) => each.id === added[0]);
594
+
595
+ // Variations of what is open are for the grid, not for the canvas to jump to.
596
+ if (fresh && !(fresh.group && fresh.group === design.group)) {
597
+ env.open(fresh.id);
598
+ }
599
+ }
600
+ }, [designs, s.wanted, s.conversation]);
601
+
602
+ // Its comments and this person's tweaks, whenever the open design changes.
603
+ useEffect(() => {
604
+ if (s.artifact !== null) {
605
+ void data.notes(s.artifact);
606
+ }
607
+ }, [s.artifact, s.conversation]);
608
+
609
+ // A deck is shown at slide size by default, and a page is never shown at it.
610
+ const deck = design?.kind === "deck" || s.deck !== null;
611
+ const device =
612
+ deck && s.device === "desktop"
613
+ ? "slide"
614
+ : !deck && s.device === "slide"
615
+ ? "desktop"
616
+ : s.device;
617
+ const size = sizeOf(device, s.custom);
618
+ const latest = design ? latestOf(design) : 1;
619
+ const shown = design
620
+ ? design.versions.some((each) => each.version === s.version)
621
+ ? s.version
622
+ : latest
623
+ : 1;
624
+ const group = design ? siblingsOf(designs, design) : [];
625
+
626
+ // Pi makes a new version while the person is in the middle of something (a mode, a draft, text edits): the page they
627
+ // work on stays, with the way to the new one, instead of loading under them.
628
+ useEffect(() => {
629
+ const before = lastLatest.current;
630
+
631
+ lastLatest.current = { id: design?.id ?? null, version: latest };
632
+
633
+ if (
634
+ design &&
635
+ before.id === design.id &&
636
+ latest > before.version &&
637
+ s.version === null &&
638
+ s.view === "canvas" &&
639
+ (s.mode !== "view" ||
640
+ s.draft !== null ||
641
+ s.edits[`${design.id}@${before.version}`]?.length > 0)
642
+ ) {
643
+ store.set({ version: before.version });
644
+ }
645
+ }, [design?.id, latest]);
646
+ // Beside the canvas on a wide panel, or a narrower one that shows a tall page (a phone or a tablet): under it otherwise.
647
+ const room = store.get().width || width;
648
+ const side = room >= SIDE || (room >= STACK && size.h > size.w);
649
+
650
+ if (s.conversation === null) {
651
+ return html`<div class="dsn" ref=${root}>
652
+ <${pocket.Loader} label="Opening Design" />
653
+ </div>`;
654
+ }
655
+
656
+ if (!design) {
657
+ return html`<div class="dsn" ref=${root} data-size=${env.sizeClass(s)}>
658
+ ${s.listed === null && appState.view.artifacts?.length > 0 ? html`<${pocket.Loader} label="Reading the designs" />` : html`<${Empty} canEdit=${canEdit} />`}
659
+ </div>`;
660
+ }
661
+
662
+ const view =
663
+ s.view === "compare" && design.versions.length > 1
664
+ ? html`<${views.CompareView} s=${s} design=${design} size=${size} />`
665
+ : s.view === "grid" && group.length > 1
666
+ ? html`<${views.GridView}
667
+ s=${s}
668
+ design=${design}
669
+ designs=${designs}
670
+ size=${size}
671
+ canEdit=${canEdit}
672
+ />`
673
+ : html`<${canvas.CanvasView}
674
+ s=${s}
675
+ design=${design}
676
+ version=${shown}
677
+ size=${size}
678
+ readOnly=${!canEdit}
679
+ />`;
680
+
681
+ return html`<div
682
+ class="dsn"
683
+ ref=${root}
684
+ data-size=${env.sizeClass(s)}
685
+ data-side=${side}
686
+ data-dock=${s.dock ?? "none"}
687
+ >
688
+ <${Bar}
689
+ s=${s}
690
+ device=${device}
691
+ size=${size}
692
+ designs=${designs}
693
+ design=${design}
694
+ shown=${shown}
695
+ latest=${latest}
696
+ group=${group}
697
+ deck=${deck}
698
+ canShare=${canShare}
699
+ canEdit=${canEdit}
700
+ />
701
+ ${
702
+ shown !== latest &&
703
+ s.view === "canvas" &&
704
+ html`<div class="dsn-old">
705
+ ${`Version ${shown} of ${latest}. `}
706
+ <button type="button" class="dsn-link" onClick=${() => env.showVersion(null)}>
707
+ Show the newest
708
+ </button>
709
+ </div>`
710
+ }
711
+ <div class="dsn-main">
712
+ <div class="dsn-center">
713
+ ${s.present ? null : view}
714
+ ${s.view === "canvas" && !s.present && deck && html`<${views.DeckStrip} s=${s} />`}
715
+ </div>
716
+ <${docks.Dock} s=${s} design=${design} shown=${shown} canEdit=${canEdit} me=${me} />
717
+ </div>
718
+ ${s.present && html`<${views.Present} s=${s} design=${design} version=${shown} />`}
719
+ </div>`;
720
+ }
721
+
722
+ return { Workspace };
723
+ }