moi-computer 0.11.0 → 0.11.1-dev.1c52199
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.
- package/README.md +2 -2
- package/client/AGENTS.md +2 -2
- package/client/api/app-config.ts +2 -1
- package/client/app/AppRouter.tsx +2 -2
- package/client/app/routes/WorkspaceRoute.tsx +33 -27
- package/client/app/shell/SidebarLayout.tsx +18 -15
- package/client/components/shared/BottomPanel.tsx +26 -14
- package/client/components/shared/PanelHeader.tsx +1 -1
- package/client/components/shared/ReorderableList.tsx +7 -2
- package/client/components/shared/WorkspaceIcon.tsx +92 -0
- package/client/components/ui/alert-dialog.tsx +1 -0
- package/client/components/ui/button.tsx +1 -53
- package/client/components/ui/checkbox.tsx +1 -0
- package/client/components/ui/collapsible.tsx +1 -15
- package/client/components/ui/dialog.tsx +1 -58
- package/client/components/ui/dropdown-menu.tsx +1 -254
- package/client/components/ui/hover-card.tsx +1 -44
- package/client/components/ui/inline-input.tsx +84 -0
- package/client/components/ui/input.tsx +1 -21
- package/client/components/ui/popover.tsx +1 -88
- package/client/components/ui/resizable.tsx +1 -40
- package/client/components/ui/skeleton.tsx +1 -13
- package/client/components/ui/slider.tsx +1 -70
- package/client/components/ui/spinner.tsx +1 -18
- package/client/components/ui/switch.tsx +1 -23
- package/client/components/ui/tabs.tsx +1 -0
- package/client/components/ui/textarea.tsx +1 -18
- package/client/components/ui/toast.tsx +2 -2
- package/client/components/ui/tooltip.tsx +1 -51
- package/client/features/applets/AppletMount.tsx +5 -2
- package/client/features/applets/applet-cache.ts +3 -3
- package/client/features/applets/applet-runtime.ts +158 -108
- package/client/features/applets/applet-styles.ts +2 -2
- package/client/features/applets/applet-thumbnail.ts +6 -8
- package/client/features/applets/useApplet.ts +10 -9
- package/client/features/chat/ChatPanel.tsx +24 -15
- package/client/features/chat/ChatPopup.tsx +9 -5
- package/client/features/chat/applet-chat-intents.ts +179 -0
- package/client/features/chat/chat-send.ts +116 -64
- package/client/features/chat/chat-store.ts +51 -62
- package/client/features/chat/composer/ChatComposer.tsx +43 -128
- package/client/features/chat/composer/ModelPicker.tsx +47 -60
- package/client/features/chat/composer/attachments/AttachmentChip.tsx +99 -0
- package/client/features/chat/composer/attachments/DrawingAttachmentChip.tsx +37 -0
- package/client/features/chat/composer/attachments/FileAttachmentChip.tsx +22 -0
- package/client/features/chat/composer/attachments/TextAttachmentChip.tsx +11 -0
- package/client/features/chat/composer/attachments/draft-attachments.ts +168 -0
- package/client/features/chat/composer/attachments/types.ts +25 -0
- package/client/features/chat/composer/attachments/uploads.ts +26 -0
- package/client/features/chat/composer/banners/ErrorBanner.tsx +20 -12
- package/client/features/chat/composer/model-order.ts +15 -0
- package/client/features/chat/composer/useWorkspaceComposerState.tsx +15 -7
- package/client/features/chat/{chat-connection.ts → connection/chat-connection.ts} +1 -1
- package/client/features/chat/{chat-frames.ts → connection/chat-frames.ts} +10 -8
- package/client/features/chat/{ChatEmptyState.tsx → messages/ChatEmptyState.tsx} +16 -11
- package/client/features/chat/{MarkdownContent.tsx → messages/MarkdownContent.tsx} +16 -1
- package/client/features/chat/{TurnView.tsx → messages/TurnView.tsx} +27 -19
- package/client/features/chat/{group-turns.ts → messages/group-turns.ts} +12 -0
- package/client/features/chat/{interleave-notices.ts → messages/interleave-notices.ts} +7 -11
- package/client/features/chat/{tool-group → messages/tool-group}/SubagentCard.tsx +1 -1
- package/client/features/chat/{tool-group → messages/tool-group}/TimelineRow.tsx +2 -2
- package/client/features/chat/{tool-group → messages/tool-group}/ToolCallGroup.tsx +2 -5
- package/client/features/chat/{tool-group → messages/tool-group}/format/codex.ts +4 -1
- package/client/features/chat/{ChatSelector.tsx → sessions/ChatSelector.tsx} +3 -3
- package/client/features/chat/{api.ts → sessions/api.ts} +5 -14
- package/client/features/chat/sessions/session-view.ts +53 -0
- package/client/features/chat/{useSelectedSession.ts → sessions/useSelectedSession.ts} +64 -20
- package/client/features/chat/useChat.ts +46 -24
- package/client/features/collab/DevCollabIdentity.tsx +170 -0
- package/client/features/collab/DevCollabKit.tsx +501 -0
- package/client/features/collab/DevCollabPage.tsx +25 -0
- package/client/features/collab/WorkspaceCollabControls.tsx +313 -0
- package/client/features/collab/client.ts +146 -0
- package/client/features/collab/components.tsx +307 -0
- package/client/features/collab/dev-persona.ts +66 -0
- package/client/features/collab/engine.ts +96 -0
- package/client/features/collab/entry.tsx +35 -0
- package/client/features/collab/facehash-avatar.ts +132 -0
- package/client/features/collab/fake-engine.ts +130 -0
- package/client/features/collab/hooks.tsx +253 -0
- package/client/features/collab/identity.ts +305 -0
- package/client/features/collab/index.ts +45 -0
- package/client/features/collab/people.ts +102 -0
- package/client/features/collab/personal-state.ts +61 -0
- package/client/features/collab/presence-publisher.ts +29 -0
- package/client/features/collab/presence-target.ts +10 -0
- package/client/features/collab/primitives.tsx +398 -0
- package/client/features/collab/proxy-identity.ts +24 -0
- package/client/features/collab/store.ts +156 -0
- package/client/features/dev/ChatStatesPage.tsx +8 -7
- package/client/features/dev/DevIndexPage.tsx +10 -0
- package/client/features/dev/DevRoutes.tsx +16 -1
- package/client/features/dev/HarnessDebugPage.tsx +11 -8
- package/client/features/dev/ToolCallsPage.tsx +1 -1
- package/client/features/dev/UiComponentPreviews.tsx +855 -0
- package/client/features/dev/UiComponentsPage.tsx +60 -0
- package/client/features/dev/chat-states-fixtures.ts +13 -2
- package/client/features/drawings/useChatAnnotation.ts +17 -17
- package/client/features/drawings/useDrawingLayer.ts +7 -9
- package/client/features/drawings/useViewBuilderSketch.ts +18 -15
- package/client/features/home/HomePage.tsx +8 -7
- package/client/features/home/WorkspacePreview.tsx +2 -2
- package/client/features/home/workspace-presentation.tsx +1 -11
- package/client/features/home/workspace-setup/DemoDialog.tsx +3 -2
- package/client/features/home/workspace-setup/ImportWorkspaceDialog.tsx +5 -8
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +2 -4
- package/client/features/home/workspace-setup/WorkspaceDialogContent.tsx +7 -20
- package/client/features/overview/HiddenPanel.tsx +109 -0
- package/client/features/overview/Overview.tsx +302 -0
- package/client/features/overview/WidgetCanvas.tsx +164 -0
- package/client/features/{widgets → overview}/WidgetFrame.tsx +25 -17
- package/client/features/overview/WorkspaceName.tsx +60 -0
- package/client/features/overview/api.ts +30 -0
- package/client/features/overview/default/ViewsWidget.tsx +252 -0
- package/client/features/overview/default/registry.tsx +36 -0
- package/client/features/{widgets → overview}/grid.ts +36 -0
- package/client/features/overview/useGridReconcile.ts +94 -0
- package/client/features/scratchpad/Scratchpad.tsx +7 -8
- package/client/features/scratchpad/ScratchpadControls.tsx +0 -1
- package/client/features/scratchpad/useScratchpadSnapshot.tsx +23 -13
- package/client/features/settings/GeneralSettings.tsx +55 -58
- package/client/features/settings/SettingsLayout.tsx +2 -2
- package/client/features/settings/WorkspaceIconPicker.tsx +318 -0
- package/client/features/settings/WorkspaceSettingsDialog.tsx +65 -0
- package/client/features/settings/api.ts +48 -24
- package/client/features/views/ViewBuilder.tsx +5 -4
- package/client/features/views/ViewManager.tsx +28 -22
- package/client/features/views/api.ts +36 -2
- package/client/features/views/useViewBuilderActions.ts +11 -5
- package/client/features/views/useViewBuilderDrafts.ts +23 -21
- package/client/features/views/view-presentation.ts +13 -0
- package/client/features/workspace/{CustomizePanel.tsx → ThemePanel.tsx} +26 -31
- package/client/features/workspace/UnavailablePage.tsx +21 -0
- package/client/features/workspace/WorkspaceLayoutContext.tsx +22 -10
- package/client/features/workspace/WorkspaceScreen.tsx +228 -172
- package/client/features/workspace/WorkspaceSplitLayout.tsx +7 -2
- package/client/features/workspace/WorkspaceTabs.tsx +2 -0
- package/client/features/workspace/api.ts +5 -31
- package/client/features/workspace/layout-save.ts +13 -0
- package/client/features/workspace/moi-context.ts +15 -11
- package/client/features/workspace/tab-resolution.ts +10 -33
- package/client/features/workspace/useWorkspaceNavigation.ts +173 -101
- package/client/index.css +23 -30
- package/client/index.tsx +4 -3
- package/client/lib/app-icon-registry.ts +2 -0
- package/client/lib/use-latest-ref.ts +11 -0
- package/client/main.tsx +1 -1
- package/client/runtime/useWorkspaceEvents.ts +59 -14
- package/client/theme.css +5 -5
- package/dist/chunk-1ftessc8.js +2 -0
- package/dist/chunk-3802vh2p.js +2 -0
- package/dist/chunk-542a2m9m.js +1 -0
- package/dist/chunk-5745d83v.js +2 -0
- package/dist/chunk-5zxdsjym.js +1 -0
- package/dist/{chunk-tqd3pkg8.css → chunk-60q353jq.css} +1 -1
- package/dist/chunk-7r4z77ry.js +2 -0
- package/dist/chunk-9cm0wjah.js +79 -0
- package/dist/{chunk-11cab5dq.css → chunk-cq2r11fg.css} +1 -1
- package/dist/chunk-cvgdm1hq.js +2 -0
- package/dist/chunk-gptm4jpd.js +546 -0
- package/dist/chunk-nf2wkvf9.js +14 -0
- package/dist/chunk-wnmf38ta.js +5 -0
- package/dist/chunk-xaehhr1j.css +1 -0
- package/dist/chunk-xpt063fx.js +1 -0
- package/dist/index.html +1 -1
- package/lib/app-icons.ts +1 -0
- package/lib/applet-selector.ts +24 -0
- package/lib/chat-title.ts +6 -1
- package/lib/collab/colors.ts +24 -0
- package/lib/collab/protocol.ts +83 -0
- package/lib/collab/types.ts +63 -0
- package/lib/default-widgets.ts +32 -0
- package/lib/experimental.ts +14 -0
- package/lib/format.ts +67 -7
- package/lib/message-attachments.ts +40 -0
- package/lib/moi-attachments.ts +204 -0
- package/lib/moi-context.ts +27 -35
- package/lib/navigation.ts +84 -0
- package/lib/themes.ts +6 -6
- package/lib/types.ts +103 -40
- package/lib/workspace-icon.ts +20 -0
- package/lib/workspace-layout.ts +22 -2
- package/lib/workspace-tabs.ts +9 -33
- package/package.json +44 -32
- package/registry.json +656 -0
- package/server/AGENTS.md +2 -0
- package/server/agent.ts +1 -1
- package/server/api.ts +162 -68
- package/server/app-config.ts +127 -34
- package/server/applet-typecheck.ts +100 -0
- package/server/applets/applet-css.ts +55 -0
- package/server/{bundler → applets}/build-applet.ts +21 -240
- package/server/applets/build-worker.ts +178 -0
- package/server/applets/config.ts +226 -0
- package/server/applets/dependencies.ts +206 -0
- package/server/{applets.ts → applets/index.ts} +44 -189
- package/server/applets/runtime/base.ts +4 -0
- package/server/applets/runtime/moi.ts +42 -0
- package/server/applets/runtime/rpc.ts +17 -0
- package/server/applets/view-source.ts +67 -0
- package/server/attachment-message.ts +95 -0
- package/server/chat-selection.ts +23 -0
- package/server/cli-ui-components.ts +28 -33
- package/server/cli.ts +226 -120
- package/server/collab/applet-module.ts +50 -0
- package/server/collab/cloudflare-access.ts +77 -0
- package/server/collab/config.ts +18 -0
- package/server/collab/ipc.ts +10 -0
- package/server/collab/manager.ts +407 -0
- package/server/collab/service.ts +137 -0
- package/server/collab/skill/collab-env.d.ts +92 -0
- package/server/collab/skill/index.ts +59 -0
- package/server/collab/worker.ts +52 -0
- package/server/control.ts +63 -42
- package/server/data-dir.ts +1 -1
- package/server/harness/README.md +45 -50
- package/server/harness/acp/NOTES.md +426 -0
- package/server/harness/acp/adapter.ts +7 -21
- package/server/harness/acp/discovery.ts +27 -30
- package/server/harness/acp/model-state.ts +57 -0
- package/server/harness/acp/session.ts +49 -56
- package/server/harness/claude-code/NOTES.md +54 -13
- package/server/harness/claude-code/adapter.ts +8 -25
- package/server/harness/claude-code/cli.ts +40 -0
- package/server/harness/claude-code/index.ts +13 -5
- package/server/harness/claude-code/models.ts +72 -19
- package/server/harness/claude-code/session.ts +447 -343
- package/server/harness/claude-code/sessions.ts +9 -4
- package/server/harness/codex/NOTES.md +222 -350
- package/server/harness/codex/adapter.ts +102 -52
- package/server/harness/codex/auth.ts +1 -3
- package/server/harness/codex/client.ts +169 -249
- package/server/harness/codex/discovery.ts +9 -17
- package/server/harness/codex/index.ts +3 -1
- package/server/harness/codex/pagination.ts +24 -0
- package/server/harness/codex/permissions.ts +32 -8
- package/server/harness/codex/probe.ts +10 -13
- package/server/harness/codex/session.ts +567 -181
- package/server/harness/codex/transport.ts +184 -0
- package/server/harness/debug.ts +1 -1
- package/server/harness/hermes/NOTES.md +19 -1
- package/server/harness/hermes/discovery.ts +20 -0
- package/server/harness/hermes/index.ts +13 -0
- package/server/harness/hermes/models.ts +28 -8
- package/server/harness/openclaw/NOTES.md +1 -1
- package/server/harness/openclaw/adapter.ts +22 -4
- package/server/harness/openclaw/gateway.ts +5 -3
- package/server/harness/openclaw/session.ts +27 -27
- package/server/harness/types.ts +9 -5
- package/server/layout.ts +24 -16
- package/server/moi-scaffold.ts +22 -14
- package/server/navigation-relay.ts +110 -0
- package/server/preview.ts +3 -1
- package/server/registry.ts +71 -41
- package/server/scratchpad-executor.ts +79 -56
- package/server/scratchpad-shape-defaults.ts +82 -0
- package/server/selected-session.ts +24 -4
- package/server/skill-update.ts +5 -3
- package/server/skill-version.ts +4 -7
- package/server/skills-template.ts +60 -9
- package/server/tabs.ts +19 -33
- package/server/tldraw-package.d.ts +4 -0
- package/server/ui-components.ts +101 -434
- package/server/uploads.ts +0 -0
- package/server/view-builders.ts +20 -2
- package/server/views.ts +126 -6
- package/server/web.ts +108 -30
- package/server/widgets.ts +12 -5
- package/server/workspace-config.ts +13 -9
- package/server/workspace-env.ts +3 -2
- package/tsconfig.json +0 -1
- package/ui-components/accordion.tsx +71 -0
- package/ui-components/alert-dialog.tsx +165 -0
- package/ui-components/alert.tsx +68 -0
- package/ui-components/attachment.tsx +195 -0
- package/ui-components/avatar.tsx +91 -0
- package/ui-components/badge.tsx +49 -0
- package/ui-components/button-group.tsx +78 -0
- package/ui-components/button.tsx +55 -0
- package/ui-components/calendar.tsx +180 -0
- package/ui-components/card.tsx +84 -0
- package/ui-components/carousel.tsx +235 -0
- package/ui-components/chart.tsx +338 -0
- package/ui-components/checkbox.tsx +28 -0
- package/ui-components/collapsible.tsx +15 -0
- package/ui-components/combobox.tsx +267 -0
- package/ui-components/context-menu.tsx +244 -0
- package/ui-components/dialog.tsx +138 -0
- package/ui-components/docs/accordion.md +463 -0
- package/ui-components/docs/alert-dialog.md +481 -0
- package/ui-components/docs/alert.md +301 -0
- package/ui-components/docs/attachment.md +689 -0
- package/ui-components/docs/avatar.md +495 -0
- package/ui-components/docs/badge.md +258 -0
- package/ui-components/docs/button-group.md +879 -0
- package/ui-components/docs/button.md +26 -0
- package/ui-components/docs/calendar.md +862 -0
- package/ui-components/docs/card.md +265 -0
- package/ui-components/docs/carousel.md +627 -0
- package/ui-components/docs/chart.md +1256 -0
- package/ui-components/docs/checkbox.md +477 -0
- package/ui-components/docs/collapsible.md +446 -0
- package/ui-components/docs/combobox.md +1003 -0
- package/ui-components/docs/context-menu.md +860 -0
- package/ui-components/docs/data-table.md +1580 -0
- package/ui-components/docs/date-picker.md +614 -0
- package/ui-components/docs/dialog.md +414 -0
- package/ui-components/docs/drawer.md +51 -0
- package/ui-components/docs/dropdown-menu.md +1000 -0
- package/ui-components/docs/field.md +1192 -0
- package/ui-components/docs/hover-card.md +275 -0
- package/ui-components/docs/input-group.md +866 -0
- package/ui-components/docs/input.md +456 -0
- package/ui-components/docs/kbd.md +191 -0
- package/ui-components/docs/label.md +295 -0
- package/ui-components/docs/pagination.md +416 -0
- package/ui-components/docs/popover.md +348 -0
- package/ui-components/docs/progress.md +214 -0
- package/ui-components/docs/radio-group.md +389 -0
- package/ui-components/docs/resizable.md +265 -0
- package/ui-components/docs/select.md +616 -0
- package/ui-components/docs/separator.md +216 -0
- package/ui-components/docs/skeleton.md +205 -0
- package/ui-components/docs/slider.md +226 -0
- package/ui-components/docs/spinner.md +332 -0
- package/ui-components/docs/switch.md +236 -0
- package/ui-components/docs/table.md +551 -0
- package/ui-components/docs/tabs.md +379 -0
- package/ui-components/docs/textarea.md +187 -0
- package/ui-components/docs/toggle-group.md +370 -0
- package/ui-components/docs/toggle.md +91 -0
- package/ui-components/docs/tooltip.md +290 -0
- package/ui-components/drawer.tsx +213 -0
- package/ui-components/dropdown-menu.tsx +263 -0
- package/ui-components/field.tsx +224 -0
- package/ui-components/hover-card.tsx +46 -0
- package/ui-components/input-group.tsx +146 -0
- package/ui-components/input.tsx +20 -0
- package/ui-components/kbd.tsx +32 -0
- package/ui-components/label.tsx +17 -0
- package/ui-components/pagination.tsx +124 -0
- package/ui-components/popover.tsx +81 -0
- package/ui-components/progress.tsx +63 -0
- package/ui-components/radio-group.tsx +38 -0
- package/ui-components/resizable.tsx +40 -0
- package/ui-components/scoped-portal.tsx +31 -0
- package/ui-components/select.tsx +195 -0
- package/ui-components/separator.tsx +18 -0
- package/ui-components/skeleton.tsx +15 -0
- package/ui-components/slider.tsx +75 -0
- package/ui-components/spinner.tsx +20 -0
- package/ui-components/switch.tsx +27 -0
- package/ui-components/table.tsx +86 -0
- package/ui-components/tabs.tsx +75 -0
- package/ui-components/textarea.tsx +17 -0
- package/ui-components/toggle-group.tsx +85 -0
- package/ui-components/toggle.tsx +42 -0
- package/ui-components/tooltip.tsx +61 -0
- package/ui-components/utils.ts +8 -0
- package/workspace/.claude/skills/moi-workspace/SKILL.md +295 -248
- package/workspace/.claude/skills/moi-workspace/references/COLLABORATIVE.md +151 -0
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +100 -152
- package/workspace/.claude/skills/moi-workspace/references/INTENTS.md +156 -0
- package/workspace/.claude/skills/moi-workspace/references/UI-COMPONENTS.md +67 -203
- package/client/features/chat/attachment-staging.ts +0 -153
- package/client/features/chat/uploads.ts +0 -18
- package/client/features/chat/useAppletChatMessage.ts +0 -68
- package/client/features/settings/IconPicker.tsx +0 -442
- package/client/features/settings/WorkspaceSettings.tsx +0 -92
- package/client/features/settings/render-icon.ts +0 -173
- package/client/features/widgets/HiddenPanel.tsx +0 -80
- package/client/features/widgets/WidgetGrid.tsx +0 -123
- package/client/features/widgets/Widgets.tsx +0 -264
- package/client/features/widgets/useGridReconcile.ts +0 -73
- package/dist/chunk-104m57qw.js +0 -2
- package/dist/chunk-59zf0m6n.js +0 -1
- package/dist/chunk-bj3akjs2.js +0 -1
- package/dist/chunk-c9pjqx9m.js +0 -14
- package/dist/chunk-j2w2vak4.js +0 -546
- package/dist/chunk-k2r9c95m.js +0 -10
- package/dist/chunk-kmbxhgwv.js +0 -1
- package/dist/chunk-p1yqa71k.js +0 -83
- package/dist/chunk-pcnahct1.js +0 -5
- package/dist/chunk-r5xrenjy.css +0 -1
- package/dist/chunk-tny85yt2.js +0 -2
- package/dist/chunk-v7wr1df4.js +0 -2
- package/server/bundler/applet-css.ts +0 -155
- /package/client/features/chat/{ChatPromptBubbles.tsx → messages/ChatPromptBubbles.tsx} +0 -0
- /package/client/features/chat/{ShimmerText.tsx → messages/ShimmerText.tsx} +0 -0
- /package/client/features/chat/{preview-turn.ts → messages/preview-turn.ts} +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/CodeBlock.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/Collapse.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ReadImagePreview.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/ToolOutput.tsx +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/detect.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/fixtures.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/claude.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/hermes.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/index.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/mcp.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/openclaw.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shared.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/format/shell.ts +0 -0
- /package/client/features/chat/{tool-group → messages/tool-group}/index.ts +0 -0
- /package/client/features/chat/{useStickToBottom.ts → messages/useStickToBottom.ts} +0 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Fragment,
|
|
3
|
+
createContext,
|
|
4
|
+
isValidElement,
|
|
5
|
+
useContext,
|
|
6
|
+
useId,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef
|
|
10
|
+
} from 'react'
|
|
11
|
+
import type { FocusEvent, PointerEvent, ReactElement, ReactNode, RefObject } from 'react'
|
|
12
|
+
|
|
13
|
+
import { IconCursorText, IconPointer } from '@tabler/icons-react'
|
|
14
|
+
import { LayoutGroup, MotionConfig } from 'motion/react'
|
|
15
|
+
|
|
16
|
+
import { cn } from '@/client/lib/cn'
|
|
17
|
+
import type { CollabJsonValue } from '@/lib/collab/types'
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
presenceChannels,
|
|
21
|
+
useMe,
|
|
22
|
+
usePeers,
|
|
23
|
+
usePresenceChannel,
|
|
24
|
+
usePresencePublisher,
|
|
25
|
+
usePublishPresenceChannel
|
|
26
|
+
} from './hooks'
|
|
27
|
+
import { Cursor, Facepile, PresenceFramePrimitive, PresenceGutterPrimitive } from './primitives'
|
|
28
|
+
import { presenceTarget } from './presence-target'
|
|
29
|
+
|
|
30
|
+
export { Facepile, User } from './primitives'
|
|
31
|
+
|
|
32
|
+
// Built-in indicators have no registration while unfocused, unselected, or absent.
|
|
33
|
+
const hasPresence = (value: CollabJsonValue) => value !== false && value !== null
|
|
34
|
+
|
|
35
|
+
export type ActivityProps = { scope?: 'page' | 'workspace'; className?: string }
|
|
36
|
+
export function Activity({ scope = 'page', className }: ActivityProps) {
|
|
37
|
+
const peers = usePeers({ scope })
|
|
38
|
+
const me = useMe()
|
|
39
|
+
const users = me ? [me, ...peers] : peers
|
|
40
|
+
return <Facepile ids={users.map(user => user.id)} max={users.length} className={className} />
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type PointerPosition = { x: number; y: number; target?: string; targetX?: number; targetY?: number }
|
|
44
|
+
function pointerPosition(value: CollabJsonValue): PointerPosition | null {
|
|
45
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) return null
|
|
46
|
+
if (typeof value.x !== 'number' || typeof value.y !== 'number') return null
|
|
47
|
+
if (!Number.isFinite(value.x) || !Number.isFinite(value.y)) return null
|
|
48
|
+
return {
|
|
49
|
+
x: value.x,
|
|
50
|
+
y: value.y,
|
|
51
|
+
...(typeof value.target === 'string' ? { target: value.target } : {}),
|
|
52
|
+
...(typeof value.targetX === 'number' && Number.isFinite(value.targetX)
|
|
53
|
+
? { targetX: value.targetX }
|
|
54
|
+
: {}),
|
|
55
|
+
...(typeof value.targetY === 'number' && Number.isFinite(value.targetY)
|
|
56
|
+
? { targetY: value.targetY }
|
|
57
|
+
: {})
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type CursorsProps = { surface?: string; children: ReactNode; className?: string }
|
|
62
|
+
export function Cursors({ surface = 'default', children, className }: CursorsProps) {
|
|
63
|
+
const root = useRef<HTMLDivElement>(null)
|
|
64
|
+
const channel = presenceChannels.cursor(surface)
|
|
65
|
+
const cursors = usePresenceChannel<CollabJsonValue>(channel)
|
|
66
|
+
const publish = usePresencePublisher<CollabJsonValue>(channel, null, hasPresence)
|
|
67
|
+
const byConnection = new Map<string, { id: string; point: PointerPosition }>()
|
|
68
|
+
for (const { connectionId, userId, value } of cursors) {
|
|
69
|
+
const point = pointerPosition(value)
|
|
70
|
+
if (point) byConnection.set(connectionId, { id: userId, point })
|
|
71
|
+
}
|
|
72
|
+
const move = (event: PointerEvent<HTMLDivElement>) => {
|
|
73
|
+
if (event.pointerType === 'touch') return
|
|
74
|
+
const element = root.current
|
|
75
|
+
if (
|
|
76
|
+
!element ||
|
|
77
|
+
!(event.target instanceof Element) ||
|
|
78
|
+
event.target.closest('[data-collab-cursors]') !== element
|
|
79
|
+
)
|
|
80
|
+
return
|
|
81
|
+
const bounds = element.getBoundingClientRect()
|
|
82
|
+
const target =
|
|
83
|
+
event.target instanceof Element
|
|
84
|
+
? event.target.closest<HTMLElement>('[data-collab-target]')
|
|
85
|
+
: null
|
|
86
|
+
const anchor = target && element.contains(target) ? target : null
|
|
87
|
+
const rect = anchor?.getBoundingClientRect()
|
|
88
|
+
publish({
|
|
89
|
+
x: event.clientX - bounds.left + element.scrollLeft,
|
|
90
|
+
y: event.clientY - bounds.top + element.scrollTop,
|
|
91
|
+
...(anchor && rect
|
|
92
|
+
? {
|
|
93
|
+
target: anchor.dataset.collabTarget ?? '',
|
|
94
|
+
targetX: rect.width ? (event.clientX - rect.left) / rect.width : 0,
|
|
95
|
+
targetY: rect.height ? (event.clientY - rect.top) / rect.height : 0
|
|
96
|
+
}
|
|
97
|
+
: {})
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
return (
|
|
101
|
+
<div
|
|
102
|
+
ref={root}
|
|
103
|
+
data-collab-cursors={surface}
|
|
104
|
+
className={cn('relative', className)}
|
|
105
|
+
onPointerMove={move}
|
|
106
|
+
onPointerLeave={event => {
|
|
107
|
+
if (
|
|
108
|
+
event.target instanceof Element &&
|
|
109
|
+
event.target.closest('[data-collab-cursors]') === event.currentTarget
|
|
110
|
+
)
|
|
111
|
+
publish(null)
|
|
112
|
+
}}
|
|
113
|
+
>
|
|
114
|
+
{children}
|
|
115
|
+
<div className="pointer-events-none absolute inset-0 overflow-hidden" aria-hidden="true">
|
|
116
|
+
{[...byConnection].map(([connectionId, { id, point }]) => (
|
|
117
|
+
<RemoteCursor key={connectionId} root={root} point={point} id={id} />
|
|
118
|
+
))}
|
|
119
|
+
</div>
|
|
120
|
+
</div>
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
type RemoteCursorProps = {
|
|
125
|
+
root: RefObject<HTMLDivElement | null>
|
|
126
|
+
point: PointerPosition
|
|
127
|
+
id: string
|
|
128
|
+
}
|
|
129
|
+
// Semantic anchors follow their own data through scrolling and reordering.
|
|
130
|
+
// A cursor on a different record's modal has no matching anchor and stays hidden.
|
|
131
|
+
function RemoteCursor({ root, point, id }: RemoteCursorProps) {
|
|
132
|
+
const marker = useRef<HTMLSpanElement>(null)
|
|
133
|
+
useLayoutEffect(() => {
|
|
134
|
+
const element = root.current
|
|
135
|
+
const node = marker.current
|
|
136
|
+
if (!element || !node) return
|
|
137
|
+
const position = () => {
|
|
138
|
+
let x = point.x - element.scrollLeft
|
|
139
|
+
let y = point.y - element.scrollTop
|
|
140
|
+
if (point.target) {
|
|
141
|
+
const target = [...element.querySelectorAll<HTMLElement>('[data-collab-target]')].find(
|
|
142
|
+
candidate => candidate.dataset.collabTarget === point.target
|
|
143
|
+
)
|
|
144
|
+
if (!target) {
|
|
145
|
+
node.hidden = true
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
const bounds = element.getBoundingClientRect()
|
|
149
|
+
const rect = target.getBoundingClientRect()
|
|
150
|
+
x = rect.left - bounds.left + rect.width * (point.targetX ?? 0)
|
|
151
|
+
y = rect.top - bounds.top + rect.height * (point.targetY ?? 0)
|
|
152
|
+
}
|
|
153
|
+
node.hidden = false
|
|
154
|
+
node.style.transform = `translate(${x}px, ${y}px)`
|
|
155
|
+
}
|
|
156
|
+
position()
|
|
157
|
+
const observer = new ResizeObserver(position)
|
|
158
|
+
observer.observe(element)
|
|
159
|
+
const changes = new MutationObserver(position)
|
|
160
|
+
changes.observe(element, {
|
|
161
|
+
childList: true,
|
|
162
|
+
subtree: true,
|
|
163
|
+
attributes: true,
|
|
164
|
+
attributeFilter: ['data-collab-target']
|
|
165
|
+
})
|
|
166
|
+
element.addEventListener('scroll', position, true)
|
|
167
|
+
window.addEventListener('resize', position)
|
|
168
|
+
return () => {
|
|
169
|
+
observer.disconnect()
|
|
170
|
+
changes.disconnect()
|
|
171
|
+
element.removeEventListener('scroll', position, true)
|
|
172
|
+
window.removeEventListener('resize', position)
|
|
173
|
+
}
|
|
174
|
+
}, [root, point])
|
|
175
|
+
return <Cursor ref={marker} id={id} />
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const PresenceGroupContext = createContext<readonly string[]>([])
|
|
179
|
+
export type PresenceGroupProps = { id: string; children: ReactNode }
|
|
180
|
+
export function PresenceGroup({ id, children }: PresenceGroupProps) {
|
|
181
|
+
const parent = useContext(PresenceGroupContext)
|
|
182
|
+
const scope = useMemo(() => {
|
|
183
|
+
presenceTarget(id)
|
|
184
|
+
return [...parent, id]
|
|
185
|
+
}, [parent, id])
|
|
186
|
+
// The shared path identifies data across browsers; the generated ID keeps
|
|
187
|
+
// avatar animations inside this particular rendered group.
|
|
188
|
+
const animationId = useId()
|
|
189
|
+
return (
|
|
190
|
+
<MotionConfig reducedMotion="user">
|
|
191
|
+
<LayoutGroup id={animationId}>
|
|
192
|
+
<PresenceGroupContext value={scope}>{children}</PresenceGroupContext>
|
|
193
|
+
</LayoutGroup>
|
|
194
|
+
</MotionConfig>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function usePresenceTarget(id: string): string {
|
|
199
|
+
return presenceTarget(...useContext(PresenceGroupContext), id)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function presenceChild(children: ReactNode): ReactElement {
|
|
203
|
+
if (!isValidElement(children) || children.type === Fragment) {
|
|
204
|
+
throw new Error(
|
|
205
|
+
'PresenceFrame and PresenceGutter require exactly one child element. ' +
|
|
206
|
+
'Wrap a list with PresenceGroup and give each item its own wrapper; fragments are not supported.'
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
return children
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function useTargetPresence(target: string) {
|
|
213
|
+
const root = useRef<HTMLDivElement>(null)
|
|
214
|
+
const channel = presenceChannels.field(target)
|
|
215
|
+
const others = usePresenceChannel<boolean>(channel)
|
|
216
|
+
const publish = usePresencePublisher<boolean>(channel, false, hasPresence)
|
|
217
|
+
const ownsFocus = (element: EventTarget | null, owner: HTMLDivElement | null) =>
|
|
218
|
+
owner !== null &&
|
|
219
|
+
element instanceof Element &&
|
|
220
|
+
element.closest('[data-presence-target]') === owner
|
|
221
|
+
useLayoutEffect(() => {
|
|
222
|
+
publish(ownsFocus(document.activeElement, root.current))
|
|
223
|
+
}, [target, publish])
|
|
224
|
+
const users = new Map<string, { id: string; connectionId: string }>()
|
|
225
|
+
for (const entry of others) {
|
|
226
|
+
if (entry.value === true && !users.has(entry.userId)) {
|
|
227
|
+
users.set(entry.userId, { id: entry.userId, connectionId: entry.connectionId })
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
users: [...users.values()],
|
|
232
|
+
props: {
|
|
233
|
+
ref: root,
|
|
234
|
+
'data-collab-target': target,
|
|
235
|
+
'data-presence-target': target,
|
|
236
|
+
onFocusCapture: (event: FocusEvent<HTMLDivElement>) =>
|
|
237
|
+
publish(ownsFocus(event.target, event.currentTarget)),
|
|
238
|
+
onBlurCapture: (event: FocusEvent<HTMLDivElement>) =>
|
|
239
|
+
publish(ownsFocus(event.relatedTarget, event.currentTarget))
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type PresenceFrameProps = {
|
|
245
|
+
id: string
|
|
246
|
+
children: ReactElement
|
|
247
|
+
className?: string
|
|
248
|
+
}
|
|
249
|
+
export function PresenceFrame({ id, children, className }: PresenceFrameProps) {
|
|
250
|
+
const target = usePresenceTarget(id)
|
|
251
|
+
const presence = useTargetPresence(target)
|
|
252
|
+
return (
|
|
253
|
+
<PresenceFramePrimitive
|
|
254
|
+
{...presence.props}
|
|
255
|
+
ids={presence.users.map(user => user.id)}
|
|
256
|
+
icon={<IconCursorText size={12} stroke={1.75} />}
|
|
257
|
+
className={className}
|
|
258
|
+
>
|
|
259
|
+
{presenceChild(children)}
|
|
260
|
+
</PresenceFramePrimitive>
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export type PresenceGutterProps = {
|
|
265
|
+
id: string
|
|
266
|
+
children: ReactElement
|
|
267
|
+
className?: string
|
|
268
|
+
}
|
|
269
|
+
export function PresenceGutter({ id, children, className }: PresenceGutterProps) {
|
|
270
|
+
const target = usePresenceTarget(id)
|
|
271
|
+
const presence = useTargetPresence(target)
|
|
272
|
+
const grouped = useContext(PresenceGroupContext).length > 0
|
|
273
|
+
return (
|
|
274
|
+
<PresenceGutterPrimitive
|
|
275
|
+
{...presence.props}
|
|
276
|
+
users={presence.users}
|
|
277
|
+
animate={grouped}
|
|
278
|
+
className={className}
|
|
279
|
+
>
|
|
280
|
+
{presenceChild(children)}
|
|
281
|
+
</PresenceGutterPrimitive>
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export type SelectionProps = {
|
|
286
|
+
id: string
|
|
287
|
+
selected: boolean
|
|
288
|
+
children: ReactNode
|
|
289
|
+
className?: string
|
|
290
|
+
}
|
|
291
|
+
export function Selection({ id, selected, children, className }: SelectionProps) {
|
|
292
|
+
const target = usePresenceTarget(id)
|
|
293
|
+
const channel = presenceChannels.selection(target)
|
|
294
|
+
usePublishPresenceChannel(channel, selected, hasPresence)
|
|
295
|
+
const others = usePresenceChannel<boolean>(channel)
|
|
296
|
+
const ids = [...new Set(others.filter(other => other.value === true).map(other => other.userId))]
|
|
297
|
+
return (
|
|
298
|
+
<PresenceFramePrimitive
|
|
299
|
+
data-collab-target={target}
|
|
300
|
+
ids={ids}
|
|
301
|
+
icon={<IconPointer size={12} stroke={1.75} />}
|
|
302
|
+
className={className}
|
|
303
|
+
>
|
|
304
|
+
{children}
|
|
305
|
+
</PresenceFramePrimitive>
|
|
306
|
+
)
|
|
307
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { PERSONA_COLORS } from '@/lib/collab/colors'
|
|
2
|
+
import type { CollabIdentity } from '@/lib/collab/types'
|
|
3
|
+
|
|
4
|
+
import { facehashDataUrl } from './facehash-avatar'
|
|
5
|
+
import { userDisplayName } from './people'
|
|
6
|
+
|
|
7
|
+
// Test profiles are activated only by explicit setup on /dev/collab.
|
|
8
|
+
|
|
9
|
+
export const PERSONA_NAMES = [
|
|
10
|
+
'Ada',
|
|
11
|
+
'Alan',
|
|
12
|
+
'Grace',
|
|
13
|
+
'Linus',
|
|
14
|
+
'Margaret',
|
|
15
|
+
'Dennis',
|
|
16
|
+
'Barbara',
|
|
17
|
+
'Ken',
|
|
18
|
+
'Radia',
|
|
19
|
+
'Hedy',
|
|
20
|
+
'Edsger',
|
|
21
|
+
'Frances',
|
|
22
|
+
'Donald',
|
|
23
|
+
'Katherine',
|
|
24
|
+
'Niklaus',
|
|
25
|
+
'Sophie',
|
|
26
|
+
'Adele',
|
|
27
|
+
'Jean',
|
|
28
|
+
'Bjarne',
|
|
29
|
+
'Guido',
|
|
30
|
+
'Yukihiro',
|
|
31
|
+
'Joe',
|
|
32
|
+
'Vint',
|
|
33
|
+
'Tim',
|
|
34
|
+
'Mary',
|
|
35
|
+
'Kathleen',
|
|
36
|
+
'Brian',
|
|
37
|
+
'Leslie'
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
export type Persona = { name: string; color: string }
|
|
41
|
+
|
|
42
|
+
function pick<T>(items: readonly T[], except?: T): T {
|
|
43
|
+
const pool = items.filter(item => item !== except)
|
|
44
|
+
return pool[Math.floor(Math.random() * pool.length)] ?? items[0]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function randomPersona(current?: Persona): Persona {
|
|
48
|
+
return {
|
|
49
|
+
name: pick(PERSONA_NAMES, current?.name),
|
|
50
|
+
color: pick(
|
|
51
|
+
PERSONA_COLORS.map(([, hex]) => hex),
|
|
52
|
+
current?.color
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function personaIdentity(id: string, persona: Persona): CollabIdentity {
|
|
58
|
+
const identity = { id, name: persona.name, color: persona.color }
|
|
59
|
+
const avatar = facehashDataUrl(userDisplayName(identity), persona.color)
|
|
60
|
+
return { ...identity, ...(avatar ? { avatar } : {}) }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// The id stays with the tab through renames, so peers keep seeing one person.
|
|
64
|
+
export function createDevIdentity(): CollabIdentity {
|
|
65
|
+
return personaIdentity(`dev-${crypto.randomUUID().slice(0, 8)}`, randomPersona())
|
|
66
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { CollabIdentity, CollabLocation, CollabPresenceRegistration } from '@/lib/collab/types'
|
|
2
|
+
|
|
3
|
+
import { CollabClient } from './client'
|
|
4
|
+
import {
|
|
5
|
+
getIdentity,
|
|
6
|
+
getWorkspaceDirectory,
|
|
7
|
+
getWorkspaceUsers,
|
|
8
|
+
subscribeIdentityStore,
|
|
9
|
+
subscribeWorkspaceUsersStore
|
|
10
|
+
} from './identity'
|
|
11
|
+
import type { WorkspaceDirectory } from './identity'
|
|
12
|
+
import { DISCONNECTED_STATE } from './store'
|
|
13
|
+
import type { CollabConnectionState, CollabPresenceEntry } from './store'
|
|
14
|
+
|
|
15
|
+
type Unsubscribe = () => void
|
|
16
|
+
|
|
17
|
+
// A workspace exposes one engine to React. The playground implements this same
|
|
18
|
+
// contract; sockets, timers, and registration storage stay behind it.
|
|
19
|
+
export type CollabEngineApi = {
|
|
20
|
+
readonly workspaceId: string
|
|
21
|
+
readonly enabled: boolean
|
|
22
|
+
start: () => Unsubscribe
|
|
23
|
+
getSnapshot: () => CollabConnectionState
|
|
24
|
+
getPeopleSnapshot: () => CollabConnectionState
|
|
25
|
+
getPresenceSnapshot: (surface: string, channel: string) => readonly CollabPresenceEntry[]
|
|
26
|
+
subscribe: (listener: () => void) => Unsubscribe
|
|
27
|
+
getIdentity: () => CollabIdentity | null
|
|
28
|
+
subscribeIdentity: (listener: () => void) => Unsubscribe
|
|
29
|
+
getWorkspaceUsers: () => readonly CollabIdentity[] | null
|
|
30
|
+
getWorkspaceDirectory: () => WorkspaceDirectory
|
|
31
|
+
subscribeWorkspaceUsers: (listener: () => void) => Unsubscribe
|
|
32
|
+
getLocation: () => CollabLocation | null
|
|
33
|
+
setLocation: (location: CollabLocation | null) => void
|
|
34
|
+
setPresence: (registration: CollabPresenceRegistration) => void
|
|
35
|
+
deletePresence: (registrationId: string) => void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const noop = () => {}
|
|
39
|
+
const EMPTY_PRESENCE: readonly CollabPresenceEntry[] = []
|
|
40
|
+
const UNAVAILABLE_DIRECTORY: WorkspaceDirectory = { status: 'unavailable', users: [] }
|
|
41
|
+
|
|
42
|
+
export const NO_ENGINE: CollabEngineApi = {
|
|
43
|
+
workspaceId: '',
|
|
44
|
+
enabled: false,
|
|
45
|
+
start: () => noop,
|
|
46
|
+
getSnapshot: () => DISCONNECTED_STATE,
|
|
47
|
+
getPeopleSnapshot: () => DISCONNECTED_STATE,
|
|
48
|
+
getPresenceSnapshot: () => EMPTY_PRESENCE,
|
|
49
|
+
subscribe: () => noop,
|
|
50
|
+
getIdentity,
|
|
51
|
+
subscribeIdentity: subscribeIdentityStore,
|
|
52
|
+
getWorkspaceUsers: () => null,
|
|
53
|
+
getWorkspaceDirectory: () => UNAVAILABLE_DIRECTORY,
|
|
54
|
+
subscribeWorkspaceUsers: () => noop,
|
|
55
|
+
getLocation: () => null,
|
|
56
|
+
setLocation: noop,
|
|
57
|
+
setPresence: noop,
|
|
58
|
+
deletePresence: noop
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class CollabEngine implements CollabEngineApi {
|
|
62
|
+
private readonly client: CollabClient
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
readonly workspaceId: string,
|
|
66
|
+
readonly enabled = true
|
|
67
|
+
) {
|
|
68
|
+
this.client = new CollabClient(workspaceId)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
start = (): Unsubscribe => (this.enabled ? this.client.start() : noop)
|
|
72
|
+
getSnapshot = (): CollabConnectionState =>
|
|
73
|
+
this.enabled ? this.client.store.getSnapshot() : DISCONNECTED_STATE
|
|
74
|
+
getPeopleSnapshot = (): CollabConnectionState =>
|
|
75
|
+
this.enabled ? this.client.store.getPeopleSnapshot() : DISCONNECTED_STATE
|
|
76
|
+
getPresenceSnapshot = (surface: string, channel: string): readonly CollabPresenceEntry[] =>
|
|
77
|
+
this.enabled ? this.client.store.getPresenceSnapshot(surface, channel) : EMPTY_PRESENCE
|
|
78
|
+
subscribe = (listener: () => void): Unsubscribe =>
|
|
79
|
+
this.enabled ? this.client.store.subscribe(listener) : noop
|
|
80
|
+
getIdentity = getIdentity
|
|
81
|
+
subscribeIdentity = subscribeIdentityStore
|
|
82
|
+
getWorkspaceUsers = (): readonly CollabIdentity[] | null => getWorkspaceUsers(this.workspaceId)
|
|
83
|
+
getWorkspaceDirectory = (): WorkspaceDirectory => getWorkspaceDirectory(this.workspaceId)
|
|
84
|
+
subscribeWorkspaceUsers = (listener: () => void): Unsubscribe =>
|
|
85
|
+
subscribeWorkspaceUsersStore(this.workspaceId, listener)
|
|
86
|
+
getLocation = (): CollabLocation | null => (this.enabled ? this.client.store.getLocation() : null)
|
|
87
|
+
setLocation = (location: CollabLocation | null): void => {
|
|
88
|
+
this.client.store.setLocation(location)
|
|
89
|
+
}
|
|
90
|
+
setPresence = (registration: CollabPresenceRegistration): void => {
|
|
91
|
+
if (this.enabled) this.client.store.setPresence(registration)
|
|
92
|
+
}
|
|
93
|
+
deletePresence = (registrationId: string): void => {
|
|
94
|
+
if (this.enabled) this.client.store.deletePresence(registrationId)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// The API remains available when presence is disabled, so the same applet can
|
|
2
|
+
// render in either mode. The provider starts its transport only when enabled.
|
|
3
|
+
import { useSyncExternalStore } from 'react'
|
|
4
|
+
import type { WorkspaceTabId } from '@/lib/types'
|
|
5
|
+
import { createAppletCollabApi } from './index'
|
|
6
|
+
import { useCollabEngine } from './hooks'
|
|
7
|
+
import { WorkspaceCollabControls } from './WorkspaceCollabControls'
|
|
8
|
+
import type { CollabTabInfo } from './WorkspaceCollabControls'
|
|
9
|
+
|
|
10
|
+
export type { CollabTabInfo } from './WorkspaceCollabControls'
|
|
11
|
+
export { AppletScope, CollabProvider } from './hooks'
|
|
12
|
+
|
|
13
|
+
export const getAppletCollabApi = createAppletCollabApi
|
|
14
|
+
|
|
15
|
+
// Identity controls personal navigation and host UI independently of storage.
|
|
16
|
+
export function useCollabIdentityEnabled(): boolean {
|
|
17
|
+
const engine = useCollabEngine()
|
|
18
|
+
const identity = useSyncExternalStore(
|
|
19
|
+
engine.subscribeIdentity,
|
|
20
|
+
engine.getIdentity,
|
|
21
|
+
engine.getIdentity
|
|
22
|
+
)
|
|
23
|
+
return engine.enabled && identity !== null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type CollabControlsProps = {
|
|
27
|
+
workspaceId: string
|
|
28
|
+
// Resolves a participant's tab to the label and icon the tab strip uses.
|
|
29
|
+
describeTab: (tab: WorkspaceTabId) => CollabTabInfo | null
|
|
30
|
+
onOpenTab: (tab: WorkspaceTabId) => void
|
|
31
|
+
}
|
|
32
|
+
export function CollabControls(props: CollabControlsProps) {
|
|
33
|
+
const enabled = useCollabIdentityEnabled()
|
|
34
|
+
return enabled ? <WorkspaceCollabControls {...props} /> : null
|
|
35
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { stringHash } from 'facehash'
|
|
2
|
+
|
|
3
|
+
import { COLLAB_MAX_AVATAR_BYTES } from '@/lib/collab/protocol'
|
|
4
|
+
|
|
5
|
+
// Rasterizes a facehash face (https://facehash.dev) into a small PNG data URL,
|
|
6
|
+
// so a test avatar travels inside the identity instead of pointing at a URL.
|
|
7
|
+
// The library paints its faces with React and CSS, which a canvas cannot
|
|
8
|
+
// consume, so the eye shapes and their selection are mirrored here: a name gets
|
|
9
|
+
// the same face it gets from the library. This is the library's solid variant;
|
|
10
|
+
// its gradient sheen alone costs more than the protocol's avatar budget.
|
|
11
|
+
|
|
12
|
+
type Face = { width: number; height: number; draw: (ctx: CanvasRenderingContext2D) => void }
|
|
13
|
+
|
|
14
|
+
function circle(ctx: CanvasRenderingContext2D, cx: number, cy: number, r: number): void {
|
|
15
|
+
ctx.beginPath()
|
|
16
|
+
ctx.arc(cx, cy, r, 0, Math.PI * 2)
|
|
17
|
+
ctx.fill()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pill(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number): void {
|
|
21
|
+
ctx.beginPath()
|
|
22
|
+
ctx.roundRect(x, y, w, h, 3.5)
|
|
23
|
+
ctx.fill()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const CURVED_EYE =
|
|
27
|
+
'M0 5.1c0-.1 0-.2 0-.3.1-.5.3-1 .7-1.3.1 0 .1-.1.2-.1C2.4 2.2 6 0 10.5 0S18.6 2.2 20.2 3.3c.1 0 .1.1.1.1.4.3.7.9.7 1.3v.3c0 1 0 1.4 0 1.7-.2 1.3-1.2 1.9-2.5 1.6-.2 0-.7-.3-1.8-.8C15 6.7 12.8 6 10.5 6s-4.5.7-6.3 1.5c-1 .5-1.5.7-1.8.8-1.3.3-2.3-.3-2.5-1.6v-1.7z'
|
|
28
|
+
|
|
29
|
+
// Same order as the library: round, cross, line, curved.
|
|
30
|
+
const FACES: Face[] = [
|
|
31
|
+
{
|
|
32
|
+
width: 63,
|
|
33
|
+
height: 15,
|
|
34
|
+
draw: ctx => {
|
|
35
|
+
circle(ctx, 7.2, 7.2, 7.2)
|
|
36
|
+
circle(ctx, 55.2, 7.2, 7.2)
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
width: 71,
|
|
41
|
+
height: 23,
|
|
42
|
+
draw: ctx => {
|
|
43
|
+
pill(ctx, 8, 0, 7, 23)
|
|
44
|
+
pill(ctx, 0, 8, 23, 7)
|
|
45
|
+
pill(ctx, 55.2, 0, 7, 23)
|
|
46
|
+
pill(ctx, 47.3, 8, 23, 7)
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
width: 82,
|
|
51
|
+
height: 8,
|
|
52
|
+
draw: ctx => {
|
|
53
|
+
pill(ctx, 0.07, 0.16, 6.9, 6.9)
|
|
54
|
+
pill(ctx, 7.9, 0.16, 20.7, 6.9)
|
|
55
|
+
pill(ctx, 74.7, 0.16, 6.9, 6.9)
|
|
56
|
+
pill(ctx, 53.1, 0.16, 20.7, 6.9)
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
width: 63,
|
|
61
|
+
height: 9,
|
|
62
|
+
draw: ctx => {
|
|
63
|
+
const eye = new Path2D(CURVED_EYE)
|
|
64
|
+
ctx.fill(eye)
|
|
65
|
+
ctx.translate(42, 0)
|
|
66
|
+
ctx.fill(eye)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
// The library tilts the face in 3D; its image renderer flattens that into a
|
|
72
|
+
// small nudge, which is what a raster copy can reproduce.
|
|
73
|
+
const TILTS = [
|
|
74
|
+
[-1, 1],
|
|
75
|
+
[1, 1],
|
|
76
|
+
[1, 0],
|
|
77
|
+
[0, 1],
|
|
78
|
+
[-1, 0],
|
|
79
|
+
[0, 0],
|
|
80
|
+
[0, -1],
|
|
81
|
+
[-1, -1],
|
|
82
|
+
[1, -1]
|
|
83
|
+
] as const
|
|
84
|
+
|
|
85
|
+
// Crisp at the largest host avatar on a 2x display, and a few kilobytes as PNG.
|
|
86
|
+
const SIZE = 96
|
|
87
|
+
|
|
88
|
+
function drawFace(ctx: CanvasRenderingContext2D, name: string, color: string, size: number): void {
|
|
89
|
+
const hash = stringHash(name)
|
|
90
|
+
const face = FACES[hash % FACES.length] ?? FACES[0]
|
|
91
|
+
const [tiltX, tiltY] = TILTS[hash % TILTS.length] ?? [0, 0]
|
|
92
|
+
const dx = tiltY * size * 0.05
|
|
93
|
+
const dy = -tiltX * size * 0.05
|
|
94
|
+
|
|
95
|
+
ctx.fillStyle = color
|
|
96
|
+
ctx.fillRect(0, 0, size, size)
|
|
97
|
+
|
|
98
|
+
const eyesWidth = size * 0.6
|
|
99
|
+
const eyesHeight = (eyesWidth * face.height) / face.width
|
|
100
|
+
const gap = size * 0.08
|
|
101
|
+
const fontSize = size * 0.26
|
|
102
|
+
const top = (size - (eyesHeight + gap + fontSize)) / 2
|
|
103
|
+
|
|
104
|
+
ctx.save()
|
|
105
|
+
ctx.translate((size - eyesWidth) / 2 + dx, top + dy)
|
|
106
|
+
ctx.scale(eyesWidth / face.width, eyesWidth / face.width)
|
|
107
|
+
ctx.fillStyle = '#000'
|
|
108
|
+
face.draw(ctx)
|
|
109
|
+
ctx.restore()
|
|
110
|
+
|
|
111
|
+
ctx.fillStyle = '#000'
|
|
112
|
+
ctx.font = `700 ${fontSize}px ui-monospace, Menlo, monospace`
|
|
113
|
+
ctx.textAlign = 'center'
|
|
114
|
+
ctx.textBaseline = 'middle'
|
|
115
|
+
ctx.fillText(
|
|
116
|
+
name.charAt(0).toUpperCase(),
|
|
117
|
+
size / 2 + dx,
|
|
118
|
+
top + eyesHeight + gap + fontSize / 2 + dy
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function facehashDataUrl(name: string, color: string): string | undefined {
|
|
123
|
+
if (typeof document === 'undefined') return undefined
|
|
124
|
+
const canvas = document.createElement('canvas')
|
|
125
|
+
canvas.width = SIZE
|
|
126
|
+
canvas.height = SIZE
|
|
127
|
+
const ctx = canvas.getContext('2d')
|
|
128
|
+
if (!ctx) return undefined
|
|
129
|
+
drawFace(ctx, name, color, SIZE)
|
|
130
|
+
const url = canvas.toDataURL('image/png')
|
|
131
|
+
return url.length <= COLLAB_MAX_AVATAR_BYTES ? url : undefined
|
|
132
|
+
}
|