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

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,35 @@
1
+ import { useDialog } from "@zyro-sec/ui/context/dialog"
2
+ import { useI18n } from "@zyro-sec/ui/context/i18n"
3
+ import { ImagePreview } from "@zyro-sec/ui/image-preview"
4
+
5
+ export function createImagePreview() {
6
+ const dialog = useDialog()
7
+ const i18n = useI18n()
8
+
9
+ const open = (event: MouseEvent | KeyboardEvent) => {
10
+ const image = event.currentTarget
11
+
12
+ if (!(image instanceof HTMLImageElement)) return
13
+ const src = image.currentSrc || image.getAttribute("src")
14
+
15
+ if (!src) return
16
+ const alt = image.alt || i18n.t("ui.imagePreview.alt")
17
+ event.preventDefault()
18
+ event.stopPropagation()
19
+ dialog.show(() => <ImagePreview src={src} alt={alt} origin={image} />)
20
+ }
21
+
22
+ return (root: HTMLElement) => {
23
+ root.querySelectorAll<HTMLImageElement>("img:not([data-markdown-favicon])").forEach((image) => {
24
+ image.setAttribute("role", "button")
25
+ image.setAttribute("aria-haspopup", "dialog")
26
+ image.setAttribute("aria-label", image.alt || i18n.t("ui.imagePreview.alt"))
27
+ image.tabIndex = 0
28
+ image.onclick = open
29
+ image.onkeydown = (event) => {
30
+ if (event.key !== "Enter" && event.key !== " ") return
31
+ open(event)
32
+ }
33
+ })
34
+ }
35
+ }
@@ -0,0 +1,580 @@
1
+ import { type DiffLineAnnotation, type SelectedLineRange } from "@pierre/diffs"
2
+ import { createEffect, createMemo, createSignal, onCleanup, Show, untrack, type Accessor, type JSX } from "solid-js"
3
+ import { render as renderSolid } from "solid-js/web"
4
+ import { useI18n } from "@zyro-sec/ui/context/i18n"
5
+ import { createHoverComment, type HoverComment, type HoverCommentLine } from "../pierre/comment-hover"
6
+ import { cloneSelectedLineRange, formatSelectedLineLabel, lineInSelectedRange } from "../pierre/selection-bridge"
7
+ import { LineComment, LineCommentEditor, type LineCommentEditorProps } from "./line-comment"
8
+ import {
9
+ IDLE_LINE_COMMENT_MODE,
10
+ transitionLineComment,
11
+ viewCommentItem,
12
+ viewLineComment,
13
+ type LineCommentEffect,
14
+ type LineCommentEvent,
15
+ type LineCommentMode,
16
+ } from "./line-comment-machine"
17
+
18
+ export type LineCommentAnnotationMeta<T> =
19
+ | { kind: "comment"; key: string; comment: T }
20
+ | { kind: "draft"; key: string; range: SelectedLineRange }
21
+
22
+ export type LineCommentAnnotation<T> = {
23
+ lineNumber: number
24
+ side?: "additions" | "deletions"
25
+ metadata: LineCommentAnnotationMeta<T>
26
+ }
27
+
28
+ type LineCommentAnnotationsProps<T> = {
29
+ comments: Accessor<T[]>
30
+ getCommentId: (comment: T) => string
31
+ getCommentSelection: (comment: T) => SelectedLineRange
32
+ draftRange: Accessor<SelectedLineRange | null>
33
+ draftKey: Accessor<string>
34
+ }
35
+
36
+ type LineCommentAnnotationsWithSideProps<T> = LineCommentAnnotationsProps<T> & {
37
+ getSide: (range: SelectedLineRange) => "additions" | "deletions"
38
+ }
39
+
40
+ /** A line-comment mode owned by the caller, such as one file's entry in a multi-file review. Omit it to keep the mode inside the controller. */
41
+ export type LineCommentMachine = {
42
+ mode: Accessor<LineCommentMode>
43
+ dispatch: (event: LineCommentEvent) => readonly LineCommentEffect[]
44
+ }
45
+
46
+ export type SavedLineComment = {
47
+ id: string
48
+ selection: SelectedLineRange
49
+ comment: string
50
+ }
51
+
52
+ type LineCommentDriverProps<T extends SavedLineComment> = {
53
+ machine?: LineCommentMachine
54
+ onSyncSelected?: (range: SelectedLineRange | null) => void
55
+ onSubmit: (input: { comment: string; selection: SelectedLineRange; send: boolean }) => void
56
+ onUpdate?: (input: { id: string; comment: string; selection: SelectedLineRange; send: boolean }) => void
57
+ onDelete?: (comment: T) => void
58
+ }
59
+
60
+ type LineCommentControllerProps<T extends SavedLineComment> = LineCommentDriverProps<T> & {
61
+ comments: Accessor<T[]>
62
+ draftKey: Accessor<string>
63
+ label: string
64
+ mention?: LineCommentEditorProps["mention"]
65
+ renderCommentActions?: (comment: T, controls: { edit: VoidFunction; remove: VoidFunction }) => JSX.Element
66
+ editSubmitLabel?: string
67
+ onDraftPopoverFocusOut?: (event: FocusEvent) => void
68
+ }
69
+
70
+ type LineCommentControllerWithSideProps<T extends SavedLineComment> = LineCommentControllerProps<T> & {
71
+ getSide: (range: SelectedLineRange) => "additions" | "deletions"
72
+ }
73
+
74
+ type CommentProps = {
75
+ id?: string
76
+ open: boolean
77
+ comment: JSX.Element
78
+ selection: JSX.Element
79
+ actions?: JSX.Element
80
+ editor?: DraftProps
81
+ onClick?: (event: MouseEvent) => void
82
+ onMouseEnter?: (event: MouseEvent) => void
83
+ }
84
+
85
+ type DraftProps = {
86
+ value: string
87
+ selection: JSX.Element
88
+ mention?: LineCommentEditorProps["mention"]
89
+ onInput: (value: string) => void
90
+ onCancel: VoidFunction
91
+ onSubmit: (value: string) => void
92
+ onPopoverFocusOut?: (event: FocusEvent) => void
93
+ cancelLabel?: string
94
+ submitLabel?: string
95
+ }
96
+
97
+ // Generic host machinery shared by the inline and panel annotation renderers: each
98
+ // annotation key gets a detached DOM host with its own Solid root, updated in
99
+ // place through a signal so Pierre can reparent the host without re-rendering.
100
+ export function createLineCommentAnnotationRenderer<T, C, D>(props: {
101
+ renderComment: (comment: T) => C
102
+ renderDraft: (range: SelectedLineRange) => D
103
+ commentElement: (view: Accessor<C>) => JSX.Element
104
+ draftElement: (view: Accessor<D>) => JSX.Element
105
+ }) {
106
+ const nodes = new Map<
107
+ string,
108
+ {
109
+ host: HTMLDivElement
110
+ dispose: VoidFunction
111
+ setMeta: (meta: LineCommentAnnotationMeta<T>) => void
112
+ }
113
+ >()
114
+
115
+ const mount = (meta: LineCommentAnnotationMeta<T>) => {
116
+ if (typeof document === "undefined") return
117
+
118
+ const host = document.createElement("div")
119
+ host.setAttribute("data-prevent-autofocus", "")
120
+ const [current, setCurrent] = createSignal(meta)
121
+
122
+ const dispose = renderSolid(() => {
123
+ const active = current()
124
+
125
+ if (active.kind === "comment") {
126
+ const view = createMemo(() => {
127
+ const next = current()
128
+
129
+ if (next.kind !== "comment") return props.renderComment(active.comment)
130
+
131
+ return props.renderComment(next.comment)
132
+ })
133
+
134
+ return props.commentElement(view)
135
+ }
136
+
137
+ const view = createMemo(() => {
138
+ const next = current()
139
+
140
+ if (next.kind !== "draft") return props.renderDraft(active.range)
141
+
142
+ return props.renderDraft(next.range)
143
+ })
144
+
145
+ return props.draftElement(view)
146
+ }, host)
147
+
148
+ const node = { host, dispose, setMeta: setCurrent }
149
+ nodes.set(meta.key, node)
150
+
151
+ return node
152
+ }
153
+
154
+ const render = <A extends { metadata: LineCommentAnnotationMeta<T> }>(annotation: A) => {
155
+ const meta = annotation.metadata
156
+ const node = nodes.get(meta.key) ?? mount(meta)
157
+
158
+ if (!node) return
159
+ node.setMeta(meta)
160
+
161
+ return node.host
162
+ }
163
+
164
+ const reconcile = <A extends { metadata: LineCommentAnnotationMeta<T> }>(annotations: A[]) => {
165
+ const next = new Set(annotations.map((annotation) => annotation.metadata.key))
166
+
167
+ for (const [key, node] of nodes) {
168
+ if (next.has(key)) continue
169
+ node.dispose()
170
+ nodes.delete(key)
171
+ }
172
+ }
173
+
174
+ const cleanup = () => {
175
+ for (const [, node] of nodes) node.dispose()
176
+ nodes.clear()
177
+ }
178
+
179
+ return { render, reconcile, cleanup }
180
+ }
181
+
182
+ function lineCommentElement(view: Accessor<CommentProps>) {
183
+ return (
184
+ <Show
185
+ when={view().editor}
186
+ fallback={
187
+ <LineComment
188
+ inline
189
+ id={view().id}
190
+ open={view().open}
191
+ comment={view().comment}
192
+ selection={view().selection}
193
+ actions={view().actions}
194
+ onClick={view().onClick}
195
+ onMouseEnter={view().onMouseEnter}
196
+ />
197
+ }
198
+ >
199
+ {(editor) => (
200
+ <LineCommentEditor
201
+ inline
202
+ id={view().id}
203
+ value={editor().value}
204
+ selection={editor().selection}
205
+ onInput={editor().onInput}
206
+ onCancel={editor().onCancel}
207
+ onSubmit={editor().onSubmit}
208
+ onPopoverFocusOut={editor().onPopoverFocusOut}
209
+ cancelLabel={editor().cancelLabel}
210
+ submitLabel={editor().submitLabel}
211
+ mention={editor().mention}
212
+ />
213
+ )}
214
+ </Show>
215
+ )
216
+ }
217
+
218
+ function lineCommentDraftElement(view: Accessor<DraftProps>) {
219
+ return (
220
+ <LineCommentEditor
221
+ inline
222
+ value={view().value}
223
+ selection={view().selection}
224
+ onInput={view().onInput}
225
+ onCancel={view().onCancel}
226
+ onSubmit={view().onSubmit}
227
+ onPopoverFocusOut={view().onPopoverFocusOut}
228
+ mention={view().mention}
229
+ />
230
+ )
231
+ }
232
+
233
+ // Runs a line-comment machine and turns its effects into the controller's callbacks.
234
+ export function createLineCommentDriver<T extends SavedLineComment>(props: LineCommentDriverProps<T>) {
235
+ const machine = props.machine ?? createLocalLineCommentMachine()
236
+
237
+ const send = (event: LineCommentEvent, comment?: T) => {
238
+ for (const effect of machine.dispatch(event)) {
239
+ if (effect.type === "sync-selected")
240
+ props.onSyncSelected?.(effect.range ? cloneSelectedLineRange(effect.range) : null)
241
+
242
+ if (effect.type === "submit-draft")
243
+ props.onSubmit({ comment: effect.comment, selection: effect.selection, send: effect.send })
244
+
245
+ if (effect.type === "submit-edit")
246
+ props.onUpdate?.({ id: effect.id, comment: effect.comment, selection: effect.selection, send: effect.send })
247
+
248
+ if (effect.type === "delete-comment" && comment) props.onDelete?.(comment)
249
+ }
250
+ }
251
+
252
+ return {
253
+ mode: machine.mode,
254
+ send,
255
+ view: () => viewLineComment(machine.mode()),
256
+ focusComment: (id: string, range: SelectedLineRange) => send({ type: "comment.focused", id, range }),
257
+ clearPassive: () => send({ type: "passive.cleared" }),
258
+ reset: () => send({ type: "reset" }),
259
+ }
260
+ }
261
+
262
+ function createLocalLineCommentMachine(): LineCommentMachine {
263
+ const [mode, setMode] = createSignal(IDLE_LINE_COMMENT_MODE)
264
+
265
+ return {
266
+ mode,
267
+ dispatch: (event) => {
268
+ const step = transitionLineComment(untrack(mode), event)
269
+ setMode(step.state)
270
+
271
+ return step.effects
272
+ },
273
+ }
274
+ }
275
+
276
+ type LineCommentController<T extends SavedLineComment, A> = {
277
+ mode: Accessor<LineCommentMode>
278
+ selected: Accessor<SelectedLineRange | null>
279
+ focusComment: (id: string, range: SelectedLineRange) => void
280
+ clearPassive: VoidFunction
281
+ reset: VoidFunction
282
+ annotations: Accessor<A[]>
283
+ renderAnnotation: ReturnType<
284
+ typeof createManagedLineCommentAnnotationRenderer<T, CommentProps, DraftProps>
285
+ >["renderAnnotation"]
286
+ renderGutterUtility: ReturnType<typeof createLineCommentGutterRenderer>
287
+ onLineSelected: (range: SelectedLineRange | null) => void
288
+ onLineSelectionEnd: (range: SelectedLineRange | null) => void
289
+ commentSelection: Accessor<((range: SelectedLineRange) => void) | undefined>
290
+ }
291
+
292
+ export function createLineCommentController<T extends SavedLineComment>(
293
+ props: LineCommentControllerWithSideProps<T>,
294
+ ): LineCommentController<T, DiffLineAnnotation<LineCommentAnnotationMeta<T>>>
295
+ export function createLineCommentController<T extends SavedLineComment>(
296
+ props: LineCommentControllerProps<T>,
297
+ ): LineCommentController<T, LineCommentAnnotation<T>>
298
+ export function createLineCommentController<T extends SavedLineComment>(
299
+ props: LineCommentControllerProps<T> | LineCommentControllerWithSideProps<T>,
300
+ ) {
301
+ const i18n = useI18n()
302
+ const driver = createLineCommentDriver(props)
303
+ const draftRange = () => driver.view().draftRange
304
+
305
+ const annotations =
306
+ "getSide" in props
307
+ ? createLineCommentAnnotations({
308
+ comments: props.comments,
309
+ getCommentId: (comment) => comment.id,
310
+ getCommentSelection: (comment) => comment.selection,
311
+ draftRange,
312
+ draftKey: props.draftKey,
313
+ getSide: props.getSide,
314
+ })
315
+ : createLineCommentAnnotations({
316
+ comments: props.comments,
317
+ getCommentId: (comment) => comment.id,
318
+ getCommentSelection: (comment) => comment.selection,
319
+ draftRange,
320
+ draftKey: props.draftKey,
321
+ })
322
+
323
+ const { renderAnnotation } = createManagedLineCommentAnnotationRenderer<T, CommentProps, DraftProps>({
324
+ annotations,
325
+ commentElement: lineCommentElement,
326
+ draftElement: lineCommentDraftElement,
327
+ renderComment: (comment) => {
328
+ const item = () => viewCommentItem(driver.mode(), comment.id)
329
+
330
+ const edit = () =>
331
+ driver.send({ type: "edit.opened", id: comment.id, range: comment.selection, text: comment.comment })
332
+
333
+ const remove = () => driver.send({ type: "comment.deleted", id: comment.id }, comment)
334
+
335
+ return {
336
+ id: comment.id,
337
+ get open() {
338
+ return item().open
339
+ },
340
+ comment: comment.comment,
341
+ selection: formatSelectedLineLabel(comment.selection, i18n.t),
342
+ get actions() {
343
+ return props.renderCommentActions?.(comment, { edit, remove })
344
+ },
345
+ get editor() {
346
+ return item().editing
347
+ ? {
348
+ get value() {
349
+ return item().editText
350
+ },
351
+ selection: formatSelectedLineLabel(comment.selection, i18n.t),
352
+ mention: props.mention,
353
+ onInput: (text: string) => driver.send({ type: "edit.changed", text }),
354
+ onCancel: () => driver.send({ type: "edit.cancelled" }),
355
+ onSubmit: (text: string) => driver.send({ type: "edit.submitted", text }),
356
+ submitLabel: props.editSubmitLabel,
357
+ }
358
+ : undefined
359
+ },
360
+ onMouseEnter: () => driver.send({ type: "comment.hovered", range: comment.selection }),
361
+ onClick: () => driver.send({ type: "comment.toggled", id: comment.id, range: comment.selection }),
362
+ }
363
+ },
364
+ renderDraft: (range) => ({
365
+ get value() {
366
+ return driver.view().draftText
367
+ },
368
+ selection: formatSelectedLineLabel(range, i18n.t),
369
+ mention: props.mention,
370
+ onInput: (text) => driver.send({ type: "draft.changed", text }),
371
+ onCancel: () => driver.send({ type: "draft.cancelled" }),
372
+ onSubmit: (text) => driver.send({ type: "draft.submitted", text }),
373
+ onPopoverFocusOut: props.onDraftPopoverFocusOut,
374
+ }),
375
+ })
376
+
377
+ return {
378
+ mode: driver.mode,
379
+ selected: () => driver.view().selected,
380
+ focusComment: driver.focusComment,
381
+ clearPassive: driver.clearPassive,
382
+ reset: driver.reset,
383
+ annotations,
384
+ renderAnnotation,
385
+ renderGutterUtility: createLineCommentGutterRenderer({
386
+ label: props.label,
387
+ getSelectedRange: () => driver.view().gutterSelected,
388
+ onOpenDraft: (range) => driver.send({ type: "draft.opened", range }),
389
+ }),
390
+ onLineSelected: (range: SelectedLineRange | null) => driver.send({ type: "line.selected", range }),
391
+ onLineSelectionEnd: (range: SelectedLineRange | null) => driver.send({ type: "line.selection-ended", range }),
392
+ /** Opens a draft on text selected in the code. Unset while a saved comment is edited, which keeps its range. */
393
+ commentSelection: () =>
394
+ driver.mode().status === "editing"
395
+ ? undefined
396
+ : (range: SelectedLineRange) => driver.send({ type: "draft.opened", range }),
397
+ }
398
+ }
399
+
400
+ export function createLineCommentAnnotations<T>(
401
+ props: LineCommentAnnotationsWithSideProps<T>,
402
+ ): Accessor<DiffLineAnnotation<LineCommentAnnotationMeta<T>>[]>
403
+ export function createLineCommentAnnotations<T>(
404
+ props: LineCommentAnnotationsProps<T>,
405
+ ): Accessor<LineCommentAnnotation<T>[]>
406
+ export function createLineCommentAnnotations<T>(
407
+ props: LineCommentAnnotationsProps<T> | LineCommentAnnotationsWithSideProps<T>,
408
+ ) {
409
+ const line = (range: SelectedLineRange) => Math.max(range.start, range.end)
410
+
411
+ if ("getSide" in props) {
412
+ return createMemo<DiffLineAnnotation<LineCommentAnnotationMeta<T>>[]>(
413
+ () => {
414
+ const list = props.comments().map((comment) => {
415
+ const range = props.getCommentSelection(comment)
416
+
417
+ return {
418
+ side: props.getSide(range),
419
+ lineNumber: line(range),
420
+ metadata: {
421
+ kind: "comment",
422
+ key: `comment:${props.getCommentId(comment)}`,
423
+ comment,
424
+ } satisfies LineCommentAnnotationMeta<T>,
425
+ }
426
+ })
427
+
428
+ const range = props.draftRange()
429
+
430
+ if (!range) return list
431
+
432
+ return [
433
+ ...list,
434
+ {
435
+ side: props.getSide(range),
436
+ lineNumber: line(range),
437
+ metadata: {
438
+ kind: "draft",
439
+ key: `draft:${props.draftKey()}`,
440
+ range,
441
+ } satisfies LineCommentAnnotationMeta<T>,
442
+ },
443
+ ]
444
+ },
445
+ [],
446
+ // Stable identity for unchanged annotations avoids no-op diff rerenders downstream.
447
+ { equals: sameAnnotationLists },
448
+ )
449
+ }
450
+
451
+ return createMemo<LineCommentAnnotation<T>[]>(
452
+ () => {
453
+ const list = props.comments().map((comment) => {
454
+ const range = props.getCommentSelection(comment)
455
+
456
+ const entry: LineCommentAnnotation<T> = {
457
+ lineNumber: line(range),
458
+ metadata: {
459
+ kind: "comment",
460
+ key: `comment:${props.getCommentId(comment)}`,
461
+ comment,
462
+ },
463
+ }
464
+
465
+ return entry
466
+ })
467
+
468
+ const range = props.draftRange()
469
+
470
+ if (!range) return list
471
+
472
+ const draft: LineCommentAnnotation<T> = {
473
+ lineNumber: line(range),
474
+ metadata: {
475
+ kind: "draft",
476
+ key: `draft:${props.draftKey()}`,
477
+ range,
478
+ },
479
+ }
480
+
481
+ return [...list, draft]
482
+ },
483
+ [],
484
+ { equals: sameAnnotationLists },
485
+ )
486
+ }
487
+
488
+ type AnnotationListItem = {
489
+ lineNumber: number
490
+ side?: unknown
491
+ metadata: { kind: string; key: string; comment?: unknown; range?: unknown }
492
+ }
493
+
494
+ function sameAnnotationLists(previous: AnnotationListItem[], next: AnnotationListItem[]) {
495
+ if (previous.length !== next.length) return false
496
+
497
+ return previous.every((item, index) => {
498
+ const other = next[index]!
499
+
500
+ return (
501
+ item.lineNumber === other.lineNumber &&
502
+ item.side === other.side &&
503
+ item.metadata.kind === other.metadata.kind &&
504
+ item.metadata.key === other.metadata.key &&
505
+ item.metadata.comment === other.metadata.comment &&
506
+ item.metadata.range === other.metadata.range
507
+ )
508
+ })
509
+ }
510
+
511
+ export function createManagedLineCommentAnnotationRenderer<T, C, D>(props: {
512
+ annotations: Accessor<LineCommentAnnotation<T>[]>
513
+ renderComment: (comment: T) => C
514
+ renderDraft: (range: SelectedLineRange) => D
515
+ commentElement: (view: Accessor<C>) => JSX.Element
516
+ draftElement: (view: Accessor<D>) => JSX.Element
517
+ }) {
518
+ const renderer = createLineCommentAnnotationRenderer<T, C, D>({
519
+ renderComment: props.renderComment,
520
+ renderDraft: props.renderDraft,
521
+ commentElement: props.commentElement,
522
+ draftElement: props.draftElement,
523
+ })
524
+
525
+ createEffect(() => {
526
+ renderer.reconcile(props.annotations())
527
+ })
528
+
529
+ onCleanup(() => {
530
+ renderer.cleanup()
531
+ })
532
+
533
+ return {
534
+ renderAnnotation: renderer.render,
535
+ }
536
+ }
537
+
538
+ export function createLineCommentGutterRenderer(props: {
539
+ label: string
540
+ getSelectedRange: Accessor<SelectedLineRange | null>
541
+ onOpenDraft: (range: SelectedLineRange) => void
542
+ }) {
543
+ let active: { comment: HoverComment; getHoveredLine: () => HoverCommentLine | undefined } | undefined
544
+
545
+ onCleanup(() => active?.comment.dispose())
546
+
547
+ // Pierre calls this on every render and rerender but inserts the result only when it holds no button yet, so hand
548
+ // back the same button for the same viewer instead of building one with fresh document listeners each time.
549
+ return (getHoveredLine: () => HoverCommentLine | undefined) => {
550
+ if (active?.getHoveredLine === getHoveredLine && active.comment.canReuse()) return active.comment.button
551
+ active?.comment.dispose()
552
+
553
+ const comment = createHoverComment({
554
+ label: props.label,
555
+ getHoveredLine,
556
+ isSelected: (hovered) => lineInSelectedRange(props.getSelectedRange(), hovered.lineNumber, hovered.side),
557
+ onSelect: (hovered) => {
558
+ const current = props.getSelectedRange()
559
+
560
+ if (current && lineInSelectedRange(current, hovered.lineNumber, hovered.side)) {
561
+ props.onOpenDraft(cloneSelectedLineRange(current))
562
+
563
+ return
564
+ }
565
+
566
+ const range: SelectedLineRange = {
567
+ start: hovered.lineNumber,
568
+ end: hovered.lineNumber,
569
+ }
570
+
571
+ if (hovered.side) range.side = hovered.side
572
+ props.onOpenDraft(range)
573
+ },
574
+ })
575
+
576
+ active = comment && { comment, getHoveredLine }
577
+
578
+ return comment?.button
579
+ }
580
+ }