diff --git a/hydra-explorer/web/src/components/HeadsDashboard/index.tsx b/hydra-explorer/web/src/components/HeadsDashboard/index.tsx index 382fb70adb9..91e3a24290d 100644 --- a/hydra-explorer/web/src/components/HeadsDashboard/index.tsx +++ b/hydra-explorer/web/src/components/HeadsDashboard/index.tsx @@ -1,6 +1,7 @@ "use client" // This is a client component 👈🏽 import { useHeadsData } from '@/providers/HeadsDataProvider' +import { totalLovelaceValueLocked } from '@/utils' import React from 'react' const HeadsDashboard = () => { @@ -8,16 +9,7 @@ const HeadsDashboard = () => { const totalHeads = heads.length - let totalLockedMoney = 0 - heads.forEach(head => { - head.members?.forEach((member) => { - if (member.commits && Object.keys(member.commits).length > 0) { - Object.values(member.commits).forEach((commit) => { - totalLockedMoney += (commit.value.lovelace) - }) - } - }) - }) + const totalLockedMoney = heads.reduce((total, head) => total + totalLovelaceValueLocked(head), 0) return (
diff --git a/hydra-explorer/web/src/components/HeadsTable/index.tsx b/hydra-explorer/web/src/components/HeadsTable/index.tsx index 36de252650d..7a3022c9dd2 100644 --- a/hydra-explorer/web/src/components/HeadsTable/index.tsx +++ b/hydra-explorer/web/src/components/HeadsTable/index.tsx @@ -4,6 +4,7 @@ import React, { useState } from 'react' import { HeadState } from "@/app/model" import { useHeadsData } from "@/providers/HeadsDataProvider" import HeadDetails from "../HeadDetails" +import { totalLovelaceValueLocked } from '@/utils' const HeadsTable: React.FC = () => { const { heads, error } = useHeadsData() @@ -13,18 +14,6 @@ const HeadsTable: React.FC = () => { setSelectedHead(head) } - const totalValueLocked = (head: HeadState) => { - let totalLockedMoney = 0 - head.members?.forEach((member) => { - if (member.commits && Object.keys(member.commits).length > 0) { - Object.values(member.commits).forEach((commit) => { - totalLockedMoney += (commit.value.lovelace) - }) - } - }) - return totalLockedMoney; - } - return (
{error ? ( @@ -44,18 +33,18 @@ const HeadsTable: React.FC = () => { - {heads?.map((entry, index) => ( + {heads?.map((head, index) => ( - {entry.headId} - {entry.status} - {entry.point.slot} - {entry.blockNo} - {entry.point.blockHash} - {totalValueLocked(entry) / 1000000} ₳ + {head.headId} + {head.status} + {head.point.slot} + {head.blockNo} + {head.point.blockHash} + {totalLovelaceValueLocked(head) / 1000000} ₳ diff --git a/hydra-explorer/web/src/utils.tsx b/hydra-explorer/web/src/utils.tsx new file mode 100644 index 00000000000..48ac5a60db1 --- /dev/null +++ b/hydra-explorer/web/src/utils.tsx @@ -0,0 +1,12 @@ +import { HeadState } from "./app/model" + +export const totalLovelaceValueLocked = (head: HeadState) => { + return (head.members || []).reduce((total, member) => { + if (member.commits && Object.keys(member.commits).length > 0) { + return total + Object.values(member.commits).reduce((memberTotal, commit) => { + return memberTotal + commit.value.lovelace + }, 0) + } + return total + }, 0) +} \ No newline at end of file