({
currentPubkey,
directMessages,
fallbackDisplayName,
profileDisplayName,
enabled = true,
}: {
currentPubkey?: string;
directMessages: Channel[];
fallbackDisplayName?: string;
profileDisplayName?: string | null;
enabled?: boolean;
})
| 8 | import type { Channel, PresenceStatus } from "@/shared/api/types"; |
| 9 | |
| 10 | export function useDmSidebarMetadata({ |
| 11 | currentPubkey, |
| 12 | directMessages, |
| 13 | fallbackDisplayName, |
| 14 | profileDisplayName, |
| 15 | enabled = true, |
| 16 | }: { |
| 17 | currentPubkey?: string; |
| 18 | directMessages: Channel[]; |
| 19 | fallbackDisplayName?: string; |
| 20 | profileDisplayName?: string | null; |
| 21 | enabled?: boolean; |
| 22 | }) { |
| 23 | const selfDmLabels = React.useMemo( |
| 24 | () => |
| 25 | new Set( |
| 26 | [profileDisplayName, fallbackDisplayName] |
| 27 | .map((value) => value?.trim().toLowerCase()) |
| 28 | .filter((value): value is string => Boolean(value)), |
| 29 | ), |
| 30 | [fallbackDisplayName, profileDisplayName], |
| 31 | ); |
| 32 | const dmParticipantPubkeys = React.useMemo( |
| 33 | () => |
| 34 | directMessages.flatMap((channel) => |
| 35 | channel.participantPubkeys.filter((pubkey, index) => { |
| 36 | const normalizedPubkey = pubkey.toLowerCase(); |
| 37 | if (normalizedPubkey === currentPubkey?.toLowerCase()) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | const participantLabel = |
| 42 | channel.participants[index]?.trim().toLowerCase() ?? null; |
| 43 | return !participantLabel || !selfDmLabels.has(participantLabel); |
| 44 | }), |
| 45 | ), |
| 46 | [currentPubkey, directMessages, selfDmLabels], |
| 47 | ); |
| 48 | const dmPresenceQuery = usePresenceQuery(dmParticipantPubkeys, { |
| 49 | enabled: enabled && directMessages.length > 0, |
| 50 | }); |
| 51 | const dmProfilesQuery = useUsersBatchQuery(dmParticipantPubkeys, { |
| 52 | enabled: enabled && directMessages.length > 0, |
| 53 | }); |
| 54 | const dmProfiles = dmProfilesQuery.data?.profiles; |
| 55 | const dmPresenceByChannelId = React.useMemo( |
| 56 | () => |
| 57 | Object.fromEntries( |
| 58 | directMessages.map((channel) => { |
| 59 | const otherParticipantPubkey = channel.participantPubkeys.find( |
| 60 | (pubkey, index) => { |
| 61 | const normalizedPubkey = pubkey.toLowerCase(); |
| 62 | if (normalizedPubkey === currentPubkey?.toLowerCase()) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | const participantLabel = |
| 67 | channel.participants[index]?.trim().toLowerCase() ?? null; |
no test coverage detected