mirror of
https://github.com/block/buzz.git
synced 2026-10-27 04:30:52 +00:00
Signed-off-by: Taylor Ho <taylorkmho@gmail.com> Co-authored-by: npub1223z34hd7vtwc6qj4s7flsxkj644nlre2nthu7lrrmkumhu3xddsrx9r6w <52a228d6edf316ec6812ac3c9fc0d696ab59fc7954d77e7be31eedcddf91335b@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub14vtk7pvazqrq9639qu7e560wnqtl0d53ca4gjuvq6jzf3k2el23qqlwa7f <ab176f059d100602ea25073d9a69ee9817f7b691c76a897180d48498d959faa2@sprout-oss.stage.blox.sqprod.co>
124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
import * as React from "react";
|
|
|
|
import type { TypingIndicatorEntry } from "@/features/messages/useChannelTyping";
|
|
import type { UserProfileLookup } from "@/features/profile/lib/identity";
|
|
import type {
|
|
Channel,
|
|
ChannelMember,
|
|
ManagedAgent,
|
|
RelayAgent,
|
|
} from "@/shared/api/types";
|
|
import { normalizePubkey } from "@/shared/lib/pubkey";
|
|
import {
|
|
buildChannelAgentSessionCandidates,
|
|
getChannelAgentSessionAgents,
|
|
} from "./useChannelAgentSessions";
|
|
|
|
export function useChannelActivityTyping({
|
|
activeChannel,
|
|
activeChannelId,
|
|
channelMembers,
|
|
managedAgents,
|
|
openThreadHeadId,
|
|
relayAgents,
|
|
typingEntries,
|
|
}: {
|
|
activeChannel: Channel | null;
|
|
activeChannelId: string | null;
|
|
channelMembers?: ChannelMember[];
|
|
managedAgents: ManagedAgent[];
|
|
openThreadHeadId: string | null;
|
|
relayAgents: RelayAgent[];
|
|
typingEntries: TypingIndicatorEntry[];
|
|
}) {
|
|
const agentCandidates = React.useMemo(
|
|
() =>
|
|
buildChannelAgentSessionCandidates({
|
|
channelMembers,
|
|
managedAgents,
|
|
relayAgents,
|
|
}),
|
|
[channelMembers, managedAgents, relayAgents],
|
|
);
|
|
const channelAgentSessionAgents = React.useMemo(
|
|
() =>
|
|
getChannelAgentSessionAgents({
|
|
activeChannel,
|
|
activeChannelId,
|
|
agents: agentCandidates,
|
|
channelMembers,
|
|
}),
|
|
[activeChannel, activeChannelId, agentCandidates, channelMembers],
|
|
);
|
|
const channelAgentPubkeys = React.useMemo(
|
|
() =>
|
|
new Set(
|
|
channelAgentSessionAgents.map((agent) => normalizePubkey(agent.pubkey)),
|
|
),
|
|
[channelAgentSessionAgents],
|
|
);
|
|
const threadTypingPubkeys = React.useMemo(
|
|
() =>
|
|
typingEntries
|
|
.filter(
|
|
(entry) =>
|
|
entry.threadHeadId === openThreadHeadId &&
|
|
!channelAgentPubkeys.has(normalizePubkey(entry.pubkey)),
|
|
)
|
|
.map((entry) => entry.pubkey),
|
|
[channelAgentPubkeys, openThreadHeadId, typingEntries],
|
|
);
|
|
const { botTypingEntries, humanTypingPubkeys } = React.useMemo<{
|
|
botTypingEntries: TypingIndicatorEntry[];
|
|
humanTypingPubkeys: string[];
|
|
}>(() => {
|
|
const botTypingEntries: TypingIndicatorEntry[] = [];
|
|
const humanTypingPubkeys: string[] = [];
|
|
for (const entry of typingEntries) {
|
|
if (channelAgentPubkeys.has(normalizePubkey(entry.pubkey))) {
|
|
botTypingEntries.push(entry);
|
|
} else if (entry.threadHeadId === null) {
|
|
humanTypingPubkeys.push(entry.pubkey);
|
|
}
|
|
}
|
|
return { botTypingEntries, humanTypingPubkeys };
|
|
}, [channelAgentPubkeys, typingEntries]);
|
|
|
|
return {
|
|
agentSessionCandidates: agentCandidates,
|
|
botTypingEntries,
|
|
channelAgentSessionAgents,
|
|
humanTypingPubkeys,
|
|
threadTypingPubkeys,
|
|
};
|
|
}
|
|
|
|
export function mergeAgentNamesIntoProfiles(
|
|
profiles: UserProfileLookup,
|
|
managedAgents: ManagedAgent[],
|
|
relayAgents: RelayAgent[],
|
|
): UserProfileLookup {
|
|
const merged = { ...profiles };
|
|
for (const agent of relayAgents) {
|
|
const key = normalizePubkey(agent.pubkey);
|
|
merged[key] = {
|
|
...merged[key],
|
|
displayName: merged[key]?.displayName || agent.name,
|
|
avatarUrl: merged[key]?.avatarUrl ?? null,
|
|
nip05Handle: merged[key]?.nip05Handle ?? null,
|
|
isAgent: true,
|
|
};
|
|
}
|
|
for (const agent of managedAgents) {
|
|
const key = normalizePubkey(agent.pubkey);
|
|
merged[key] = {
|
|
...merged[key],
|
|
displayName: merged[key]?.displayName || agent.name,
|
|
avatarUrl: merged[key]?.avatarUrl ?? agent.avatarUrl,
|
|
nip05Handle: merged[key]?.nip05Handle ?? null,
|
|
isAgent: true,
|
|
};
|
|
}
|
|
return merged;
|
|
}
|