Browse Source

refactor: createPersonAnchorNodes

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

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

@ -107,31 +107,19 @@ export function extractUniquePersons(
} }
/** /**
* Creates person anchor nodes * Helper to build eligible person info for anchor nodes.
*/ */
export function createPersonAnchorNodes( function buildEligiblePerson(
personMap: Map<string, PersonConnection>, pubkey: string,
width: number, connection: PersonConnection,
height: number,
showSignedBy: boolean, showSignedBy: boolean,
showReferenced: boolean, showReferenced: boolean
limit: number = MAX_PERSON_NODES ): {
): { nodes: NetworkNode[], totalCount: number } {
const anchorNodes: NetworkNode[] = [];
const centerX = width / 2;
const centerY = height / 2;
// Calculate eligible persons and their connection counts
const eligiblePersons: Array<{
pubkey: string; pubkey: string;
connection: PersonConnection; connection: PersonConnection;
connectedEventIds: Set<string>; connectedEventIds: Set<string>;
totalConnections: number; totalConnections: number;
}> = []; } | null {
Array.from(personMap.entries()).forEach(([pubkey, connection]) => {
// Get all connected event IDs based on filters
const connectedEventIds = new Set<string>(); const connectedEventIds = new Set<string>();
if (showSignedBy) { if (showSignedBy) {
@ -142,16 +130,40 @@ export function createPersonAnchorNodes(
connection.referencedInEventIds.forEach(id => connectedEventIds.add(id)); connection.referencedInEventIds.forEach(id => connectedEventIds.add(id));
} }
// Skip if no connections match the filter if (connectedEventIds.size === 0) {
if (connectedEventIds.size === 0) return; return null;
}
eligiblePersons.push({ return {
pubkey, pubkey,
connection, connection,
connectedEventIds, connectedEventIds,
totalConnections: connectedEventIds.size totalConnections: connectedEventIds.size
}); };
}); }
/**
* Creates person anchor nodes
*/
export function createPersonAnchorNodes(
personMap: Map<string, PersonConnection>,
width: number,
height: number,
showSignedBy: boolean,
showReferenced: boolean,
limit: number = MAX_PERSON_NODES
): { nodes: NetworkNode[], totalCount: number } {
const anchorNodes: NetworkNode[] = [];
const centerX = width / 2;
const centerY = height / 2;
// Calculate eligible persons and their connection counts
const eligiblePersons = Array.from(personMap.entries())
.map(([pubkey, connection]) =>
buildEligiblePerson(pubkey, connection, showSignedBy, showReferenced)
)
.filter((p): p is NonNullable<typeof p> => p !== null);
// 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