Files
buzz/desktop/src/features/profile/lib/identity.ts
Tyler 2cc0eb5396 perf(desktop): GUI performance sweep — async offload, poll reduction, render stabilization (#1641)
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub17jjz49l9jjmhhk7cac63j8yt9z555n9cw8vk7v5jz4vzw4ppld5qgj57cc <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1cc3ha7z055mu0rwwu7806t2wt8mj3pvu0uv5mfp2c50dahaqhczshdalg6 <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1jmc9dt2lyvzu3h0kxlwxt5zg4fxp9476awyxw6gwxn72g6cw7exqs64whm <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
2026-07-08 14:58:27 -04:00

190 lines
5.0 KiB
TypeScript

import type { Profile, UserProfileSummary } from "@/shared/api/types";
import { normalizePubkey, truncatePubkey } from "@/shared/lib/pubkey";
export type UserProfileLookup = Record<string, UserProfileSummary>;
export { truncatePubkey };
/**
* Deep-equal two profile lookups by value. Used to stabilise the merged
* `messageProfiles` reference at the ChannelScreen boundary: the underlying
* `users-batch` query re-keys on the full sorted pubkey set, so typing churn
* (a transient typing-only pubkey entering/leaving the set) produces a fresh
* lookup object identity even when no profile value actually changed. That new
* reference fails MessageRow's `prev.profiles === next.profiles` memo check and
* re-renders the entire timeline on every keystroke-adjacent typing event.
* Returning the previous reference when this reports equal keeps the memo
* intact. Consumers read profiles by pubkey value only, never treating identity
* as a change signal, so returning the stale-but-value-identical reference is
* safe.
*/
export function profileLookupsEqual(
a: UserProfileLookup,
b: UserProfileLookup,
): boolean {
if (a === b) {
return true;
}
const aKeys = Object.keys(a);
if (aKeys.length !== Object.keys(b).length) {
return false;
}
for (const key of aKeys) {
const prev = a[key];
const next = b[key];
if (
next === undefined ||
prev.displayName !== next.displayName ||
prev.avatarUrl !== next.avatarUrl ||
prev.nip05Handle !== next.nip05Handle ||
prev.ownerPubkey !== next.ownerPubkey ||
prev.isAgent !== next.isAgent
) {
return false;
}
}
return true;
}
function getResolvedProfile(
pubkey: string,
profiles: UserProfileLookup | undefined,
) {
if (!profiles) {
return null;
}
return profiles[normalizePubkey(pubkey)] ?? null;
}
export function mergeCurrentProfileIntoLookup(
profiles: UserProfileLookup | undefined,
currentProfile:
| Pick<Profile, "pubkey" | "displayName" | "avatarUrl" | "nip05Handle">
| null
| undefined,
) {
if (!currentProfile) {
return profiles;
}
return {
...(profiles ?? {}),
[normalizePubkey(currentProfile.pubkey)]: {
displayName: currentProfile.displayName,
avatarUrl: currentProfile.avatarUrl,
nip05Handle: currentProfile.nip05Handle,
isAgent: profiles?.[normalizePubkey(currentProfile.pubkey)]?.isAgent,
ownerPubkey:
profiles?.[normalizePubkey(currentProfile.pubkey)]?.ownerPubkey ?? null,
},
};
}
export function resolveUserLabel(input: {
pubkey: string;
currentPubkey?: string;
fallbackName?: string | null;
profiles?: UserProfileLookup;
preferResolvedSelfLabel?: boolean;
}) {
const {
currentPubkey,
fallbackName,
preferResolvedSelfLabel = false,
profiles,
pubkey,
} = input;
if (
typeof currentPubkey === "string" &&
normalizePubkey(currentPubkey) === normalizePubkey(pubkey)
) {
if (!preferResolvedSelfLabel) {
return "You";
}
}
const profile = getResolvedProfile(pubkey, profiles);
const displayName = profile?.displayName?.trim();
if (displayName) {
return displayName;
}
const nip05Handle = profile?.nip05Handle?.trim();
if (nip05Handle) {
return nip05Handle;
}
const safeFallback = fallbackName?.trim();
if (safeFallback) {
return safeFallback;
}
return truncatePubkey(pubkey);
}
/**
* Returns true when the current user owns the agent that authored a message.
* Mirrors the relay's `is_agent_owner` gate: ownership is determined by the
* NIP-OA `ownerPubkey` field on the author's profile, NOT by the local
* managed-agents list (which can diverge from server-side ownership).
*/
export function ownsAuthorAgent(
profile: { ownerPubkey: string | null } | undefined,
currentPubkey: string | undefined,
): boolean {
return (
!!currentPubkey &&
!!profile?.ownerPubkey &&
normalizePubkey(profile.ownerPubkey) === normalizePubkey(currentPubkey)
);
}
export function resolveUserSecondaryLabel(input: {
pubkey: string;
profiles?: UserProfileLookup;
}) {
const profile = getResolvedProfile(input.pubkey, input.profiles);
const displayName = profile?.displayName?.trim();
const nip05Handle = profile?.nip05Handle?.trim();
if (displayName && nip05Handle) {
return nip05Handle;
}
return null;
}
/**
* Label for an agent's owner: "you" when the current user owns it, otherwise
* the owner's display name, NIP-05 handle, or truncated pubkey.
*/
export function formatOwnerLabel(
ownerPubkey: string | null | undefined,
currentPubkey: string | null | undefined,
ownerProfiles?: UserProfileLookup,
) {
if (!ownerPubkey) {
return null;
}
const normalizedOwnerPubkey = normalizePubkey(ownerPubkey);
if (
currentPubkey &&
normalizedOwnerPubkey === normalizePubkey(currentPubkey)
) {
return "you";
}
const owner = ownerProfiles?.[normalizedOwnerPubkey];
return (
owner?.displayName?.trim() ||
owner?.nip05Handle?.trim() ||
truncatePubkey(ownerPubkey)
);
}