mirror of
https://github.com/block/buzz.git
synced 2026-11-26 19:21:50 +00:00
Switching channels triggered a full-roster fetch (kind:39002 plus a kind:0 profile batch with every member pubkey as an author) in the common case, and several render paths walked the full roster per render. None of this scales past a few hundred members; the product target is 10k+. - **Members query staleTime 30s → 5min.** Every membership change the client can observe already invalidates the key explicitly: live join/leave system messages for the active channel, member-added/removed notifications for the current identity, and all membership mutations — including previously-uncovered direct write paths (moderation kick, agent-deletion cleanup), which now invalidate through a shared helper. The 30s window bought correctness we already had and charged a roster fetch per switch. - **ChannelMembersBar no longer mounts the roster query for non-DM channels** — the count renders from the channel summary, and the private-channel huddle gate accepts `channel.isMember` (derived from the same kind:39002 event as the roster's self entry). - **Roster-derived lookups are cached on roster identity** (`rosterDerivations.ts`): role map, agent-member subset, member/bot pubkey sets. These were rebuilt O(members) on every live message / profile re-key. React Query's structural sharing keeps the roster identity stable, so each derivation computes once per distinct roster. - **Backend: the kind:0 profile join in `get_channel_members` is capped at the first 500 members** (roster order). Members past the cap keep `display_name: None` (UI falls back to pubkey labels and profile caches); `role=="bot"` agent flags are roster-derived and unaffected. Full roster pagination is the structural follow-up. - **Composer keystroke path**: `useCanAddChannelMembers` re-scanned channels + roster per keystroke; now memoized on data identities, sharing the cached pubkey set. ### Measured / estimated impact | metric | before | after | |---|---|---| | roster fetches while switching (live trace) | nearly every switch | ≤1 per channel per 5 min | | roster fetch cost on the wire (live, 51-member channel) | 273ms per fetch | amortized away | | kind:0 `authors` filter size at 10k members | ~670KB per request (~67 B/pubkey) | capped at 500 authors (~34KB) | | warm-switch longtask at 10k members (mock harness, 4× throttle) | 364ms | 318ms | | per-render roster walks (role map, agent sets) at 10k members | O(members) per live message | once per distinct roster | Deferred deliberately: protocol-level roster pagination and removing `memberPubkeys` from channel summaries (needs relay support). --------- Signed-off-by: Max Lampert <maxwell@squareup.com>
45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import * as React from "react";
|
|
|
|
import { canAddChannelMembers } from "@/features/channels/lib/channelMemberAdmission";
|
|
import {
|
|
useChannelMembersQuery,
|
|
useChannelsQuery,
|
|
} from "@/features/channels/hooks";
|
|
import { useIdentityQuery } from "@/shared/api/hooks";
|
|
|
|
// Split from hooks.ts to keep that file under the per-file line cap.
|
|
|
|
/**
|
|
* Whether the signed-in identity may add *another* identity to this channel,
|
|
* per {@link canAddChannelMembers}. Both queries are the ones the channel UI
|
|
* already holds, so this shares their cache rather than fetching again.
|
|
*/
|
|
export function useCanAddChannelMembers(channelId: string | null) {
|
|
const channelsQuery = useChannelsQuery();
|
|
const membersQuery = useChannelMembersQuery(channelId);
|
|
const identityQuery = useIdentityQuery();
|
|
|
|
const channels = channelsQuery.data;
|
|
const members = membersQuery.data;
|
|
const selfPubkey = identityQuery.data?.pubkey ?? null;
|
|
// Memoized: this hook sits in the composer (useMentionSendFlow), which
|
|
// re-renders per keystroke, and these scans are O(channels) + O(members)
|
|
// — rosters can be 10k+. Structural sharing keeps the inputs' identities
|
|
// stable, so the scans run once per data change instead of per keystroke.
|
|
return React.useMemo(() => {
|
|
const channel =
|
|
channels?.find((candidate) => candidate.id === channelId) ?? null;
|
|
const selfRole = selfPubkey
|
|
? (members?.find(
|
|
(member) => member.pubkey.toLowerCase() === selfPubkey.toLowerCase(),
|
|
)?.role ?? null)
|
|
: null;
|
|
|
|
return canAddChannelMembers({
|
|
channelType: channel?.channelType,
|
|
visibility: channel?.visibility,
|
|
selfRole,
|
|
});
|
|
}, [channelId, channels, members, selfPubkey]);
|
|
}
|