Files
buzz/desktop/src/app/useTrayMenu.ts
Logan Johnson ad2a84131e fix(desktop): npub identity displays for mention, member, and workflow surfaces (#7495)
🤖
## Summary

Every Buzz account is identified by a long public key. Before this
change, when someone had no display name, surfaces fell back to
inconsistent labels — mostly raw hex fragments like `abcd1234…wxyz`,
sometimes a generic role label with no key — so the same person looked
different from surface to surface, and nothing looked like an npub
address. This PR applies the npub identity foundation from #7488 to the
everyday surfaces: a person without a display name now falls back to the
same compact npub everywhere — `npub1xxxx…yyyy`, the human-readable
spelling of their public key (first 8 + last 4 characters of the full
npub) — across messages and mentions, reactions, huddles, member and
participant lists, the sidebar and channel activity, search, projects,
tray, notifications, and workflow surfaces.

- **Mentions and messages**: key-only mention chips render the compact
npub. Pasting a copied mention back still re-binds it byte-exactly to
the identity it declares, for both the new npub chips and legacy
hex-truncated chips copied by older clients — wrong, missing, or
tampered key qualification is rejected instead of silently degrading to
plain text.
- **Reactions and huddles**: huddle reaction events and the huddle
roster/participants render the compact npub for unnamed participants;
workflow reaction triggers describe authors with the same form.
- **Members and sidebar**: channel and community member lists,
add-member results and invites, the members sidebar, the
channel-activity popover, search, projects (assignees/reviewers/PR
panels), the tray menu, and desktop notifications all fall back to the
compact npub; titles and aria labels keep the machine-readable full
labels.
- **Profile labels**: panel/popover display names and owner handles fall
back to the compact npub (never raw hex) when there is no name;
linked-event (nevent) message metadata shows the npub-shaped author
fallback while the event lookup and event IDs are unchanged.
- **Workflows**: author-picker secondary labels, step destination keys,
and trigger-author references render compact npubs; event and blob IDs
keep their existing hex compacts (they are not identities).
- **Avatars stay distinct**: fallback avatars for key-only identities
derive initials from the key's tail, so prefixed role labels like
"Participant npub1…" no longer collapse every unnamed participant onto
the same initials; people with names keep their name initials.

Preserved exactly: display names and distinct avatars, internal hex keys
(storage/API forms unchanged), clipboard identity roundtrips, event/blob
ID compaction, private keys (no nsec path is touched), and nevent link
handling.

Scope: this PR changes what identity labels **display**, not identity
controls — profile/settings copy controls, the respond-to allowlist,
workflow key fields, and agent dialogs are the sibling slice #7489, and
the shared primitives (`canonicalNpub`, `truncateNpub`, the `<PubKey>`
gate, strict input parsing) come from the foundation #7488.

### Related issue

- Fixes: N/A. Searched existing issues/PRs for duplicates — none found;
the related work is the npub identity stack this slice belongs to.
- Base/dependency: stacks on #7488 (foundation) — this PR does not stand
alone on main.
- #7489 is a sibling slice on the same #7488 base
(profile/agent/workflow controls), not a dependency: this PR does not
require #7489, and #7489 does not require this PR — both only require
#7488.

### Testing

At exact head `4763cbeae1dd521309755e6d61f657324cb98667` (base:
`fix/desktop-npub-identity-d1a` @
`5f3a4a8111998c8aa41ad77cf66992bd1c85343c`; 71 files, +656/−189 —
production +277/−136, test support +379/−53):

- At this head: targeted `mentions.spec.ts` (1/1), the e2e build,
typecheck, and biome — green.
- 9 changed/related unit files: 100/100 green; typecheck, e2e build,
biome, and px text/truncation checks clean; huddle-roster focused run
green; channel-activity e2e 11/11; mutation checks confirm the fallback
wiring (removing it collapses shared initials and drops fallback rows).
- Known pre-existing local e2e failures, unchanged by this PR and
reproduced identically at the upstream merge-base: huddle-transcription
voice-menu attribution (25 pass / 1 fail) and the
`workflow-local-controls` 438px caret drift. Not claimed green locally.
- Update at head `236af9e6137386737e84d3a474d6bc808a704c50` (test-only
follow-ups `1143af345` + `236af9e6`): the `workflow-local-controls`
races were fixed in the test drivers, and the 438px diff was shown to be
a stale Darwin snapshot baseline (name-row enable switch already absent
and `message_posted` already MessageSquare at recording commit
`9390e11c9`) and refreshed — the focused screenshot test, including
keyboard/caret assertions, now passes locally (twice). The full spec was
not rerun after the snapshot refresh; the huddle-transcription item
above is unchanged.

Label/copy text changes are asserted by the e2e specs (`mentions`,
`mention-recipients`, `pubkey-display-screenshots`,
`huddle-transcription`, `channel-activity-popover`,
`workflow-local-controls`) rather than new screenshots; the screenshot
spec pins the compact npub text forms.

### Task provenance

Buzz channel: `1f0e4a3d-7e01-4efe-bb16-843b357f85c9`

Task:
buzz://message?channel=1f0e4a3d-7e01-4efe-bb16-843b357f85c9&id=86b34eb4bd84a1472419e9af22636c011c0fe273e3c196f967d7a36996e149b6

---------

Signed-off-by: Logan Johnson <loganj@squareup.com>
Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
2026-09-09 14:07:24 -04:00

161 lines
4.6 KiB
TypeScript

import * as React from "react";
import { isTauri, invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import {
getActiveTurnsForAgent,
useActiveAgentTurnsByChannel,
} from "@/features/agents/activeAgentTurnsStore";
import {
useManagedAgentsQuery,
useRelayAgentsQuery,
} from "@/features/agents/hooks";
import { normalizePubkey, truncateNpub } from "@/shared/lib/pubkey";
import { useNow } from "@/shared/lib/useNow";
import { formatElapsed } from "@/features/agents/ui/agentSessionUtils";
import type { Channel } from "@/shared/api/types";
type TrayAgentActivity = {
activityId: string;
agentName: string;
channelId: string;
channelName: string;
elapsed: string;
};
type TrayAction =
| { kind: "newChannel" }
| { kind: "openChannel"; channelId: string };
const MAX_RECENT_TRAY_ACTIVITIES = 5;
/**
* Keeps Buzz's native tray menu synchronized with active agent turns and
* forwards its navigation actions into the React app.
*/
export function useTrayMenu({
channels,
goChannel,
openCreateChannel,
}: {
channels: Channel[];
goChannel: (channelId: string) => Promise<unknown>;
openCreateChannel: () => void;
}): void {
const activeTurns = useActiveAgentTurnsByChannel();
const now = useNow(1000);
const managedAgents = useManagedAgentsQuery().data;
const relayAgents = useRelayAgentsQuery().data;
const previousActivitiesRef = React.useRef(
new Map<string, TrayAgentActivity>(),
);
const [recentActivities, setRecentActivities] = React.useState<
TrayAgentActivity[]
>([]);
const activities = React.useMemo<TrayAgentActivity[]>(() => {
const channelNames = new Map(
channels.map((channel) => [channel.id, channel.name]),
);
const agentNames = new Map<string, string>();
for (const agent of [...(managedAgents ?? []), ...(relayAgents ?? [])]) {
agentNames.set(normalizePubkey(agent.pubkey), agent.name);
}
return activeTurns.flatMap((channelTurn) =>
channelTurn.agentPubkeys.map((pubkey) => {
const agentTurn = getActiveTurnsForAgent(pubkey).find(
(turn) => turn.channelId === channelTurn.channelId,
);
return {
activityId: `${channelTurn.channelId}:${normalizePubkey(pubkey)}`,
agentName:
agentNames.get(normalizePubkey(pubkey)) ??
`Agent ${truncateNpub(pubkey)}`,
channelId: channelTurn.channelId,
channelName:
channelNames.get(channelTurn.channelId) ?? "Unknown channel",
elapsed: formatElapsed(
now - (agentTurn?.anchorAt ?? channelTurn.anchorAt),
),
};
}),
);
}, [activeTurns, channels, managedAgents, now, relayAgents]);
React.useEffect(() => {
const currentActivities = new Map(
activities.map((activity) => [activity.activityId, activity]),
);
const completedActivities = [...previousActivitiesRef.current.entries()]
.filter(([activityId]) => !currentActivities.has(activityId))
.map(([, activity]) => ({
...activity,
activityId: `recent:${activity.activityId}:${Date.now()}`,
}));
if (completedActivities.length > 0) {
setRecentActivities((current) =>
[...completedActivities, ...current].slice(
0,
MAX_RECENT_TRAY_ACTIVITIES,
),
);
}
previousActivitiesRef.current = currentActivities;
}, [activities]);
React.useEffect(() => {
if (!isTauri()) return;
void invoke("update_tray_agent_activity", {
activities,
recentActivities,
}).catch((error) => {
console.error("Failed to update the macOS tray menu", error);
});
}, [activities, recentActivities]);
React.useEffect(() => {
if (!isTauri()) return;
let disposed = false;
let unlisten: (() => void) | undefined;
const handlePendingActions = async () => {
if (disposed) return;
const actions = await invoke<TrayAction[]>("take_tray_actions");
if (disposed) {
if (actions.length > 0) {
await invoke("requeue_tray_actions", { actions });
}
return;
}
for (const action of actions) {
if (action.kind === "newChannel") {
openCreateChannel();
} else {
void goChannel(action.channelId);
}
}
};
void (async () => {
const nextUnlisten = await listen("tray-action-available", () => {
void handlePendingActions();
});
if (disposed) {
nextUnlisten();
return;
}
unlisten = nextUnlisten;
await handlePendingActions();
})();
return () => {
disposed = true;
unlisten?.();
};
}, [goChannel, openCreateChannel]);
}