@tai42/studio-sdk 15.0.0 → 16.0.1

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 (322) hide show
  1. package/dist/components/explorer-body.d.ts +49 -0
  2. package/dist/components/explorer-body.d.ts.map +1 -0
  3. package/dist/components/explorer-body.js +67 -0
  4. package/dist/components/explorer-body.js.map +1 -0
  5. package/dist/components/explorer-page-size.d.ts +4 -0
  6. package/dist/components/explorer-page-size.d.ts.map +1 -0
  7. package/dist/components/explorer-page-size.js +36 -0
  8. package/dist/components/explorer-page-size.js.map +1 -0
  9. package/dist/components/explorer-tag-filter.d.ts +17 -0
  10. package/dist/components/explorer-tag-filter.d.ts.map +1 -0
  11. package/dist/components/explorer-tag-filter.js +29 -0
  12. package/dist/components/explorer-tag-filter.js.map +1 -0
  13. package/dist/components/explorer-tags.d.ts +22 -0
  14. package/dist/components/explorer-tags.d.ts.map +1 -0
  15. package/dist/components/explorer-tags.js +51 -0
  16. package/dist/components/explorer-tags.js.map +1 -0
  17. package/dist/components/explorer-view.d.ts +3 -17
  18. package/dist/components/explorer-view.d.ts.map +1 -1
  19. package/dist/components/explorer-view.js +53 -170
  20. package/dist/components/explorer-view.js.map +1 -1
  21. package/dist/components/extension-combo-builder.d.ts +0 -5
  22. package/dist/components/extension-combo-builder.d.ts.map +1 -1
  23. package/dist/components/extension-combo-builder.js +4 -42
  24. package/dist/components/extension-combo-builder.js.map +1 -1
  25. package/dist/components/extension-combo-parts.d.ts +44 -0
  26. package/dist/components/extension-combo-parts.d.ts.map +1 -0
  27. package/dist/components/extension-combo-parts.js +57 -0
  28. package/dist/components/extension-combo-parts.js.map +1 -0
  29. package/dist/components/fleet-report.d.ts.map +1 -1
  30. package/dist/components/fleet-report.js +51 -37
  31. package/dist/components/fleet-report.js.map +1 -1
  32. package/dist/components/icons/chrome-marks.d.ts +50 -0
  33. package/dist/components/icons/chrome-marks.d.ts.map +1 -0
  34. package/dist/components/icons/chrome-marks.js +51 -0
  35. package/dist/components/icons/chrome-marks.js.map +1 -0
  36. package/dist/components/icons/icon-frame.d.ts +29 -0
  37. package/dist/components/icons/icon-frame.d.ts.map +1 -0
  38. package/dist/components/icons/icon-frame.js +19 -0
  39. package/dist/components/icons/icon-frame.js.map +1 -0
  40. package/dist/components/icons/index.d.ts +40 -0
  41. package/dist/components/icons/index.d.ts.map +1 -0
  42. package/dist/components/icons/index.js +6 -0
  43. package/dist/components/icons/index.js.map +1 -0
  44. package/dist/components/icons/nav-map.d.ts +14 -0
  45. package/dist/components/icons/nav-map.d.ts.map +1 -0
  46. package/dist/components/icons/nav-map.js +28 -0
  47. package/dist/components/icons/nav-map.js.map +1 -0
  48. package/dist/components/icons/nav-marks.d.ts +44 -0
  49. package/dist/components/icons/nav-marks.d.ts.map +1 -0
  50. package/dist/components/icons/nav-marks.js +45 -0
  51. package/dist/components/icons/nav-marks.js.map +1 -0
  52. package/dist/components/icons/status-marks.d.ts +21 -0
  53. package/dist/components/icons/status-marks.d.ts.map +1 -0
  54. package/dist/components/icons/status-marks.js +22 -0
  55. package/dist/components/icons/status-marks.js.map +1 -0
  56. package/dist/components/icons/theme-marks.d.ts +9 -0
  57. package/dist/components/icons/theme-marks.d.ts.map +1 -0
  58. package/dist/components/icons/theme-marks.js +10 -0
  59. package/dist/components/icons/theme-marks.js.map +1 -0
  60. package/dist/components/json-diff.d.ts.map +1 -1
  61. package/dist/components/json-diff.js +1 -3
  62. package/dist/components/json-diff.js.map +1 -1
  63. package/dist/components/json-tree-context.d.ts +13 -0
  64. package/dist/components/json-tree-context.d.ts.map +1 -0
  65. package/dist/components/json-tree-context.js +15 -0
  66. package/dist/components/json-tree-context.js.map +1 -0
  67. package/dist/components/json-tree-model.d.ts +56 -0
  68. package/dist/components/json-tree-model.d.ts.map +1 -0
  69. package/dist/components/json-tree-model.js +125 -0
  70. package/dist/components/json-tree-model.js.map +1 -0
  71. package/dist/components/json-tree-nodes.d.ts +30 -0
  72. package/dist/components/json-tree-nodes.d.ts.map +1 -0
  73. package/dist/components/json-tree-nodes.js +118 -0
  74. package/dist/components/json-tree-nodes.js.map +1 -0
  75. package/dist/components/json-tree.d.ts.map +1 -1
  76. package/dist/components/json-tree.js +7 -295
  77. package/dist/components/json-tree.js.map +1 -1
  78. package/dist/components/json-value-format.d.ts +20 -0
  79. package/dist/components/json-value-format.d.ts.map +1 -0
  80. package/dist/components/json-value-format.js +63 -0
  81. package/dist/components/json-value-format.js.map +1 -0
  82. package/dist/components/markdown-blocks.d.ts +38 -0
  83. package/dist/components/markdown-blocks.d.ts.map +1 -0
  84. package/dist/components/markdown-blocks.js +168 -0
  85. package/dist/components/markdown-blocks.js.map +1 -0
  86. package/dist/components/markdown-inline.d.ts +24 -0
  87. package/dist/components/markdown-inline.d.ts.map +1 -0
  88. package/dist/components/markdown-inline.js +257 -0
  89. package/dist/components/markdown-inline.js.map +1 -0
  90. package/dist/components/markdown.d.ts +3 -52
  91. package/dist/components/markdown.d.ts.map +1 -1
  92. package/dist/components/markdown.js +4 -376
  93. package/dist/components/markdown.js.map +1 -1
  94. package/dist/components/observer-registry.d.ts +38 -0
  95. package/dist/components/observer-registry.d.ts.map +1 -0
  96. package/dist/components/observer-registry.js +131 -0
  97. package/dist/components/observer-registry.js.map +1 -0
  98. package/dist/components/overflow-measure.d.ts +38 -0
  99. package/dist/components/overflow-measure.d.ts.map +1 -0
  100. package/dist/components/overflow-measure.js +54 -0
  101. package/dist/components/overflow-measure.js.map +1 -0
  102. package/dist/components/prose-regions.d.ts +81 -0
  103. package/dist/components/prose-regions.d.ts.map +1 -0
  104. package/dist/components/prose-regions.js +128 -0
  105. package/dist/components/prose-regions.js.map +1 -0
  106. package/dist/components/scroll-region.d.ts.map +1 -1
  107. package/dist/components/scroll-region.js +3 -298
  108. package/dist/components/scroll-region.js.map +1 -1
  109. package/dist/components/tab-stop-parse.d.ts +53 -0
  110. package/dist/components/tab-stop-parse.d.ts.map +1 -0
  111. package/dist/components/tab-stop-parse.js +202 -0
  112. package/dist/components/tab-stop-parse.js.map +1 -0
  113. package/dist/components/templated-text-field.d.ts.map +1 -1
  114. package/dist/components/templated-text-field.js +10 -148
  115. package/dist/components/templated-text-field.js.map +1 -1
  116. package/dist/components/templated-text-kwargs-editor.d.ts +24 -0
  117. package/dist/components/templated-text-kwargs-editor.d.ts.map +1 -0
  118. package/dist/components/templated-text-kwargs-editor.js +48 -0
  119. package/dist/components/templated-text-kwargs-editor.js.map +1 -0
  120. package/dist/components/templated-text-kwargs.d.ts +37 -0
  121. package/dist/components/templated-text-kwargs.d.ts.map +1 -0
  122. package/dist/components/templated-text-kwargs.js +66 -0
  123. package/dist/components/templated-text-kwargs.js.map +1 -0
  124. package/dist/components/templated-text-source.d.ts +33 -0
  125. package/dist/components/templated-text-source.d.ts.map +1 -0
  126. package/dist/components/templated-text-source.js +53 -0
  127. package/dist/components/templated-text-source.js.map +1 -0
  128. package/dist/components/tool-picker-parts.d.ts +39 -0
  129. package/dist/components/tool-picker-parts.d.ts.map +1 -0
  130. package/dist/components/tool-picker-parts.js +87 -0
  131. package/dist/components/tool-picker-parts.js.map +1 -0
  132. package/dist/components/tool-picker.d.ts +1 -2
  133. package/dist/components/tool-picker.d.ts.map +1 -1
  134. package/dist/components/tool-picker.js +4 -66
  135. package/dist/components/tool-picker.js.map +1 -1
  136. package/dist/components/version-history-panel.d.ts.map +1 -1
  137. package/dist/components/version-history-panel.js +7 -34
  138. package/dist/components/version-history-panel.js.map +1 -1
  139. package/dist/components/version-history-rows.d.ts +48 -0
  140. package/dist/components/version-history-rows.d.ts.map +1 -0
  141. package/dist/components/version-history-rows.js +51 -0
  142. package/dist/components/version-history-rows.js.map +1 -0
  143. package/dist/guards.d.ts +6 -0
  144. package/dist/guards.d.ts.map +1 -0
  145. package/dist/guards.js +8 -0
  146. package/dist/guards.js.map +1 -0
  147. package/dist/hooks/interactions-connection.d.ts +43 -0
  148. package/dist/hooks/interactions-connection.d.ts.map +1 -0
  149. package/dist/hooks/interactions-connection.js +171 -0
  150. package/dist/hooks/interactions-connection.js.map +1 -0
  151. package/dist/hooks/interactions-stream.d.ts +80 -0
  152. package/dist/hooks/interactions-stream.d.ts.map +1 -0
  153. package/dist/hooks/interactions-stream.js +233 -0
  154. package/dist/hooks/interactions-stream.js.map +1 -0
  155. package/dist/hooks/useApi.d.ts +2409 -2293
  156. package/dist/hooks/useApi.d.ts.map +1 -1
  157. package/dist/hooks/useSse.d.ts +3 -6
  158. package/dist/hooks/useSse.d.ts.map +1 -1
  159. package/dist/hooks/useSse.js +38 -451
  160. package/dist/hooks/useSse.js.map +1 -1
  161. package/dist/navigation/app-link.d.ts +25 -0
  162. package/dist/navigation/app-link.d.ts.map +1 -0
  163. package/dist/navigation/app-link.js +33 -0
  164. package/dist/navigation/app-link.js.map +1 -0
  165. package/dist/navigation/context.d.ts +3 -20
  166. package/dist/navigation/context.d.ts.map +1 -1
  167. package/dist/navigation/context.js +7 -267
  168. package/dist/navigation/context.js.map +1 -1
  169. package/dist/navigation/entry-state.d.ts +23 -0
  170. package/dist/navigation/entry-state.d.ts.map +1 -0
  171. package/dist/navigation/entry-state.js +39 -0
  172. package/dist/navigation/entry-state.js.map +1 -0
  173. package/dist/navigation/guard-registry.d.ts +30 -0
  174. package/dist/navigation/guard-registry.d.ts.map +1 -0
  175. package/dist/navigation/guard-registry.js +46 -0
  176. package/dist/navigation/guard-registry.js.map +1 -0
  177. package/dist/navigation/guarded-navigation.d.ts +24 -0
  178. package/dist/navigation/guarded-navigation.d.ts.map +1 -0
  179. package/dist/navigation/guarded-navigation.js +102 -0
  180. package/dist/navigation/guarded-navigation.js.map +1 -0
  181. package/dist/navigation/index.d.ts +3 -2
  182. package/dist/navigation/index.d.ts.map +1 -1
  183. package/dist/navigation/index.js +2 -1
  184. package/dist/navigation/index.js.map +1 -1
  185. package/dist/navigation/types.d.ts +8 -11
  186. package/dist/navigation/types.d.ts.map +1 -1
  187. package/dist/navigation/use-back-forward-guard.d.ts +5 -0
  188. package/dist/navigation/use-back-forward-guard.d.ts.map +1 -0
  189. package/dist/navigation/use-back-forward-guard.js +82 -0
  190. package/dist/navigation/use-back-forward-guard.js.map +1 -0
  191. package/dist/plugin/registry.d.ts +6 -15
  192. package/dist/plugin/registry.d.ts.map +1 -1
  193. package/dist/plugin/registry.js +83 -75
  194. package/dist/plugin/registry.js.map +1 -1
  195. package/dist/schema-editor/SchemaEditor.d.ts +8 -1
  196. package/dist/schema-editor/SchemaEditor.d.ts.map +1 -1
  197. package/dist/schema-editor/SchemaEditor.js +2 -2
  198. package/dist/schema-editor/SchemaEditor.js.map +1 -1
  199. package/dist/schema-form/SecretRefField.d.ts +2 -3
  200. package/dist/schema-form/SecretRefField.d.ts.map +1 -1
  201. package/dist/schema-form/SecretRefField.js +23 -12
  202. package/dist/schema-form/SecretRefField.js.map +1 -1
  203. package/dist/schema-form/classify.d.ts +1 -122
  204. package/dist/schema-form/classify.d.ts.map +1 -1
  205. package/dist/schema-form/classify.js +174 -237
  206. package/dist/schema-form/classify.js.map +1 -1
  207. package/dist/schema-form/default-value.d.ts.map +1 -1
  208. package/dist/schema-form/default-value.js +42 -19
  209. package/dist/schema-form/default-value.js.map +1 -1
  210. package/dist/schema-form/enum-field.d.ts +1 -1
  211. package/dist/schema-form/enum-field.d.ts.map +1 -1
  212. package/dist/schema-form/field-model.d.ts +121 -0
  213. package/dist/schema-form/field-model.d.ts.map +1 -0
  214. package/dist/schema-form/field-model.js +2 -0
  215. package/dist/schema-form/field-model.js.map +1 -0
  216. package/dist/schema-form/field-node.d.ts.map +1 -1
  217. package/dist/schema-form/field-node.js +25 -14
  218. package/dist/schema-form/field-node.js.map +1 -1
  219. package/dist/schema-form/media-field.d.ts +1 -1
  220. package/dist/schema-form/media-field.d.ts.map +1 -1
  221. package/dist/schema-form/media-field.js +47 -28
  222. package/dist/schema-form/media-field.js.map +1 -1
  223. package/dist/schema-form/string-annotations.d.ts +20 -0
  224. package/dist/schema-form/string-annotations.d.ts.map +1 -0
  225. package/dist/schema-form/string-annotations.js +105 -0
  226. package/dist/schema-form/string-annotations.js.map +1 -0
  227. package/dist/schema-form/string-field.d.ts +1 -1
  228. package/dist/schema-form/string-field.d.ts.map +1 -1
  229. package/dist/schema-form/union-field.d.ts +1 -1
  230. package/dist/schema-form/union-field.d.ts.map +1 -1
  231. package/dist/schema-form/union-field.js.map +1 -1
  232. package/dist/schema-form/union.d.ts +1 -1
  233. package/dist/schema-form/union.d.ts.map +1 -1
  234. package/dist/schema-form/validate.d.ts.map +1 -1
  235. package/dist/schema-form/validate.js +126 -105
  236. package/dist/schema-form/validate.js.map +1 -1
  237. package/dist/state-binding/AdapterMapping.d.ts.map +1 -1
  238. package/dist/state-binding/AdapterMapping.js +0 -0
  239. package/dist/state-binding/AdapterMapping.js.map +1 -1
  240. package/dist/state-binding/StateAttachRow.d.ts.map +1 -1
  241. package/dist/state-binding/StateAttachRow.js +52 -36
  242. package/dist/state-binding/StateAttachRow.js.map +1 -1
  243. package/dist/state-binding/adapter-mapping-row.d.ts +17 -0
  244. package/dist/state-binding/adapter-mapping-row.d.ts.map +1 -0
  245. package/dist/state-binding/adapter-mapping-row.js +87 -0
  246. package/dist/state-binding/adapter-mapping-row.js.map +1 -0
  247. package/dist/structured-output/StructuredOutput.d.ts.map +1 -1
  248. package/dist/structured-output/StructuredOutput.js +1 -3
  249. package/dist/structured-output/StructuredOutput.js.map +1 -1
  250. package/package.json +2 -2
  251. package/src/components/explorer-body.tsx +239 -0
  252. package/src/components/explorer-page-size.ts +39 -0
  253. package/src/components/explorer-tag-filter.tsx +80 -0
  254. package/src/components/explorer-tags.ts +64 -0
  255. package/src/components/explorer-view.tsx +114 -366
  256. package/src/components/extension-combo-builder.tsx +35 -152
  257. package/src/components/extension-combo-parts.tsx +237 -0
  258. package/src/components/fleet-report.tsx +97 -54
  259. package/src/components/icons/chrome-marks.tsx +195 -0
  260. package/src/components/icons/icon-frame.tsx +50 -0
  261. package/src/components/icons/index.ts +84 -0
  262. package/src/components/icons/nav-map.ts +55 -0
  263. package/src/components/icons/nav-marks.tsx +205 -0
  264. package/src/components/icons/status-marks.tsx +46 -0
  265. package/src/components/icons/theme-marks.tsx +33 -0
  266. package/src/components/json-diff.tsx +1 -4
  267. package/src/components/json-tree-context.ts +26 -0
  268. package/src/components/json-tree-model.ts +139 -0
  269. package/src/components/json-tree-nodes.tsx +224 -0
  270. package/src/components/json-tree.tsx +15 -434
  271. package/src/components/json-value-format.ts +64 -0
  272. package/src/components/markdown-blocks.ts +198 -0
  273. package/src/components/markdown-inline.tsx +365 -0
  274. package/src/components/markdown.tsx +5 -471
  275. package/src/components/observer-registry.ts +139 -0
  276. package/src/components/overflow-measure.ts +69 -0
  277. package/src/components/prose-regions.ts +153 -0
  278. package/src/components/scroll-region.tsx +20 -360
  279. package/src/components/tab-stop-parse.ts +217 -0
  280. package/src/components/templated-text-field.tsx +38 -330
  281. package/src/components/templated-text-kwargs-editor.tsx +147 -0
  282. package/src/components/templated-text-kwargs.ts +92 -0
  283. package/src/components/templated-text-source.tsx +226 -0
  284. package/src/components/tool-picker-parts.tsx +184 -0
  285. package/src/components/tool-picker.tsx +22 -114
  286. package/src/components/version-history-panel.tsx +38 -142
  287. package/src/components/version-history-rows.tsx +220 -0
  288. package/src/guards.ts +7 -0
  289. package/src/hooks/interactions-connection.ts +227 -0
  290. package/src/hooks/interactions-stream.ts +274 -0
  291. package/src/hooks/useSse.ts +50 -477
  292. package/src/navigation/app-link.tsx +70 -0
  293. package/src/navigation/context.tsx +11 -330
  294. package/src/navigation/entry-state.ts +42 -0
  295. package/src/navigation/guard-registry.ts +65 -0
  296. package/src/navigation/guarded-navigation.ts +147 -0
  297. package/src/navigation/index.ts +2 -2
  298. package/src/navigation/types.ts +8 -11
  299. package/src/navigation/use-back-forward-guard.ts +90 -0
  300. package/src/plugin/registry.ts +111 -90
  301. package/src/schema-editor/SchemaEditor.tsx +9 -1
  302. package/src/schema-form/SecretRefField.tsx +121 -71
  303. package/src/schema-form/classify.ts +244 -395
  304. package/src/schema-form/default-value.ts +48 -17
  305. package/src/schema-form/enum-field.tsx +1 -1
  306. package/src/schema-form/field-model.ts +133 -0
  307. package/src/schema-form/field-node.tsx +94 -38
  308. package/src/schema-form/media-field.tsx +134 -65
  309. package/src/schema-form/string-annotations.ts +111 -0
  310. package/src/schema-form/string-field.tsx +1 -1
  311. package/src/schema-form/union-field.tsx +2 -1
  312. package/src/schema-form/union.ts +1 -1
  313. package/src/schema-form/validate.ts +184 -103
  314. package/src/state-binding/AdapterMapping.tsx +0 -0
  315. package/src/state-binding/StateAttachRow.tsx +222 -147
  316. package/src/state-binding/adapter-mapping-row.tsx +232 -0
  317. package/src/structured-output/StructuredOutput.tsx +1 -4
  318. package/dist/components/icons.d.ts +0 -160
  319. package/dist/components/icons.d.ts.map +0 -1
  320. package/dist/components/icons.js +0 -160
  321. package/dist/components/icons.js.map +0 -1
  322. package/src/components/icons.tsx +0 -586
@@ -14,52 +14,36 @@
14
14
  *
15
15
  * The published list is `merge(seed, live)`: every seeded pending item, plus any
16
16
  * item added on the tail since connect, minus any removed on the tail, with the
17
- * client `answered` flag set on an item the tail reported answered.
17
+ * client `answered` flag set on an item the tail reported answered. The published
18
+ * `count` is the live PENDING tally, derived from the same overlay without a
19
+ * refetch, reconciled by EPOCH so a resync never double-counts.
18
20
  *
19
- * The published `count` is the live PENDING tally, derived from the same overlay
20
- * without a refetch, reconciled by EPOCH so a resync never double-counts. Each
21
- * (re)connect refetches the base under a fresh epoch and, once that refetch LANDS
22
- * fresh, records it as `countEpoch` (a failed refetch does not advance it, so
23
- * prior-epoch deltas keep applying against the consistent prior total rather than
24
- * being discarded against a stale one); every overlay add and every terminal
25
- * (answered or removed) entry is stamped with the epoch current when its frame
26
- * arrived. The count
27
- * is the seed `total` plus live adds not in the seed AND stamped at or after
28
- * `countEpoch`, minus one for every terminal id stamped at or after `countEpoch`
29
- * (deduped per id) — a terminal or an add already folded into the refetched `total`
30
- * carries an older stamp and is not applied again. The single residual: a frame
31
- * arriving while a refetch is in flight may or may not be reflected in that refetch's
32
- * `total`, and is reconciled on the next resync.
33
- *
34
- * ANSWERED-CARD LIFECYCLE: the server emits no removal for an answered card —
35
- * only `interaction.answered`, never `interaction.removed` for it — and the paged
36
- * base never carries answered items. On its answered frame a card is promoted into
37
- * the overlay's adds (a seed-origin card too), so it lingers uniformly whether it
38
- * began as a live add or a seed item, even after a refetch drops it from the base.
39
- * This hook AGES it out itself: an answered card is dropped `ANSWERED_RETENTION_MS`
40
- * after its answered frame (tombstoned so a still-stale seed cannot resurrect it),
41
- * with `ANSWERED_CAP` as a hard newest-N flood backstop. Both are swept lazily on
42
- * frame/reconnect ticks (the stream carries no idle heartbeat frame), never a
43
- * per-item timer.
44
- *
45
- * `add` / `answered` frames are at-least-once; entries are keyed by
46
- * `interaction_id`, a redelivered `add` overwrites in place, and a redelivered
47
- * `answered` keeps the original stamp so the retention window is not extended.
21
+ * The pure frame parsing, overlay reducer and derived projections live in
22
+ * `interactions-stream`; the fetch/reconnect/resync loop lives in
23
+ * `interactions-connection`. This hook holds the React state those two operate on
24
+ * and wires them together: the overlay is a ref (mutated in place by the driver),
25
+ * the published list is `merge(seed, overlay)` held in state and recomputed on
26
+ * every overlay change and every seed change, and the count is derived on render.
48
27
  *
49
28
  * The `Interaction` type is imported type-only (no runtime dep on api-client);
50
- * frames are validated defensively and a malformed frame is surfaced as an error,
51
- * never rendered as a silent blank. `answered` is not a wire field — it is a
52
- * client flag this hook flips on an `interaction.answered` event.
29
+ * `answered` is not a wire field — it is a client flag flipped on an
30
+ * `interaction.answered` event.
53
31
  */
54
32
  import { useEffect, useRef, useState } from 'react';
55
33
  import type { Interaction } from '@tai42/api-client';
56
34
 
57
35
  import { useApi } from './useApi';
58
36
  import { useOnUnauthorized } from './useUnauthorized';
59
- import { isFeatureDisabled } from '../feature-disabled';
37
+ import {
38
+ emptyOverlay,
39
+ merge,
40
+ pendingCount,
41
+ type LiveOverlay,
42
+ type StreamInteraction,
43
+ } from './interactions-stream';
44
+ import { startInteractionsStream, type ConnectionState } from './interactions-connection';
60
45
 
61
- /** A live interaction plus the client-maintained `answered` flag. */
62
- export type StreamInteraction = Interaction & { readonly answered: boolean };
46
+ export type { StreamInteraction } from './interactions-stream';
63
47
 
64
48
  /** The inputs the caller drives the tail-only stream with. */
65
49
  export interface InteractionsStreamOptions {
@@ -102,259 +86,34 @@ export interface InteractionsStreamState {
102
86
  /**
103
87
  * The interactions store is not configured on this deployment: the stream
104
88
  * answered with a terminal 501 `interactions-not-configured`. Reconnection is
105
- * abandoned (retrying would replay the same refusal forever) and the consumer
106
- * renders the muted OFF state instead of a red error or a floating badge.
89
+ * abandoned and the consumer renders the muted OFF state instead of a red error.
107
90
  */
108
91
  readonly disabled: boolean;
109
92
  }
110
93
 
111
- // Reconnect backoff: capped exponential with full jitter. The delay for attempt
112
- // n is a random value in [0, min(CAP, BASE * 2**n)); the attempt counter resets
113
- // only when a connection PROVES HEALTHY — it stayed open at least
114
- // `HEALTHY_CONNECTION_MS` — not merely when it opens, so a server that accepts the
115
- // request then drops the body still backs off instead of tight-looping. A healthy
116
- // tail-reconnect is near-instant while a sustained outage backs off, and the
117
- // jitter spreads many clients apart rather than retrying in lockstep.
118
- const RECONNECT_BASE_MS = 1500;
119
- const RECONNECT_CAP_MS = 30000;
120
- const HEALTHY_CONNECTION_MS = RECONNECT_BASE_MS;
121
-
122
- // The server emits no removal for an answered card — only `interaction.answered`,
123
- // never `interaction.removed` for it — and the paged base never carries answered
124
- // items, so answered cards would otherwise pile up unbounded in the always-mounted
125
- // badge/inbox. The client ages them out: drop an answered card
126
- // ANSWERED_RETENTION_MS after its answered frame, keeping at most ANSWERED_CAP as a
127
- // hard flood backstop. Swept lazily on frame/reconnect ticks (the stream carries no
128
- // idle heartbeat frame), never a per-item timer.
129
- const ANSWERED_RETENTION_MS = 10 * 60 * 1000;
130
- const ANSWERED_CAP = 200;
131
-
132
- // The wire `answer_format` values. Duplicated from the api-client enum by design:
133
- // the SDK is the leaf and must not import api-client at runtime. The `satisfies`
134
- // pin against `Interaction['answer_format']` fails to compile if a listed value
135
- // leaves the union, so this runtime guard cannot silently drift from the type.
136
- const ANSWER_FORMATS = new Set<string>([
137
- 'text',
138
- 'confirm',
139
- 'select',
140
- 'form',
141
- 'external',
142
- ] as const satisfies readonly Interaction['answer_format'][]);
143
-
144
- function isRecord(value: unknown): value is Record<string, unknown> {
145
- return typeof value === 'object' && value !== null && !Array.isArray(value);
146
- }
147
-
148
- /** Parse a frame's JSON to a plain object, or null when it is not one. */
149
- function parseJson(data: string): Record<string, unknown> | null {
150
- if (!data) return null;
151
- try {
152
- const obj: unknown = JSON.parse(data);
153
- return isRecord(obj) ? obj : null;
154
- } catch {
155
- return null;
156
- }
157
- }
158
-
159
- /**
160
- * Validate an `interaction.add` frame into an `Interaction`, or null when it is
161
- * malformed. This mirrors the api-client `interaction` zod schema exactly — it
162
- * accepts what that schema accepts, no more, no less. The strict-required fields
163
- * — `interaction_id`, `group_id`, `created_at`, `timeout_at` — must be a string
164
- * and `answer_format` a known enum value; any missing or non-string one returns
165
- * null so the caller surfaces it as an error rather than rendering a blank card.
166
- * Two fields are laxer, matching the schema: `question` is
167
- * `z.string().default('')`, whose default substitutes ONLY an absent/undefined
168
- * value, so a missing question normalizes to `''` while a present `null` (or any
169
- * present non-string) is rejected; `format_payload` is nullish, so an absent/null
170
- * value (text/confirm carry none) normalizes to `{}` while a present non-object is
171
- * rejected — never silently coerced to a blank card.
172
- */
173
- function parseAddFrame(data: string): Interaction | null {
174
- const obj = parseJson(data);
175
- if (obj === null) return null;
176
- const {
177
- interaction_id,
178
- group_id,
179
- question,
180
- answer_format,
181
- format_payload,
182
- created_at,
183
- timeout_at,
184
- sensitive,
185
- server_verified,
186
- channel,
187
- recipient,
188
- origin,
189
- audience,
190
- media,
191
- } = obj;
192
- if (typeof interaction_id !== 'string') return null;
193
- if (typeof group_id !== 'string') return null;
194
- // `question` is z.string().default('') in the schema: the default substitutes
195
- // ONLY an absent/undefined value, so a missing question is valid while a
196
- // present null (or any other present non-string) is malformed.
197
- if (question !== undefined && typeof question !== 'string') return null;
198
- if (typeof created_at !== 'string') return null;
199
- if (typeof timeout_at !== 'string') return null;
200
- if (typeof answer_format !== 'string' || !ANSWER_FORMATS.has(answer_format)) return null;
201
- // `format_payload` is nullish in the schema: absent/null normalizes to {}, but
202
- // a present non-object is malformed and must not be coerced to a blank card.
203
- if (format_payload != null && !isRecord(format_payload)) return null;
204
- // `sensitive` is z.boolean().default(false): an absent value normalizes to
205
- // false, while a present non-boolean is malformed.
206
- if (sensitive !== undefined && typeof sensitive !== 'boolean') return null;
207
- // `server_verified` is z.boolean().optional(): an absent value stays undefined,
208
- // while a present non-boolean is malformed.
209
- if (server_verified !== undefined && typeof server_verified !== 'boolean') return null;
210
- // `channel` is z.string().optional(): the delivery-channel name rides the frame
211
- // only for a channel-delivered question; absent stays undefined, a present
212
- // non-string is malformed.
213
- if (channel !== undefined && typeof channel !== 'string') return null;
214
- // Attribution fields are each z.string().optional(): `recipient` (delivery
215
- // address), `origin` (asking run id), `audience` (addressed user_id) ride the
216
- // frame only when set; absent stays undefined, a present non-string is malformed.
217
- if (recipient !== undefined && typeof recipient !== 'string') return null;
218
- if (origin !== undefined && typeof origin !== 'string') return null;
219
- if (audience !== undefined && typeof audience !== 'string') return null;
220
- // `media` is z.array(z.unknown()).optional(): the display-only media rides the
221
- // frame only when the question carries some; absent stays undefined, a present
222
- // non-array is malformed. The ITEMS stay unknown here (loose by design) — each is
223
- // validated per item by the renderer, so one bad item never fails this frame.
224
- if (media !== undefined && !Array.isArray(media)) return null;
225
- return {
226
- interaction_id,
227
- group_id,
228
- question: typeof question === 'string' ? question : '',
229
- answer_format: answer_format as Interaction['answer_format'],
230
- format_payload: isRecord(format_payload) ? format_payload : {},
231
- created_at,
232
- timeout_at,
233
- sensitive: sensitive ?? false,
234
- ...(typeof server_verified === 'boolean' ? { server_verified } : {}),
235
- ...(typeof channel === 'string' ? { channel } : {}),
236
- ...(typeof recipient === 'string' ? { recipient } : {}),
237
- ...(typeof origin === 'string' ? { origin } : {}),
238
- ...(typeof audience === 'string' ? { audience } : {}),
239
- ...(Array.isArray(media) ? { media } : {}),
240
- };
241
- }
242
-
243
- /** The interaction id from an `answered` / `removed` frame, or null. */
244
- function parseId(data: string): string | null {
245
- const id = parseJson(data)?.interaction_id;
246
- return typeof id === 'string' ? id : null;
247
- }
248
-
249
- /**
250
- * The live overlay a connection accumulates over the paged base: the tail's own
251
- * adds (kept whether or not the base already lists them, so a fresher add wins),
252
- * the answered stamps (wall-clock ms, for the aging sweep), and the tombstones for
253
- * ids removed on the tail OR aged out of the answered window (a tombstone stops a
254
- * still-stale seed from re-showing a gone card; ids are never reused). `addEpoch`
255
- * and `goneEpoch` carry the resync epoch current when an add / a first terminal frame
256
- * arrived, so the count applies an add (or subtracts a terminal) only while it is not
257
- * yet folded into the refetched `total`; the merge and the aging sweep ignore them.
258
- */
259
- interface LiveOverlay {
260
- readonly adds: Map<string, Interaction>;
261
- readonly addEpoch: Map<string, number>;
262
- readonly answeredAt: Map<string, number>;
263
- readonly removed: Set<string>;
264
- readonly goneEpoch: Map<string, number>;
265
- }
266
-
267
- function emptyOverlay(): LiveOverlay {
268
- return {
269
- adds: new Map(),
270
- addEpoch: new Map(),
271
- answeredAt: new Map(),
272
- removed: new Set(),
273
- goneEpoch: new Map(),
274
- };
275
- }
276
-
277
- /** Fold the paged base and the live overlay into the published, ordered list. */
278
- function merge(seed: readonly Interaction[], live: LiveOverlay): StreamInteraction[] {
279
- const out = new Map<string, StreamInteraction>();
280
- for (const item of seed) {
281
- if (live.removed.has(item.interaction_id)) continue;
282
- out.set(item.interaction_id, { ...item, answered: live.answeredAt.has(item.interaction_id) });
283
- }
284
- for (const [id, item] of live.adds) {
285
- if (live.removed.has(id)) continue;
286
- out.set(id, { ...item, answered: live.answeredAt.has(id) });
287
- }
288
- return Array.from(out.values());
289
- }
290
-
291
- /**
292
- * The live pending tally: the door's `total` for the seed, plus live adds whose id
293
- * is absent from the seed AND stamped at or after `countEpoch` (a new question not
294
- * yet folded into `total`), minus one for every terminal id — answered OR removed —
295
- * stamped at or after `countEpoch`, deduped per id so an id counts once however many
296
- * terminal frames it drew. `countEpoch` is the epoch of the last resync whose refetch
297
- * LANDED (a failed refetch never advances it): a delta stamped before it is already
298
- * reflected in the fresh `total`, so it is NOT applied again — this is what stops a
299
- * resync from re-subtracting a terminal it already dropped from `total`, while a
300
- * failed refetch keeps the prior-epoch deltas applying against the prior total. Every
301
- * terminal frame a client receives refers to a
302
- * question inside its own `total` (the stream and the list door apply the same
303
- * audience filter). One residual race remains: a frame arriving while a refetch is in
304
- * flight may already be reflected in that refetch's `total` (the resync GET landing
305
- * after an answer POST), so the raw tally can transiently dip below zero until the
306
- * next resync reconciles — observed live as a "-1 pending questions" badge. A
307
- * pending tally below zero is meaningless, so the published count clamps at 0. The
308
- * clamp covers only the negative symptom: the same race can also leave a transient
309
- * positive under-count (a double-subtracted question hiding a still-pending one),
310
- * which reconciles on the next resync, not here.
311
- */
312
- function pendingCount(
313
- seed: readonly Interaction[],
314
- total: number,
315
- live: LiveOverlay,
316
- countEpoch: number,
317
- ): number {
318
- const seedIds = new Set(seed.map((item) => item.interaction_id));
319
- let count = total;
320
- for (const id of live.adds.keys()) {
321
- if (!seedIds.has(id) && (live.addEpoch.get(id) ?? 0) >= countEpoch) count += 1;
322
- }
323
- const gone = new Set<string>([...live.answeredAt.keys(), ...live.removed]);
324
- for (const id of gone) {
325
- if ((live.goneEpoch.get(id) ?? 0) >= countEpoch) count -= 1;
326
- }
327
- return Math.max(count, 0);
328
- }
329
-
330
94
  export function useInteractionsStream(options: InteractionsStreamOptions): InteractionsStreamState {
331
95
  const { seed, total, onResync } = options;
332
96
  const api = useApi();
333
97
  const onUnauthorized = useOnUnauthorized();
334
98
 
335
- const [connectionState, setConnectionState] = useState<{
336
- connected: boolean;
337
- error: Error | null;
338
- disabled: boolean;
339
- }>({ connected: false, error: null, disabled: false });
99
+ const [connectionState, setConnectionState] = useState<ConnectionState>({
100
+ connected: false,
101
+ error: null,
102
+ disabled: false,
103
+ });
340
104
 
341
- // The live overlay is a ref (mutated in place by the stream loop); the published
342
- // list is `merge(seed, overlay)` held in state, recomputed on every overlay
343
- // change (the stream loop) and every seed change (a refetch of the paged base).
105
+ // The live overlay is a ref (mutated in place by the connection driver); the
106
+ // published list is `merge(seed, overlay)` held in state, recomputed on every
107
+ // overlay change (the driver) and every seed change (a refetch of the paged base).
344
108
  const overlayRef = useRef<LiveOverlay>(emptyOverlay());
345
109
  const [interactions, setInteractions] = useState<StreamInteraction[]>(() =>
346
110
  merge(seed, overlayRef.current),
347
111
  );
348
112
 
349
- // Resync epochs. `epochRef` increments on each (re)connect and stamps every frame
350
- // that arrives on that connection; `countEpoch` is set to the connect's epoch only
351
- // once its refetch landed fresh (onResync resolved true) — a failed refetch leaves
352
- // it unchanged so prior-epoch deltas keep applying against the prior total.
353
- // `pendingCount` applies only overlay entries stamped at or after `countEpoch`, so a
354
- // resync never re-subtracts a terminal it already dropped from the fresh `total`.
113
+ // Resync epochs. `epochRef` increments on each (re)connect and stamps every frame;
114
+ // `countEpoch` advances to a connect's epoch only once its refetch landed fresh.
355
115
  // `countEpoch` is STATE, not a ref: advancing it must republish the derived count
356
- // even when the reconnect carries no frame after the refetch. Both reset with the
357
- // overlay on remount.
116
+ // even when the reconnect carries no frame after the refetch.
358
117
  const epochRef = useRef(0);
359
118
  const [countEpoch, setCountEpoch] = useState(0);
360
119
 
@@ -370,211 +129,25 @@ export function useInteractionsStream(options: InteractionsStreamOptions): Inter
370
129
  setInteractions(merge(seed, overlayRef.current));
371
130
  }, [seed]);
372
131
 
373
- useEffect(() => {
374
- const controller = new AbortController();
375
- const aborted = () => controller.signal.aborted;
376
- const overlay = overlayRef.current;
377
- let reconnectAttempt = 0;
378
- // The id of the last frame seen on this mount, sent as Last-Event-ID on each
379
- // reconnect so the server resumes AFTER it and replays any frame that landed
380
- // during the gap — including a card's `answered`, which heals a card that would
381
- // otherwise vanish (absent from both the refetched pending base and the tail).
382
- // Persists across reconnects within one mount; resets with the overlay on remount.
383
- let lastEventId: string | undefined;
384
-
385
- // Age answered cards out (the server sends no removal for them): tombstone any
386
- // past the retention window, then — as a flood backstop — the oldest beyond the
387
- // count cap. A tombstone (not a bare delete) so a still-stale seed cannot
388
- // resurrect a card the client has retired.
389
- const sweepAnswered = () => {
390
- const now = Date.now();
391
- const retire = (id: string) => {
392
- overlay.answeredAt.delete(id);
393
- overlay.adds.delete(id);
394
- overlay.addEpoch.delete(id);
395
- overlay.removed.add(id);
396
- // The id keeps its original `goneEpoch` (stamped on its answered frame) so
397
- // moving answered→removed never re-dates it into a fresher resync.
398
- };
399
- for (const [id, at] of overlay.answeredAt) {
400
- if (now - at >= ANSWERED_RETENTION_MS) retire(id);
401
- }
402
- if (overlay.answeredAt.size > ANSWERED_CAP) {
403
- const oldest = [...overlay.answeredAt.entries()].sort((a, b) => a[1] - b[1]);
404
- for (const [id] of oldest.slice(0, oldest.length - ANSWERED_CAP)) retire(id);
405
- }
406
- };
407
-
408
- const surfaceMalformed = (event: string) => {
409
- setConnectionState((prev) => ({
410
- ...prev,
411
- error: new Error(`malformed interaction frame: ${event}`),
412
- }));
413
- };
414
-
415
- // A successful data frame sweeps, republishes the merged list, and clears any
416
- // transient frame-parse error — one bad frame on a healthy stream must not
417
- // stick. It does NOT refetch: the overlay already updated the list and the
418
- // derived count, and `onResync` fires on (re)connect only.
419
- const commit = () => {
420
- if (aborted()) return;
421
- sweepAnswered();
422
- setConnectionState((prev) => (prev.error === null ? prev : { ...prev, error: null }));
423
- setInteractions(merge(seedRef.current, overlay));
424
- };
425
-
426
- const applyFrame = (event: string, data: string) => {
427
- if (event === 'interaction.removed') {
428
- const id = parseId(data);
429
- if (id === null) {
430
- surfaceMalformed(event);
431
- return;
432
- }
433
- overlay.adds.delete(id);
434
- overlay.addEpoch.delete(id);
435
- overlay.answeredAt.delete(id);
436
- overlay.removed.add(id);
437
- // Stamp the terminal epoch on the FIRST terminal frame for this id only.
438
- if (!overlay.goneEpoch.has(id)) overlay.goneEpoch.set(id, epochRef.current);
439
- commit();
440
- return;
441
- }
442
- if (event === 'interaction.answered') {
443
- const id = parseId(data);
444
- if (id === null) {
445
- surfaceMalformed(event);
446
- return;
447
- }
448
- // Stamp the answered time on the FIRST answered frame only; a redelivered
449
- // answered keeps the original stamp so the retention window is not extended.
450
- if (!overlay.answeredAt.has(id)) overlay.answeredAt.set(id, Date.now());
451
- // Stamp the terminal epoch on the FIRST terminal frame for this id only, so a
452
- // later terminal never re-dates it into a fresher resync.
453
- if (!overlay.goneEpoch.has(id)) overlay.goneEpoch.set(id, epochRef.current);
454
- // Promote a seed-origin card into the overlay so it lingers uniformly with a
455
- // live-add card: the paged base never carries answered items, so a refetch
456
- // would otherwise drop it before its retention window elapses.
457
- if (!overlay.adds.has(id)) {
458
- const seeded = seedRef.current.find((item) => item.interaction_id === id);
459
- if (seeded !== undefined) {
460
- overlay.adds.set(id, seeded);
461
- overlay.addEpoch.set(id, epochRef.current);
462
- }
463
- }
464
- commit();
465
- return;
466
- }
467
- // interaction.add — the full question. Ids are never reused, so a live add
468
- // simply overwrites any prior copy in place.
469
- const interaction = parseAddFrame(data);
470
- if (interaction === null) {
471
- surfaceMalformed(event);
472
- return;
473
- }
474
- overlay.adds.set(interaction.interaction_id, interaction);
475
- overlay.addEpoch.set(interaction.interaction_id, epochRef.current);
476
- commit();
477
- };
478
-
479
- // The abort signal is the single cancellation source of truth: the cleanup
480
- // aborts it, and every loop guard reads it fresh.
481
- const run = async () => {
482
- while (!aborted()) {
483
- const openedAt = Date.now();
484
- try {
485
- const frames = await api.streamInteractions(controller.signal, lastEventId);
486
- if (aborted()) return;
487
- setConnectionState((prev) => ({
488
- ...prev,
489
- connected: true,
490
- error: null,
491
- disabled: false,
492
- }));
493
- // Tail-only: the pending base is not on the stream. Refetch it on every
494
- // (re)connect under a fresh epoch so anything removed or added while
495
- // disconnected is reconciled by the fresh page; record that epoch as
496
- // `countEpoch` only once the refetch LANDED (onResync resolved true), so the
497
- // count applies only deltas not yet folded into the fresh `total`. A failed
498
- // refetch (false) leaves `countEpoch` unchanged, so prior-epoch deltas keep
499
- // applying against the consistent prior total instead of being discarded
500
- // against a stale one. Every frame on this connection is stamped with `epoch`.
501
- const epoch = (epochRef.current += 1);
502
- const landed = await onResyncRef.current();
503
- if (aborted()) return;
504
- if (landed) setCountEpoch(epoch);
505
- for await (const frame of frames) {
506
- if (aborted()) return;
507
- applyFrame(frame.event, frame.data);
508
- // Advance the resume cursor to every id-bearing frame (even a malformed
509
- // one) so a reconnect never re-requests a frame already delivered.
510
- if (frame.id !== undefined) lastEventId = frame.id;
511
- }
512
- } catch (err) {
513
- if (aborted()) return;
514
- const error = err instanceof Error ? err : new Error(String(err));
515
- setConnectionState((prev) => ({ ...prev, connected: false, error }));
516
- if (error.name === 'ApiUnauthorizedError') {
517
- // A 401 means the stored credential is dead — reconnecting would just
518
- // replay the same bad key every delay forever. Treat it as terminal
519
- // and hand off to the app's 401→login handler, the same path every
520
- // other data call takes; do not loop.
521
- onUnauthorized();
522
- return;
523
- }
524
- if (isFeatureDisabled(error)) {
525
- // A terminal 501 `interactions-not-configured`: the deployment runs no
526
- // interactions store, so reconnecting would replay the same refusal on
527
- // every backoff forever (this stream is always-mounted via the shell
528
- // badge). Stop, and flag the OFF state so the badge disappears and the
529
- // page renders the muted "not configured" note instead of a red error.
530
- setConnectionState((prev) => ({ ...prev, connected: false, disabled: true }));
531
- return;
532
- }
533
- }
534
- // A connection that stayed open at least HEALTHY_CONNECTION_MS proved
535
- // healthy — clear the reconnect backoff so the next drop reconnects
536
- // promptly; an immediate accept-then-drop flap does not reset it.
537
- if (Date.now() - openedAt >= HEALTHY_CONNECTION_MS) reconnectAttempt = 0;
538
- // Stream ended or errored → wait, then reconnect. Guard before the
539
- // setState, like every other setState in run(): a microtask scheduled after
540
- // the `for await` must not write state after the effect cleanup has aborted
541
- // (and unmounted) the hook.
542
- if (aborted()) return;
543
- setConnectionState((prev) => ({ ...prev, connected: false }));
544
- const ceiling = Math.min(RECONNECT_CAP_MS, RECONNECT_BASE_MS * 2 ** reconnectAttempt);
545
- const delay = Math.random() * ceiling;
546
- reconnectAttempt += 1;
547
- await new Promise<void>((resolve) => {
548
- const onAbort = () => {
549
- clearTimeout(timer);
550
- resolve();
551
- };
552
- const timer = setTimeout(() => {
553
- // Normal wake: drop the abort listener so it does not accumulate
554
- // across reconnects ({ once: true } only self-removes on firing).
555
- controller.signal.removeEventListener('abort', onAbort);
556
- resolve();
557
- }, delay);
558
- controller.signal.addEventListener('abort', onAbort, { once: true });
559
- });
560
- }
561
- };
562
-
563
- void run();
564
- return () => {
565
- controller.abort();
566
- // A fresh mount starts from an empty overlay at epoch 0: the effect owns the
567
- // connection and its live deltas, so a re-run must not inherit the prior run's
568
- // tail or its epoch stamps.
569
- overlayRef.current = emptyOverlay();
570
- epochRef.current = 0;
571
- setCountEpoch(0);
572
- };
573
- }, [api, onUnauthorized]);
132
+ useEffect(
133
+ () =>
134
+ startInteractionsStream({
135
+ api,
136
+ overlayRef,
137
+ epochRef,
138
+ seedRef,
139
+ onResyncRef,
140
+ onUnauthorized,
141
+ setConnectionState,
142
+ setInteractions,
143
+ setCountEpoch,
144
+ }),
145
+ [api, onUnauthorized],
146
+ );
574
147
 
575
- // Derived live, not stored: every overlay mutation republishes `interactions` (a
576
- // setState), and every seed/total change and every `countEpoch` advance is itself a
577
- // render, so this recomputes from the current overlay ref on the same render.
148
+ // Derived live, not stored: every overlay mutation republishes `interactions`, and
149
+ // every seed/total change and every `countEpoch` advance is itself a render, so
150
+ // this recomputes from the current overlay ref on the same render.
578
151
  const count = pendingCount(seed, total, overlayRef.current, countEpoch);
579
152
 
580
153
  return {
@@ -0,0 +1,70 @@
1
+ /**
2
+ * `AppLink` — the SDK's route-token anchor: a real `<a>` whose plain left-click is
3
+ * intercepted for a client-side transition.
4
+ */
5
+ import {
6
+ createElement,
7
+ useCallback,
8
+ type AriaAttributes,
9
+ type CSSProperties,
10
+ type MouseEvent,
11
+ type ReactNode,
12
+ } from 'react';
13
+
14
+ import { useNavigation } from './context';
15
+ import type { RouteSearch, RouteToken } from './types';
16
+
17
+ export interface AppLinkProps<T extends RouteToken> {
18
+ to: T;
19
+ search?: RouteSearch<T>;
20
+ children: ReactNode;
21
+ className?: string;
22
+ 'aria-label'?: string;
23
+ 'aria-current'?: AriaAttributes['aria-current'];
24
+ /** Native tooltip text (also a sensible non-AT hover hint). */
25
+ title?: string;
26
+ /** Inline styles for the anchor. */
27
+ style?: CSSProperties;
28
+ }
29
+
30
+ /**
31
+ * A real anchor (so middle-click / open-in-new-tab keep working) that drives a
32
+ * client-side transition on plain left-click. Modified clicks (new tab/window,
33
+ * download) fall through to the browser's default handling.
34
+ */
35
+ export function AppLink<T extends RouteToken>({
36
+ to,
37
+ search,
38
+ children,
39
+ className,
40
+ 'aria-label': ariaLabel,
41
+ 'aria-current': ariaCurrent,
42
+ title,
43
+ style,
44
+ }: AppLinkProps<T>): ReactNode {
45
+ const { navigate, resolvePath } = useNavigation();
46
+ const href = resolvePath(to, search);
47
+ const onClick = useCallback(
48
+ (event: MouseEvent<HTMLAnchorElement>) => {
49
+ if (event.defaultPrevented) return;
50
+ if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
51
+ return;
52
+ event.preventDefault();
53
+ navigate(to, search);
54
+ },
55
+ [navigate, to, search],
56
+ );
57
+ return createElement(
58
+ 'a',
59
+ {
60
+ href,
61
+ className,
62
+ 'aria-label': ariaLabel,
63
+ 'aria-current': ariaCurrent,
64
+ title,
65
+ style,
66
+ onClick,
67
+ },
68
+ children,
69
+ );
70
+ }