Skip to content

Commit 76803df

Browse files
kalvinnchaupeonclaude
authored
perf(identity-names): re-render a names consumer only when a label it resolved changes (#2100)
Signed-off-by: Kalvin Chau <[email protected]> Co-authored-by: peon <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent e7ae7b1 commit 76803df

10 files changed

Lines changed: 366 additions & 35 deletions

File tree

‎src/app/shell/SearchResults.tsx‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { workflowLabel } from "../../features/relay/workflow-attribution";
2-
import { useIdentityNames } from "../../features/identity-names/react";
2+
import {
3+
useIdentityNames,
4+
useNamesRevision,
5+
} from "../../features/identity-names/react";
36
import { npubEncode } from "nostr-tools/nip19";
47
import {
58
useEffect,
@@ -88,6 +91,7 @@ function conversationName(
8891

8992
/** The most rows the People and Agents groups each show. */
9093
const PERSON_ROWS = 6;
94+
const noMembers: readonly string[] = [];
9195

9296
/** "Search in this conversation" narrows the search and "Show more messages"
9397
* extends it; neither is a result. */
@@ -331,6 +335,8 @@ export function SearchResults({
331335
peopleActive,
332336
);
333337
useMentionArchives(session, showAuthorPicker || peopleActive);
338+
// People labels come from one names scope built each render.
339+
useNamesRevision(session.names, peopleActive);
334340
// The viewer's own agents open a new chat, not a direct message: the
335341
// same agents the new-chat composer accepts. Their earlier chats
336342
// still show as conversations.
@@ -599,13 +605,14 @@ export function SearchResults({
599605
name: person.name,
600606
...(person.isAgent ? { isAgent: true } : {}),
601607
}));
608+
const named = session.names?.scope(candidates, displayFacts);
602609
for (const { pubkey, source, agent } of eligible) {
603610
const dm = relationships.dm(pubkey);
604611
const agentName =
605612
messageableAgents.get(pubkey)?.name ?? sessionAgents.get(pubkey)?.name;
606613
people.push({
607614
pubkey,
608-
label: resolveName(pubkey, source.name, candidates, displayFacts),
615+
label: named?.(pubkey)?.name ?? source.name,
609616
username: source.username,
610617
agentName: agentName !== source.name ? agentName : undefined,
611618
agent,
@@ -977,7 +984,7 @@ export function SearchResults({
977984
displayId,
978985
profiles.get(displayId)?.name ?? displayId.slice(0, 10),
979986
list.channels.find((channel) => channel.id === message.channelId)
980-
?.members ?? [],
987+
?.members ?? noMembers,
981988
);
982989
return {
983990
key: message.id,

‎src/bundled/channels/useSoloShare.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
useEffect,
3+
useMemo,
34
useRef,
45
useState,
56
useSyncExternalStore,
@@ -94,14 +95,17 @@ export function useSoloShare({
9495
const chosen = list.channels.find(
9596
(item) => item.id === destination && item.id !== channelId,
9697
);
97-
const destinationMembers =
98+
const readyMembers =
9899
chosen &&
99100
destinationRoster?.id === chosen.id &&
100101
destinationRoster.status === "ready" &&
101-
!chosen.cached &&
102-
chosen.members
103-
? [...new Set(chosen.members)]
102+
!chosen.cached
103+
? chosen.members
104104
: undefined;
105+
const destinationMembers = useMemo(
106+
() => (readyMembers ? [...new Set(readyMembers)] : undefined),
107+
[readyMembers],
108+
);
105109
useEffect(() => {
106110
void rosterReload;
107111
if (!enabled) return;
@@ -199,9 +203,12 @@ export function useSoloShare({
199203
.catch(() => {});
200204
}, [session, destinationProfileKeys]);
201205
const resolveName = useIdentityNames(session.names);
202-
const displayKeys = [
203-
...new Set([...(source?.members ?? []), ...(destinationMembers ?? [])]),
204-
];
206+
const displayKeys = useMemo(
207+
() => [
208+
...new Set([...(source?.members ?? []), ...(destinationMembers ?? [])]),
209+
],
210+
[source?.members, destinationMembers],
211+
);
205212
const labels = new Map(
206213
displayKeys.map((key) => [
207214
key,

‎src/bundled/mentions/team-mentions.test.tsx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,33 @@ it("directory pages rebuild the name index only for people they add", async () =
675675
view.unmount();
676676
h.library.dispose();
677677
});
678+
679+
it("an open chooser relabels choices when only names change", async () => {
680+
const h = fixture();
681+
let name = "Before";
682+
let revision = 0;
683+
const listeners = new Set<() => void>();
684+
Object.assign(h.session, {
685+
names: {
686+
subscribe: (listener: () => void) => {
687+
listeners.add(listener);
688+
return () => listeners.delete(listener);
689+
},
690+
snapshot: () => revision,
691+
scope: () => () => ({ name, source: "agent-directory" }),
692+
},
693+
});
694+
function Chooser() {
695+
const model = useMentionChoices(h.session, "parent", false, "", "open");
696+
return <>{model.candidates.map((choice) => choice.label).join(",")}</>;
697+
}
698+
render(<Chooser />);
699+
await waitFor(() => expect(screen.getByText("Before")).toBeInTheDocument());
700+
act(() => {
701+
name = "After";
702+
revision++;
703+
for (const listener of listeners) listener();
704+
});
705+
expect(screen.getByText("After")).toBeInTheDocument();
706+
h.library.dispose();
707+
});

‎src/bundled/mentions/use-mention-choices.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import type { MentionRecipient } from "../../features/messages/mention-draft";
1212
import type { Agent } from "../../features/agents/service";
1313
import { useAgentChoices } from "../../features/agents/use-choices";
1414
import { AgentsContext, useAgentsSnapshot } from "../../features/agents/react";
15-
import { useIdentityNames } from "../../features/identity-names/react";
1615
import { DraftMentionRoster } from "../../features/messages/draft-mention-roster";
1716
import { SelectedMentionContext } from "../../features/messages/selected-mention-context";
1817
import { archivedMention } from "../../features/messages/mention-admission";
1918
import { mentionHistory } from "../../features/messages/mention-history";
2019
import { directoryChanges, mentionCandidates } from "./mention-candidates";
2120
import { useListedChannel } from "../../features/relay/listed-channel";
21+
import { useNamesRevision } from "../../features/identity-names/react";
2222
import {
2323
exactMention,
2424
matchingMentions,
@@ -223,7 +223,8 @@ export function useMentionChoices(
223223
lifetime,
224224
);
225225
const archives = useMentionArchives(session, open);
226-
const resolve = useIdentityNames(session.names);
226+
// Labels come from names scopes directly: watch every name change while open.
227+
const names = useNamesRevision(session.names, open);
227228
const [installed, install] = useState<{
228229
session: ComposerSession;
229230
key: string;
@@ -278,7 +279,7 @@ export function useMentionChoices(
278279
void channel;
279280
void agents;
280281
void archives;
281-
void resolve;
282+
void names;
282283
void revision;
283284
// A closed or disabled chooser shows nothing, so it names nothing.
284285
if (!open)
@@ -296,7 +297,7 @@ export function useMentionChoices(
296297
channel,
297298
agents,
298299
archives,
299-
resolve,
300+
names,
300301
revision,
301302
]);
302303
// Install membership first; agent discovery must not hide known members.

‎src/bundled/relay-notes/NotesPage.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ export function NotesCommunity({
222222
}: { session: RelaySession } & NotesNavigation) {
223223
const choices = useAgentChoices(session);
224224
const resolveName = useIdentityNames(session.names);
225-
const candidates = choices.identities.map((agent) => agent.pubkey);
225+
const candidates = useMemo(
226+
() => choices.identities.map((agent) => agent.pubkey),
227+
[choices.identities],
228+
);
226229
const agents = choices.identities.map((agent) => ({
227230
pubkey: agent.pubkey,
228231
picture: agent.avatar,

‎src/bundled/todos/TodosPanel.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type Draft = {
8080
base: string | null;
8181
input: string;
8282
};
83+
const noMembers: readonly string[] = [];
8384
const empty: Draft = { content: "", original: "", base: null, input: "" };
8485
const statuses: [Status, string][] = [
8586
["todo", "To do"],
@@ -145,7 +146,7 @@ export function TodosPanel({
145146
(channel) => channel.id === context.channelId,
146147
)?.members;
147148
const resolveName = (pubkey: string, fallback: string) =>
148-
resolveIdentity(pubkey, fallback, members ?? []);
149+
resolveIdentity(pubkey, fallback, members ?? noMembers);
149150
const key = `todos-draft-v1:${context.channelId}`;
150151
const [saved] = useState(() => readDraft(context.scope, key));
151152
const savedDirty = !!saved && saved.content !== saved.original;

‎src/features/identity-names/consumers.test.tsx‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ stubAvatarBrowserApis();
44
import "@testing-library/jest-dom/vitest";
55
import { act, cleanup, render, screen } from "@testing-library/react";
66
import { afterEach, beforeEach, expect, it, vi } from "vitest";
7-
import { createRef } from "react";
7+
import { createRef, useSyncExternalStore } from "react";
88
import { Context } from "@deepseek-ai/cordis";
99
import userEvent from "@testing-library/user-event";
1010
import { PluginRuntime } from "../../plugins/runtime";
@@ -385,8 +385,12 @@ it.each(["live", "reconnect", "clear-cache"] as const)(
385385
it("uses channel scope in link previews and activity, and participant scope in sidebar DMs", async () => {
386386
const f = fixture();
387387
function Sidebar() {
388+
const list = useSyncExternalStore(
389+
f.session.channels.subscribeList,
390+
f.session.channels.list,
391+
);
388392
const labels = useChannelLabels(
389-
f.session.channels.list().channels,
393+
list.channels,
390394
f.session.profiles,
391395
f.session.names,
392396
);

0 commit comments

Comments
 (0)