moi-computer 0.3.0-next.0 → 0.3.0-next.2

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 (150) hide show
  1. package/bunfig.toml +5 -1
  2. package/client/AGENTS.md +15 -0
  3. package/client/accept-hmr-dependencies.ts +22 -0
  4. package/client/api/http.ts +31 -0
  5. package/client/api/query-options.ts +6 -0
  6. package/client/api/workspace-keys.ts +17 -0
  7. package/client/app/AppRouter.tsx +35 -0
  8. package/client/app/routes/HomeRoute.tsx +13 -0
  9. package/client/app/routes/WorkspaceRoute.tsx +89 -0
  10. package/client/app/shell/SidebarLayout.tsx +191 -0
  11. package/client/assets.d.ts +9 -0
  12. package/client/components/playground/PixelPainter.tsx +10 -21
  13. package/client/components/playground/PlaygroundPage.tsx +9 -11
  14. package/client/components/playground/ToolCallsPage.tsx +9 -11
  15. package/client/components/{BottomPanel.tsx → shared/BottomPanel.tsx} +1 -1
  16. package/client/components/shared/PanelHeader.tsx +9 -0
  17. package/client/components/shared/ReorderableList.tsx +199 -0
  18. package/client/components/ui/button.tsx +10 -7
  19. package/client/components/ui/collapsible.tsx +15 -0
  20. package/client/components/ui/dialog.tsx +2 -2
  21. package/client/components/ui/dropdown-menu.tsx +5 -5
  22. package/client/components/ui/hover-card.tsx +1 -1
  23. package/client/components/ui/popover.tsx +1 -1
  24. package/client/components/ui/skeleton.tsx +13 -0
  25. package/client/components/ui/tooltip.tsx +1 -1
  26. package/client/externalize-react.ts +7 -1
  27. package/client/{components → features/applets}/WidgetErrorBoundary.tsx +1 -1
  28. package/client/{components → features/applets}/WidgetShell.tsx +1 -1
  29. package/client/{hooks → features/applets}/useApplet.ts +6 -7
  30. package/client/{components/ChatInput.tsx → features/chat/ChatComposer.tsx} +29 -23
  31. package/client/features/chat/ChatPanel.tsx +188 -0
  32. package/client/{components → features/chat}/ChatPopup.tsx +23 -12
  33. package/client/{components → features/chat}/MarkdownContent.tsx +1 -1
  34. package/client/{components → features/chat}/ModelPicker.tsx +68 -17
  35. package/client/{components → features/chat}/ThreadSelector.tsx +11 -9
  36. package/client/{components → features/chat}/TurnView.tsx +16 -9
  37. package/client/features/chat/api.ts +78 -0
  38. package/client/features/chat/chat-connection.ts +107 -0
  39. package/client/features/chat/chat-frames.ts +131 -0
  40. package/client/{lib → features/chat}/preview-turn.ts +1 -1
  41. package/client/{components → features/chat}/tool-group/ReadImagePreview.tsx +1 -1
  42. package/client/{components → features/chat}/tool-group/SubagentCard.tsx +12 -10
  43. package/client/{components → features/chat}/tool-group/TimelineRow.tsx +5 -5
  44. package/client/{components → features/chat}/tool-group/ToolCallGroup.tsx +12 -4
  45. package/client/{components → features/chat}/tool-group/ToolOutput.tsx +12 -18
  46. package/client/{hooks → features/chat}/useChat.ts +7 -11
  47. package/client/features/connectors/ConnectorsPage.tsx +92 -0
  48. package/client/features/connectors/McpMenu.tsx +162 -0
  49. package/client/features/connectors/api.ts +28 -0
  50. package/client/features/home/CreateWorkspaceDialog.tsx +224 -0
  51. package/client/features/home/HomePage.tsx +220 -0
  52. package/client/{components → features/home}/WorkspacePreview.tsx +2 -2
  53. package/client/features/home/api.ts +148 -0
  54. package/client/features/home/workspace-presentation.tsx +44 -0
  55. package/client/features/scratchpad/Scratchpad.tsx +312 -0
  56. package/client/features/scratchpad/ScratchpadControls.tsx +503 -0
  57. package/client/features/scratchpad/useScratchpadSnapshot.tsx +115 -0
  58. package/client/features/settings/EnvironmentSettings.tsx +149 -0
  59. package/client/features/settings/GeneralSettings.tsx +90 -0
  60. package/client/{components → features}/settings/IconPicker.tsx +73 -222
  61. package/client/features/settings/SettingsLayout.tsx +53 -0
  62. package/client/{components → features}/settings/WorkspaceSettings.tsx +12 -16
  63. package/client/features/settings/api.ts +88 -0
  64. package/client/features/settings/icon-picker-options.ts +161 -0
  65. package/client/{hooks → features/settings}/useEnvVars.ts +4 -4
  66. package/client/{components → features/widgets}/HiddenPanel.tsx +3 -3
  67. package/client/{components → features/widgets}/WidgetFrame.tsx +3 -8
  68. package/client/{components → features/widgets}/WidgetGrid.tsx +2 -9
  69. package/client/features/widgets/Widgets.tsx +215 -0
  70. package/client/{lib/grid-pack.ts → features/widgets/grid.ts} +7 -1
  71. package/client/{hooks → features/widgets}/useGridReconcile.ts +1 -1
  72. package/client/{components → features/workspace}/CustomizePanel.tsx +5 -6
  73. package/client/{lib → features/workspace}/WorkspaceLayoutContext.tsx +15 -8
  74. package/client/features/workspace/WorkspaceScreen.tsx +536 -0
  75. package/client/features/workspace/WorkspaceTabs.tsx +205 -0
  76. package/client/features/workspace/api.ts +56 -0
  77. package/client/features/workspace/useFitsSplitLayout.ts +37 -0
  78. package/client/global.d.ts +5 -0
  79. package/client/index.css +6 -10
  80. package/client/index.html +1 -1
  81. package/client/index.tsx +14 -31
  82. package/client/lib/ws-url.ts +10 -0
  83. package/client/main.tsx +2 -2
  84. package/client/runtime/hmr-watchdog.ts +39 -0
  85. package/client/{hooks/useMeiEvents.ts → runtime/useWorkspaceEvents.ts} +11 -7
  86. package/client/store/ui.ts +5 -1
  87. package/client/theme.css +0 -2
  88. package/dist/chunk-11cab5dq.css +1 -0
  89. package/dist/chunk-23cgr1tz.css +1 -0
  90. package/dist/chunk-3hy3qy6d.js +10 -0
  91. package/dist/chunk-c4j722at.js +546 -0
  92. package/dist/chunk-dyfaffyz.js +119 -0
  93. package/dist/chunk-kpe40hm8.js +1 -0
  94. package/dist/chunk-p68fnz3q.js +2 -0
  95. package/dist/chunk-pcnahct1.js +5 -0
  96. package/dist/chunk-tqd3pkg8.css +1 -0
  97. package/dist/chunk-ty6dvda0.js +1 -0
  98. package/dist/index.html +2 -2
  99. package/lib/types.ts +18 -7
  100. package/lib/workspace-name.ts +14 -0
  101. package/package.json +3 -2
  102. package/server/api.ts +127 -17
  103. package/server/applets.ts +84 -8
  104. package/server/cli.ts +11 -5
  105. package/server/layout.ts +38 -7
  106. package/server/mcp.ts +72 -15
  107. package/server/moi-scaffold.ts +55 -10
  108. package/server/registry.ts +16 -0
  109. package/server/views.ts +0 -0
  110. package/server/web.ts +1 -0
  111. package/server/widgets.ts +3 -2
  112. package/server/workspace-init.ts +5 -19
  113. package/tsconfig.json +1 -1
  114. package/workspace/.claude/skills/moi-workspace/DESIGN.md +1 -1
  115. package/workspace/.claude/skills/moi-workspace/VIEW-DESIGN.md +1 -1
  116. package/client/api/workspaces.ts +0 -427
  117. package/client/components/App.tsx +0 -41
  118. package/client/components/ChatPanel.tsx +0 -275
  119. package/client/components/HomePage.tsx +0 -17
  120. package/client/components/McpMenu.tsx +0 -125
  121. package/client/components/Scratchpad.tsx +0 -935
  122. package/client/components/Widgets.tsx +0 -132
  123. package/client/components/WorkspacesPage.tsx +0 -245
  124. package/client/components/layout/SidebarLayout.tsx +0 -258
  125. package/client/components/routes/workspace/[id].tsx +0 -675
  126. package/client/components/routes/workspace/create.tsx +0 -155
  127. package/client/components/settings/SettingsPages.tsx +0 -255
  128. package/client/hooks/useFitsSidebar.ts +0 -34
  129. package/client/lib/connection.ts +0 -237
  130. package/dist/chunk-07qf2tv1.css +0 -1
  131. package/dist/chunk-c3zjp9ez.js +0 -672
  132. /package/client/components/{playground → shared}/LedLogo.tsx +0 -0
  133. /package/client/{lib → features/applets}/applet-cache.ts +0 -0
  134. /package/client/{components → features/chat}/ShimmerText.tsx +0 -0
  135. /package/client/{store/live.ts → features/chat/chat-store.ts} +0 -0
  136. /package/client/{lib → features/chat}/group-turns.ts +0 -0
  137. /package/client/{components → features/chat}/tool-group/CodeBlock.tsx +0 -0
  138. /package/client/{components → features/chat}/tool-group/Collapse.tsx +0 -0
  139. /package/client/{components → features/chat}/tool-group/detect.ts +0 -0
  140. /package/client/{components → features/chat}/tool-group/fixtures.ts +0 -0
  141. /package/client/{components → features/chat}/tool-group/format.ts +0 -0
  142. /package/client/{components → features/chat}/tool-group/index.ts +0 -0
  143. /package/client/{lib → features/chat}/uploads.ts +0 -0
  144. /package/client/{hooks → features/chat}/useStickToBottom.ts +0 -0
  145. /package/client/{components → features/connectors}/IconMcp.tsx +0 -0
  146. /package/client/{lib → features/connectors}/mcp-icons.ts +0 -0
  147. /package/client/{lib → features/scratchpad}/scratch-executor.ts +0 -0
  148. /package/client/{lib → features/settings}/render-icon.ts +0 -0
  149. /package/client/{lib → features/workspace}/WorkspaceContext.tsx +0 -0
  150. /package/client/{hooks → features/workspace}/useWorkspaceTheme.ts +0 -0
@@ -0,0 +1,503 @@
1
+ import { Fragment, type ReactElement, useEffect, useState } from 'react'
2
+
3
+ import {
4
+ type Editor,
5
+ type StyleProp,
6
+ type TLDefaultColorStyle,
7
+ type TLDefaultDashStyle,
8
+ type TLDefaultFillStyle,
9
+ type TLDefaultSizeStyle,
10
+ DefaultColorStyle,
11
+ DefaultDashStyle,
12
+ DefaultFillStyle,
13
+ DefaultFontStyle,
14
+ DefaultHorizontalAlignStyle,
15
+ DefaultSizeStyle,
16
+ DefaultVerticalAlignStyle,
17
+ GeoShapeGeoStyle,
18
+ react
19
+ } from 'tldraw'
20
+ import { motion } from 'motion/react'
21
+ import {
22
+ IconArrowUpRight,
23
+ IconEraser,
24
+ IconHandStop,
25
+ IconHighlight,
26
+ IconLine,
27
+ IconPointer2,
28
+ IconSketching,
29
+ IconSquare,
30
+ IconSticker2,
31
+ IconTypography
32
+ } from '@tabler/icons-react'
33
+
34
+ import { cn } from '@/client/lib/cn'
35
+
36
+ type IconC = typeof IconPointer2
37
+
38
+ // The curated left-bar entries, grouped by `sep` dividers. `tool` selects a
39
+ // tldraw tool; `geo` selects the geo tool (rectangle); `menu` is a placeholder
40
+ // (noop for now); `sep` is a divider.
41
+ type ToolEntry =
42
+ | { kind: 'tool'; id: string; label: string; Icon: IconC }
43
+ | { kind: 'geo'; label: string; Icon: IconC }
44
+ | { kind: 'menu'; label: string; Icon: IconC }
45
+ | { kind: 'sep' }
46
+
47
+ const TOOL_ENTRIES: ToolEntry[] = [
48
+ { kind: 'tool', id: 'select', label: 'Select', Icon: IconPointer2 },
49
+ { kind: 'tool', id: 'hand', label: 'Hand', Icon: IconHandStop },
50
+ { kind: 'sep' },
51
+ { kind: 'tool', id: 'draw', label: 'Pencil', Icon: IconSketching },
52
+ { kind: 'geo', label: 'Rectangle', Icon: IconSquare },
53
+ { kind: 'tool', id: 'note', label: 'Sticker', Icon: IconSticker2 },
54
+ { kind: 'tool', id: 'text', label: 'Text', Icon: IconTypography },
55
+ { kind: 'tool', id: 'line', label: 'Line', Icon: IconLine },
56
+ { kind: 'tool', id: 'arrow', label: 'Arrow', Icon: IconArrowUpRight },
57
+ { kind: 'sep' },
58
+ { kind: 'tool', id: 'highlight', label: 'Highlighter', Icon: IconHighlight },
59
+ { kind: 'tool', id: 'eraser', label: 'Eraser', Icon: IconEraser }
60
+ // Temporarily hidden — `menu` kind + IconDots import kept for future use.
61
+ // { kind: "menu", label: "Menu", Icon: IconDots },
62
+ ]
63
+
64
+ type ToolButtonProps = {
65
+ label: string
66
+ active: boolean
67
+ Icon: IconC
68
+ onClick: () => void
69
+ }
70
+
71
+ function ToolButton({ label, active, Icon, onClick }: ToolButtonProps) {
72
+ return (
73
+ <button
74
+ type="button"
75
+ title={label}
76
+ onClick={onClick}
77
+ className={cn(
78
+ 'flex size-9 items-center justify-center rounded-lg text-muted-foreground hover:bg-accent hover:text-accent-foreground',
79
+ active &&
80
+ 'bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground'
81
+ )}
82
+ >
83
+ <Icon size={20} stroke={1.5} />
84
+ </button>
85
+ )
86
+ }
87
+
88
+ type ScratchToolbarProps = { editor: Editor }
89
+
90
+ // tldraw shares style props (dash, size, font…) globally across tools, so a
91
+ // dashed pen would leak into a freshly-picked rect. On each tool activation we
92
+ // pin the styles that tool doesn't expose to fixed values, keeping every tool
93
+ // visually independent.
94
+ function applyToolLocks(editor: Editor, toolId: string) {
95
+ if (toolId === 'geo') {
96
+ // Rects: always a rough (hand-drawn) outline at a consistent stroke — never
97
+ // inherit the pen's dashed/solid or its size.
98
+ editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
99
+ editor.setStyleForNextShapes(DefaultSizeStyle, 'm')
100
+ } else if (toolId === 'note') {
101
+ editor.setStyleForNextShapes(DefaultFontStyle, 'draw')
102
+ editor.setStyleForNextShapes(DefaultHorizontalAlignStyle, 'middle')
103
+ editor.setStyleForNextShapes(DefaultVerticalAlignStyle, 'middle')
104
+ ensurePaletteColor(editor, 'sticker', 'yellow')
105
+ } else if (toolId === 'text') {
106
+ editor.setStyleForNextShapes(DefaultFontStyle, 'draw')
107
+ editor.setStyleForNextShapes(DefaultHorizontalAlignStyle, 'middle')
108
+ editor.setStyleForNextShapes(DefaultVerticalAlignStyle, 'middle')
109
+ } else if (toolId === 'highlight') {
110
+ ensurePaletteColor(editor, 'highlight', 'yellow')
111
+ } else if (toolId === 'arrow') {
112
+ // Arrows don't expose dash — pin it rough to match the hand-drawn look and
113
+ // never inherit a dashed/solid stroke from a previous tool.
114
+ editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
115
+ } else if (toolId === 'draw' || toolId === 'line') {
116
+ // Pen/line only offer rough or dashed — if we're arriving from a rect's
117
+ // 'solid', fall back to rough so a valid dash is always selected.
118
+ const dash = editor.getStyleForNextShape(DefaultDashStyle)
119
+ if (dash !== 'draw' && dash !== 'dashed') {
120
+ editor.setStyleForNextShapes(DefaultDashStyle, 'draw')
121
+ }
122
+ }
123
+ }
124
+
125
+ // Custom vertical tool bar, rendered as an overlay over the canvas (outside
126
+ // tldraw's own UI layout). Tracks the current tool reactively via `react`.
127
+ export function ScratchToolbar({ editor }: ScratchToolbarProps) {
128
+ const [toolId, setToolId] = useState(() => editor.getCurrentToolId())
129
+
130
+ useEffect(() => {
131
+ setToolId(editor.getCurrentToolId())
132
+ return react('scratch current tool', () => setToolId(editor.getCurrentToolId()))
133
+ }, [editor])
134
+
135
+ const activate = (toolId: string) => {
136
+ editor.run(() => {
137
+ applyToolLocks(editor, toolId)
138
+ editor.setCurrentTool(toolId)
139
+ })
140
+ }
141
+
142
+ const selectRectangle = () => {
143
+ editor.run(() => {
144
+ editor.setStyleForNextShapes(GeoShapeGeoStyle, 'rectangle')
145
+ applyToolLocks(editor, 'geo')
146
+ editor.setCurrentTool('geo')
147
+ })
148
+ }
149
+
150
+ return (
151
+ <div className="absolute top-1/2 left-3 z-10 -translate-y-1/2">
152
+ <motion.div
153
+ initial={{ x: -72, opacity: 0 }}
154
+ animate={{ x: 0, opacity: 1 }}
155
+ transition={{ type: 'spring', bounce: 0.4, duration: 0.55 }}
156
+ className="flex flex-col gap-1 rounded-xl bg-background/95 p-1 shadow-[0_2px_8px_rgba(0,0,0,0.08),0_4px_16px_rgba(0,0,0,0.06),0_0_0_1px_rgba(0,0,0,0.04)] backdrop-blur"
157
+ >
158
+ {TOOL_ENTRIES.map((entry, i) => {
159
+ if (entry.kind === 'sep') {
160
+ return <div key={`sep-${i}`} className="mx-1 my-0.5 h-px bg-border" />
161
+ }
162
+ if (entry.kind === 'geo') {
163
+ return (
164
+ <ToolButton
165
+ key="geo"
166
+ label={entry.label}
167
+ Icon={entry.Icon}
168
+ active={toolId === 'geo'}
169
+ onClick={selectRectangle}
170
+ />
171
+ )
172
+ }
173
+ if (entry.kind === 'menu') {
174
+ return (
175
+ <ToolButton
176
+ key="menu"
177
+ label={entry.label}
178
+ Icon={entry.Icon}
179
+ active={false}
180
+ onClick={() => {}}
181
+ />
182
+ )
183
+ }
184
+ return (
185
+ <ToolButton
186
+ key={entry.id}
187
+ label={entry.label}
188
+ Icon={entry.Icon}
189
+ active={toolId === entry.id}
190
+ onClick={() => activate(entry.id)}
191
+ />
192
+ )
193
+ })}
194
+ </motion.div>
195
+ </div>
196
+ )
197
+ }
198
+
199
+ // --- Second bar: per-tool contextual style controls -----------------------
200
+ // Each tool exposes a different subset of controls (color/size/dash/fill). The
201
+ // sub-bar shows the active tool's set, or — when the Select tool has a selection
202
+ // — the union of the selected shapes' sets. Size maps small→'m', large→'xl'.
203
+ // Sticker & text also lock align=center and font=rough (set on mount, not shown).
204
+
205
+ type StyleControl = 'color' | 'size' | 'dash' | 'fill'
206
+ const CONTROL_ORDER: StyleControl[] = ['color', 'size', 'dash', 'fill']
207
+
208
+ // Controls exposed while a creating tool is active.
209
+ const CONTROLS_BY_TOOL: Record<string, StyleControl[]> = {
210
+ draw: ['color', 'size', 'dash'],
211
+ geo: ['color', 'fill'],
212
+ note: ['color', 'size'],
213
+ text: ['color', 'size'],
214
+ line: ['color', 'dash', 'size'],
215
+ highlight: ['color', 'size'],
216
+ arrow: ['color', 'size']
217
+ }
218
+
219
+ // Controls exposed per selected shape type (Select tool with a selection).
220
+ const CONTROLS_BY_SHAPE: Record<string, StyleControl[]> = {
221
+ draw: ['color', 'size', 'dash'],
222
+ geo: ['color', 'fill'],
223
+ note: ['color', 'size'],
224
+ text: ['color', 'size'],
225
+ line: ['color', 'dash', 'size'],
226
+ highlight: ['color', 'size'],
227
+ arrow: ['color', 'size']
228
+ }
229
+
230
+ // Three contextual color palettes (all subsets of tldraw's color enum):
231
+ // regular (most tools), highlighter-friendly, and sticky-note pastels.
232
+ type Swatch = { value: TLDefaultColorStyle; klass: string }
233
+ type PaletteKind = 'regular' | 'highlight' | 'sticker'
234
+ type Palette = { swatches: Swatch[] }
235
+
236
+ // Swatch display hexes are tldraw's actual light-theme colors, picked to match
237
+ // how each tool paints: regular → stroke `solid`, highlight → `highlightSrgb`,
238
+ // sticker → `noteFill`.
239
+ const PALETTES: Record<PaletteKind, Palette> = {
240
+ regular: {
241
+ swatches: [
242
+ { value: 'black', klass: 'bg-[#1d1d1d]' },
243
+ { value: 'red', klass: 'bg-[#e03131]' },
244
+ { value: 'yellow', klass: 'bg-[#f1ac4b]' },
245
+ { value: 'green', klass: 'bg-[#099268]' },
246
+ { value: 'blue', klass: 'bg-[#4465e9]' },
247
+ { value: 'grey', klass: 'bg-[#9fa8b2]' }
248
+ ]
249
+ },
250
+ highlight: {
251
+ swatches: [
252
+ { value: 'yellow', klass: 'bg-[#fddd00]' },
253
+ { value: 'green', klass: 'bg-[#00ffc8]' },
254
+ { value: 'blue', klass: 'bg-[#10acff]' }
255
+ ]
256
+ },
257
+ sticker: {
258
+ swatches: [
259
+ { value: 'yellow', klass: 'bg-[#fed49a]' },
260
+ { value: 'blue', klass: 'bg-[#8aa3ff]' },
261
+ { value: 'green', klass: 'bg-[#6fc896]' }
262
+ ]
263
+ }
264
+ }
265
+
266
+ // Which palette applies right now — by active creating tool, else by the single
267
+ // selected shape type, else regular.
268
+ function paletteKind(editor: Editor): PaletteKind {
269
+ const toolId = editor.getCurrentToolId()
270
+ if (toolId === 'highlight') return 'highlight'
271
+ if (toolId === 'note') return 'sticker'
272
+ if (CONTROLS_BY_TOOL[toolId]) return 'regular'
273
+
274
+ const types = new Set(editor.getSelectedShapeIds().map(id => editor.getShape(id)?.type))
275
+ if (types.size === 1) {
276
+ if (types.has('highlight')) return 'highlight'
277
+ if (types.has('note')) return 'sticker'
278
+ }
279
+ return 'regular'
280
+ }
281
+
282
+ // If the next-shape color isn't in `kind`'s palette, snap it to a sensible
283
+ // default so the swatch row always has an active selection.
284
+ function ensurePaletteColor(editor: Editor, kind: PaletteKind, fallback: TLDefaultColorStyle) {
285
+ const values = PALETTES[kind].swatches.map(s => s.value)
286
+ if (!values.includes(editor.getStyleForNextShape(DefaultColorStyle))) {
287
+ editor.setStyleForNextShapes(DefaultColorStyle, fallback)
288
+ }
289
+ }
290
+
291
+ // Two sizes only — small→'m', large→'xl'. Each option carries previews for the
292
+ // three size renderings (see sizeGlyphKind): `dot` blob for most tools, `stroke`
293
+ // pen icon for the pencil, `glyph` letter px for text.
294
+ const SIZE_OPTIONS: {
295
+ value: TLDefaultSizeStyle
296
+ label: string
297
+ dot: string
298
+ glyph: number
299
+ }[] = [
300
+ { value: 'm', label: 'Small', dot: 'size-1.5', glyph: 16 },
301
+ { value: 'xl', label: 'Large', dot: 'size-3', glyph: 24 }
302
+ ]
303
+
304
+ // Rough (hand-drawn) vs dashed stroke.
305
+ const DASH_OPTIONS: { value: TLDefaultDashStyle; label: string; preview: string }[] = [
306
+ { value: 'draw', label: 'Rough', preview: 'border-b-2 border-current' },
307
+ { value: 'dashed', label: 'Dash', preview: 'border-b-2 border-dashed border-current' }
308
+ ]
309
+
310
+ // Three fills: empty, full color, hatch. (tldraw's translucent 'semi' read as
311
+ // "stroke only", so we drop it — 'solid' is the fullest native color fill.)
312
+ const FILL_OPTIONS: { value: TLDefaultFillStyle; label: string; preview: string }[] = [
313
+ // tldraw quirk (see defaultFills): fillStyle 'solid' paints the lighter "semi"
314
+ // color, while 'fill' paints the true solid (body === border color).
315
+ { value: 'none', label: 'None', preview: 'border border-foreground/50' },
316
+ { value: 'solid', label: 'Semi', preview: 'border border-foreground/40 bg-foreground/25' },
317
+ { value: 'fill', label: 'Solid', preview: 'border border-foreground bg-foreground' },
318
+ {
319
+ value: 'pattern',
320
+ label: 'Pattern',
321
+ preview:
322
+ 'border border-foreground/40 bg-[repeating-linear-gradient(45deg,currentColor_0,currentColor_1px,transparent_1px,transparent_3px)]'
323
+ }
324
+ ]
325
+
326
+ // Read the active value of a style: shared value across the selection, or the
327
+ // next-shape value when nothing is selected.
328
+ function readStyleValue<T>(editor: Editor, style: StyleProp<T>): T | undefined {
329
+ if (editor.getSelectedShapeIds().length > 0) {
330
+ return editor.getSharedStyles().getAsKnownValue(style)
331
+ }
332
+ return editor.getStyleForNextShape(style)
333
+ }
334
+
335
+ // Apply to the selection (if any) and make it the default for the next shape.
336
+ function applyStyle<T>(editor: Editor, style: StyleProp<T>, value: T) {
337
+ editor.run(() => {
338
+ editor.setStyleForNextShapes(style, value)
339
+ if (editor.getSelectedShapeIds().length > 0) {
340
+ editor.setStyleForSelectedShapes(style, value)
341
+ }
342
+ })
343
+ }
344
+
345
+ // Which controls the sub-bar should show right now: the active creating tool's
346
+ // set, else the union for the current selection, else none (bar hidden).
347
+ function activeControls(editor: Editor): StyleControl[] {
348
+ const toolControls = CONTROLS_BY_TOOL[editor.getCurrentToolId()]
349
+ if (toolControls) return toolControls
350
+
351
+ const ids = editor.getSelectedShapeIds()
352
+ if (ids.length === 0) return []
353
+ const set = new Set<StyleControl>()
354
+ for (const id of ids) {
355
+ const shape = editor.getShape(id)
356
+ if (!shape) continue
357
+ for (const control of CONTROLS_BY_SHAPE[shape.type] ?? []) set.add(control)
358
+ }
359
+ return CONTROL_ORDER.filter(control => set.has(control))
360
+ }
361
+
362
+ // How the size control previews itself, matched to what "size" means for the
363
+ // active tool: the pencil's is a stroke weight (pen glyph at varying strokes),
364
+ // text's is a font size (a letter at varying sizes), everything else a plain blob.
365
+ // Keyed off the active tool, or a uniform selection under the Select tool.
366
+ function sizeGlyphKind(editor: Editor): 'pen' | 'letter' | 'dot' {
367
+ const toolId = editor.getCurrentToolId()
368
+ if (toolId === 'draw') return 'pen'
369
+ if (toolId === 'text') return 'letter'
370
+ const ids = editor.getSelectedShapeIds()
371
+ const types = new Set(ids.map(id => editor.getShape(id)?.type))
372
+ if (types.size === 1) {
373
+ if (types.has('draw')) return 'pen'
374
+ if (types.has('text')) return 'letter'
375
+ }
376
+ return 'dot'
377
+ }
378
+
379
+ type StyleBarState = {
380
+ controls: StyleControl[]
381
+ palette: Palette
382
+ color: TLDefaultColorStyle | undefined
383
+ size: TLDefaultSizeStyle | undefined
384
+ sizeGlyph: 'pen' | 'letter' | 'dot'
385
+ dash: TLDefaultDashStyle | undefined
386
+ fill: TLDefaultFillStyle | undefined
387
+ }
388
+
389
+ function readStyleBarState(editor: Editor): StyleBarState {
390
+ return {
391
+ controls: activeControls(editor),
392
+ palette: PALETTES[paletteKind(editor)],
393
+ color: readStyleValue(editor, DefaultColorStyle),
394
+ size: readStyleValue(editor, DefaultSizeStyle),
395
+ sizeGlyph: sizeGlyphKind(editor),
396
+ dash: readStyleValue(editor, DefaultDashStyle),
397
+ fill: readStyleValue(editor, DefaultFillStyle)
398
+ }
399
+ }
400
+
401
+ type ScratchStyleBarProps = { editor: Editor }
402
+
403
+ export function ScratchStyleBar({ editor }: ScratchStyleBarProps) {
404
+ const [state, setState] = useState<StyleBarState>(() => readStyleBarState(editor))
405
+
406
+ useEffect(() => {
407
+ setState(readStyleBarState(editor))
408
+ return react('scratch styles', () => setState(readStyleBarState(editor)))
409
+ }, [editor])
410
+
411
+ if (state.controls.length === 0) return null
412
+
413
+ const groups: Record<StyleControl, ReactElement> = {
414
+ color: (
415
+ <div className="flex flex-col items-center gap-2 pt-1">
416
+ {state.palette.swatches.map(c => (
417
+ <button
418
+ key={c.value}
419
+ type="button"
420
+ title={c.value}
421
+ onClick={() => applyStyle(editor, DefaultColorStyle, c.value)}
422
+ className={cn(
423
+ 'size-5.5 rounded-full ring-offset-1 ring-offset-background',
424
+ c.klass,
425
+ state.color === c.value ? 'ring-2 ring-primary' : 'ring-1 ring-black/15'
426
+ )}
427
+ />
428
+ ))}
429
+ </div>
430
+ ),
431
+ size: (
432
+ <div className="flex flex-col items-center gap-1 text-foreground">
433
+ {SIZE_OPTIONS.map(s => (
434
+ <button
435
+ key={s.value}
436
+ type="button"
437
+ title={s.label}
438
+ onClick={() => applyStyle(editor, DefaultSizeStyle, s.value)}
439
+ className={cn(
440
+ 'flex size-7.5 items-center justify-center rounded-full hover:bg-accent hover:text-accent-foreground',
441
+ state.size === s.value && 'bg-accent text-accent-foreground'
442
+ )}
443
+ >
444
+ {state.sizeGlyph === 'pen' ? (
445
+ <IconSketching size={20} stroke={1.5} />
446
+ ) : state.sizeGlyph === 'letter' ? (
447
+ <IconTypography size={s.glyph} stroke={s.glyph === 16 ? 1.75 : 1.5} />
448
+ ) : (
449
+ <span className={cn('rounded-full bg-current', s.dot)} />
450
+ )}
451
+ </button>
452
+ ))}
453
+ </div>
454
+ ),
455
+ dash: (
456
+ <div className="flex flex-col items-center gap-1 text-foreground">
457
+ {DASH_OPTIONS.map(d => (
458
+ <button
459
+ key={d.value}
460
+ type="button"
461
+ title={d.label}
462
+ onClick={() => applyStyle(editor, DefaultDashStyle, d.value)}
463
+ className={cn(
464
+ 'flex size-7.5 items-center justify-center rounded-full hover:bg-accent hover:text-accent-foreground',
465
+ state.dash === d.value && 'bg-accent text-accent-foreground'
466
+ )}
467
+ >
468
+ <span className={cn('w-3.5', d.preview)} />
469
+ </button>
470
+ ))}
471
+ </div>
472
+ ),
473
+ fill: (
474
+ <div className="flex flex-col items-center gap-1 text-foreground">
475
+ {FILL_OPTIONS.map(f => (
476
+ <button
477
+ key={f.value}
478
+ type="button"
479
+ title={f.label}
480
+ onClick={() => applyStyle(editor, DefaultFillStyle, f.value)}
481
+ className={cn(
482
+ 'flex size-7.5 items-center justify-center rounded-full hover:bg-accent hover:text-accent-foreground',
483
+ state.fill === f.value && 'bg-accent text-accent-foreground'
484
+ )}
485
+ >
486
+ <span className={cn('size-4.5 rounded-sm', f.preview)} />
487
+ </button>
488
+ ))}
489
+ </div>
490
+ )
491
+ }
492
+
493
+ return (
494
+ <div className="absolute top-1/2 left-15 z-10 flex -translate-y-1/2 flex-col items-center gap-1.5 rounded-full bg-background/95 px-1 py-1 shadow-[0_2px_8px_rgba(0,0,0,0.08),0_4px_16px_rgba(0,0,0,0.06),0_0_0_1px_rgba(0,0,0,0.04)] backdrop-blur">
495
+ {state.controls.map((control, i) => (
496
+ <Fragment key={control}>
497
+ {i > 0 && <div className="h-px w-5 bg-border" />}
498
+ {groups[control]}
499
+ </Fragment>
500
+ ))}
501
+ </div>
502
+ )
503
+ }
@@ -0,0 +1,115 @@
1
+ import { useEffect, useRef, useState } from 'react'
2
+
3
+ import { IconVersions } from '@tabler/icons-react'
4
+ import {
5
+ type TLEditorSnapshot,
6
+ type TLStore,
7
+ createTLStore,
8
+ defaultBindingUtils,
9
+ defaultShapeUtils
10
+ } from 'tldraw'
11
+
12
+ import { describeNewerWriter, sequencesAhead } from '@/lib/scratchpad-skew'
13
+ import type { ScratchpadWriter } from '@/lib/types'
14
+
15
+ export type ScratchpadFetch = {
16
+ document: TLEditorSnapshot['document'] | null
17
+ writer?: ScratchpadWriter
18
+ }
19
+
20
+ export type ScratchpadSkew = {
21
+ newer: boolean
22
+ writer?: ScratchpadWriter
23
+ detail: string
24
+ }
25
+
26
+ let runtimeSchemaCache: TLStore['schema'] | null = null
27
+
28
+ function runtimeSchema(): TLStore['schema'] {
29
+ runtimeSchemaCache ??= createTLStore({
30
+ shapeUtils: defaultShapeUtils,
31
+ bindingUtils: defaultBindingUtils
32
+ }).schema
33
+ return runtimeSchemaCache
34
+ }
35
+
36
+ export function detectScratchpadSkew(
37
+ document: NonNullable<TLEditorSnapshot['document']>,
38
+ writer: ScratchpadWriter | undefined
39
+ ): ScratchpadSkew | null {
40
+ const schema = runtimeSchema()
41
+ try {
42
+ if (schema.migrateStoreSnapshot(document).type === 'success') return null
43
+ } catch {}
44
+ const ahead = sequencesAhead(document.schema, schema.serialize().sequences)
45
+ if (ahead.length > 0) {
46
+ return { newer: true, writer, detail: describeNewerWriter(writer, ahead) }
47
+ }
48
+ return { newer: false, writer, detail: 'The saved snapshot failed to load.' }
49
+ }
50
+
51
+ export function useScratchpadSnapshot(workspaceId: string): {
52
+ loaded: boolean
53
+ snapshot: TLEditorSnapshot['document'] | undefined
54
+ skew: ScratchpadSkew | null
55
+ flagSkew: (skew: ScratchpadSkew) => void
56
+ } {
57
+ const [loaded, setLoaded] = useState(false)
58
+ const [skew, setSkew] = useState<ScratchpadSkew | null>(null)
59
+ const snapshot = useRef<TLEditorSnapshot['document'] | undefined>(undefined)
60
+
61
+ useEffect(() => {
62
+ let cancelled = false
63
+ setLoaded(false)
64
+ setSkew(null)
65
+ snapshot.current = undefined
66
+ fetch(`/api/workspaces/${workspaceId}/scratchpad`)
67
+ .then(response => response.json())
68
+ .then((data: ScratchpadFetch) => {
69
+ if (cancelled) return
70
+ if (data?.document) {
71
+ const found = detectScratchpadSkew(data.document, data.writer)
72
+ if (found) setSkew(found)
73
+ else snapshot.current = data.document
74
+ }
75
+ setLoaded(true)
76
+ })
77
+ .catch(() => {
78
+ if (!cancelled) setLoaded(true)
79
+ })
80
+ return () => {
81
+ cancelled = true
82
+ }
83
+ }, [workspaceId])
84
+
85
+ return { loaded, snapshot: snapshot.current, skew, flagSkew: setSkew }
86
+ }
87
+
88
+ export function ScratchpadSkewNotice({ skew }: { skew: ScratchpadSkew }) {
89
+ return (
90
+ <div className="flex min-h-0 flex-1 items-center justify-center bg-muted/40 bg-[radial-gradient(var(--color-border)_1px,transparent_1px)] [background-size:16px_16px] p-6">
91
+ <div className="flex max-w-md animate-in flex-col gap-3 rounded-md bg-background p-6 shadow-xs duration-200 fade-in-0 zoom-in-95">
92
+ <div className="flex items-center gap-2">
93
+ <IconVersions size={20} stroke={1.5} className="shrink-0 text-amber-600" />
94
+ <h2 className="font-medium text-foreground">
95
+ {skew.newer ? 'This canvas needs a newer moi' : 'This canvas couldn’t be loaded'}
96
+ </h2>
97
+ </div>
98
+ {skew.newer ? (
99
+ <>
100
+ <p className="text-sm text-pretty text-muted-foreground">
101
+ This scratchpad was saved by a newer version of moi
102
+ {skew.writer ? ` (v${skew.writer.moi})` : ''}.<br /> Please update moi and restart the
103
+ server:
104
+ </p>
105
+ <code className="self-start rounded bg-muted px-2 py-1 font-mono text-xs text-foreground">
106
+ bun install -g moi-computer@latest
107
+ </code>
108
+ </>
109
+ ) : (
110
+ <p className="text-sm text-muted-foreground">{skew.detail}</p>
111
+ )}
112
+ </div>
113
+ </div>
114
+ )
115
+ }