From 7c5cfcdbf4ca8cb7488d69527cf77644bb685400 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Wed, 13 Nov 2024 12:37:37 +0700 Subject: [PATCH] chore(buddybook): sort by signatures --- .../src/components/Chain/View/ChainList.tsx | 4 +++- examples/buddybook/src/lib/waku.ts | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/examples/buddybook/src/components/Chain/View/ChainList.tsx b/examples/buddybook/src/components/Chain/View/ChainList.tsx index ee88d50..2649135 100644 --- a/examples/buddybook/src/components/Chain/View/ChainList.tsx +++ b/examples/buddybook/src/components/Chain/View/ChainList.tsx @@ -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 = ({ chainsData, onChainUpdate, isLoad }; const rootBlocks = chainsData.filter(block => !block.parentBlockUUID); + const sortedRootBlocks = sortBlocksBySignatures(rootBlocks, chainsData); return ( @@ -124,7 +126,7 @@ const ChainList: React.FC = ({ chainsData, onChainUpdate, isLoad

No books found.

) : (
    - {rootBlocks.map((block) => renderBlock(block, 0))} + {sortedRootBlocks.map((block) => renderBlock(block, 0))}
)} diff --git a/examples/buddybook/src/lib/waku.ts b/examples/buddybook/src/lib/waku.ts index 0b20bf9..a9077f5 100644 --- a/examples/buddybook/src/lib/waku.ts +++ b/examples/buddybook/src/lib/waku.ts @@ -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; + }); +}