moi-computer 0.11.1-next.4 → 0.12.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 (131) hide show
  1. package/client/api/app-config.ts +7 -4
  2. package/client/app/AppRouter.tsx +2 -2
  3. package/client/app/routes/WorkspaceRoute.tsx +27 -19
  4. package/client/features/applets/{AppletMount.tsx → AppletContainer.tsx} +7 -5
  5. package/client/features/applets/WidgetShell.tsx +3 -3
  6. package/client/features/applets/applet-cache.ts +3 -5
  7. package/client/features/applets/applet-runtime.ts +27 -12
  8. package/client/features/applets/applet-thumbnail.ts +2 -1
  9. package/client/features/chat/applet-chat-intents.ts +14 -6
  10. package/client/features/chat/composer/attachments/draft-attachments.ts +21 -4
  11. package/client/features/chat/messages/MarkdownContent.tsx +5 -2
  12. package/client/features/chat/sessions/browser-tab-state.ts +18 -0
  13. package/client/features/chat/sessions/useSelectedSession.ts +55 -21
  14. package/client/features/chat/useChat.ts +4 -0
  15. package/client/features/collab/applet-api.ts +34 -0
  16. package/client/features/collab/client.ts +239 -0
  17. package/client/features/collab/components/cursors.tsx +199 -0
  18. package/client/features/collab/components/presence-avatars.tsx +61 -0
  19. package/client/features/collab/components/presence-frame.tsx +126 -0
  20. package/client/features/collab/components/presence-group.tsx +23 -0
  21. package/client/features/collab/components/presence-helpers.ts +50 -0
  22. package/client/features/collab/components/user-avatar-group.tsx +22 -0
  23. package/client/features/collab/components/user-avatar.tsx +40 -0
  24. package/client/features/collab/components/user.tsx +50 -0
  25. package/client/features/collab/hooks.tsx +164 -0
  26. package/client/features/collab/host-state.ts +254 -0
  27. package/client/features/collab/index.ts +5 -0
  28. package/client/features/collab/local-user.ts +46 -0
  29. package/client/features/collab/presence-publisher.ts +30 -0
  30. package/client/features/collab/presence-target.ts +8 -0
  31. package/client/features/collab/provider.tsx +79 -0
  32. package/client/features/collab/proxy-user.ts +35 -0
  33. package/client/features/collab/store.ts +156 -0
  34. package/client/features/collab/testing/component-fixtures.tsx +54 -0
  35. package/client/features/collab/testing/fake-engine.ts +129 -0
  36. package/client/features/collab/users.ts +106 -0
  37. package/client/features/collab/workspace-users-menu.tsx +194 -0
  38. package/client/features/connectors/mcp-icons.ts +0 -2
  39. package/client/features/dev/DevIndexPage.tsx +5 -0
  40. package/client/features/dev/DevRoutes.tsx +16 -4
  41. package/client/features/dev/collab/collab-page.tsx +24 -0
  42. package/client/features/dev/collab/collab-playground.tsx +614 -0
  43. package/client/features/dev/collab/local-user-settings.tsx +166 -0
  44. package/client/features/dev/collab/playground-room.ts +33 -0
  45. package/client/features/dev/{UiComponentsPage.tsx → ui-components/ui-components-page.tsx} +1 -1
  46. package/client/features/overview/WidgetCanvas.tsx +3 -1
  47. package/client/features/views/ViewManager.tsx +12 -7
  48. package/client/features/views/api.ts +2 -2
  49. package/client/features/views/useViewBuilderActions.ts +3 -0
  50. package/client/features/workspace/WorkspaceContext.tsx +1 -1
  51. package/client/features/workspace/WorkspaceLayoutContext.tsx +8 -2
  52. package/client/features/workspace/WorkspaceScreen.tsx +19 -11
  53. package/client/features/workspace/WorkspaceTabs.tsx +3 -3
  54. package/client/features/workspace/api.ts +2 -1
  55. package/client/features/workspace/browser-tab-state.ts +44 -0
  56. package/client/features/workspace/layout-save.ts +13 -0
  57. package/client/features/workspace/moi-context.ts +2 -1
  58. package/client/features/workspace/useWorkspaceNavigation.ts +20 -8
  59. package/client/index.css +45 -0
  60. package/client/index.tsx +9 -4
  61. package/client/main.tsx +1 -11
  62. package/client/theme.css +4 -1
  63. package/dist/chunk-2jncyq65.js +1 -0
  64. package/dist/{chunk-brr0zbeh.js → chunk-3a48fyf7.js} +2 -2
  65. package/dist/{chunk-tzz101ea.js → chunk-97eg9mjn.js} +1 -1
  66. package/dist/chunk-e2q5ken7.js +68 -0
  67. package/dist/{chunk-zs4mte79.js → chunk-efdyz6xp.js} +1 -1
  68. package/dist/chunk-h2kj1ysj.js +1 -0
  69. package/dist/chunk-jf6czgrz.js +2 -0
  70. package/dist/chunk-nez8sdta.js +1 -0
  71. package/dist/chunk-nzwgm8vc.css +1 -0
  72. package/dist/chunk-ph0h6h86.js +1 -0
  73. package/dist/chunk-ryg9z3kn.js +10 -0
  74. package/dist/chunk-tpn177bt.js +2 -0
  75. package/dist/chunk-vfpsc75k.js +2 -0
  76. package/dist/{chunk-m1fvqssh.js → chunk-z3byfve5.js} +2 -2
  77. package/dist/index.html +1 -1
  78. package/lib/collab/colors.ts +30 -0
  79. package/lib/collab/protocol.ts +84 -0
  80. package/lib/collab/types.ts +52 -0
  81. package/lib/experimental.ts +14 -0
  82. package/lib/moi-context.ts +11 -3
  83. package/lib/navigation.ts +25 -5
  84. package/lib/types.ts +20 -33
  85. package/package.json +2 -2
  86. package/server/AGENTS.md +1 -1
  87. package/server/api.ts +28 -5
  88. package/server/app-config.ts +127 -34
  89. package/server/applet-typecheck.ts +8 -3
  90. package/server/applets/applet-css.ts +1 -1
  91. package/server/applets/build-applet.ts +9 -3
  92. package/server/applets/declarations/base.d.ts +75 -0
  93. package/server/applets/declarations/collab.d.ts +88 -0
  94. package/server/applets/declarations.ts +46 -0
  95. package/server/applets/index.ts +8 -8
  96. package/server/applets/runtime/collab.ts +94 -0
  97. package/server/applets/runtime/moi.ts +12 -19
  98. package/server/applets/runtime/rpc.ts +2 -4
  99. package/server/chat-selection.ts +22 -0
  100. package/server/cli.ts +86 -21
  101. package/server/collab/cloudflare-access.ts +78 -0
  102. package/server/collab/config.ts +18 -0
  103. package/server/collab/manager.ts +407 -0
  104. package/server/collab/service.ts +136 -0
  105. package/server/collab/skill/index.ts +53 -0
  106. package/server/collab/worker-messages.ts +10 -0
  107. package/server/collab/worker.ts +52 -0
  108. package/server/layout.ts +10 -3
  109. package/server/moi-scaffold.ts +55 -84
  110. package/server/preview.ts +1 -1
  111. package/server/skill-update.ts +7 -9
  112. package/server/skills-template.ts +14 -2
  113. package/server/web.ts +65 -17
  114. package/server/workspace-init.ts +6 -3
  115. package/ui-components/avatar.tsx +24 -11
  116. package/ui-components/button.tsx +9 -9
  117. package/ui-components/docs/avatar.md +11 -30
  118. package/workspace/.claude/skills/moi-workspace/SKILL.md +14 -11
  119. package/workspace/.claude/skills/moi-workspace/references/COLLAB.md +262 -0
  120. package/workspace/.claude/skills/moi-workspace/references/INTENTS.md +10 -7
  121. package/client/assets/mcp/agentation.png +0 -0
  122. package/dist/agentation-4s0nn45p.png +0 -0
  123. package/dist/chunk-2gs8jjx4.css +0 -1
  124. package/dist/chunk-7vvg5fyz.js +0 -68
  125. package/dist/chunk-94m3q9ce.js +0 -1
  126. package/dist/chunk-by2wqgak.js +0 -1
  127. package/dist/chunk-p34hvqt3.js +0 -1
  128. package/dist/chunk-p83f0jnj.js +0 -2
  129. package/dist/chunk-xg01f6pn.js +0 -10
  130. /package/client/features/dev/{UiComponentPreviews.tsx → ui-components/ui-component-previews.tsx} +0 -0
  131. /package/server/applets/{runtime/base.ts → api-base.ts} +0 -0
@@ -0,0 +1,614 @@
1
+ import { useEffect, useState } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import type { PresenceAvatarsProps } from 'moi/collab'
4
+
5
+ import { IconChevronDown, IconPlus, IconTrash } from '@tabler/icons-react'
6
+
7
+ import { Button } from '@/client/components/ui/button'
8
+ import {
9
+ Collapsible,
10
+ CollapsibleContent,
11
+ CollapsibleTrigger
12
+ } from '@/client/components/ui/collapsible'
13
+ import {
14
+ Dialog,
15
+ DialogContent,
16
+ DialogDescription,
17
+ DialogTitle
18
+ } from '@/client/components/ui/dialog'
19
+ import { Input } from '@/client/components/ui/input'
20
+ import { Textarea } from '@/client/components/ui/textarea'
21
+ import {
22
+ Select,
23
+ SelectContent,
24
+ SelectGroup,
25
+ SelectItem,
26
+ SelectTrigger,
27
+ SelectValue
28
+ } from '@/ui-components/select'
29
+ import type { UserProfile, Connection } from '@/lib/collab/types'
30
+
31
+ import { PresenceFrame } from '@/client/features/collab/components/presence-frame'
32
+ import { PresenceGroup } from '@/client/features/collab/components/presence-group'
33
+ import { PresenceAvatars } from '@/client/features/collab/components/presence-avatars'
34
+ import { Cursors } from '@/client/features/collab/components/cursors'
35
+ import { UserAvatarGroup } from '@/client/features/collab/components/user-avatar-group'
36
+ import { User } from '@/client/features/collab/components/user'
37
+ import { UserAvatar } from '@/client/features/collab/components/user-avatar'
38
+ import { createFakeEngine } from '@/client/features/collab/testing/fake-engine'
39
+ import type { FakeEngine } from '@/client/features/collab/testing/fake-engine'
40
+ import {
41
+ useMe,
42
+ usePeers,
43
+ usePresence,
44
+ usePublishPresence,
45
+ useUser,
46
+ useWorkspaceUsers
47
+ } from '@/client/features/collab/hooks'
48
+ import { AppletPresenceProvider, CollabContext } from '@/client/features/collab/provider'
49
+ import { connectPlaygroundParticipants } from './playground-room'
50
+
51
+ const YOU: UserProfile = {
52
+ id: 'you',
53
+ name: 'You',
54
+ email: 'you@example.com',
55
+ color: 'violet'
56
+ }
57
+ const USERS: UserProfile[] = [
58
+ YOU,
59
+ { id: 'fig', name: 'Fig', email: 'fig@example.com', color: 'amber' },
60
+ { id: 'alex', name: 'Alex Hao', email: 'alex@example.com', color: 'blue' },
61
+ { id: 'andrea', name: 'Andrea Lim', email: 'andrea@example.com', color: 'pink' },
62
+ { id: 'pierre', name: 'Pierre', email: 'pierre@example.com', color: 'orange' },
63
+ { id: 'david', name: 'David Tibbitts', email: 'david@example.com', color: 'emerald' }
64
+ ]
65
+ const PAGE = 'kit'
66
+ const APPLET_ID = 'views/kit'
67
+ const BACKGROUND_CONNECTIONS: Connection[] = [
68
+ {
69
+ connectionId: 'sample-alex',
70
+ userId: 'alex',
71
+ location: { page: PAGE, status: 'active' },
72
+ presence: []
73
+ },
74
+ {
75
+ connectionId: 'sample-pierre',
76
+ userId: 'pierre',
77
+ location: { page: 'views/board', status: 'active' },
78
+ presence: []
79
+ },
80
+ { connectionId: 'sample-andrea', userId: 'andrea', location: null, presence: [] }
81
+ ]
82
+
83
+ type CodeExampleProps = { code: string }
84
+ function CodeExample({ code }: CodeExampleProps) {
85
+ return (
86
+ <Collapsible className="flex flex-col items-start gap-2">
87
+ <CollapsibleTrigger
88
+ render={<Button variant="ghost" size="sm" className="text-muted-foreground" />}
89
+ >
90
+ <IconChevronDown stroke={1.75} />
91
+ Code
92
+ </CollapsibleTrigger>
93
+ <CollapsibleContent className="w-full">
94
+ <pre className="overflow-x-auto rounded-lg bg-muted p-3 font-mono text-xs leading-5">
95
+ <code>{code}</code>
96
+ </pre>
97
+ </CollapsibleContent>
98
+ </Collapsible>
99
+ )
100
+ }
101
+
102
+ type SectionProps = { title: string; hint: string; code?: string; children: ReactNode }
103
+ function Section({ title, hint, code, children }: SectionProps) {
104
+ return (
105
+ <section className="flex min-w-0 flex-col gap-4">
106
+ <header>
107
+ <h2 className="text-sm font-medium">{title}</h2>
108
+ <p className="text-sm text-muted-foreground">{hint}</p>
109
+ </header>
110
+ {children}
111
+ {code && <CodeExample code={code} />}
112
+ </section>
113
+ )
114
+ }
115
+
116
+ type Task = { id: string; title: string }
117
+ const INITIAL_TASKS: Task[] = [
118
+ { id: 'launch', title: 'Ship the demo' },
119
+ { id: 'notes', title: 'Write the announcement' }
120
+ ]
121
+
122
+ const AVATAR_ALIGNMENTS = [
123
+ { value: 'start', label: 'Start (top)' },
124
+ { value: 'center', label: 'Center (default)' },
125
+ { value: 'end', label: 'End (bottom)' }
126
+ ] as const
127
+
128
+ function TargetPresenceDemo() {
129
+ const [selected, setSelected] = useState(false)
130
+ const [editing, setEditing] = useState(false)
131
+ const [avatarAlign, setAvatarAlign] =
132
+ useState<NonNullable<PresenceAvatarsProps['align']>>('center')
133
+ return (
134
+ <Section
135
+ title="Automatic and controlled presence"
136
+ hint="Each automatic target contains two fields. Move between them: the other pane keeps showing presence at that target. Cards publish the boolean you control with their button, even when focus moves away."
137
+ code={
138
+ '<PresenceGroup id="comparison">\n <PresenceFrame id="focus">\n <Input />\n <Input />\n </PresenceFrame>\n <PresenceAvatars id="avatars" align={avatarAlign}>\n <>\n <Input />\n <Input />\n </>\n </PresenceAvatars>\n <PresenceAvatars id="controlled-avatars" present={editing} align={avatarAlign}>{children}</PresenceAvatars>\n <PresenceFrame id="selection" present={selected} align="start">\n <Button onClick={() => setSelected(value => !value)}>Select item</Button>\n </PresenceFrame>\n</PresenceGroup>'
139
+ }
140
+ >
141
+ <div className="flex flex-wrap items-center gap-2">
142
+ <span className="text-sm text-muted-foreground">Avatar alignment in this pane</span>
143
+ <Select
144
+ items={AVATAR_ALIGNMENTS}
145
+ value={avatarAlign}
146
+ onValueChange={value => value && setAvatarAlign(value)}
147
+ >
148
+ <SelectTrigger size="sm" aria-label="Avatar alignment">
149
+ <SelectValue />
150
+ </SelectTrigger>
151
+ <SelectContent>
152
+ <SelectGroup>
153
+ {AVATAR_ALIGNMENTS.map(({ value, label }) => (
154
+ <SelectItem key={value} value={value}>
155
+ {label}
156
+ </SelectItem>
157
+ ))}
158
+ </SelectGroup>
159
+ </SelectContent>
160
+ </Select>
161
+ </div>
162
+ <PresenceGroup id="comparison">
163
+ <div className="grid gap-8 py-3 sm:grid-cols-2">
164
+ <PresenceFrame id="focus" className="space-y-2">
165
+ <label className="flex flex-col gap-2 text-sm">
166
+ PresenceFrame
167
+ <Input aria-label="PresenceFrame example" defaultValue="Focus this field" />
168
+ </label>
169
+ <Input
170
+ aria-label="PresenceFrame second field"
171
+ placeholder="Another field, same target"
172
+ />
173
+ </PresenceFrame>
174
+ <PresenceAvatars id="avatars" align={avatarAlign}>
175
+ <>
176
+ <label className="flex flex-col gap-2 text-sm">
177
+ PresenceAvatars
178
+ <Input aria-label="PresenceAvatars example" defaultValue="Focus this field" />
179
+ </label>
180
+ <Input
181
+ aria-label="PresenceAvatars second field"
182
+ className="mt-2"
183
+ placeholder="Another field, same target"
184
+ />
185
+ </>
186
+ </PresenceAvatars>
187
+ </div>
188
+ <PresenceFrame id="selection" present={selected} align="start">
189
+ <div className="flex flex-col items-start gap-3 rounded-lg bg-accent p-4">
190
+ <p className="text-sm">Selection: a task card</p>
191
+ <Button
192
+ size="sm"
193
+ variant="secondary"
194
+ aria-pressed={selected}
195
+ onClick={() => setSelected(value => !value)}
196
+ >
197
+ {selected ? 'Deselect your item' : 'Select your item'}
198
+ </Button>
199
+ <p className="text-sm text-muted-foreground">
200
+ Your selection: {selected ? 'selected' : 'unselected'}. Focus another field to keep
201
+ this selection.
202
+ </p>
203
+ </div>
204
+ </PresenceFrame>
205
+ <PresenceAvatars id="controlled-avatars" present={editing} align={avatarAlign}>
206
+ <div className="flex flex-col items-start gap-3 rounded-lg bg-accent p-4">
207
+ <p className="text-sm">Controlled avatars: editing a card</p>
208
+ <Button
209
+ size="sm"
210
+ variant="secondary"
211
+ aria-pressed={editing}
212
+ onClick={() => setEditing(value => !value)}
213
+ >
214
+ {editing ? 'Stop editing' : 'Start editing'}
215
+ </Button>
216
+ <Input
217
+ aria-label="Controlled editing example"
218
+ defaultValue="Focus alone does not publish here"
219
+ />
220
+ </div>
221
+ </PresenceAvatars>
222
+ </PresenceGroup>
223
+ </Section>
224
+ )
225
+ }
226
+
227
+ function EditableList() {
228
+ const [tasks, setTasks] = useState(INITIAL_TASKS)
229
+ return (
230
+ <Section
231
+ title="PresenceAvatars and stable list IDs"
232
+ hint="PresenceGroup gives each task a stable focus ID. Focus a task in the other pane, then reorder or remove rows here. These buttons keep that field focused. Remote avatars and cursors follow the task ID, and disappear if it is removed. Text edits stay local."
233
+ code={
234
+ '<PresenceGroup id="tasks">\n <div className="space-y-4">\n {tasks.map(task => (\n <PresenceAvatars key={task.id} id={task.id}>\n <Input value={task.title} onChange={...} />\n </PresenceAvatars>\n ))}\n </div>\n</PresenceGroup>'
235
+ }
236
+ >
237
+ <div className="flex flex-wrap gap-2">
238
+ <Button
239
+ size="sm"
240
+ variant="secondary"
241
+ onMouseDown={event => event.preventDefault()}
242
+ onClick={() => setTasks(current => [...current].reverse())}
243
+ >
244
+ Reverse order
245
+ </Button>
246
+ <Button
247
+ size="sm"
248
+ variant="secondary"
249
+ onClick={() =>
250
+ setTasks(current => [...current, { id: crypto.randomUUID(), title: 'New task' }])
251
+ }
252
+ >
253
+ <IconPlus stroke={1.75} /> Add task
254
+ </Button>
255
+ <Button size="sm" variant="ghost" onClick={() => setTasks(INITIAL_TASKS)}>
256
+ Reset list
257
+ </Button>
258
+ </div>
259
+ <PresenceGroup id="tasks">
260
+ <div className="flex flex-col gap-8 py-3">
261
+ {tasks.map(task => (
262
+ <PresenceAvatars key={task.id} id={task.id}>
263
+ <div className="flex items-center gap-2">
264
+ <Input
265
+ className="min-w-0 flex-1"
266
+ aria-label={`Title for ${task.id}`}
267
+ value={task.title}
268
+ onChange={event =>
269
+ setTasks(current =>
270
+ current.map(item =>
271
+ item.id === task.id ? { ...item, title: event.target.value } : item
272
+ )
273
+ )
274
+ }
275
+ />
276
+ <Button
277
+ size="icon-sm"
278
+ variant="ghost"
279
+ aria-label={`Remove ${task.title}`}
280
+ onMouseDown={event => event.preventDefault()}
281
+ onClick={() => setTasks(current => current.filter(item => item.id !== task.id))}
282
+ >
283
+ <IconTrash stroke={1.75} />
284
+ </Button>
285
+ </div>
286
+ </PresenceAvatars>
287
+ ))}
288
+ </div>
289
+ </PresenceGroup>
290
+ {!tasks.length && (
291
+ <p className="text-sm text-muted-foreground">No tasks. Add a task or reset the list.</p>
292
+ )}
293
+ </Section>
294
+ )
295
+ }
296
+
297
+ function NestedForm() {
298
+ const [name, setName] = useState('Q3 launch')
299
+ const [notes, setNotes] = useState('Confirm the venue by Friday.')
300
+ return (
301
+ <Section
302
+ title="PresenceFrame and nested groups"
303
+ hint="PresenceGroup combines IDs without adding layout. PresenceFrame automatically tracks the controls inside each label. The nearest frame owns focus, so the outer frame stays quiet."
304
+ code={
305
+ '<PresenceGroup id="project">\n <PresenceGroup id="launch">\n <PresenceFrame id="section">\n <div className="space-y-6">\n <PresenceGroup id="fields">\n <PresenceFrame id="name">\n <label>Project name <Input /></label>\n </PresenceFrame>\n <PresenceFrame id="notes">\n <label>Project notes <Textarea /></label>\n </PresenceFrame>\n </PresenceGroup>\n </div>\n </PresenceFrame>\n </PresenceGroup>\n</PresenceGroup>'
306
+ }
307
+ >
308
+ <PresenceGroup id="project">
309
+ <PresenceGroup id="launch">
310
+ <PresenceFrame id="section">
311
+ <div className="flex flex-col gap-6 rounded-lg bg-muted p-4">
312
+ <PresenceGroup id="fields">
313
+ <PresenceFrame id="name">
314
+ <label className="flex flex-col gap-2 text-sm">
315
+ Project name
316
+ <Input value={name} onChange={event => setName(event.target.value)} />
317
+ </label>
318
+ </PresenceFrame>
319
+ <PresenceFrame id="notes">
320
+ <label className="flex flex-col gap-2 text-sm">
321
+ Project notes
322
+ <Textarea value={notes} onChange={event => setNotes(event.target.value)} />
323
+ </label>
324
+ </PresenceFrame>
325
+ <PresenceFrame id="reset">
326
+ <Button
327
+ size="sm"
328
+ variant="secondary"
329
+ className="self-start"
330
+ onClick={() => setNotes('Confirm the venue by Friday.')}
331
+ >
332
+ Reset notes
333
+ </Button>
334
+ </PresenceFrame>
335
+ </PresenceGroup>
336
+ </div>
337
+ </PresenceFrame>
338
+ </PresenceGroup>
339
+ </PresenceGroup>
340
+ </Section>
341
+ )
342
+ }
343
+
344
+ type RecordEditorProps = { record: Task; onChange: (record: Task) => void }
345
+ function RecordEditor({ record, onChange }: RecordEditorProps) {
346
+ return (
347
+ <PresenceGroup id="todo">
348
+ <PresenceGroup id={record.id}>
349
+ <PresenceFrame id="title">
350
+ <label className="flex flex-col gap-2 text-sm">
351
+ Record title ({record.id})
352
+ <Input
353
+ value={record.title}
354
+ onChange={event => onChange({ ...record, title: event.target.value })}
355
+ />
356
+ </label>
357
+ </PresenceFrame>
358
+ </PresenceGroup>
359
+ </PresenceGroup>
360
+ )
361
+ }
362
+
363
+ function RecordDialogs() {
364
+ const [record, setRecord] = useState(INITIAL_TASKS[0]!)
365
+ const [open, setOpen] = useState(false)
366
+ return (
367
+ <Section
368
+ title="Record scope and dialogs"
369
+ hint="Choose the same record in both panes, then focus its title. Change the record in the receiving pane: the remote frame disappears. Open a dialog to test presence across a portal."
370
+ code={
371
+ '<PresenceGroup id="todo">\n <PresenceGroup id={record.id}>\n <PresenceFrame id="title"><Input /></PresenceFrame>\n </PresenceGroup>\n</PresenceGroup>'
372
+ }
373
+ >
374
+ <div className="flex flex-wrap gap-2">
375
+ {INITIAL_TASKS.map(task => (
376
+ <Button
377
+ key={task.id}
378
+ size="sm"
379
+ variant="secondary"
380
+ aria-pressed={record.id === task.id}
381
+ onMouseDown={event => event.preventDefault()}
382
+ onClick={() => setRecord(task)}
383
+ >
384
+ {task.id}
385
+ </Button>
386
+ ))}
387
+ <Button size="sm" variant="secondary" onClick={() => setOpen(true)}>
388
+ Open dialog
389
+ </Button>
390
+ </div>
391
+ {!open && <RecordEditor record={record} onChange={setRecord} />}
392
+ <Dialog open={open} onOpenChange={setOpen}>
393
+ <DialogContent>
394
+ <DialogTitle className="font-medium">Edit {record.id}</DialogTitle>
395
+ <DialogDescription>Watch the matching record in the other pane.</DialogDescription>
396
+ <Cursors id="examples" className="pt-6">
397
+ <RecordEditor record={record} onChange={setRecord} />
398
+ </Cursors>
399
+ </DialogContent>
400
+ </Dialog>
401
+ </Section>
402
+ )
403
+ }
404
+
405
+ type OutputProps = { value: unknown }
406
+ function Output({ value }: OutputProps) {
407
+ return (
408
+ <pre className="max-h-48 overflow-auto rounded-lg bg-muted p-3 font-mono text-xs leading-5">
409
+ {JSON.stringify(value, (key, entry) => (key === 'avatar' ? undefined : entry), 2) ??
410
+ 'undefined'}
411
+ </pre>
412
+ )
413
+ }
414
+
415
+ function ConnectedUsersDemo() {
416
+ const me = useMe()
417
+ const pagePeers = usePeers()
418
+ const workspacePeers = usePeers({ scope: 'workspace' })
419
+ const ownIds = me ? [me.id] : []
420
+ const pageIds = [...ownIds, ...pagePeers.map(user => user.id)]
421
+ const workspaceIds = [...ownIds, ...workspacePeers.map(user => user.id)]
422
+
423
+ return (
424
+ <Section
425
+ title="Connected users"
426
+ hint="Compose useMe and usePeers with UserAvatarGroup to choose who appears. Workspace scope includes other pages and away users. Offline users are excluded."
427
+ code={
428
+ "const me = useMe()\nconst peers = usePeers() // Or { scope: 'workspace' }\nconst ids = [...(me ? [me.id] : []), ...peers.map(user => user.id)]\n<UserAvatarGroup ids={ids} max={ids.length} />"
429
+ }
430
+ >
431
+ <div className="grid gap-4 sm:grid-cols-2">
432
+ <div className="flex flex-col items-start gap-2">
433
+ <p className="text-sm text-muted-foreground">This page</p>
434
+ <UserAvatarGroup ids={pageIds} max={pageIds.length} />
435
+ </div>
436
+ <div className="flex flex-col items-start gap-2">
437
+ <p className="text-sm text-muted-foreground">Workspace</p>
438
+ <UserAvatarGroup ids={workspaceIds} max={workspaceIds.length} />
439
+ </div>
440
+ </div>
441
+ </Section>
442
+ )
443
+ }
444
+
445
+ function MoodDemo() {
446
+ const [mood, setMood] = useState('Exploring')
447
+ usePublishPresence('mood', mood)
448
+ const presence = usePresence<string>('mood')
449
+ return (
450
+ <Section
451
+ title="Custom presence"
452
+ hint="Change your mood here and watch it update in the other pane. Reading presence returns the other participants’ values."
453
+ code={"usePublishPresence('mood', mood)\nconst presence = usePresence<string>('mood')"}
454
+ >
455
+ <label className="flex flex-col gap-2 text-sm">
456
+ Your mood
457
+ <Input value={mood} onChange={event => setMood(event.target.value)} />
458
+ </label>
459
+ <div className="flex flex-col gap-3">
460
+ <p className="text-sm text-muted-foreground">Received from the other pane</p>
461
+ {presence.map(entry => (
462
+ <div key={entry.connectionId} className="flex flex-col gap-1">
463
+ <User id={entry.userId} size="sm" />
464
+ <p className="text-sm">{entry.value || '(empty)'}</p>
465
+ </div>
466
+ ))}
467
+ {!presence.length && <p className="text-sm">No presence received yet.</p>}
468
+ </div>
469
+ </Section>
470
+ )
471
+ }
472
+
473
+ function HooksInspector() {
474
+ const me = useMe()
475
+ const peers = usePeers({ scope: 'workspace' })
476
+ const workspaceUsers = useWorkspaceUsers()
477
+ const offline = useWorkspaceUsers({ status: 'offline' })
478
+ const user = useUser('david')
479
+ const presence = usePresence<string>('mood')
480
+ const values = [
481
+ ['useMe()', me],
482
+ ["usePeers({ scope: 'workspace' })", peers],
483
+ ['useWorkspaceUsers()', workspaceUsers],
484
+ ["useWorkspaceUsers({ status: 'offline' })", offline],
485
+ ["useUser('david')", user],
486
+ ["usePresence('mood')", presence]
487
+ ] as const
488
+ return (
489
+ <Collapsible className="flex min-w-0 flex-col items-start gap-2">
490
+ <CollapsibleTrigger render={<Button variant="ghost" size="sm" />}>
491
+ <IconChevronDown stroke={1.75} />
492
+ Inspect hook values
493
+ </CollapsibleTrigger>
494
+ <CollapsibleContent className="flex w-full min-w-0 flex-col gap-4">
495
+ {values.map(([label, value]) => (
496
+ <div key={label} className="flex min-w-0 flex-col gap-2">
497
+ <code className="font-mono text-xs">{label}</code>
498
+ <Output value={value} />
499
+ </div>
500
+ ))}
501
+ </CollapsibleContent>
502
+ </Collapsible>
503
+ )
504
+ }
505
+
506
+ type ParticipantPaneProps = { room: FakeEngine; user: UserProfile }
507
+ function ParticipantPane({ room, user }: ParticipantPaneProps) {
508
+ return (
509
+ <CollabContext value={room}>
510
+ <AppletPresenceProvider appletId={APPLET_ID}>
511
+ <section
512
+ aria-label={user.id === YOU.id ? 'Your playground' : `${user.name}’s playground`}
513
+ className="flex min-w-0 flex-col gap-4"
514
+ >
515
+ <User id={user.id} description="Interact here; presence appears in the other pane" />
516
+ <div className="h-96 overflow-auto rounded-lg border border-border">
517
+ <Cursors id="examples" className="flex min-h-full flex-col gap-8 p-4">
518
+ <TargetPresenceDemo />
519
+ <EditableList />
520
+ <NestedForm />
521
+ <RecordDialogs />
522
+ <MoodDemo />
523
+ <HooksInspector />
524
+ </Cursors>
525
+ </div>
526
+ </section>
527
+ </AppletPresenceProvider>
528
+ </CollabContext>
529
+ )
530
+ }
531
+
532
+ export function CollabPlayground() {
533
+ const [rooms] = useState(() =>
534
+ [YOU, USERS[1]!].map(self => createFakeEngine({ self, page: PAGE, users: USERS }))
535
+ )
536
+ const room = rooms[0]!
537
+ const [renamed, setRenamed] = useState(false)
538
+ const [david, setDavid] = useState(true)
539
+ useEffect(() => connectPlaygroundParticipants(rooms, BACKGROUND_CONNECTIONS), [rooms])
540
+ useEffect(() => {
541
+ const users = USERS.filter(user => david || user.id !== 'david').map(user =>
542
+ user.id === 'fig' && renamed
543
+ ? { ...user, name: 'Fig Newton', color: 'emerald' as const }
544
+ : user
545
+ )
546
+ rooms.forEach(room => room.setUsers(users))
547
+ }, [rooms, renamed, david])
548
+ return (
549
+ <CollabContext value={room}>
550
+ <AppletPresenceProvider appletId={APPLET_ID}>
551
+ <div className="flex flex-col gap-10 border-t border-border pt-8">
552
+ <header>
553
+ <h2 className="text-base font-medium">Presence playground</h2>
554
+ <p className="mt-1 text-sm text-muted-foreground">
555
+ Two participants share presence in memory. You control both panes. Alex is on this
556
+ page, Pierre is on another page, Andrea is away, and David is offline. No server
557
+ connection is used.
558
+ </p>
559
+ </header>
560
+ <Section
561
+ title="User, UserAvatar, and UserAvatarGroup"
562
+ hint="User shows an avatar and name; UserAvatar shows the avatar alone. UserAvatarGroup groups avatars with an overflow count. Profile edits update all three; removed IDs become unknown."
563
+ code={
564
+ '<User id="alex" size="xs" description="Online" />\n<User id="alex" size="sm" />\n<User id="alex" size="default" />\n<UserAvatar id="alex" size="sm" />\n<UserAvatarGroup ids={watcherIds} size="xs" />\n<UserAvatarGroup ids={watcherIds} size="sm" />\n<UserAvatarGroup ids={watcherIds} size="default" />\n<UserAvatarGroup ids={watcherIds} size="lg" />'
565
+ }
566
+ >
567
+ <div className="flex flex-wrap items-center gap-5">
568
+ <User id="fig" /> <User id="andrea" description="Away" />{' '}
569
+ <User id="david" description={david ? 'Offline' : 'Removed'} /> <User id="unknown" />
570
+ </div>
571
+ <div className="flex flex-wrap items-center gap-5">
572
+ <User id="alex" size="xs" description="Online" />
573
+ <User id="alex" size="sm" description="Online" />
574
+ <User id="alex" size="default" description="Online" />
575
+ </div>
576
+ <div className="flex flex-wrap items-center gap-5">
577
+ <UserAvatar id="alex" size="xs" />
578
+ <UserAvatar id="alex" size="sm" />
579
+ <UserAvatar id="alex" size="default" />
580
+ <UserAvatar id="alex" size="lg" />
581
+ </div>
582
+ <div className="flex flex-wrap items-center gap-4">
583
+ <UserAvatarGroup ids={USERS.map(user => user.id)} size="xs" />
584
+ <UserAvatarGroup ids={USERS.map(user => user.id)} size="sm" />
585
+ <UserAvatarGroup ids={USERS.map(user => user.id)} size="default" />
586
+ <UserAvatarGroup ids={USERS.map(user => user.id)} size="lg" />
587
+ </div>
588
+ <div className="flex flex-wrap gap-2">
589
+ <Button size="sm" variant="secondary" onClick={() => setRenamed(value => !value)}>
590
+ {renamed ? 'Restore Fig' : 'Rename Fig'}
591
+ </Button>
592
+ <Button size="sm" variant="secondary" onClick={() => setDavid(value => !value)}>
593
+ {david ? 'Remove David' : 'Restore David'}
594
+ </Button>
595
+ </div>
596
+ </Section>
597
+ <ConnectedUsersDemo />
598
+ <Section
599
+ title="Try collaboration"
600
+ hint="Focus, select, or move your pointer in either pane and watch the other. Each pane scrolls independently. Field values stay local: collaboration shares presence, not document content."
601
+ code={
602
+ '<Cursors id="examples">\n <PresenceFrame id="title"><Input /></PresenceFrame>\n <PresenceAvatars id="notes"><Input /></PresenceAvatars>\n <PresenceFrame id="card" present={selected}>{children}</PresenceFrame>\n</Cursors>'
603
+ }
604
+ >
605
+ <div className="grid gap-6 md:grid-cols-2">
606
+ <ParticipantPane room={rooms[0]!} user={YOU} />
607
+ <ParticipantPane room={rooms[1]!} user={USERS[1]!} />
608
+ </div>
609
+ </Section>
610
+ </div>
611
+ </AppletPresenceProvider>
612
+ </CollabContext>
613
+ )
614
+ }