Skip to content

Commit 9365ab9

Browse files
loganjLarry
andcommitted
fix(desktop): abbreviate bound keys in rendered mention chips
Use the member-list display formatter while retaining full literal labels and keys for profile targets, clipboard round trips, and recipient resolution. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent 2997bfb commit 9365ab9

12 files changed

Lines changed: 345 additions & 9 deletions

‎desktop/src/features/messages/lib/mentionClipboard.test.mjs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,25 @@ test("does not treat an uncapped label's ellipsis as a truncation", () => {
440440
// whole must not gain a second accepted form.
441441
assert.equal(matchChipTextToLabel("John…", "John Smith", "@"), "fragment");
442442
});
443+
444+
test("whole compact mention labels are restored only for their declared exact key", () => {
445+
const key = `150b20bd${"a".repeat(52)}15dc`;
446+
const label = `Scout (${key}) 2`;
447+
const compact = "Scout (150b20bd…15dc) 2";
448+
assert.equal(matchChipTextToLabel(compact, label, "@", key), "truncated");
449+
assert.equal(
450+
matchChipTextToLabel(`@${compact}`, label, "@", key),
451+
"truncated",
452+
);
453+
assert.equal(matchChipTextToLabel(compact, label, "@"), "fragment");
454+
assert.equal(
455+
matchChipTextToLabel(compact, label, "@", "b".repeat(64)),
456+
"fragment",
457+
);
458+
assert.equal(matchChipTextToLabel(compact, label, "#", key), "fragment");
459+
assert.equal(
460+
matchChipTextToLabel("Scout (150b20bd…15dc)", label, "@", key),
461+
"fragment",
462+
);
463+
assert.equal(matchChipTextToLabel("Scout", label, "@", key), "fragment");
464+
});

‎desktop/src/features/messages/lib/mentionClipboard.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { formatMentionDisplayLabel } from "@/shared/lib/mentionDisplay";
12
import { truncateInlineChipLabel } from "@/shared/ui/mentionChip";
23

34
import { getMentionOffsets } from "./hasMention";
@@ -77,6 +78,7 @@ export function matchChipTextToLabel(
7778
text: string,
7879
label: string,
7980
sigil: "@" | "#",
81+
pubkey?: string,
8082
): ChipTextMatch {
8183
const body = canonicalMentionLabel(text);
8284
const matches = (form: string) => body === form || body === `${sigil}${form}`;
@@ -87,6 +89,13 @@ export function matchChipTextToLabel(
8789
if (truncated !== label && matches(canonicalMentionLabel(truncated))) {
8890
return "truncated";
8991
}
92+
// Read-only mentions abbreviate a bound key, never the identity carried by
93+
// the clipboard. Restore the full literal label only for the whole display.
94+
const compact =
95+
sigil === "@" ? formatMentionDisplayLabel(label, pubkey) : label;
96+
if (compact !== label && matches(canonicalMentionLabel(compact))) {
97+
return "truncated";
98+
}
9099
return "fragment";
91100
}
92101

‎desktop/src/features/messages/lib/normalizeMentionClipboard.test.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,19 @@ test("an ordinary chip still flattens to a registrable mention", () => {
265265
[JOHN_SMITH_PUBKEY],
266266
);
267267
});
268+
269+
test("compact mention paste expands only complete bound labels", () => {
270+
const key = `150b20bd${"a".repeat(52)}15dc`;
271+
const label = `Scout (${key}) 2`;
272+
const html = (text) =>
273+
`<span data-mention="" data-mention-pubkey="${key}" ` +
274+
`data-mention-label="${label}">${text}</span>`;
275+
assert.equal(
276+
normalizeMentionClipboardContent(html("Scout (150b20bd…15dc) 2")).text,
277+
`@${label}`,
278+
);
279+
assert.equal(
280+
normalizeMentionClipboardContent(html("Scout (150b20bd…15dc)")).text,
281+
"Scout (150b20bd…15dc)",
282+
);
283+
});

‎desktop/src/features/messages/lib/normalizeMentionClipboard.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CHANNEL_LABEL_ATTRIBUTE,
33
matchChipTextToLabel,
44
MENTION_LABEL_ATTRIBUTE,
5+
MENTION_PUBKEY_ATTRIBUTE,
56
} from "./mentionClipboard";
67

78
/**
@@ -173,7 +174,14 @@ export function normalizeMentionClipboardContent(
173174
// from the same attribute the records carry keeps the two provably
174175
// consistent, whatever the classifier goes on to tolerate.
175176
const match =
176-
label === null ? "full" : matchChipTextToLabel(text, label, sigil);
177+
label === null
178+
? "full"
179+
: matchChipTextToLabel(
180+
text,
181+
label,
182+
sigil,
183+
el.getAttribute(MENTION_PUBKEY_ATTRIBUTE) ?? undefined,
184+
);
177185
span.textContent =
178186
match === "fragment"
179187
? text

‎desktop/src/features/messages/lib/timelineMentionCopy.test.mjs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,18 @@ test("copy inlines a blockified chip but preserves its block ancestor", () => {
187187
else delete prototype.innerText;
188188
}
189189
});
190+
191+
test("copy expands compact key text but preserves the exact label and identity", () => {
192+
const label = `Scout (${JOHN_SMITH_PUBKEY}) 2`;
193+
const flavors = copyRenderedBody(
194+
`<span data-mention="" data-mention-pubkey="${JOHN_SMITH_PUBKEY}" ` +
195+
`data-mention-label="${label}" class="mention-chip">` +
196+
'<span class="inline-chip-leading-fragment">Scout</span> (7c7c7c7c…7c7c) 2</span>',
197+
);
198+
assert.ok(flavors);
199+
assert.ok(flavors.html.includes(`@${label}`));
200+
assert.ok(
201+
flavors.html.includes(`data-mention-pubkey="${JOHN_SMITH_PUBKEY}"`),
202+
);
203+
assert.ok(!flavors.html.includes("…"));
204+
});

‎desktop/src/features/messages/lib/timelineMentionCopy.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ function restoreChipSigils(root: HTMLElement): boolean {
5050
const label = element.getAttribute(MENTION_LABEL_ATTRIBUTE);
5151
if (
5252
label &&
53-
matchChipTextToLabel(element.textContent ?? "", label, "@") !== "fragment"
53+
matchChipTextToLabel(
54+
element.textContent ?? "",
55+
label,
56+
"@",
57+
element.getAttribute(MENTION_PUBKEY_ATTRIBUTE) ?? undefined,
58+
) !== "fragment"
5459
) {
5560
element.textContent = `@${label}`;
5661
restored = true;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
import { formatMentionDisplayLabel } from "./mentionDisplay.ts";
4+
import { truncatePubkey } from "./pubkey.ts";
5+
6+
const KEY = `150b20bd${"a".repeat(52)}15dc`;
7+
8+
test("compact mention display uses the member-list formatter and keeps collision suffixes", () => {
9+
for (const suffix of ["", " 2", " 10"]) {
10+
assert.equal(
11+
formatMentionDisplayLabel(`Bad Janet (${KEY})${suffix}`, KEY),
12+
`Bad Janet (${truncatePubkey(KEY)})${suffix}`,
13+
);
14+
}
15+
assert.equal(formatMentionDisplayLabel(KEY, KEY), truncatePubkey(KEY));
16+
assert.equal(
17+
formatMentionDisplayLabel(KEY.toUpperCase(), KEY),
18+
truncatePubkey(KEY.toUpperCase()),
19+
);
20+
});
21+
22+
test("display leaves unbound, mismatched, malformed and ordinary labels literal", () => {
23+
for (const [label, key] of [
24+
[`Bad Janet (${KEY})`, undefined],
25+
[`Bad Janet (${KEY})`, "b".repeat(64)],
26+
[`Bad Janet (${KEY})`, "bad-key"],
27+
[`Bad Janet (${KEY}) 1`, KEY],
28+
[`Bad Janet (${KEY}) notes`, KEY],
29+
[`Release ${KEY}`, KEY],
30+
["Bad Janet", KEY],
31+
])
32+
assert.equal(formatMentionDisplayLabel(label, key), label);
33+
});
34+
35+
test("matching compact keys do not become identity keys", () => {
36+
const other = KEY.replace("aaaa", "bbbb");
37+
assert.notEqual(KEY, other);
38+
assert.equal(
39+
formatMentionDisplayLabel(`Scout (${KEY})`, KEY),
40+
formatMentionDisplayLabel(`Scout (${other})`, other),
41+
);
42+
assert.equal(
43+
formatMentionDisplayLabel(`Scout (${KEY})`, other),
44+
`Scout (${KEY})`,
45+
);
46+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { truncatePubkey } from "./pubkey";
2+
3+
/** Compact only a bound mention's key; its literal label remains authoritative. */
4+
export function formatMentionDisplayLabel(
5+
label: string,
6+
pubkey: string | undefined,
7+
): string {
8+
if (!pubkey || !/^[0-9a-f]{64}$/i.test(pubkey)) return label;
9+
if (label.toLowerCase() === pubkey.toLowerCase()) {
10+
return truncatePubkey(label);
11+
}
12+
const qualified = label.match(
13+
/^(.*) \(([0-9a-f]{64})\)((?: (?:[2-9]|[1-9][0-9]+))?)$/i,
14+
);
15+
if (qualified?.[2].toLowerCase() !== pubkey.toLowerCase()) return label;
16+
return `${qualified[1]} (${truncatePubkey(qualified[2])})${qualified[3]}`;
17+
}

‎desktop/src/shared/ui/markdown/MarkdownMention.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type * as React from "react";
22
import { AgentManagementMarker } from "@/features/agents/ui/OtherSetupAgentMarker";
33
import { UserProfilePopover } from "@/features/profile/ui/UserProfilePopover";
44
import { cn } from "@/shared/lib/cn";
5+
import { formatMentionDisplayLabel } from "@/shared/lib/mentionDisplay";
56
import {
67
inlineChipIconClasses,
78
inlineChipLeadingEnd,
@@ -32,8 +33,9 @@ export function createMarkdownMention(interactive: boolean) {
3233
pubkey !== undefined &&
3334
agentMentionPubkeysByName?.[mentionName] === pubkey;
3435
const mentionLabel = mentionText.replace(/^@/, "");
36+
const displayLabel = formatMentionDisplayLabel(mentionLabel, pubkey);
3537
const icon = isAgentMention ? "agent" : "human";
36-
const leadingEnd = inlineChipLeadingEnd(mentionLabel);
38+
const leadingEnd = inlineChipLeadingEnd(displayLabel);
3739
// Only chips that actually open a profile get the clickable affordance.
3840
// A mention whose pubkey didn't resolve stays a plain chip — a pointer
3941
// cursor there promises a click that does nothing.
@@ -62,9 +64,9 @@ export function createMarkdownMention(interactive: boolean) {
6264
inlineChipIconClasses(icon),
6365
)}
6466
>
65-
{mentionLabel.slice(0, leadingEnd)}
67+
{displayLabel.slice(0, leadingEnd)}
6668
</span>
67-
{mentionLabel.slice(leadingEnd)}
69+
{displayLabel.slice(leadingEnd)}
6870
{isAgentMention ? <AgentManagementMarker pubkey={pubkey} /> : null}
6971
</InlineChip>
7072
);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
import React from "react";
4+
import { renderToStaticMarkup } from "react-dom/server";
5+
import { truncatePubkey } from "../lib/pubkey.ts";
6+
import { createMarkdownComponents } from "./markdown.tsx";
7+
import { renderCachedMarkdown } from "./markdown/nodeCache.ts";
8+
import { MarkdownRuntimeContext } from "./markdown/runtimeContext.ts";
9+
10+
const KEY = `150b20bd${"a".repeat(52)}15dc`;
11+
12+
for (const agent of [false, true]) {
13+
test(`rendered ${agent ? "agent" : "human"} abbreviates a bound key without changing its metadata`, () => {
14+
const label = `Scout (${KEY}) 2`;
15+
const name = label.toLowerCase();
16+
const html = renderToStaticMarkup(
17+
React.createElement(
18+
MarkdownRuntimeContext.Provider,
19+
{
20+
value: {
21+
channels: [],
22+
mentionPubkeysByName: { [name]: KEY },
23+
agentMentionPubkeysByName: agent ? { [name]: KEY } : {},
24+
},
25+
},
26+
renderCachedMarkdown({
27+
content: `Ask @${label}`,
28+
mentionNames: [label],
29+
components: createMarkdownComponents(false, false),
30+
variant: `compact-mention-${agent}`,
31+
}),
32+
),
33+
);
34+
assert.equal(
35+
html.replace(/<[^>]+>/g, ""),
36+
`Ask Scout (${truncatePubkey(KEY)}) 2`,
37+
);
38+
assert.ok(html.includes(`data-mention-label="${label}"`));
39+
assert.ok(html.includes(`data-mention-pubkey="${KEY}"`));
40+
assert.ok(html.includes(`title="${label}"`));
41+
assert.ok(html.includes(`aria-label="${label}"`));
42+
assert.match(
43+
html,
44+
new RegExp(
45+
`inline-chip-leading-fragment[^>]*inline-chip-icon-${agent ? "agent" : "human"}`,
46+
),
47+
);
48+
});
49+
}
50+
51+
test("an unresolved qualified mention stays literal rather than claiming an abbreviated identity", () => {
52+
const label = `Scout (${KEY})`;
53+
const html = renderToStaticMarkup(
54+
React.createElement(
55+
MarkdownRuntimeContext.Provider,
56+
{
57+
value: { channels: [], mentionPubkeysByName: {} },
58+
},
59+
renderCachedMarkdown({
60+
content: `Ask @${label}`,
61+
mentionNames: [label],
62+
components: createMarkdownComponents(false, false),
63+
variant: "unresolved-compact-mention",
64+
}),
65+
),
66+
);
67+
assert.ok(html.includes(`@${label}`));
68+
assert.doesNotMatch(html, /data-mention=/);
69+
});

0 commit comments

Comments
 (0)