mirror of
https://github.com/waku-org/js-waku-lab.git
synced 2025-01-12 18:14:12 +00:00
chore(buddybook): sort by signatures
This commit is contained in:
parent
7613b1e9e4
commit
7c5cfcdbf4
@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button';
|
|||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, DialogFooter } from "@/components/ui/dialog";
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, DialogFooter } from "@/components/ui/dialog";
|
||||||
import QRCode from '@/components/QRCode';
|
import QRCode from '@/components/QRCode';
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { sortBlocksBySignatures } from '@/lib/waku';
|
||||||
|
|
||||||
interface ChainListProps {
|
interface ChainListProps {
|
||||||
chainsData: BlockPayload[];
|
chainsData: BlockPayload[];
|
||||||
@ -105,6 +106,7 @@ const ChainList: React.FC<ChainListProps> = ({ chainsData, onChainUpdate, isLoad
|
|||||||
};
|
};
|
||||||
|
|
||||||
const rootBlocks = chainsData.filter(block => !block.parentBlockUUID);
|
const rootBlocks = chainsData.filter(block => !block.parentBlockUUID);
|
||||||
|
const sortedRootBlocks = sortBlocksBySignatures(rootBlocks, chainsData);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="w-full max-w-4xl mx-auto">
|
<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>
|
<p>No books found.</p>
|
||||||
) : (
|
) : (
|
||||||
<ul className="space-y-4">
|
<ul className="space-y-4">
|
||||||
{rootBlocks.map((block) => renderBlock(block, 0))}
|
{sortedRootBlocks.map((block) => renderBlock(block, 0))}
|
||||||
</ul>
|
</ul>
|
||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
|
@ -142,3 +142,18 @@ export async function subscribeToFilter(node: LightNode, callback: (message: Blo
|
|||||||
throw new Error("Failed to subscribe to filter")
|
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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user