chore(buddybook): sort by signatures

This commit is contained in:
Danish Arora 2024-11-13 12:37:37 +07:00
parent 7613b1e9e4
commit 7c5cfcdbf4
No known key found for this signature in database
GPG Key ID: 1C6EF37CDAE1426E
2 changed files with 18 additions and 1 deletions

View File

@ -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[];
@ -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">
@ -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>

View File

@ -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;
});
}