Browse Source

refactor: createPersonAnchorNodes

- created helper function buildEligiblePerson
- switch to map
master
limina1 8 months ago
parent
commit
ce51e886f9
  1. 70
      src/lib/navigator/EventNetwork/utils/personNetworkBuilder.ts

70
src/lib/navigator/EventNetwork/utils/personNetworkBuilder.ts

@ -106,6 +106,42 @@ export function extractUniquePersons(
return personMap; return personMap;
} }
/**
* Helper to build eligible person info for anchor nodes.
*/
function buildEligiblePerson(
pubkey: string,
connection: PersonConnection,
showSignedBy: boolean,
showReferenced: boolean
): {
pubkey: string;
connection: PersonConnection;
connectedEventIds: Set<string>;
totalConnections: number;
} | null {
const connectedEventIds = new Set<string>();
if (showSignedBy) {
connection.signedByEventIds.forEach(id => connectedEventIds.add(id));
}
if (showReferenced) {
connection.referencedInEventIds.forEach(id => connectedEventIds.add(id));
}
if (connectedEventIds.size === 0) {
return null;
}
return {
pubkey,
connection,
connectedEventIds,
totalConnections: connectedEventIds.size
};
}
/** /**
* Creates person anchor nodes * Creates person anchor nodes
*/ */
@ -123,35 +159,11 @@ export function createPersonAnchorNodes(
const centerY = height / 2; const centerY = height / 2;
// Calculate eligible persons and their connection counts // Calculate eligible persons and their connection counts
const eligiblePersons: Array<{ const eligiblePersons = Array.from(personMap.entries())
pubkey: string; .map(([pubkey, connection]) =>
connection: PersonConnection; buildEligiblePerson(pubkey, connection, showSignedBy, showReferenced)
connectedEventIds: Set<string>; )
totalConnections: number; .filter((p): p is NonNullable<typeof p> => p !== null);
}> = [];
Array.from(personMap.entries()).forEach(([pubkey, connection]) => {
// Get all connected event IDs based on filters
const connectedEventIds = new Set<string>();
if (showSignedBy) {
connection.signedByEventIds.forEach(id => connectedEventIds.add(id));
}
if (showReferenced) {
connection.referencedInEventIds.forEach(id => connectedEventIds.add(id));
}
// Skip if no connections match the filter
if (connectedEventIds.size === 0) return;
eligiblePersons.push({
pubkey,
connection,
connectedEventIds,
totalConnections: connectedEventIds.size
});
});
// Sort by total connections (descending) and take only top N // Sort by total connections (descending) and take only top N
eligiblePersons.sort((a, b) => b.totalConnections - a.totalConnections); eligiblePersons.sort((a, b) => b.totalConnections - a.totalConnections);

Loading…
Cancel
Save