@zyro-sec/session-ui 0.0.0-stage → 1.0.3

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 (261) hide show
  1. package/AGENTS.md +25 -0
  2. package/bunfig.toml +2 -0
  3. package/component-tests/basic-tool.fixture.tsx +46 -0
  4. package/component-tests/basic-tool.spec.ts +63 -0
  5. package/component-tests/file-media.fixture.tsx +39 -0
  6. package/component-tests/file-media.spec.ts +13 -0
  7. package/component-tests/file-tool.spec.ts +62 -0
  8. package/component-tests/file-viewer.fixture.tsx +109 -0
  9. package/component-tests/file-viewer.spec.ts +134 -0
  10. package/component-tests/file.spec.ts +33 -0
  11. package/component-tests/line-comment-v2.spec.ts +15 -0
  12. package/component-tests/markdown-code-wrap.spec.ts +94 -0
  13. package/component-tests/markdown-readiness.spec.ts +95 -0
  14. package/component-tests/markdown.fixture.tsx +104 -0
  15. package/component-tests/markdown.spec.ts +1564 -0
  16. package/component-tests/mermaid-preview.spec.ts +43 -0
  17. package/component-tests/patch-group.spec.ts +79 -0
  18. package/component-tests/read-group.spec.ts +72 -0
  19. package/component-tests/read-image.fixture.tsx +66 -0
  20. package/component-tests/read-image.spec.ts +154 -0
  21. package/component-tests/session-compaction.spec.ts +254 -0
  22. package/component-tests/session-lifecycle.spec.ts +190 -0
  23. package/component-tests/session-message-projection.spec.ts +639 -0
  24. package/component-tests/session-notice-projection.spec.ts +20 -0
  25. package/component-tests/session-review-comments.spec.ts +179 -0
  26. package/component-tests/session-shell.spec.ts +93 -0
  27. package/component-tests/session-timeline-mobile.spec.ts +201 -0
  28. package/component-tests/session-timeline-notices.spec.ts +38 -0
  29. package/component-tests/session-timeline-reasoning.spec.ts +107 -0
  30. package/component-tests/session-timeline.spec.ts +157 -0
  31. package/component-tests/session-tool-projection.spec.ts +323 -0
  32. package/component-tests/text-lifecycle.fixture.tsx +26 -0
  33. package/component-tests/text-lifecycle.spec.ts +42 -0
  34. package/component-tests/timeline-subagent-rows.spec.ts +33 -0
  35. package/component-tests/tool-disclosure.spec.ts +48 -0
  36. package/component-tests/tool-group-sticky.spec.ts +37 -0
  37. package/component-tests/tool-group.spec.ts +290 -0
  38. package/component-tests/tool-status-title.fixture.tsx +22 -0
  39. package/component-tests/tool-status-title.spec.ts +30 -0
  40. package/component-tests/user-message-quotes.spec.ts +111 -0
  41. package/package.json +91 -4
  42. package/playwright.components.config.ts +4 -0
  43. package/playwright.local.config.ts +2 -0
  44. package/src/actions.ts +54 -0
  45. package/src/components/apply-patch-file.ts +122 -0
  46. package/src/components/basic-tool.css +487 -0
  47. package/src/components/basic-tool.tsx +258 -0
  48. package/src/components/copy-machine.ts +78 -0
  49. package/src/components/copy.ts +85 -0
  50. package/src/components/diagram-preview.tsx +28 -0
  51. package/src/components/disclosure-machine.ts +188 -0
  52. package/src/components/disclosure.ts +129 -0
  53. package/src/components/dock-prompt.stories.tsx +73 -0
  54. package/src/components/dock-prompt.tsx +29 -0
  55. package/src/components/file-comment-selection.tsx +146 -0
  56. package/src/components/file-media-machine.ts +165 -0
  57. package/src/components/file-media.stories.tsx +112 -0
  58. package/src/components/file-media.tsx +323 -0
  59. package/src/components/file-search.tsx +73 -0
  60. package/src/components/file.css +59 -0
  61. package/src/components/file.tsx +1300 -0
  62. package/src/components/image-preview.tsx +35 -0
  63. package/src/components/line-comment-annotations.tsx +580 -0
  64. package/src/components/line-comment-machine.ts +262 -0
  65. package/src/components/line-comment-styles.ts +293 -0
  66. package/src/components/line-comment.stories.tsx +152 -0
  67. package/src/components/line-comment.tsx +448 -0
  68. package/src/components/link-segments.ts +20 -0
  69. package/src/components/linked-text.tsx +21 -0
  70. package/src/components/markdown-cache-machine.ts +257 -0
  71. package/src/components/markdown-cache.tsx +556 -0
  72. package/src/components/markdown-clipboard.ts +101 -0
  73. package/src/components/markdown-code-machine.ts +200 -0
  74. package/src/components/markdown-code-state.ts +22 -0
  75. package/src/components/markdown-fragment-links.ts +74 -0
  76. package/src/components/markdown-github.svg +3 -0
  77. package/src/components/markdown-globe.svg +5 -0
  78. package/src/components/markdown-image-machine.ts +170 -0
  79. package/src/components/markdown-image.ts +156 -0
  80. package/src/components/markdown-inline-code-kind.ts +39 -0
  81. package/src/components/markdown-mermaid.ts +18 -0
  82. package/src/components/markdown-phase-machine.ts +102 -0
  83. package/src/components/markdown-projection.ts +13 -0
  84. package/src/components/markdown-session-links.ts +183 -0
  85. package/src/components/markdown-solid.test.tsx +92 -0
  86. package/src/components/markdown-solid.tsx +113 -0
  87. package/src/components/markdown-stream.ts +95 -0
  88. package/src/components/markdown-worker-machine.ts +516 -0
  89. package/src/components/markdown-worker-protocol.ts +73 -0
  90. package/src/components/markdown-worker.ts +250 -0
  91. package/src/components/markdown.css +672 -0
  92. package/src/components/markdown.stories.tsx +199 -0
  93. package/src/components/markdown.tsx +1612 -0
  94. package/src/components/markdown.worker.ts +188 -0
  95. package/src/components/message-file.ts +102 -0
  96. package/src/components/message-nav.css +134 -0
  97. package/src/components/message-nav.tsx +159 -0
  98. package/src/components/message-part.css +2109 -0
  99. package/src/components/message-part.tsx +3 -0
  100. package/src/components/question-prompt-experimental.css +576 -0
  101. package/src/components/question-prompt.css +527 -0
  102. package/src/components/quote-highlight.css +59 -0
  103. package/src/components/review-focus-driver.ts +142 -0
  104. package/src/components/review-focus-machine.ts +95 -0
  105. package/src/components/session-compaction.css +270 -0
  106. package/src/components/session-diff.ts +358 -0
  107. package/src/components/session-error.tsx +41 -0
  108. package/src/components/session-retry.tsx +92 -0
  109. package/src/components/session-review-machine.ts +241 -0
  110. package/src/components/session-review.css +264 -0
  111. package/src/components/session-review.stories.tsx +232 -0
  112. package/src/components/session-review.tsx +733 -0
  113. package/src/components/session-turn.css +282 -0
  114. package/src/components/shell-submessage.css +24 -0
  115. package/src/components/timeline-separator.tsx +69 -0
  116. package/src/components/tool-count-label.css +57 -0
  117. package/src/components/tool-count-label.tsx +76 -0
  118. package/src/components/tool-count-summary.css +102 -0
  119. package/src/components/tool-count-summary.stories.tsx +99 -0
  120. package/src/components/tool-count-summary.tsx +44 -0
  121. package/src/components/tool-error-card.css +178 -0
  122. package/src/components/tool-error-card.stories.tsx +113 -0
  123. package/src/components/tool-error-card.tsx +202 -0
  124. package/src/components/tool-status-title-machine.ts +128 -0
  125. package/src/components/tool-status-title.css +89 -0
  126. package/src/components/tool-status-title.tsx +163 -0
  127. package/src/context/data.tsx +107 -0
  128. package/src/context/index.ts +1 -0
  129. package/src/context/markdown.tsx +129 -0
  130. package/src/context/sources.tsx +34 -0
  131. package/src/document.ts +8 -0
  132. package/src/file-presentation.ts +34 -0
  133. package/src/message/action-machine.ts +72 -0
  134. package/src/message/agent-label.ts +3 -0
  135. package/src/message/attachment-card.css +119 -0
  136. package/src/message/attachment-card.tsx +40 -0
  137. package/src/message/attachment-media-machine.ts +213 -0
  138. package/src/message/comment-card.tsx +155 -0
  139. package/src/message/compaction-entrance-machine.ts +93 -0
  140. package/src/message/current-message.tsx +157 -0
  141. package/src/message/current-tool-state.ts +161 -0
  142. package/src/message/message-content.tsx +1391 -0
  143. package/src/message/paced-text-machine.ts +73 -0
  144. package/src/message/prompt-message.stories.tsx +149 -0
  145. package/src/message/relative-time.ts +45 -0
  146. package/src/message/user-message-quote-parts.ts +16 -0
  147. package/src/message/user-message-quotes.stories.tsx +797 -0
  148. package/src/message/user-message-quotes.tsx +135 -0
  149. package/src/pierre/comment-hover-machine.ts +53 -0
  150. package/src/pierre/comment-hover.ts +131 -0
  151. package/src/pierre/comment-selection-machine.ts +257 -0
  152. package/src/pierre/commented-lines.ts +106 -0
  153. package/src/pierre/diff-selection.ts +91 -0
  154. package/src/pierre/file-find-machine.ts +323 -0
  155. package/src/pierre/file-find.ts +537 -0
  156. package/src/pierre/file-runtime.ts +42 -0
  157. package/src/pierre/file-selection.ts +140 -0
  158. package/src/pierre/index.ts +344 -0
  159. package/src/pierre/media.ts +140 -0
  160. package/src/pierre/render-machine.ts +381 -0
  161. package/src/pierre/selection-bridge.ts +82 -0
  162. package/src/pierre/selection-machine.ts +198 -0
  163. package/src/pierre/virtualizer.ts +114 -0
  164. package/src/pierre/worker.ts +46 -0
  165. package/src/sources/experiment.ts +59 -0
  166. package/src/sources/site-icon.tsx +53 -0
  167. package/src/sources/sources-chip.tsx +57 -0
  168. package/src/sources/sources-expand-machine.ts +101 -0
  169. package/src/sources/sources-panel.tsx +249 -0
  170. package/src/sources/sources-title.tsx +21 -0
  171. package/src/sources/sources.css +266 -0
  172. package/src/sources/sources.ts +141 -0
  173. package/src/storybook/current-session-fixtures.ts +1492 -0
  174. package/src/storybook/current-session-scenarios.ts +76 -0
  175. package/src/storybook/current-session-story.tsx +154 -0
  176. package/src/storybook/index.ts +43 -0
  177. package/src/styles/index.css +19 -0
  178. package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
  179. package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
  180. package/src/timeline/code-blocks.stories.tsx +105 -0
  181. package/src/timeline/commentary.stories.tsx +70 -0
  182. package/src/timeline/commentary.ts +38 -0
  183. package/src/timeline/detail.ts +111 -0
  184. package/src/timeline/error-card.stories.tsx +134 -0
  185. package/src/timeline/error-spacing.stories.tsx +71 -0
  186. package/src/timeline/facts.ts +493 -0
  187. package/src/timeline/file-changes.stories.tsx +436 -0
  188. package/src/timeline/group-label-fixtures.ts +97 -0
  189. package/src/timeline/mermaid.stories.tsx +131 -0
  190. package/src/timeline/place.ts +280 -0
  191. package/src/timeline/placement-fixtures.ts +365 -0
  192. package/src/timeline/placement-reactivity.ts +133 -0
  193. package/src/timeline/projection.ts +1261 -0
  194. package/src/timeline/research-and-agents.stories.tsx +461 -0
  195. package/src/timeline/session-timeline-row.tsx +1675 -0
  196. package/src/timeline/session-timeline.tsx +68 -0
  197. package/src/timeline/steer-arrival-machine.ts +134 -0
  198. package/src/timeline/terminal-work.stories.tsx +464 -0
  199. package/src/timeline/timeline-row.stories.tsx +1069 -0
  200. package/src/timeline/timeline-row.ts +238 -0
  201. package/src/timeline/turn-phase-machine.ts +112 -0
  202. package/src/timeline/user-message-fold.stories.tsx +194 -0
  203. package/src/timeline/user-runs.ts +87 -0
  204. package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
  205. package/src/timeline-next/compaction.tsx +236 -0
  206. package/src/timeline-next/errors.ts +101 -0
  207. package/src/timeline-next/event-row.tsx +168 -0
  208. package/src/timeline-next/events.ts +103 -0
  209. package/src/timeline-next/index.ts +10 -0
  210. package/src/timeline-next/nested-rail.ts +125 -0
  211. package/src/timeline-next/projection.ts +1028 -0
  212. package/src/timeline-next/row.tsx +1728 -0
  213. package/src/timeline-next/rules.ts +89 -0
  214. package/src/timeline-next/separator.tsx +73 -0
  215. package/src/timeline-next/spacing.ts +127 -0
  216. package/src/timeline-next/status-line.tsx +41 -0
  217. package/src/timeline-next/status.ts +33 -0
  218. package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
  219. package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
  220. package/src/timeline-next/stories/compare.stories.tsx +486 -0
  221. package/src/timeline-next/stories/playground.stories.tsx +297 -0
  222. package/src/timeline-next/stories/session-replay.ts +1156 -0
  223. package/src/timeline-next/stories/session-timeline.tsx +50 -0
  224. package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
  225. package/src/timeline-next/timeline-next.css +649 -0
  226. package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
  227. package/src/timeline-next/tools/activity-label.ts +166 -0
  228. package/src/timeline-next/tools/assistant-content.tsx +84 -0
  229. package/src/timeline-next/tools/context-group.tsx +619 -0
  230. package/src/timeline-next/tools/error-card.tsx +147 -0
  231. package/src/timeline-next/tools/explore.tsx +119 -0
  232. package/src/timeline-next/tools/file-group.tsx +128 -0
  233. package/src/timeline-next/tools/patch.tsx +174 -0
  234. package/src/timeline-next/tools/shell.tsx +186 -0
  235. package/src/timeline-next/tools/thought.tsx +87 -0
  236. package/src/timeline-next/tools/tool-display.tsx +119 -0
  237. package/src/tools/ansi.ts +8 -0
  238. package/src/tools/context-group-label.ts +123 -0
  239. package/src/tools/file-runs.ts +35 -0
  240. package/src/tools/project-path.ts +18 -0
  241. package/src/tools/read-group.stories.tsx +159 -0
  242. package/src/tools/run-machine.ts +185 -0
  243. package/src/tools/shell-output-machine.ts +233 -0
  244. package/src/tools/shell-output.ts +91 -0
  245. package/src/tools/subagent-row-variants.stories.tsx +129 -0
  246. package/src/tools/tool-group.stories.tsx +345 -0
  247. package/src/tools/tool-renderer.tsx +2876 -0
  248. package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
  249. package/src/v2/components/session-file-panel-v2.tsx +149 -0
  250. package/src/v2/components/session-progress-indicator-v2.css +5 -0
  251. package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
  252. package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
  253. package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
  254. package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
  255. package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
  256. package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
  257. package/src/v2/components/session-review-v2.css +653 -0
  258. package/src/v2/components/session-review-v2.tsx +422 -0
  259. package/src/v2/components/sidebar-presence-machine.ts +69 -0
  260. package/tsconfig.json +20 -0
  261. package/README.md +0 -3
@@ -0,0 +1,114 @@
1
+ import { type VirtualFileMetrics, Virtualizer } from "@pierre/diffs"
2
+
3
+ type Target = {
4
+ key: Document | HTMLElement
5
+ root: Document | HTMLElement
6
+ content: HTMLElement | undefined
7
+ }
8
+
9
+ type Entry = {
10
+ virtualizer: Virtualizer
11
+ refs: number
12
+ }
13
+
14
+ const cache = new WeakMap<Document | HTMLElement, Entry>()
15
+
16
+ // Pierre keeps the viewport plus 1,000px of overscan on each side (about 100 rows) mounted; measured, windowing
17
+ // breaks even on main-thread render time near 200 lines and wins on DOM size and render time beyond it.
18
+ export const virtualizeLineThreshold = 200
19
+
20
+ export const virtualMetrics: Partial<VirtualFileMetrics> = {
21
+ lineHeight: 24,
22
+ hunkSeparatorHeight: 24,
23
+ spacing: 0,
24
+ }
25
+
26
+ function scrollable(value: string) {
27
+ return value === "auto" || value === "scroll" || value === "overlay"
28
+ }
29
+
30
+ function scrollRoot(container: HTMLElement) {
31
+ let node = container.parentElement
32
+
33
+ while (node) {
34
+ const style = getComputedStyle(node)
35
+
36
+ if (scrollable(style.overflowY)) return node
37
+ node = node.parentElement
38
+ }
39
+ }
40
+
41
+ function target(container: HTMLElement): Target | undefined {
42
+ if (typeof document === "undefined") return
43
+
44
+ const review = container.closest("[data-component='session-review']")
45
+
46
+ if (review instanceof HTMLElement) {
47
+ const root = scrollRoot(container) ?? review
48
+ const content = review.querySelector("[data-slot='session-review-container']")
49
+
50
+ return {
51
+ key: review,
52
+ root,
53
+ content: content instanceof HTMLElement ? content : undefined,
54
+ }
55
+ }
56
+
57
+ const root = scrollRoot(container)
58
+
59
+ if (root) {
60
+ const content = root.querySelector("[role='log']")
61
+
62
+ return {
63
+ key: root,
64
+ root,
65
+ content: content instanceof HTMLElement ? content : undefined,
66
+ }
67
+ }
68
+
69
+ return {
70
+ key: document,
71
+ root: document,
72
+ content: undefined,
73
+ }
74
+ }
75
+
76
+ export function acquireVirtualizer(container: HTMLElement) {
77
+ const resolved = target(container)
78
+
79
+ if (!resolved) return
80
+
81
+ let entry = cache.get(resolved.key)
82
+
83
+ if (!entry) {
84
+ const virtualizer = new Virtualizer()
85
+ virtualizer.setup(resolved.root, resolved.content)
86
+ entry = {
87
+ virtualizer,
88
+ refs: 0,
89
+ }
90
+ cache.set(resolved.key, entry)
91
+ }
92
+
93
+ entry.refs += 1
94
+ let done = false
95
+
96
+ return {
97
+ virtualizer: entry.virtualizer,
98
+ release() {
99
+ if (done) return
100
+ done = true
101
+
102
+ const current = cache.get(resolved.key)
103
+
104
+ if (!current) return
105
+
106
+ current.refs -= 1
107
+
108
+ if (current.refs > 0) return
109
+
110
+ current.virtualizer.cleanUp()
111
+ cache.delete(resolved.key)
112
+ },
113
+ }
114
+ }
@@ -0,0 +1,46 @@
1
+ import { WorkerPoolManager } from "@pierre/diffs/worker"
2
+ import DiffsWorkerUrl from "@pierre/diffs/worker/worker.js?worker&url"
3
+ import { highlightOptions } from "./index"
4
+
5
+ function createPool(lineDiffType: "none" | "word-line") {
6
+ const pool = new WorkerPoolManager(
7
+ {
8
+ workerFactory: () => new Worker(DiffsWorkerUrl, { type: "module" }),
9
+ // poolSize defaults to 8. More workers = more parallelism but
10
+ // also more memory. Too many can actually slow things down.
11
+ // NOTE: 2 is probably better for OpenCode, as I think 8 might be
12
+ // a bit overkill, especially because Safari has a significantly slower
13
+ // boot up time for workers
14
+ poolSize: 2,
15
+ },
16
+ {
17
+ ...highlightOptions,
18
+ lineDiffType,
19
+ // Pierre renders with the pool's options, not the viewer's, whenever the pool works. The "none" pool only
20
+ // serves diffs above the large-file threshold, so it carries the plain-text fallback the viewer requests.
21
+ ...(lineDiffType === "none" && { maxLineDiffLength: 0, tokenizeMaxLineLength: 1 }),
22
+ },
23
+ )
24
+
25
+ void pool.initialize()
26
+
27
+ return pool
28
+ }
29
+
30
+ let plain: WorkerPoolManager | undefined
31
+
32
+ let diff: WorkerPoolManager | undefined
33
+
34
+ export function getWorkerPool(lineDiffType: "none" | "word-line" = "word-line"): WorkerPoolManager | undefined {
35
+ if (typeof window === "undefined") return
36
+
37
+ if (lineDiffType === "none") {
38
+ if (!plain) plain = createPool("none")
39
+
40
+ return plain
41
+ }
42
+
43
+ if (!diff) diff = createPool("word-line")
44
+
45
+ return diff
46
+ }
@@ -0,0 +1,59 @@
1
+ import { motionTuning } from "@zyro-sec/ui/motion-tuning"
2
+ import { createStore } from "solid-js/store"
3
+
4
+ export type SiteIconMode = "letters" | "site"
5
+
6
+ const enabled = motionTuning.toggle("sources.enabled", {
7
+ label: "Enabled",
8
+ group: "Experiments",
9
+ subsection: "Sources",
10
+ description: "Pages a turn searched and read",
11
+ target: '[data-component="sources-chip"]',
12
+ experiment: true,
13
+ value: false,
14
+ })
15
+
16
+ // A site's own icon is fetched from that site, which tells it the page was looked at; letters stay on this device.
17
+ const icons = motionTuning.choice("sources.icons", {
18
+ label: "Site icons",
19
+ group: "Experiments",
20
+ subsection: "Sources",
21
+ when: enabled,
22
+ value: "letters",
23
+ labels: { letters: "Letters", site: "Site icon" },
24
+ })
25
+
26
+ /** The chip fading in as its turn finishes. */
27
+ export const sourcesFade = motionTuning.spring("sources.fade", {
28
+ label: "Fade in",
29
+ group: "Experiments",
30
+ subsection: "Sources",
31
+ when: enabled,
32
+ shipped: "smooth",
33
+ presets: {
34
+ snappy: { duration: 0.2, bounce: 0 },
35
+ smooth: { duration: 0.3, bounce: 0 },
36
+ calm: { duration: 0.45, bounce: 0 },
37
+ },
38
+ })
39
+
40
+ /** A search opening to its results, and closing again. */
41
+ export const sourcesExpand = motionTuning.spring("sources.expand", {
42
+ label: "Expand",
43
+ group: "Experiments",
44
+ subsection: "Sources",
45
+ when: enabled,
46
+ shipped: "smooth",
47
+ presets: {
48
+ snappy: { duration: 0.25, bounce: 0 },
49
+ smooth: { duration: 0.35, bounce: 0 },
50
+ calm: { duration: 0.45, bounce: 0 },
51
+ },
52
+ })
53
+
54
+ const [state, setState] = createStore({ enabled: enabled(), icons: icons() })
55
+
56
+ motionTuning.subscribe(() => setState({ enabled: enabled(), icons: icons() }))
57
+
58
+ /** Experiment: a "N sources" chip under a reply that searched the web, opening the turn's searches in the side panel. */
59
+ export const sourcesExperiment = state
@@ -0,0 +1,53 @@
1
+ import { createSignal, Show } from "solid-js"
2
+ import type { SiteIconMode } from "./experiment"
3
+
4
+ // The project avatar colours, so a site's tile reads like the rest of the app's letter tiles in light and dark.
5
+ const colors = ["orange", "yellow", "cyan", "green", "red", "pink", "blue", "purple"]
6
+
7
+ // Hosts whose favicon failed this session, so every later tile for them shows its letter at once.
8
+ const failed = new Set<string>()
9
+
10
+ /** A site's letter tile, or with `site` its own favicon over the tile, falling back to the letter if that fails. */
11
+ export function SiteIcon(props: { site: string; host: string; mode: SiteIconMode }) {
12
+ const [state, setState] = createSignal<"loading" | "loaded" | "failed">(failed.has(props.host) ? "failed" : "loading")
13
+ const favicon = () => props.mode === "site" && state() !== "failed"
14
+
15
+ return (
16
+ <span
17
+ data-component="site-icon"
18
+ data-state={favicon() ? state() : undefined}
19
+ aria-hidden="true"
20
+ style={{
21
+ "--site-icon-bg": `var(--v2-avatar-bg-${colors[hash(props.site) % colors.length]})`,
22
+ }}
23
+ >
24
+ <Show when={favicon()} fallback={letter(props.site)}>
25
+ <img
26
+ data-slot="site-icon-image"
27
+ src={`https://${props.host}/favicon.ico`}
28
+ alt=""
29
+ draggable={false}
30
+ referrerPolicy="no-referrer"
31
+ onLoad={(event) => {
32
+ if (event.currentTarget.naturalWidth > 0) return setState("loaded")
33
+ failed.add(props.host)
34
+ setState("failed")
35
+ }}
36
+ onError={() => {
37
+ failed.add(props.host)
38
+ setState("failed")
39
+ }}
40
+ />
41
+ </Show>
42
+ </span>
43
+ )
44
+ }
45
+
46
+ function letter(site: string) {
47
+ return Array.from(site)[0]?.toUpperCase() ?? ""
48
+ }
49
+
50
+ // FNV-1a, so a site keeps its colour everywhere it shows.
51
+ function hash(value: string) {
52
+ return Array.from(value).reduce((sum, char) => Math.imul(sum ^ char.codePointAt(0)!, 16777619) >>> 0, 2166136261)
53
+ }
@@ -0,0 +1,57 @@
1
+ import { useI18n } from "@zyro-sec/ui/context/i18n"
2
+ import { Icon } from "@zyro-sec/ui/icon"
3
+ import { frameLoop, prefersReducedMotion, Spring } from "@zyro-sec/ui/motion"
4
+ import { createMemo, For, onCleanup, onMount, Show } from "solid-js"
5
+ import { sourcesFade, type SiteIconMode } from "./experiment"
6
+ import { SiteIcon } from "./site-icon"
7
+ import type { TurnSources } from "./sources"
8
+ import "./sources.css"
9
+
10
+ /**
11
+ * "N sources" under a reply, with the first three sites stacked. `appear` fades it in, for a turn that finished while
12
+ * it was on screen; a turn that was already done shows it at rest.
13
+ */
14
+ export function SourcesChip(props: { sources: TurnSources; icons: SiteIconMode; appear: boolean; onOpen: () => void }) {
15
+ const i18n = useI18n()
16
+ let button: HTMLButtonElement | undefined
17
+
18
+ const sites = createMemo(() =>
19
+ props.sources.sources
20
+ .filter((source, index, all) => all.findIndex((item) => item.site === source.site) === index)
21
+ .slice(0, 3),
22
+ )
23
+
24
+ onMount(() => {
25
+ if (!props.appear || !button) return
26
+ const el = button
27
+ const spring = Spring.fromDuration(0, prefersReducedMotion() ? { duration: 0.15, bounce: 0 } : sourcesFade())
28
+ spring.set(1)
29
+ el.style.opacity = "0"
30
+
31
+ onCleanup(
32
+ frameLoop.add((dt) => {
33
+ el.style.opacity = String(spring.step(dt))
34
+
35
+ if (!spring.resting) return true
36
+ el.style.opacity = ""
37
+
38
+ return false
39
+ }),
40
+ )
41
+ })
42
+
43
+ return (
44
+ <button ref={button} type="button" data-component="sources-chip" onClick={() => props.onOpen()}>
45
+ <span data-slot="sources-chip-icons">
46
+ <Show when={props.sources.sources.length > 0} fallback={<Icon name="globe" />}>
47
+ <For each={sites()}>{(source) => <SiteIcon site={source.site} host={source.host} mode={props.icons} />}</For>
48
+ </Show>
49
+ </span>
50
+ <span data-slot="sources-chip-label">
51
+ {props.sources.sources.length > 0
52
+ ? i18n.plural("ui.sources.count", props.sources.sources.length)
53
+ : i18n.t("ui.sources.none")}
54
+ </span>
55
+ </button>
56
+ )
57
+ }
@@ -0,0 +1,101 @@
1
+ import type { DurationOptions } from "@zyro-sec/ui/motion"
2
+
3
+ /** Reduced motion trades the height for a short fade. */
4
+ export const SOURCES_FADE_CURVE: DurationOptions = Object.freeze({ duration: 0.15, bounce: 0 })
5
+
6
+ /**
7
+ * A search step's results: the region springs its height open and closed, or fades in under reduced motion. The
8
+ * spring itself belongs to the driver, which steps it each frame without dispatching and reports `settled` once it
9
+ * rests. `reversed` means the flight turned around mid-way, so the spring keeps its position and velocity.
10
+ */
11
+ export type SourcesExpandState =
12
+ | { readonly status: "closed"; readonly attempt: number }
13
+ | { readonly status: "open"; readonly attempt: number }
14
+ | {
15
+ readonly status: "opening" | "closing"
16
+ readonly attempt: number
17
+ readonly mode: "height" | "fade"
18
+ readonly reversed: boolean
19
+ }
20
+ | { readonly status: "disposed" }
21
+
22
+ export type SourcesExpandEvent =
23
+ | { readonly type: "toggle"; readonly expandable: boolean; readonly reduced: boolean }
24
+ | { readonly type: "settled"; readonly attempt: number }
25
+ | { readonly type: "detached"; readonly attempt: number }
26
+ | { readonly type: "reduced" }
27
+ | { readonly type: "collapse" }
28
+ | { readonly type: "disposed" }
29
+
30
+ export const INITIAL_SOURCES_EXPAND_STATE: SourcesExpandState = Object.freeze({ status: "closed", attempt: 0 })
31
+
32
+ export const DISPOSED_SOURCES_EXPAND_STATE: SourcesExpandState = Object.freeze({ status: "disposed" })
33
+
34
+ export const REDUCED_SOURCES_EXPAND_EVENT: SourcesExpandEvent = Object.freeze({ type: "reduced" })
35
+
36
+ export const COLLAPSE_SOURCES_EXPAND_EVENT: SourcesExpandEvent = Object.freeze({ type: "collapse" })
37
+
38
+ export const DISPOSED_SOURCES_EXPAND_EVENT: SourcesExpandEvent = Object.freeze({ type: "disposed" })
39
+
40
+ type Moving = Extract<SourcesExpandState, { status: "opening" | "closing" }>
41
+
42
+ export function transitionSourcesExpand(state: SourcesExpandState, event: SourcesExpandEvent): SourcesExpandState {
43
+ if (state.status === "disposed") return state
44
+
45
+ if (event.type === "disposed") return DISPOSED_SOURCES_EXPAND_STATE
46
+
47
+ if (event.type === "toggle") return toggle(state, event)
48
+
49
+ if (event.type === "collapse")
50
+ return state.status === "closed" ? state : { status: "closed", attempt: state.attempt + 1 }
51
+
52
+ if (state.status !== "opening" && state.status !== "closing") return state
53
+
54
+ if (event.type === "reduced") return land(state)
55
+
56
+ if (event.attempt !== state.attempt) return state
57
+
58
+ // A spring at rest, or results that left the document and cannot be stepped: settle where the step was heading.
59
+ return land(state)
60
+ }
61
+
62
+ export function isSourcesStepOpen(state: SourcesExpandState) {
63
+ return state.status === "opening" || state.status === "open"
64
+ }
65
+
66
+ export function isSourcesStepMounted(state: SourcesExpandState) {
67
+ return state.status === "opening" || state.status === "open" || state.status === "closing"
68
+ }
69
+
70
+ export function isSourcesStepMoving(state: SourcesExpandState): state is Moving {
71
+ return state.status === "opening" || state.status === "closing"
72
+ }
73
+
74
+ function toggle(
75
+ state: Exclude<SourcesExpandState, { status: "disposed" }>,
76
+ event: Extract<SourcesExpandEvent, { type: "toggle" }>,
77
+ ): SourcesExpandState {
78
+ const attempt = state.attempt + 1
79
+
80
+ if (state.status === "closed" || state.status === "closing") {
81
+ if (!event.expandable) return state
82
+
83
+ if (event.reduced) return { status: "opening", attempt, mode: "fade", reversed: false }
84
+
85
+ return { status: "opening", attempt, mode: "height", reversed: state.status === "closing" }
86
+ }
87
+
88
+ if (event.reduced) return { status: "closed", attempt }
89
+
90
+ // Closing mid-way through a height flight turns the same spring around; a fade has no height to reverse.
91
+ return {
92
+ status: "closing",
93
+ attempt,
94
+ mode: "height",
95
+ reversed: state.status === "opening" && state.mode === "height",
96
+ }
97
+ }
98
+
99
+ function land(state: Moving): SourcesExpandState {
100
+ return { status: state.status === "opening" ? "open" : "closed", attempt: state.attempt }
101
+ }
@@ -0,0 +1,249 @@
1
+ import { useI18n } from "@zyro-sec/ui/context/i18n"
2
+ import { Icon } from "@zyro-sec/ui/icon"
3
+ import {
4
+ frameLoop,
5
+ observeReducedMotion,
6
+ prefersReducedMotion,
7
+ Spring,
8
+ type DurationOptions,
9
+ } from "@zyro-sec/ui/motion"
10
+ import { createEffect, createMemo, createSignal, For, Match, onCleanup, Show, Switch } from "solid-js"
11
+ import { sourcesExpand, type SiteIconMode } from "./experiment"
12
+ import {
13
+ COLLAPSE_SOURCES_EXPAND_EVENT,
14
+ DISPOSED_SOURCES_EXPAND_EVENT,
15
+ INITIAL_SOURCES_EXPAND_STATE,
16
+ isSourcesStepMounted,
17
+ isSourcesStepMoving,
18
+ isSourcesStepOpen,
19
+ REDUCED_SOURCES_EXPAND_EVENT,
20
+ SOURCES_FADE_CURVE,
21
+ transitionSourcesExpand,
22
+ type SourcesExpandEvent,
23
+ } from "./sources-expand-machine"
24
+ import { SiteIcon } from "./site-icon"
25
+ import type { SourceResult, SourceStep, TurnSources } from "./sources"
26
+ import "./sources.css"
27
+
28
+ /** A turn's searches and opened pages in the order they ran. `onOpen` opens a result's or page's URL. */
29
+ export function SourcesPanel(props: { sources: TurnSources; icons: SiteIconMode; onOpen: (url: string) => void }) {
30
+ return (
31
+ <div data-component="sources-panel">
32
+ <For each={props.sources.steps}>
33
+ {(step) => (
34
+ <Switch>
35
+ <Match when={step.type === "search" ? step : undefined}>
36
+ {(search) => <SearchStep step={search()} icons={props.icons} onOpen={props.onOpen} />}
37
+ </Match>
38
+ <Match when={step.type === "fetch" ? step : undefined}>
39
+ {(page) => <FetchStep step={page()} onOpen={props.onOpen} />}
40
+ </Match>
41
+ </Switch>
42
+ )}
43
+ </For>
44
+ </div>
45
+ )
46
+ }
47
+
48
+ function SearchStep(props: {
49
+ step: Extract<SourceStep, { type: "search" }>
50
+ icons: SiteIconMode
51
+ onOpen: (url: string) => void
52
+ }) {
53
+ const i18n = useI18n()
54
+ let current = INITIAL_SOURCES_EXPAND_STATE
55
+ const [state, setState] = createSignal(current)
56
+ const open = createMemo(() => isSourcesStepOpen(state()))
57
+ const mounted = createMemo(() => isSourcesStepMounted(state()))
58
+ const expandable = () => props.step.results.length > 0
59
+ let region: HTMLDivElement | undefined
60
+ let list: HTMLDivElement | undefined
61
+ let stopFrame: (() => void) | undefined
62
+ // Height in px, or opacity while fading. The driver owns it: frames step it in place and only its rest is an event.
63
+ const spring = new Spring(0)
64
+
65
+ const dispatch = (event: SourcesExpandEvent) => {
66
+ const previous = current
67
+ const next = transitionSourcesExpand(previous, event)
68
+
69
+ if (next === previous) return
70
+ current = next
71
+
72
+ // Every move out of a flight is a new attempt or a landing, so the old frame job always stops here.
73
+ if (isSourcesStepMoving(previous)) {
74
+ stopFrame?.()
75
+ stopFrame = undefined
76
+
77
+ if (region) delete region.dataset.moving
78
+
79
+ if (list) list.style.opacity = ""
80
+ }
81
+
82
+ if (next.status === "disposed") return
83
+ setState(next)
84
+
85
+ if (!isSourcesStepMoving(next)) {
86
+ if (region) region.style.height = next.status === "open" ? "" : "0px"
87
+
88
+ return
89
+ }
90
+
91
+ if (!region?.isConnected || !list?.isConnected) return dispatch({ type: "detached", attempt: next.attempt })
92
+
93
+ if (next.mode === "fade") {
94
+ tune(SOURCES_FADE_CURVE)
95
+ spring.snap(0).set(1)
96
+ region.style.height = ""
97
+ list.style.opacity = "0"
98
+ }
99
+
100
+ // Layout is read once per flight, never per frame: where a resting region starts, and the results' height now
101
+ // that they render. A reversed flight keeps its spring's position and velocity.
102
+ if (next.mode === "height") {
103
+ tune(sourcesExpand())
104
+
105
+ if (!next.reversed) spring.snap(region.offsetHeight)
106
+ spring.set(next.status === "opening" ? list.offsetHeight : 0)
107
+ region.dataset.moving = ""
108
+ region.style.height = `${spring.value}px`
109
+ }
110
+
111
+ if (spring.resting) return dispatch({ type: "settled", attempt: next.attempt })
112
+ stopFrame = frameLoop.add(tick)
113
+ }
114
+
115
+ const tune = (curve: DurationOptions) => {
116
+ spring.omega = (2 * Math.PI) / curve.duration
117
+ spring.zeta = 1 - curve.bounce
118
+ }
119
+
120
+ // Allocation-free apart from the style string: no event and no state until the spring rests.
121
+ const tick = (dt: number) => {
122
+ const moving = current
123
+
124
+ if (!isSourcesStepMoving(moving)) return false
125
+
126
+ if (!region?.isConnected || !list?.isConnected) {
127
+ dispatch({ type: "detached", attempt: moving.attempt })
128
+
129
+ return false
130
+ }
131
+
132
+ const value = spring.step(dt)
133
+
134
+ if (spring.resting) {
135
+ dispatch({ type: "settled", attempt: moving.attempt })
136
+
137
+ return false
138
+ }
139
+
140
+ if (moving.mode === "fade") list.style.opacity = String(value)
141
+
142
+ if (moving.mode === "height") region.style.height = `${Math.max(0, value)}px`
143
+
144
+ return true
145
+ }
146
+
147
+ createEffect(() => {
148
+ if (!expandable()) dispatch(COLLAPSE_SOURCES_EXPAND_EVENT)
149
+ })
150
+
151
+ onCleanup(
152
+ observeReducedMotion(() => {
153
+ if (prefersReducedMotion()) dispatch(REDUCED_SOURCES_EXPAND_EVENT)
154
+ }),
155
+ )
156
+
157
+ onCleanup(() => dispatch(DISPOSED_SOURCES_EXPAND_EVENT))
158
+
159
+ const toggle = () => dispatch({ type: "toggle", expandable: expandable(), reduced: prefersReducedMotion() })
160
+
161
+ return (
162
+ <div data-slot="sources-step" data-open={open() ? "" : undefined}>
163
+ <button
164
+ type="button"
165
+ data-slot="sources-step-row"
166
+ aria-expanded={expandable() ? open() : undefined}
167
+ disabled={!expandable()}
168
+ onClick={toggle}
169
+ >
170
+ <span data-slot="sources-step-icon">
171
+ <Icon name="magnifying-glass" />
172
+ </span>
173
+ <span data-slot="sources-step-text">
174
+ <span data-slot="sources-step-label">
175
+ {i18n.t(props.step.status === "error" ? "ui.sources.searchFailed" : "ui.sources.searched")}
176
+ </span>
177
+ <span data-slot="sources-step-detail">{props.step.query}</span>
178
+ <Show when={props.step.error}>{(error) => <span data-slot="sources-step-error">{error()}</span>}</Show>
179
+ </span>
180
+ <Show when={props.step.status === "completed"}>
181
+ <span data-slot="sources-step-count" data-empty={expandable() ? undefined : ""}>
182
+ {expandable() ? props.step.results.length : i18n.t("ui.sources.noResults")}
183
+ </span>
184
+ </Show>
185
+ </button>
186
+ <div ref={region} data-slot="sources-step-results" style={{ height: "0px" }}>
187
+ <div ref={list} data-slot="sources-step-list">
188
+ <Show when={mounted()}>
189
+ <For each={props.step.results}>
190
+ {(result) => <ResultRow result={result} icons={props.icons} onOpen={props.onOpen} />}
191
+ </For>
192
+ </Show>
193
+ </div>
194
+ </div>
195
+ </div>
196
+ )
197
+ }
198
+
199
+ function ResultRow(props: { result: SourceResult; icons: SiteIconMode; onOpen: (url: string) => void }) {
200
+ return (
201
+ <a
202
+ data-slot="sources-result"
203
+ href={props.result.url}
204
+ title={props.result.url}
205
+ onClick={(event) => {
206
+ event.preventDefault()
207
+ props.onOpen(props.result.url)
208
+ }}
209
+ >
210
+ <SiteIcon site={props.result.site} host={props.result.host} mode={props.icons} />
211
+ <span data-slot="sources-result-text">
212
+ <span data-slot="sources-result-title">{props.result.title ?? props.result.url}</span>
213
+ <span data-slot="sources-result-site">{props.result.site}</span>
214
+ </span>
215
+ </a>
216
+ )
217
+ }
218
+
219
+ function FetchStep(props: { step: Extract<SourceStep, { type: "fetch" }>; onOpen: (url: string) => void }) {
220
+ const i18n = useI18n()
221
+
222
+ return (
223
+ <div data-slot="sources-step">
224
+ <a
225
+ data-slot="sources-step-row"
226
+ href={props.step.url}
227
+ title={props.step.url}
228
+ onClick={(event) => {
229
+ event.preventDefault()
230
+ props.onOpen(props.step.url)
231
+ }}
232
+ >
233
+ <span data-slot="sources-step-icon">
234
+ <Icon name="select-element" />
235
+ </span>
236
+ <span data-slot="sources-step-text">
237
+ <span data-slot="sources-step-label">
238
+ {i18n.t(props.step.status === "error" ? "ui.sources.openFailed" : "ui.sources.opened")}
239
+ </span>
240
+ <span data-slot="sources-step-detail">
241
+ {props.step.site}
242
+ <span data-slot="sources-step-path">{props.step.path}</span>
243
+ </span>
244
+ <Show when={props.step.error}>{(error) => <span data-slot="sources-step-error">{error()}</span>}</Show>
245
+ </span>
246
+ </a>
247
+ </div>
248
+ )
249
+ }