Skip to content

Commit

Permalink
chore(buddybook): sort by signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
danisharora099 committed Nov 14, 2024
1 parent 7613b1e commit 7c5cfcd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/buddybook/src/components/Chain/View/ChainList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, DialogFooter } from "@/components/ui/dialog";
import QRCode from '@/components/QRCode';
import { Loader2 } from "lucide-react";
import { sortBlocksBySignatures } from '@/lib/waku';

interface ChainListProps {
chainsData: BlockPayload[];
Expand Down Expand Up @@ -105,6 +106,7 @@ const ChainList: React.FC<ChainListProps> = ({ chainsData, onChainUpdate, isLoad
};

const rootBlocks = chainsData.filter(block => !block.parentBlockUUID);
const sortedRootBlocks = sortBlocksBySignatures(rootBlocks, chainsData);

return (
<Card className="w-full max-w-4xl mx-auto">
Expand All @@ -124,7 +126,7 @@ const ChainList: React.FC<ChainListProps> = ({ chainsData, onChainUpdate, isLoad
<p>No books found.</p>
) : (
<ul className="space-y-4">
{rootBlocks.map((block) => renderBlock(block, 0))}
{sortedRootBlocks.map((block) => renderBlock(block, 0))}
</ul>
)}
</CardContent>
Expand Down
15 changes: 15 additions & 0 deletions examples/buddybook/src/lib/waku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,18 @@ export async function subscribeToFilter(node: LightNode, callback: (message: Blo
throw new Error("Failed to subscribe to filter")
}
}

export function calculateTotalSignatures(block: BlockPayload, allBlocks: BlockPayload[]): number {
const childBlocks = allBlocks.filter(b => b.parentBlockUUID === block.blockUUID);
return block.signatures.length + childBlocks.reduce((acc, child) =>
acc + calculateTotalSignatures(child, allBlocks), 0
);
}

export function sortBlocksBySignatures(blocks: BlockPayload[], allBlocks: BlockPayload[]): BlockPayload[] {
return [...blocks].sort((a, b) => {
const totalSignaturesA = calculateTotalSignatures(a, allBlocks);
const totalSignaturesB = calculateTotalSignatures(b, allBlocks);
return totalSignaturesB - totalSignaturesA;
});
}

0 comments on commit 7c5cfcd

Please sign in to comment.