Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Show only the latest Slack messages in inbox items (#1683)

Joshua Francea9a7931ebce9
diff --git a/plugins/web-ui/src/inbox.ts b/plugins/web-ui/src/inbox.ts
index 7a4be5c6b..975ee9796 100644
--- a/plugins/web-ui/src/inbox.ts
+++ b/plugins/web-ui/src/inbox.ts
@@ -16,6 +16,7 @@ import {
} from "./sent-mail";
import { html, nothing, render, type TemplateResult } from "lit";
import { live } from "lit/directives/live.js";
+import { ref } from "lit/directives/ref.js";
import {
Archive,
ArrowUpRight,
@@ -1156,6 +1157,36 @@ function itemImagesTpl(item: InboxItem, urls: string[] | undefined, ctxIndex: nu
</div>`;
}
+export const CONTEXT_TAIL = 6;
+const CONTEXT_PAGE = 20;
+const revealedContext = new Map<string, number>();
+
+function revealEarlier(item: InboxItem, marker: Element): void {
+ const scroller = marker.closest<HTMLElement>(".inbox-scroll");
+ const before = scroller ? scroller.scrollHeight - scroller.scrollTop : 0;
+ revealedContext.set(item.id, (revealedContext.get(item.id) ?? 0) + CONTEXT_PAGE);
+ drawAll();
+ if (scroller) scroller.scrollTop = scroller.scrollHeight - before;
+}
+
+const watchedEarlier = new WeakSet<Element>();
+
+function watchEarlier(item: InboxItem, el: Element | undefined): void {
+ const scroller = el?.closest<HTMLElement>(".inbox-scroll");
+ if (!el || !scroller || watchedEarlier.has(el) || typeof IntersectionObserver === "undefined") return;
+ watchedEarlier.add(el);
+ let userScrolled = false;
+ const markScrolled = (): void => void (userScrolled = true);
+ scroller.addEventListener("wheel", markScrolled, { passive: true });
+ scroller.addEventListener("touchmove", markScrolled, { passive: true });
+ const observer = new IntersectionObserver((entries) => {
+ if (!el.isConnected) return observer.disconnect();
+ if (!userScrolled || !entries.some((entry) => entry.isIntersecting)) return;
+ revealEarlier(item, el);
+ });
+ observer.observe(el);
+}
+
export function contextTpl(item: InboxItem): TemplateResult | typeof nothing {
const context = (item.context ?? []).map((message, index) => ({ ...message, imageIndex: index }));
const rows = item.sourceContextFetched
@@ -1172,10 +1203,22 @@ export function contextTpl(item: InboxItem): TemplateResult | typeof nothing {
nearby: false,
},
];
+ const hidden = Math.max(0, rows.length - CONTEXT_TAIL - (revealedContext.get(item.id) ?? 0));
return html`<div class="inbox-context">
+ ${
+ hidden
+ ? html`<button
+ class="inbox-source-notice inbox-earlier"
+ ${ref((el) => watchEarlier(item, el))}
+ @click=${(event: MouseEvent) => revealEarlier(item, event.currentTarget as Element)}
+ >
+ ${hidden === 1 ? "1 earlier message" : `${hidden} earlier messages`}
+ </button>`
+ : nothing
+ }
${item.sourceRefreshError ? html`<div class="inbox-source-notice" role="status">${item.sourceRefreshError}</div>` : nothing}
${item.sourceContextPartial ? html`<div class="inbox-source-notice">Showing part of this conversation. Open in Slack for the full history.</div>` : nothing}
- ${rows.map((m) => {
+ ${rows.slice(hidden).map((m) => {
const name = participantName(m.author) || m.author;
return html`
<div class="inbox-context-msg ${m.reply ? "inbox-thread-reply" : ""} ${m.nearby ? "inbox-nearby-context" : ""}">
diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.css
index 0650bb821..06f1eaa32 100644
--- a/plugins/web-ui/src/shell.css
+++ b/plugins/web-ui/src/shell.css
@@ -12993,6 +12993,17 @@ body.app-edit-embed .custom-chat-shell.app-edit-chat > .chat-bottom-dock {
font-size: 12px;
padding: 8px 0;
}
+.inbox-earlier {
+ align-self: center;
+ background: none;
+ border: 0;
+ cursor: pointer;
+ font: inherit;
+ font-size: 12px;
+}
+.inbox-earlier:hover {
+ color: var(--foreground);
+}
.inline-steer {
margin-block: 16px;
diff --git a/plugins/web-ui/test/inbox-context-tail.test.ts b/plugins/web-ui/test/inbox-context-tail.test.ts
new file mode 100644
index 000000000..a200b8438
--- /dev/null
+++ b/plugins/web-ui/test/inbox-context-tail.test.ts
@@ -0,0 +1,71 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+import { JSDOM } from "jsdom";
+import { createServer } from "vite";
+
+test("long Slack conversations show the latest messages and reveal earlier ones on request", async () => {
+ const dom = new JSDOM('<!doctype html><div id="app"></div><main id="main"></main>', {
+ url: "http://localhost/web-ui/",
+ });
+ Object.defineProperty(dom.window, "matchMedia", {
+ value: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }),
+ });
+ for (const key of ["window", "document", "location", "history", "localStorage", "navigator", "HTMLElement", "Node"])
+ Object.defineProperty(globalThis, key, {
+ configurable: true,
+ value: key === "window" ? dom.window : dom.window[key as keyof typeof dom.window],
+ });
+ const vite = await createServer({ server: { middlewareMode: true, hmr: false }, appType: "custom" });
+ try {
+ await vite.ssrLoadModule("/src/shell.ts");
+ const { contextTpl, toInboxItem, CONTEXT_TAIL } = await vite.ssrLoadModule("/src/inbox.ts");
+ const { render } = await vite.ssrLoadModule("lit");
+ const host = dom.window.document.getElementById("main")!;
+ const texts = (): string[] => [...host.querySelectorAll(".inbox-context-text")].map((el) => el.textContent!.trim());
+ const item = (id: string, count: number) =>
+ toInboxItem({
+ id,
+ loopId: "loop-1",
+ dedupeKey: id,
+ state: "held",
+ source: "slack",
+ sourcePayload: {
+ source: "slack",
+ title: "Conversation",
+ from: "Sam",
+ snippet: "latest",
+ sourceContextFetched: true,
+ context: Array.from({ length: count }, (_, i) => ({ author: "Alex", at: 1000 + i, text: `m${i}` })),
+ },
+ sourceAt: 2000,
+ updatedAt: 3000,
+ thread: [],
+ });
+
+ const short = item("short", CONTEXT_TAIL);
+ render(contextTpl(short), host);
+ assert.equal(texts().length, CONTEXT_TAIL);
+ assert.equal(host.querySelector(".inbox-earlier"), null);
+
+ const long = item("long", 60);
+ render(contextTpl(long), host);
+ assert.deepEqual(
+ texts(),
+ Array.from({ length: CONTEXT_TAIL }, (_, i) => `m${60 - CONTEXT_TAIL + i}`),
+ );
+ const earlier = host.querySelector<HTMLButtonElement>(".inbox-earlier")!;
+ assert.equal(earlier.textContent!.trim(), `${60 - CONTEXT_TAIL} earlier messages`);
+ earlier.click();
+ render(contextTpl(long), host);
+ assert.equal(texts().length, CONTEXT_TAIL + 20);
+ assert.equal(texts().at(-1), "m59");
+ host.querySelector<HTMLButtonElement>(".inbox-earlier")!.click();
+ host.querySelector<HTMLButtonElement>(".inbox-earlier")?.click();
+ render(contextTpl(long), host);
+ assert.equal(texts().length, 60);
+ assert.equal(texts()[0], "m0");
+ assert.equal(host.querySelector(".inbox-earlier"), null);
+ } finally {
+ await vite.close();
+ }
+});