iterate.com / git

git clone https://git.iterate.com/repos/git

Fold line threads in the conversation, and show a comment's range

In the conversation a thread that is read elsewhere is folded under one
line that says what it is: a thread on lines, which stands under its
lines in the diff, and a resolved one. A thread whose lines have
changed is nowhere in the diff, so it stays open there.

While several lines are chosen for a comment, each of them wears the
"+" that pierre shows beside one. And each posted comment on lines has
a thin line down the left of the lines it is on, so a comment on a
range shows its range. Both are a few rules handed to pierre's
component (`unsafeCSS`).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Misha Kaletsky, 2026-11-24 23:01 UTC · f5a0b95a3fc60e849013bd99902ef83353b9a7a0 · parent 4333e93

4 files changed

M pages.ts
@@ -390,15 +390,29 @@
     const outdated = Boolean(thread.place && now?.text !== thread.place.quote);
     return { ...thread, diff: now ? now.diff : null, outdated };
   });
+  // In the conversation a thread that is read elsewhere is folded under one line that says what
+  // it is: a thread on lines, which the script shows under its lines in the diff, and a resolved
+  // one. A thread whose lines have changed is nowhere in the diff, so it is open here.
   const conversation = threads.map((thread) => {
-    const place = thread.place
-      ? `<p class="place">${thread.diff === null ? "" : `<a href="#diff-${thread.diff}">`}<code>${esc(thread.place.path)}</code>${thread.diff === null ? "" : "</a>"} ${linesOf(thread.place)}${thread.outdated ? ", changed since" : ""}<code class="quote">${esc(thread.place.quote)}</code></p>`
-      : "";
-    const comments = thread.comments.map(
-      (comment) =>
-        `<div class="comment"><p class="by">${esc(comment.by)}, ${utcStamp(Date.parse(comment.at))}</p><pre>${esc(comment.body)}</pre></div>`,
-    );
-    return `<article class="thread" id="comment-${thread.id}">${place}${comments.join("")}${thread.resolved ? `<p class="by">Resolved.</p>` : ""}</article>`;
+    const { place } = thread;
+    const comments = thread.comments
+      .map(
+        (comment) =>
+          `<div class="comment"><p class="by">${esc(comment.by)}, ${utcStamp(Date.parse(comment.at))}</p><pre>${esc(comment.body)}</pre></div>`,
+      )
+      .join("");
+    const look = `class="thread${thread.resolved ? " resolved" : ""}" id="comment-${thread.id}"`;
+    if (!place && !thread.resolved) return `<article ${look}>${comments}</article>`;
+    const first = thread.comments[0]!;
+    const path = place ? `<code>${esc(place.path)}</code>` : "";
+    const what = place
+      ? `${thread.diff === null ? path : `<a href="#diff-${thread.diff}">${path}</a>`} ${linesOf(place)}`
+      : esc(`${first.by}: ${first.body.split("\n")[0]}`);
+    const count = `${thread.comments.length} comment${thread.comments.length === 1 ? "" : "s"}`;
+    const open = place && thread.outdated && !thread.resolved ? " open" : "";
+    const summary = `<summary class="place">${what}${thread.outdated ? ", changed since" : ""}${thread.resolved ? ", resolved" : ""} <span class="count">${count}</span></summary>`;
+    const quote = place ? `<code class="quote">${esc(place.quote)}</code>` : "";
+    return `<details ${look}${open}>${summary}${quote}${comments}</details>`;
   });
 
   const branch = `<code>${esc(pull.branch)}</code>`;
M public/page.css
@@ -80,10 +80,13 @@
   border-radius: 0.5rem; background: var(--background);
   font-family: var(--font-sans, system-ui, sans-serif); font-size: 0.8125rem; white-space: normal; }
 .thread.resolved { opacity: 0.6; }
-.thread .place { padding: 0.4rem 0.75rem; border-bottom: 1px solid var(--border);
-  color: var(--muted-foreground); font-size: 0.75rem; }
-.thread .quote { display: block; max-height: 7.5rem; margin-top: 0.25rem; overflow: auto;
-  white-space: pre; color: var(--foreground); }
+/* a thread folded under one line, in the conversation */
+.thread > .place { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.35rem;
+  padding: 0.4rem 0.75rem; cursor: pointer; color: var(--muted-foreground); font-size: 0.75rem; }
+.thread > .place .count { margin-left: auto; }
+.thread[open] > .place { border-bottom: 1px solid var(--border); }
+.thread .quote { display: block; max-height: 7.5rem; padding: 0.4rem 0.75rem; overflow: auto;
+  border-bottom: 1px solid var(--border); white-space: pre; }
 .comment { padding: 0.5rem 0.75rem; }
 .comment .by { margin-bottom: 0.25rem; font-size: 0.75rem; }
 .comment pre { white-space: pre-wrap; font-family: inherit; font-size: inherit; }
M public/page.js
@@ -146,41 +146,50 @@
 }
 
 /** One thread: what was said, and for a member the box that replies and the button that
- *  resolves. `listed` is the thread in the conversation, where it says which line it is on. */
+ *  resolves. `listed` is the thread in the conversation. There a thread that is read elsewhere is
+ *  folded under one line that says what it is: a thread on lines, which stands under its lines in
+ *  the diff, and a resolved one. A thread whose lines have changed is nowhere in the diff, so it
+ *  is open here. */
 function Thread({ thread, listed }) {
   const place = thread.place;
-  const where =
-    listed &&
-    place &&
-    html`<p className="place">
-      ${thread.diff === null ? html`<code>${place.path}</code>` : html`<a href=${"#diff-" + thread.diff}><code>${place.path}</code></a>`}
-      ${place.endLine > place.line ? " lines " + place.line + " to " + place.endLine : " line " + place.line}${thread.outdated ? ", changed since" : ""}
-      <code className="quote">${place.quote}</code>
-    </p>`;
+  const said = thread.comments.map(
+    (each) => html`<div className="comment" key=${each.id}>
+      <p className="by">${each.by}, ${stamp(each.at)}</p>
+      <${Markdown}>${each.body}<//>
+    </div>`,
+  );
   // the reply box opens under "Reply": a thread is read far more often than answered
   const writes =
     data.me &&
-    html`<details className="reply">
-      <summary>Reply</summary>
-      <${Form} button="Reply" send=${({ text }) => reply(thread, text)}>
-        <textarea name="text" placeholder="Reply" required></textarea>
-      <//>
-    </details>`;
-  const resolves =
-    data.me &&
-    !thread.resolved &&
-    html`<button type="button" className="resolve" onClick=${() => resolve(thread)}>Resolve</button>`;
-  return html`<article className=${thread.resolved ? "thread resolved" : "thread"} id=${listed ? "comment-" + thread.id : null}>
-    ${where}
-    ${thread.comments.map(
-      (each) => html`<div className="comment" key=${each.id}>
-        <p className="by">${each.by}, ${stamp(each.at)}</p>
-        <${Markdown}>${each.body}<//>
-      </div>`,
-    )}
-    ${thread.resolved && html`<p className="by comment">Resolved.</p>`}
-    ${data.me && html`<div className="actions">${writes} ${resolves}</div>`}
-  </article>`;
+    html`<div className="actions">
+      <details className="reply">
+        <summary>Reply</summary>
+        <${Form} button="Reply" send=${({ text }) => reply(thread, text)}>
+          <textarea name="text" placeholder="Reply" required></textarea>
+        <//>
+      </details>
+      ${!thread.resolved && html`<button type="button" className="resolve" onClick=${() => resolve(thread)}>Resolve</button>`}
+    </div>`;
+  const look = thread.resolved ? "thread resolved" : "thread";
+  if (!listed) return html`<article className=${look}>${said} ${writes}</article>`;
+  const anchor = "comment-" + thread.id;
+  if (!place && !thread.resolved) return html`<article className=${look} id=${anchor}>${said} ${writes}</article>`;
+
+  const first = thread.comments[0];
+  const path = place && html`<code>${place.path}</code>`;
+  const what = place
+    ? html`${thread.diff === null ? path : html`<a href=${"#diff-" + thread.diff}>${path}</a>`}
+        ${place.endLine > place.line ? " lines " + place.line + " to " + place.endLine : " line " + place.line}`
+    : first.by + ": " + first.body.split("\n")[0];
+  const count = thread.comments.length + (thread.comments.length === 1 ? " comment" : " comments");
+  const open = Boolean(place && thread.outdated && !thread.resolved);
+  return html`<details className=${look} id=${anchor} open=${open}>
+    <summary className="place">
+      ${what}${thread.outdated ? ", changed since" : ""}${thread.resolved ? ", resolved" : ""}
+      <span className="count">${count}</span>
+    </summary>
+    ${place && html`<code className="quote">${place.quote}</code>`} ${said} ${writes}
+  </details>`;
 }
 
 function Conversation() {
@@ -254,19 +263,57 @@
 // ── THE DIFFS ──
 
 let MultiFileDiff;
-/** Each diff's root, by its number on the page, once it is drawn; and what it is drawn from,
- *  kept so that a redraw hands pierre the same objects and it does not work the diff out again. */
+/** Each diff's root, by its number on the page, once it is drawn; and what it is drawn from. */
 const diffRoots = new Map();
 const diffProps = new Map();
 
+/** WHAT THE PAGE ADDS TO PIERRE'S OWN LOOK, inside its component (`unsafeCSS`).
+ *
+ *  Pierre shows its button beside one line. While several lines are chosen, each of them wears
+ *  the same mark, so the reader sees which lines the comment will be on.
+ *
+ *  And each posted comment on lines has a thin line down the left of the lines it is on, so a
+ *  comment on a range shows its range. `threadsHere` are the threads shown in this diff. */
+function lookOf(threadsHere) {
+  const chosen = `
+    [data-column-number][data-selected-line] { position: relative; }
+    [data-column-number][data-selected-line]::after { content: "+"; position: absolute; top: 0;
+      right: calc(1ch - 1lh); width: 1lh; height: 1lh; display: flex; align-items: center;
+      justify-content: center; border-radius: 4px; background: var(--diffs-modified-base);
+      color: var(--diffs-bg); font-weight: 600; z-index: 3; }
+  `;
+  // a line of the new side is an added line or an unchanged one, by its new number; a line of
+  // the old side is a deleted line, or an unchanged one by its old number
+  const rows = threadsHere.flatMap(({ place }) =>
+    Array.from({ length: place.endLine - place.line + 1 }, (_, at) => place.line + at).map((line) =>
+      place.side === "additions"
+        ? `[data-line="${line}"]:is([data-line-type="change-addition"], [data-line-type="context"])`
+        : `[data-line="${line}"][data-line-type="change-deletion"], [data-alt-line="${line}"][data-line-type="context"]`,
+    ),
+  );
+  const commented = rows.length
+    ? `${rows.join(", ")} { box-shadow: inset 3px 0 0 color-mix(in srgb, var(--diffs-modified-base) 55%, transparent); }`
+    : "";
+  return chosen + commented;
+}
+
 function drawDiff(index) {
   const { path, before, after } = data.diffs[index];
-  const fixed = diffProps.get(index) || {
+  const here = threads.filter((thread) => thread.diff === index && !thread.outdated && !thread.resolved);
+  // What pierre is drawn from is kept, so that a redraw hands it the same objects and it does
+  // not work the diff out again. Its options are made again only when the look changes.
+  const kept = diffProps.get(index) || {
     oldFile: { name: path, contents: before },
     newFile: { name: path, contents: after },
-    options: {
+    renderAnnotation: ({ metadata }) =>
+      metadata.thread ? html`<${Thread} thread=${metadata.thread} />` : html`<${Draft} draft=${metadata.draft} />`,
+  };
+  const unsafeCSS = lookOf(here);
+  if (!kept.options || kept.options.unsafeCSS !== unsafeCSS)
+    kept.options = {
       ...pierre,
       diffStyle: "unified",
+      unsafeCSS,
       // a member comments on lines of a pull: the button that pierre shows beside a line, which
       // a click takes for that line and a drag for every line it passes
       enableGutterUtility: Boolean(data.pull && data.me),
@@ -275,12 +322,8 @@
         drafts.set(index, draftOf(index, range));
         drawDiff(index);
       },
-    },
-    renderAnnotation: ({ metadata }) =>
-      metadata.thread ? html`<${Thread} thread=${metadata.thread} />` : html`<${Draft} draft=${metadata.draft} />`,
-  };
-  diffProps.set(index, fixed);
-  const here = threads.filter((thread) => thread.diff === index && !thread.outdated && !thread.resolved);
+    };
+  diffProps.set(index, kept);
   const draft = drafts.get(index);
   // a thread and a draft stand under the last of their lines
   const lineAnnotations = [
@@ -291,7 +334,7 @@
   // choice, and its button beside it, until it is told.
   const selectedLines = draft ? { start: draft.line, end: draft.endLine, side: draft.side, endSide: draft.side } : null;
   diffRoots.get(index).render(
-    html`<${MultiFileDiff} ...${fixed} lineAnnotations=${lineAnnotations} selectedLines=${selectedLines} />`,
+    html`<${MultiFileDiff} ...${kept} lineAnnotations=${lineAnnotations} selectedLines=${selectedLines} />`,
   );
 }
 
M tests/pulls.test.mjs
@@ -89,6 +89,12 @@
   // the conversation is in the page as it stands, with no script: who, when, and their words
   assert.ok(open.includes(`<p class="by">grace, 2026-11-24 12:02 UTC</p><pre>Looks right to me.</pre>`));
   assert.ok(open.includes(`<pre>Why not \`crimson\`?</pre>`));
+  // the thread on a line, resolved: folded under one line that says so
+  assert.ok(
+    open.includes(
+      `<details class="thread resolved" id="comment-3"><summary class="place"><a href="#diff-0"><code>header.css</code></a> line 1, resolved <span class="count">2 comments</span></summary>`,
+    ),
+  );
   assert.deepEqual(dataOf(open), {
     repo,
     me: "ada",
@@ -219,7 +225,12 @@
     { lines: [2, 3], diff: 0, outdated: false },
     { lines: [4, 4], diff: 0, outdated: false },
   ]);
-  assert.ok((await page()).includes(`</a> lines 2 to 3<code class="quote">  color: red;\n  margin: 0;</code>`));
+  // in the conversation a comment on lines is folded under one line: it is read in the diff
+  assert.ok(
+    (await page()).includes(
+      `<details class="thread" id="comment-2"><summary class="place"><a href="#diff-0"><code>header.css</code></a> lines 2 to 3 <span class="count">1 comment</span></summary><code class="quote">  color: red;\n  margin: 0;</code>`,
+    ),
+  );
 
   // a new commit on the branch changes one of the two lines, and leaves the last line as it was
   site.more({ on: "red-header", message: "Crimson", files: { "header.css": "header {\n  color: crimson;\n  margin: 0;\n}\n" } });
@@ -228,7 +239,12 @@
     { lines: [2, 3], diff: 0, outdated: true },
     { lines: [4, 4], diff: 0, outdated: false },
   ]);
-  assert.ok((await page()).includes(`</a> lines 2 to 3, changed since<code class="quote">  color: red;\n  margin: 0;</code>`));
+  // its lines have changed, so it is nowhere in the diff: here it is open, and says so
+  assert.ok(
+    (await page()).includes(
+      `<details class="thread" id="comment-2" open><summary class="place"><a href="#diff-0"><code>header.css</code></a> lines 2 to 3, changed since <span class="count">1 comment</span></summary>`,
+    ),
+  );
 });
 
 test("a squash is taken only while the branch has main's newest commit: when main has moved, the branch merges main in first", async () => {