@yaks/browse 0.0.0-stage → 0.2.36

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 (271) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +324 -2
  3. package/_dist/app.d.ts +8 -0
  4. package/_dist/app.d.ts.map +1 -0
  5. package/_dist/cli.d.ts +16 -0
  6. package/_dist/cli.d.ts.map +1 -0
  7. package/_dist/hosting.d.ts +24 -0
  8. package/_dist/hosting.d.ts.map +1 -0
  9. package/_dist/mod.d.ts +7 -0
  10. package/_dist/mod.d.ts.map +1 -0
  11. package/_dist/types.d.ts +5 -0
  12. package/_dist/types.d.ts.map +1 -0
  13. package/_dist/url.d.ts +2 -0
  14. package/_dist/url.d.ts.map +1 -0
  15. package/app.js +10 -0
  16. package/app.js.map +1 -0
  17. package/app.ts +10 -0
  18. package/browser-links.js +22 -0
  19. package/browser-links.js.map +1 -0
  20. package/browser-links.ts +28 -0
  21. package/cli.js +172 -0
  22. package/cli.js.map +1 -0
  23. package/cli.ts +168 -0
  24. package/client.js +748 -0
  25. package/client.js.map +1 -0
  26. package/client.ts +734 -0
  27. package/clipboard.js +18 -0
  28. package/clipboard.js.map +1 -0
  29. package/clipboard.ts +17 -0
  30. package/commands.js +1157 -0
  31. package/commands.js.map +1 -0
  32. package/commands.ts +1108 -0
  33. package/components/Ansi.tsx +160 -0
  34. package/components/App.tsx +446 -0
  35. package/components/Canvas.tsx +625 -0
  36. package/components/Card.tsx +276 -0
  37. package/components/Chat.tsx +205 -0
  38. package/components/Configuration.tsx +218 -0
  39. package/components/Decision.tsx +49 -0
  40. package/components/Dot.tsx +52 -0
  41. package/components/Entity.tsx +609 -0
  42. package/components/Fault.tsx +43 -0
  43. package/components/Filter.tsx +103 -0
  44. package/components/Keybindings.tsx +100 -0
  45. package/components/Keys.tsx +67 -0
  46. package/components/Markdown.tsx +90 -0
  47. package/components/Navigation.tsx +161 -0
  48. package/components/New.tsx +490 -0
  49. package/components/Peek.tsx +168 -0
  50. package/components/Prio.tsx +20 -0
  51. package/components/Run.tsx +263 -0
  52. package/components/Search.tsx +280 -0
  53. package/components/Stamp.tsx +44 -0
  54. package/components/Status.tsx +514 -0
  55. package/components/Sync.tsx +173 -0
  56. package/components/Tile.tsx +50 -0
  57. package/components/answered.js +23 -0
  58. package/components/answered.js.map +1 -0
  59. package/components/answered.ts +25 -0
  60. package/components/comp.js +11 -0
  61. package/components/comp.js.map +1 -0
  62. package/components/comp.ts +10 -0
  63. package/components/drag.js +173 -0
  64. package/components/drag.js.map +1 -0
  65. package/components/drag.ts +198 -0
  66. package/components/fields.tsx +90 -0
  67. package/components/fixtures/README.md +10 -0
  68. package/components/fixtures/archetypes.json +3646 -0
  69. package/components/hits.js +63 -0
  70. package/components/hits.js.map +1 -0
  71. package/components/hits.ts +84 -0
  72. package/components/icons.tsx +177 -0
  73. package/components/inspect.tsx +331 -0
  74. package/components/mount.ts +79 -0
  75. package/components/nav.tsx +479 -0
  76. package/components/offers.js +26 -0
  77. package/components/offers.js.map +1 -0
  78. package/components/offers.ts +54 -0
  79. package/components/page.js +21 -0
  80. package/components/page.js.map +1 -0
  81. package/components/page.ts +18 -0
  82. package/components/read.js +5 -0
  83. package/components/read.js.map +1 -0
  84. package/components/read.ts +7 -0
  85. package/components/registry.js +285 -0
  86. package/components/registry.js.map +1 -0
  87. package/components/registry.ts +317 -0
  88. package/components/session_status.tsx +115 -0
  89. package/components/subscriptions.js +149 -0
  90. package/components/subscriptions.js.map +1 -0
  91. package/components/subscriptions.ts +161 -0
  92. package/components/title.tsx +21 -0
  93. package/components/useQuery.js +166 -0
  94. package/components/useQuery.js.map +1 -0
  95. package/components/useQuery.ts +211 -0
  96. package/components/views/Board.tsx +450 -0
  97. package/components/views/BoardTile.tsx +65 -0
  98. package/components/views/Dashboard.tsx +148 -0
  99. package/components/views/Debug.tsx +445 -0
  100. package/components/views/Dependency.tsx +51 -0
  101. package/components/views/Entry.tsx +500 -0
  102. package/components/views/Gist.tsx +28 -0
  103. package/components/views/Inline.tsx +54 -0
  104. package/components/views/Json.tsx +19 -0
  105. package/components/views/Layout.tsx +211 -0
  106. package/components/views/List.tsx +238 -0
  107. package/components/views/Md.tsx +43 -0
  108. package/components/views/Media.tsx +48 -0
  109. package/components/views/Memory.tsx +32 -0
  110. package/components/views/Relate.tsx +205 -0
  111. package/components/views/Search.tsx +42 -0
  112. package/components/views/Session.tsx +850 -0
  113. package/components/views/SessionManage.tsx +127 -0
  114. package/components/views/Show.tsx +758 -0
  115. package/components/views/Shown.tsx +72 -0
  116. package/components/views/Split.tsx +58 -0
  117. package/components/views/TaskTile.tsx +37 -0
  118. package/components/views/Title.tsx +105 -0
  119. package/components/views/Wake.tsx +77 -0
  120. package/components/views/Web.tsx +49 -0
  121. package/components/wells.js +30 -0
  122. package/components/wells.js.map +1 -0
  123. package/components/wells.ts +33 -0
  124. package/configuration.js +38 -0
  125. package/configuration.js.map +1 -0
  126. package/configuration.ts +41 -0
  127. package/decision_sync.js +88 -0
  128. package/decision_sync.js.map +1 -0
  129. package/decision_sync.ts +93 -0
  130. package/deno.json +1 -0
  131. package/domain-host.tsx +108 -0
  132. package/edge.js +193 -0
  133. package/edge.js.map +1 -0
  134. package/edge.ts +196 -0
  135. package/entry_log.js +241 -0
  136. package/entry_log.js.map +1 -0
  137. package/entry_log.ts +214 -0
  138. package/failure.js +22 -0
  139. package/failure.js.map +1 -0
  140. package/failure.ts +34 -0
  141. package/front.json +61 -0
  142. package/highlight.js +143 -0
  143. package/highlight.js.map +1 -0
  144. package/highlight.ts +149 -0
  145. package/history.js +68 -0
  146. package/history.js.map +1 -0
  147. package/history.ts +72 -0
  148. package/host_commands.js +107 -0
  149. package/host_commands.js.map +1 -0
  150. package/host_commands.ts +38 -0
  151. package/hosting.js +53 -0
  152. package/hosting.js.map +1 -0
  153. package/hosting.ts +72 -0
  154. package/idb.js +14 -0
  155. package/idb.js.map +1 -0
  156. package/idb.ts +15 -0
  157. package/index.js +232 -0
  158. package/index.js.map +1 -0
  159. package/index.ts +252 -0
  160. package/keybindings.js +136 -0
  161. package/keybindings.js.map +1 -0
  162. package/keybindings.ts +37 -0
  163. package/kit.js +103 -0
  164. package/kit.js.map +1 -0
  165. package/kit.ts +119 -0
  166. package/layout.js +254 -0
  167. package/layout.js.map +1 -0
  168. package/layout.ts +210 -0
  169. package/legacy_outbox.js +53 -0
  170. package/legacy_outbox.js.map +1 -0
  171. package/legacy_outbox.ts +63 -0
  172. package/live.js +2883 -0
  173. package/live.js.map +1 -0
  174. package/live.ts +3334 -0
  175. package/live_archetypes.js +92 -0
  176. package/live_archetypes.js.map +1 -0
  177. package/live_archetypes.ts +113 -0
  178. package/live_client.js +145 -0
  179. package/live_client.js.map +1 -0
  180. package/live_client.ts +120 -0
  181. package/main.tsx +5 -0
  182. package/mod.js +7 -0
  183. package/mod.js.map +1 -0
  184. package/mod.ts +7 -0
  185. package/mount.tsx +50 -0
  186. package/navigation.js +120 -0
  187. package/navigation.js.map +1 -0
  188. package/navigation.ts +152 -0
  189. package/near.js +83 -0
  190. package/near.js.map +1 -0
  191. package/near.ts +83 -0
  192. package/new.js +295 -0
  193. package/new.js.map +1 -0
  194. package/new.ts +361 -0
  195. package/outbox.js +112 -0
  196. package/outbox.js.map +1 -0
  197. package/outbox.ts +129 -0
  198. package/owner.js +39 -0
  199. package/owner.js.map +1 -0
  200. package/owner.ts +37 -0
  201. package/package.json +81 -4
  202. package/paste.js +157 -0
  203. package/paste.js.map +1 -0
  204. package/paste.ts +122 -0
  205. package/props.js +303 -0
  206. package/props.js.map +1 -0
  207. package/props.ts +358 -0
  208. package/providers.js +136 -0
  209. package/providers.js.map +1 -0
  210. package/providers.ts +194 -0
  211. package/query.js +987 -0
  212. package/query.js.map +1 -0
  213. package/query.ts +1113 -0
  214. package/remote.js +37 -0
  215. package/remote.js.map +1 -0
  216. package/remote.ts +46 -0
  217. package/resolver.js +149 -0
  218. package/resolver.js.map +1 -0
  219. package/resolver.ts +213 -0
  220. package/rfc2047.js +37 -0
  221. package/rfc2047.js.map +1 -0
  222. package/rfc2047.ts +40 -0
  223. package/row_holds.js +107 -0
  224. package/row_holds.js.map +1 -0
  225. package/row_holds.ts +93 -0
  226. package/served.ts +42 -0
  227. package/session_worktree.js +7 -0
  228. package/session_worktree.js.map +1 -0
  229. package/session_worktree.ts +16 -0
  230. package/sessions.js +57 -0
  231. package/sessions.js.map +1 -0
  232. package/sessions.ts +53 -0
  233. package/sha.js +161 -0
  234. package/sha.js.map +1 -0
  235. package/sha.ts +164 -0
  236. package/styles.css +2823 -0
  237. package/subs.js +36 -0
  238. package/subs.js.map +1 -0
  239. package/subs.ts +40 -0
  240. package/terminal-route.js +13 -0
  241. package/terminal-route.js.map +1 -0
  242. package/terminal-route.ts +17 -0
  243. package/terminal.js +251 -0
  244. package/terminal.js.map +1 -0
  245. package/terminal.ts +186 -0
  246. package/time.js +142 -0
  247. package/time.js.map +1 -0
  248. package/time.ts +133 -0
  249. package/title.js +25 -0
  250. package/title.js.map +1 -0
  251. package/title.ts +50 -0
  252. package/tui/doc.ts +6 -0
  253. package/tui/testing.ts +19 -0
  254. package/twin.js +11 -0
  255. package/twin.js.map +1 -0
  256. package/twin.ts +13 -0
  257. package/types.js +456 -0
  258. package/types.js.map +1 -0
  259. package/types.ts +1370 -0
  260. package/url.js +77 -0
  261. package/url.js.map +1 -0
  262. package/url.ts +85 -0
  263. package/verb.js +82 -0
  264. package/verb.js.map +1 -0
  265. package/verb.ts +137 -0
  266. package/warmth.js +41 -0
  267. package/warmth.js.map +1 -0
  268. package/warmth.ts +54 -0
  269. package/wire.js +86 -0
  270. package/wire.js.map +1 -0
  271. package/wire.ts +63 -0
@@ -0,0 +1,514 @@
1
+ /** @jsxRuntime automatic *//** @jsxImportSourceTypes npm:react@19.3.0 *//** @jsxImportSource npm:react@19.3.0 */
2
+ import { frontOverlay } from 'jsr:@yaks/ui@0.2.36/overlays'
3
+ import { drafts } from 'jsr:@yaks/draft@0.2.36/input'
4
+ import { useDerived } from './read.ts'
5
+ import { entityPath } from '../url.ts'
6
+ /* @ts-types="npm:@types/react@19.3.0" */ import { useEffect, useRef, useState } from 'npm:react@19.3.0'
7
+ import { signal } from 'npm:@preact/signals-core@1.14.5'
8
+ import {
9
+ camera,
10
+ capable,
11
+ clientId,
12
+ ent,
13
+ mode,
14
+ mutate,
15
+ pinned,
16
+ problem,
17
+ rows,
18
+ uuid,
19
+ } from '../live.ts'
20
+ import { type Change, idOf, vocab } from '../types.ts'
21
+ import { hostCommands } from '../host_commands.ts'
22
+ import {
23
+ type Command,
24
+ type Ctx,
25
+ ghost,
26
+ run,
27
+ type SpawnIntent,
28
+ spawnTask,
29
+ suggest,
30
+ } from '../commands.ts'
31
+ import { num, slotsOf } from '../verb.ts'
32
+ import { navigate, screenTarget } from './nav.tsx'
33
+ import { pickLine, useDraft, useHits, useObserved } from 'jsr:@yaks/ux@0.2.36'
34
+ import { typing } from 'jsr:@yaks/ux@0.2.36/keys'
35
+ import { editKeys } from 'jsr:@yaks/ux@0.2.36/edit-keys'
36
+ import { spawnOf } from './Run.tsx'
37
+ import { block, Combobox } from 'jsr:@yaks/ui@0.2.36'
38
+ import { Entity } from './Entity.tsx'
39
+ import { Id } from './views/Inline.tsx'
40
+ import { hits } from './hits.ts'
41
+ import { spawnHit } from './Canvas.tsx'
42
+ import { fields } from './fields.tsx'
43
+ import { Sync } from './Sync.tsx'
44
+
45
+ let Frame = block('footer', 'Status', {
46
+ Left: 'div',
47
+ Right: 'div',
48
+ Close: 'button',
49
+ Mode: 'span',
50
+ Colon: 'span',
51
+ Line: 'span',
52
+ Ghost: 'span',
53
+ Was: 'i',
54
+ Verb: 'span',
55
+ Msg: 'span',
56
+ You: 'span',
57
+ Person: 'button',
58
+ })
59
+ let {
60
+ Left,
61
+ Right,
62
+ Close,
63
+ Mode,
64
+ Colon,
65
+ Line,
66
+ Ghost,
67
+ Was,
68
+ Verb,
69
+ Msg,
70
+ You,
71
+ Person,
72
+ } = Frame
73
+
74
+ type Message = string | { task: string; session: string }
75
+
76
+ // The command line's field in the page's own graph, and its draft.
77
+ let CMD = 'cmd'
78
+
79
+ let msg = signal<Message>('')
80
+ let last = signal('') // what ↑ recalls
81
+
82
+ // A spawn answer stays attached to the graph instead of freezing the
83
+ // optimistic S-0. The session becomes linkable only once the server has
84
+ // minted its number; its lifecycle comes from that same authoritative row.
85
+ export let FixMessage = (
86
+ { task, session }: { task: string; session: string },
87
+ ) => {
88
+ let t = useDerived(() => ent(task), [task])
89
+ let s = useDerived(() => ent(session), [session])
90
+ return (
91
+ <>
92
+ <Id e={t} /> → {s.num
93
+ ? (
94
+ <>
95
+ <Entity eid={s.eid} view='Status.Session.Id' />
96
+ </>
97
+ )
98
+ : 'agent starting'}
99
+ </>
100
+ )
101
+ }
102
+
103
+ // A thumb has no `:` key, so the bar's left side is the door. The keyboard
104
+ // WAITS: under a coarse pointer the line opens unfocused, which leaves the
105
+ // hints standing as a palette of verbs to TAP; picking one writes it in and
106
+ // takes the keyboard then, for the arguments it actually needs.
107
+ let thumb = () => !!globalThis.matchMedia?.('(pointer: coarse)').matches
108
+
109
+ export let commandMode = () => {
110
+ msg.value = ''
111
+ mode.value = 'command'
112
+ }
113
+
114
+ // A command line can lose focus to the canvas without spending its draft.
115
+ // `:` takes it back, but remains ordinary text when the line already owns it.
116
+ export let commandFocus = (
117
+ e: Pick<KeyboardEvent, 'key' | 'target' | 'preventDefault'>,
118
+ input: HTMLTextAreaElement | null,
119
+ ) => {
120
+ if (e.key != ':' || !input || e.target == input) return false
121
+ e.preventDefault()
122
+ input.focus()
123
+ return true
124
+ }
125
+
126
+ // A final newline gives a textarea another caret row, but an inline mirror
127
+ // needs content after the break to retain that row. Zero width keeps wrapping
128
+ // identical while the mirror continues to size the textarea.
129
+ export let mirrorTail = (line: string, faded: string) =>
130
+ faded || (line.endsWith('\n') ? '\u200b' : '')
131
+
132
+ // The context a command runs in: what you're LOOKING at is what you're
133
+ // commanding — the root card (the URL) is the focused entity. A browser
134
+ // speaks for no session, so :claim must name one here.
135
+ let ctx = (): Ctx => ({ eid: screenTarget()?.eid, rows: rows() })
136
+
137
+ // The web's own verbs — the camera is the one thing a terminal has no
138
+ // answer for. Everything else is the shared list (commands.ts).
139
+ let local: Record<string, Command> = capable('canvas')
140
+ ? {
141
+ zoom: {
142
+ args: [{ name: 'factor', kind: num, eg: '0.25–4', need: false }],
143
+ about: 'zoom the canvas',
144
+ run: (rest) => {
145
+ let z = Math.min(4, Math.max(0.25, Number(rest.trim()) || 1))
146
+ camera.value = { ...camera.value, zoom: z }
147
+ return { msg: `zoom ${z}` }
148
+ },
149
+ },
150
+ }
151
+ : {}
152
+ let all = () => ({ ...hostCommands(vocab), ...local })
153
+
154
+ // What the typist was looking at when the words were typed, EMITTED as a
155
+ // `scene` notice on the task (D-13858): the url, the root entity, the camera,
156
+ // and the cards in view — enough for a fix agent to find the pixel the words
157
+ // point at. A capture is machinery, not conversation, so it is a notice, not
158
+ // a comment — it reaches the fix agent and the bus but stays out of the
159
+ // task's thread and off the mail relay. Each platform attaches its own
160
+ // scene (a TUI would say its screen); this is the web's. The instrument is
161
+ // this browser's client entity, whose row carries the full user agent.
162
+ let scene = (task: string): Change[] => {
163
+ let root = screenTarget()?.eid
164
+ let r = root ? ent(root) : null
165
+ let { x, y, zoom, w, h } = camera.value
166
+ let seen = r?.canvas
167
+ ? pinned(r.eid).filter((p) =>
168
+ p.x < x + w / zoom / 2 && p.x + (p.w || 480) > x - w / zoom / 2 &&
169
+ p.y < y + h / zoom / 2 && p.y + (p.h || 200) > y - h / zoom / 2
170
+ )
171
+ .toSorted((a, b) => b.z - a.z)
172
+ .slice(0, 12)
173
+ .map((p) => {
174
+ let t = ent(p.target)
175
+ return `${idOf(t)} "${t.doc?.title ?? t.kind}" (${p.view})`
176
+ })
177
+ : []
178
+ let body = [
179
+ `fix filed from ${location.pathname}`,
180
+ r && `- looking at: ${idOf(r)} "${r.doc?.title ?? r.kind}"`,
181
+ r?.canvas &&
182
+ `- camera: ${Math.round(x)},${Math.round(y)} @ ${zoom.toFixed(2)}×`,
183
+ seen.length && `- in view: ${seen.join(' · ')}`,
184
+ `- client: web-${ent(clientId()).num}`,
185
+ ].filter(Boolean).join('\n')
186
+ let c = uuid()
187
+ return [
188
+ { eid: c, name: 'doc', comp: { title: '', body } },
189
+ { eid: c, name: 'signal', comp: { target: task, event: 'scene' } },
190
+ ]
191
+ }
192
+
193
+ // The spawn intent (:fix, :chat): Run.tsx spawnOf, the one spawn every door
194
+ // writes, on the same socket the task just rode, so ordering is free. The bar
195
+ // narrates from the graph; anything the host can't honor shows on the session.
196
+ let launch = async (intent: string | SpawnIntent) => {
197
+ let { session, changes } = await spawnOf({
198
+ ...(typeof intent == 'string' ? {} : intent),
199
+ task: spawnTask(intent),
200
+ })
201
+ mutate(...changes)
202
+ return session
203
+ }
204
+
205
+ // Run a line and spend its intent: writes go out through mutate like every
206
+ // other view, `go` is a real navigation, spawn starts an agent, and a
207
+ // throw lands in the bar rather than a toast — the message is about the
208
+ // line you just typed, so it belongs where you typed it.
209
+ let exec = async (line: string) => {
210
+ let launching = ''
211
+ try {
212
+ let name = line.trim().match(/^\S+/)?.[0]
213
+ if (name && !all()[name]) throw new Error(`not a command here: ${name}`)
214
+ let r = run(line, ctx(), local)
215
+ let changes = (r.changes ?? []).filter((c) => vocab.comp(c.name))
216
+ let task = spawnTask(r.spawn)
217
+ if (task) changes = [...changes, ...scene(task)]
218
+ if (changes.length) mutate(...changes)
219
+ if (r.go) navigate(entityPath(idOf(ent(r.go))))
220
+ if (r.page) navigate(r.page)
221
+ if (r.card) {
222
+ let root = screenTarget()
223
+ if (capable('canvas') && root && ent(root.eid).canvas) {
224
+ spawnHit(root.eid, r.card)
225
+ } else navigate(entityPath(idOf(ent(r.card))))
226
+ }
227
+ if (r.spawn) {
228
+ launching = line.trim().split(/\s/)[0]
229
+ msg.value = r.msg ?? ''
230
+ let session = await launch(r.spawn)
231
+ if (task) msg.value = { task, session }
232
+ else navigate(entityPath(idOf(ent(session))))
233
+ } else {
234
+ msg.value = r.msg ?? ''
235
+ }
236
+ } catch (e) {
237
+ let why = e instanceof Error ? e.message : String(e)
238
+ msg.value = `${launching ? `${launching}: ` : ''}${why}`
239
+ }
240
+ }
241
+
242
+ // The identity chain's web end: who this browser acts for. A lone person
243
+ // binds on sight (Canvas); with CANDIDATES the bar asks — "you are …?" —
244
+ // and one click asserts it. An assertion, not a login: a wrong answer
245
+ // only garbles your own bylines, and the client card's actor row (any
246
+ // prop editor) can always change it. Bound or personless, the bar stays
247
+ // quiet.
248
+ let WhoAmI = () => {
249
+ let me = ent(clientId())
250
+ // Ask only when this browser is bound to no one yet — and ask the SERVER
251
+ // (@yaks/ux useHits) for the people, so a partial cache can't hide candidates or
252
+ // under-count them into a false "no choice". The empty line while already
253
+ // bound skips the round trip; the hook still runs (its rule) every render.
254
+ let asking = !!me.client && !me.client.actor
255
+ let people = useHits(asking ? pickLine('', 'person') : '', 50, hits)
256
+ if (!asking || people.length < 2) return null
257
+ return (
258
+ <You>
259
+ you are {people.map((p) => (
260
+ <Person
261
+ key={p.eid}
262
+ onClick={() =>
263
+ mutate({
264
+ eid: me.eid,
265
+ name: 'client',
266
+ comp: { actor: p.eid },
267
+ })}
268
+ >
269
+ <Entity eid={p.eid} view='Identity.Inline' />
270
+ </Person>
271
+ ))}
272
+ ?
273
+ </You>
274
+ )
275
+ }
276
+
277
+ // The vim statusbar: -- NORMAL -- / -- INSERT -- / a live : command line. It
278
+ // owns the mode transitions: : opens the command line, and Escape always
279
+ // returns to normal (and blurs whatever was being typed in). Insert mode is
280
+ // a field holding the focus, however it got it (i or Enter on the cursor's
281
+ // item, @yaks/ux/keys, or a press); the walk's cursor is normal mode's.
282
+ // On the document, so a terminal's keys and focus reach it as a browser's.
283
+ export let Status = () => {
284
+ let input = useRef<HTMLTextAreaElement>(null)
285
+ // The line is the `cmd` field (fields.tsx), whose text is the person's
286
+ // draft; the hints and the ghost read it there.
287
+ let line = useDraft(CMD, drafts)
288
+ let active = useObserved(mode)
289
+ let message = useObserved(msg)
290
+ let issue = useObserved(problem)
291
+ let [pick, setPick] = useState(0)
292
+
293
+ useEffect(() => {
294
+ let key = (e: KeyboardEvent) => {
295
+ if (e.defaultPrevented || e.key == 'Escape' && frontOverlay(document)) {
296
+ return
297
+ }
298
+ if (mode.value == 'command') {
299
+ commandFocus(e, input.current)
300
+ return // the command input owns every other key
301
+ }
302
+ let t = e.target as HTMLElement | null
303
+ if (mode.value == 'insert' || typing(t) || t?.matches?.('select')) {
304
+ if (e.key == 'Escape') {
305
+ mode.value = 'normal'
306
+ t?.blur?.()
307
+ }
308
+ return
309
+ }
310
+ if (e.key == ':') {
311
+ e.preventDefault()
312
+ msg.value = ''
313
+ mode.value = 'command'
314
+ } else if (e.key == 'Escape') {
315
+ msg.value = ''
316
+ document.getSelection?.()?.removeAllRanges() // drops visual too
317
+ }
318
+ }
319
+ document.addEventListener('keydown', key)
320
+
321
+ // VISUAL is derived, not entered: a live selection outside a text input.
322
+ let sel = () => {
323
+ if (mode.value == 'insert' || mode.value == 'command') return
324
+ let s = document.getSelection?.()
325
+ let n = s?.anchorNode
326
+ let host = n instanceof Element ? n : n?.parentElement
327
+ let has = !!s && !s.isCollapsed && !host?.closest('input, textarea')
328
+ mode.value = has ? 'visual' : 'normal'
329
+ }
330
+ document.addEventListener('selectionchange', sel)
331
+
332
+ // INSERT is derived too: the keyboard in a field it types into.
333
+ let focus = (e: FocusEvent) => {
334
+ if (typing(e.target) && mode.value != 'command') mode.value = 'insert'
335
+ }
336
+ let unfocus = (e: FocusEvent) => {
337
+ if (typing(e.target) && mode.value == 'insert') mode.value = 'normal'
338
+ }
339
+ document.addEventListener('focusin', focus)
340
+ document.addEventListener('focusout', unfocus)
341
+
342
+ // Pointer clicks don't keep keyboard focus: a clicked tab/×/link would
343
+ // otherwise swallow the next <space> (browsers activate the focused
344
+ // button on space). Keyboard activations (detail == 0) keep theirs.
345
+ let declick = (e: MouseEvent) => {
346
+ if (e.detail == 0) return
347
+ let a = document.activeElement
348
+ if (a instanceof HTMLElement && a.matches('button, a')) a.blur()
349
+ }
350
+ addEventListener('click', declick)
351
+ return () => {
352
+ document.removeEventListener('keydown', key)
353
+ document.removeEventListener('selectionchange', sel)
354
+ document.removeEventListener('focusin', focus)
355
+ document.removeEventListener('focusout', unfocus)
356
+ removeEventListener('click', declick)
357
+ }
358
+ }, [])
359
+
360
+ // Take the keyboard when the command input APPEARS (a thumb waits).
361
+ useEffect(() => {
362
+ if (active != 'command') return
363
+ if (!thumb()) input.current?.focus()
364
+ }, [active])
365
+
366
+ // A half-typed : line is a draft: wherever it was typed (before a reload,
367
+ // in another tab, in the terminal), the first time this page hears of it
368
+ // the command line reopens on it. Running or Escaping the line is what
369
+ // spends it.
370
+ let reopened = useRef(false)
371
+ useEffect(() => {
372
+ if (!line || reopened.current) return
373
+ reopened.current = true
374
+ if (mode.value == 'normal') mode.value = 'command'
375
+ }, [line])
376
+
377
+ // The hints the line offers, and which is picked (0 = the best match).
378
+ let hints = active == 'command' ? suggest(line, all()).slice(0, 8) : []
379
+ let [, pre, verb, rest] = line.match(/^(\s*)(\S+)(.*)$/s) ?? []
380
+ let faded = ghost(line, all())
381
+ let put = (v: string) => {
382
+ if (input.current) input.current.value = v
383
+ fields.set(CMD, v)
384
+ setPick(0)
385
+ if (v) input.current?.focus() // a picked verb takes the keyboard
386
+ }
387
+ let close = () => {
388
+ put('')
389
+ mode.value = 'normal'
390
+ }
391
+
392
+ // Enter runs the line, Escape drops it; either closes it.
393
+ let runs = editKeys({
394
+ keep: () => {
395
+ let v = input.current?.value ?? ''
396
+ last.value = v.trim() || last.value
397
+ exec(v)
398
+ close()
399
+ },
400
+ cancel: close,
401
+ })
402
+ // The field's list took its keys before these.
403
+ let cmdKey = (e: KeyboardEvent) => {
404
+ let v = input.current?.value ?? ''
405
+ // shift+Enter is the textarea's own newline — spec() reads line 2 on
406
+ // as the body, so a : line can file a task with prose attached.
407
+ let multi = v.includes('\n')
408
+ if (runs(e)) return
409
+ if (e.key == 'Tab') {
410
+ // completion is the PICKED hint's name (0 = best match)
411
+ e.preventDefault()
412
+ let name = hints[pick]?.[0]
413
+ if (name) put(`${name} `)
414
+ } else if (e.key == 'ArrowUp' && !multi) {
415
+ e.preventDefault() // the caret would jump home instead
416
+ // empty line: recall. Otherwise walk the list, away from the bar.
417
+ if (!v) put(last.value)
418
+ else if (hints.length) setPick((p) => Math.min(p + 1, hints.length - 1))
419
+ } else if (e.key == 'ArrowDown' && !multi) {
420
+ e.preventDefault()
421
+ setPick((p) => Math.max(p - 1, 0))
422
+ }
423
+ }
424
+
425
+ return (
426
+ <Frame>
427
+ <Left onClick={active == 'command' ? undefined : commandMode}>
428
+ {active == 'command'
429
+ ? (
430
+ <>
431
+ <Colon>:</Colon>
432
+ <Line>
433
+ {
434
+ /* the ghost sits UNDER the input and paints the line: the
435
+ copy shows the typed text (the verb greens once it names a
436
+ command), the faded completion follows — the input above
437
+ keeps only the caret and the selection */
438
+ }
439
+ <Ghost aria-hidden>
440
+ <Was>
441
+ {verb
442
+ ? (
443
+ <>
444
+ {pre}
445
+ <Verb mod={all()[verb] && 'known'}>{verb}</Verb>
446
+ {rest}
447
+ </>
448
+ )
449
+ : line}
450
+ </Was>
451
+ {mirrorTail(line, faded)}
452
+ </Ghost>
453
+ <fields.Filter
454
+ id={CMD}
455
+ lines
456
+ mod='bare'
457
+ className='Status_Cmd'
458
+ initial={line}
459
+ elRef={input}
460
+ onKey={cmdKey}
461
+ onInput={() => setPick(0)}
462
+ />
463
+ </Line>
464
+ <Combobox.List
465
+ id={CMD + '-verbs'}
466
+ anchor={input}
467
+ side='above'
468
+ options={hints.map(([name, c]) => ({
469
+ value: name,
470
+ label: [name, slotsOf(c.args)].filter(Boolean).join(' '),
471
+ note: c.about,
472
+ }))}
473
+ active={pick}
474
+ onHover={setPick}
475
+ onChoose={(name) => put(`${name} `)}
476
+ onDismiss={close}
477
+ />
478
+ </>
479
+ )
480
+ : (
481
+ <>
482
+ <Mode mod={active}>
483
+ {active == 'insert'
484
+ ? '-- INSERT --'
485
+ : active == 'visual'
486
+ ? '-- VISUAL --'
487
+ : '-- NORMAL --'}
488
+ </Mode>
489
+ {(message || issue) && (
490
+ <Msg
491
+ title={typeof message == 'string'
492
+ ? message || issue
493
+ : undefined}
494
+ >
495
+ {typeof message == 'string'
496
+ ? message || issue
497
+ : <FixMessage {...message} />}
498
+ </Msg>
499
+ )}
500
+ </>
501
+ )}
502
+ </Left>
503
+ <Right>
504
+ <Sync />
505
+ {capable('canvas') && <WhoAmI />}
506
+ {active == 'command' && (
507
+ <Close type='button' aria-label='exit command mode' onClick={close}>
508
+ ×
509
+ </Close>
510
+ )}
511
+ </Right>
512
+ </Frame>
513
+ )
514
+ }
@@ -0,0 +1,173 @@
1
+ /** @jsxRuntime automatic *//** @jsxImportSourceTypes npm:react@19.3.0 *//** @jsxImportSource npm:react@19.3.0 */
2
+ import { useObserved } from 'jsr:@yaks/ux@0.2.36'
3
+ // The standing sync indicator (T-21441). An optimistic cache shows every local
4
+ // edit as landed the instant it is typed; when the server is unreachable the
5
+ // write sits undelivered in the outbox (live.ts) with nothing on screen to say
6
+ // so — the exact silence that let the T-21413 data-loss incident run unnoticed
7
+ // for 25 minutes. This lives in the App bar and BREAKS that silence: while any
8
+ // write is unsent it shows how many wait and how long the oldest has waited, and
9
+ // a write the server REFUSED persists as a returnable error the user can read and
10
+ // dismiss (M-16612) — durable under its delivery id, surfaced again after a
11
+ // reload, never ephemeral or opaque. A write is mentioned only once it has
12
+ // waited LATE, so a save that settles in a few frames never flashes the badge.
13
+ // While the server is away it says, calmly, that the page is reconnecting,
14
+ // until the socket opens again.
15
+ /* @ts-types="npm:@types/react@19.3.0" */ import { useEffect, useState } from 'npm:react@19.3.0'
16
+ import { away, clearRefusal, outboxWrites, refused } from '../live.ts'
17
+ import { block } from 'jsr:@yaks/ui@0.2.36'
18
+
19
+ // A span, so a terminal runs the badges into the bar's line rather than
20
+ // giving them a row of their own.
21
+ let Frame = block('span', 'Sync', {
22
+ Away: 'span',
23
+ Pending: 'button',
24
+ Refused: 'button',
25
+ Dot: 'span',
26
+ Count: 'span',
27
+ Age: 'span',
28
+ Panel: 'div',
29
+ Item: 'div',
30
+ Head: 'div',
31
+ Reason: 'span',
32
+ When: 'span',
33
+ Summary: 'span',
34
+ Dismiss: 'button',
35
+ })
36
+ let {
37
+ Away,
38
+ Pending,
39
+ Refused,
40
+ Dot,
41
+ Count,
42
+ Age,
43
+ Panel,
44
+ Item,
45
+ Head,
46
+ Reason,
47
+ When,
48
+ Summary,
49
+ Dismiss,
50
+ } = Frame
51
+
52
+ // Compact elapsed time — seconds until a minute, then minutes/hours/days. The
53
+ // indicator wants sub-minute resolution during an incident, where the shared
54
+ // minute tick (Stamp.tsx) is too coarse to show a write is stuck.
55
+ let brief = (since: number, now: number) => {
56
+ let s = Math.max(0, Math.round((now - since) / 1000))
57
+ if (s < 60) return `${s}s`
58
+ if (s < 3600) return `${Math.floor(s / 60)}m`
59
+ if (s < 86400) return `${Math.floor(s / 3600)}h`
60
+ return `${Math.floor(s / 86400)}d`
61
+ }
62
+
63
+ // How long a write may wait before the bar mentions it. A save normally
64
+ // settles in a few frames, and a badge that flashes for those frames is noise
65
+ // that catches the eye on every keystroke; past two seconds the wait is one a
66
+ // person would want to know about.
67
+ export let LATE = 2000
68
+
69
+ // The oldest write still unsent, or Infinity when none is.
70
+ let oldestOf = (writes: { since: number }[]) =>
71
+ writes.reduce((m, w) => Math.min(m, w.since), Infinity)
72
+
73
+ // Whether any write has waited long enough to show.
74
+ /// late([{since: 0}], 1999) -> false
75
+ /// late([{since: 5000}, {since: 0}], 2000) -> true
76
+ /// late([], 9e9) -> false
77
+ export let late = (writes: { since: number }[], now: number) =>
78
+ now - oldestOf(writes) >= LATE
79
+
80
+ export let Sync = () => {
81
+ let writes = useObserved(outboxWrites)
82
+ let errs = useObserved(refused)
83
+ let gone = useObserved(away)
84
+ let [open, setOpen] = useState(false)
85
+ let [now, setNow] = useState(Date.now())
86
+
87
+ // A one-second tick, armed ONLY while a write is pending (or the refusal panel
88
+ // is open, whose ages also want to move) — self-limiting exactly as the outbox
89
+ // redelivery timer is: nothing waiting, no idle tick.
90
+ useEffect(() => {
91
+ if (!writes.length && !open) return
92
+ let t = setInterval(() => setNow(Date.now()), 1000)
93
+ return () => clearInterval(t)
94
+ }, [writes.length > 0, open])
95
+
96
+ // Wake exactly when the oldest write turns late, so it shows on time
97
+ // rather than on the next tick.
98
+ let oldest = oldestOf(writes)
99
+ useEffect(() => {
100
+ if (oldest == Infinity) return
101
+ let wait = oldest + LATE - Date.now()
102
+ if (wait <= 0) return // late already, and the tick keeps its age moving
103
+ let t = setTimeout(() => setNow(Date.now()), wait)
104
+ return () => clearTimeout(t)
105
+ }, [oldest])
106
+
107
+ let slow = late(writes, now)
108
+ if (!slow && !errs.length && !gone) return null
109
+
110
+ let sends = writes.length
111
+ let edits = writes.reduce((n, w) => n + w.count, 0)
112
+
113
+ return (
114
+ <Frame>
115
+ {gone && (
116
+ <Away
117
+ role='status'
118
+ title='the server is away; this page reconnects on its own'
119
+ >
120
+ <Dot mod='pending' />
121
+ reconnecting
122
+ </Away>
123
+ )}
124
+ {slow && (
125
+ <Pending
126
+ title={`${edits} edit${edits > 1 ? 's' : ''} in ${sends} write${
127
+ sends > 1 ? 's' : ''
128
+ } not yet saved to the server — retrying`}
129
+ >
130
+ <Dot mod='pending' />
131
+ <Count>{sends} unsynced</Count>
132
+ <Age>{brief(oldest, now)}</Age>
133
+ </Pending>
134
+ )}
135
+ {errs.length > 0 && (
136
+ <Refused
137
+ mod={open && 'open'}
138
+ onClick={() => setOpen((v) => !v)}
139
+ title='writes the server refused — click to review'
140
+ >
141
+ <Dot mod='refused' />
142
+ <Count>
143
+ {errs.length} refused
144
+ </Count>
145
+ </Refused>
146
+ )}
147
+ {open && errs.length > 0 && (
148
+ <Panel>
149
+ {errs.map((e) => (
150
+ <Item key={e.id}>
151
+ <Head>
152
+ <Reason>{e.reason}</Reason>
153
+ <When>{brief(e.at, now)} ago</When>
154
+ <Dismiss
155
+ type='button'
156
+ aria-label='dismiss'
157
+ onClick={() => clearRefusal(e.id)}
158
+ >
159
+ ×
160
+ </Dismiss>
161
+ </Head>
162
+ {e.summary && (
163
+ <Summary>
164
+ {e.summary} was refused by the server.
165
+ </Summary>
166
+ )}
167
+ </Item>
168
+ ))}
169
+ </Panel>
170
+ )}
171
+ </Frame>
172
+ )
173
+ }