Skip to content

Commit

Permalink
Add account stats, add loaders, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kacpersaw committed Jul 24, 2024
1 parent 8c06b24 commit 5cfa8f8
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 121 deletions.
12 changes: 6 additions & 6 deletions src/components/InfoBlock/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ const InfoBlock = ({ accounts, security, epoch, layer, rewards, smeshers }) => (
<li className="infoBlock-item">
<p className="infoBlock-item-number">
<Link to={`/${ACCOUNTS}`}>
{JSON.stringify(accounts) || (<Loader size={15} />)}
{accounts || (<Loader size={20} />)}
</Link>
</p>
<p className="infoBlock-item-title">Accounts</p>
</li>
<li className="infoBlock-item">
<p className="infoBlock-item-number">
<Link to={`/${REWARDS}`}>
{formatSmidge(rewards) || (<Loader size={15} />)}
{rewards ? formatSmidge(rewards) : (<Loader size={20} />)}
</Link>
</p>
<p className="infoBlock-item-title">smeshing rewards</p>
</li>
<li className="infoBlock-item">
<p className="infoBlock-item-number">{byteConverter(security)}</p>
<p className="infoBlock-item-number">{security ? byteConverter(security) : (<Loader size={20} />)}</p>
<p className="infoBlock-item-title">security</p>
</li>
<li className="infoBlock-item">
<p className="infoBlock-item-number">
<Link to={`/${EPOCHS}/${epoch}`}>
{JSON.stringify(epoch) || (<Loader size={15} />)}
{epoch || (<Loader size={20} />)}
</Link>
</p>
<p className="infoBlock-item-title">epoch</p>
</li>
<li className="infoBlock-item">
<p className="infoBlock-item-number">
<Link to={`/${LAYERS}/${layer}`}>
{JSON.stringify(layer) || (<Loader size={15} />)}
{layer || (<Loader size={20} />)}
</Link>
</p>
<p className="infoBlock-item-title">layer</p>
</li>
<li className="infoBlock-item">
<p className="infoBlock-item-number">
<Link to={`/${SMESHER}`}>
{JSON.stringify(smeshers) || (<Loader size={15} />)}
{smeshers || (<Loader size={20} />)}
</Link>
</p>
<p className="infoBlock-item-title">active smeshers</p>
Expand Down
14 changes: 3 additions & 11 deletions src/components/Table/AccountsRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ type Props = {
const AccountsRow = ({ data }: Props) => {
const store = useStore();
const [stats, setStats] = useState({});
const [isFetching, setIsFetching] = useState(true);

useEffect(() => {
const fetchData = async () => {
if (data && data.length > 0) {
setIsFetching(true);
try {
const promises = data.map(async (item) => {
const response = await fetch(`${store.statsApiUrl}/stats/account/${item.address}`);
const response = await fetch(`${store.statsApiUrl}/account/${item.address}`);
if (!response.ok) {
throw new Error(`Error fetching data for item ${item.address}`);
}
Expand All @@ -41,18 +39,12 @@ const AccountsRow = ({ data }: Props) => {
setStats(combinedStats);
} catch (error) {
console.error(error);
} finally {
setIsFetching(false);
}
}
};
fetchData();
}, [data]);

if (isFetching) {
return <Loader size={100} />;
}

return data && data.map((item: Spacemeshv2alpha1Account) => (
<div key={nanoid()} className="tr">
<div className="td">
Expand All @@ -61,10 +53,10 @@ const AccountsRow = ({ data }: Props) => {
</Link>
</div>
<div className="td">
{stats[item.address] ? formatSmidge(stats[item.address].sent) : '---'}
{stats[item.address] ? formatSmidge(stats[item.address].sent) : <Loader size={20} />}
</div>
<div className="td">
{stats[item.address] ? formatSmidge(stats[item.address].received) : '---'}
{stats[item.address] ? formatSmidge(stats[item.address].received) : <Loader size={20} />}
</div>
<div className="td">
<CustomTimeAgo time={store.layerTimestamp(item.current.layer)} />
Expand Down
22 changes: 9 additions & 13 deletions src/components/Table/EpochsRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import {
} from '../../config/constants';
import CustomTimeAgo from '../CustomTimeAgo';
import { useStore } from '../../store';
import Loader from '../Loader';
import { formatSmidge } from '../../helper/converter';
import Loader from '../Loader';

const EpochsRow = ({ data }) => {
const store = useStore();

const [stats, setStats] = useState({});
const [isFetching, setIsFetching] = useState(true);

useEffect(() => {
const fetchData = async () => {
if (data && data.length > 0) {
setIsFetching(true);
try {
const promises = data.map(async (item) => {
const response = await fetch(`${store.statsApiUrl}/stats/epoch/${item.number}`);
const response = await fetch(`${store.statsApiUrl}/epoch/${item.number}`);
if (!response.ok) {
throw new Error(`Error fetching data for item ${item.number}`);
}
Expand All @@ -39,18 +37,12 @@ const EpochsRow = ({ data }) => {
setStats(combinedStats);
} catch (error) {
console.error(error);
} finally {
setIsFetching(false);
}
}
};
fetchData();
}, [data]);

if (isFetching) {
return <Loader size={100} />;
}

return data && data.map((item) => {
const futureEpoch = store.layerTimestamp(item.startLayer) * 1000 > Date.now();
return (
Expand All @@ -67,14 +59,18 @@ const EpochsRow = ({ data }) => {
<Link to={`/${EPOCHS}/${item.number}/${LAYERS}`}>{item.layers}</Link>
</div>
<div className="td">
<Link to={`/${EPOCHS}/${item.number}/${TXNS}`}>{stats[item.number].transactions_count}</Link>
<Link to={`/${EPOCHS}/${item.number}/${TXNS}`}>
{stats[item.number] ? stats[item.number]?.transactions_count : <Loader size={20} />}
</Link>
</div>
<div className="td">
<Link to={`/${EPOCHS}/${item.number}/${SMESHER}`}>{stats[item.number].activations_count}</Link>
<Link to={`/${EPOCHS}/${item.number}/${SMESHER}`}>
{stats[item.number] ? stats[item.number]?.activations_count : <Loader size={20} />}
</Link>
</div>
<div className="td" style={{ flexGrow: 2 }}>
<Link to={`/${EPOCHS}/${item.number}/${REWARDS}`}>
{formatSmidge(stats[item.number].rewards_sum)}
{stats[item.number] ? formatSmidge(stats[item.number].rewards_sum || 0) : <Loader size={20} />}
</Link>
</div>
</div>
Expand Down
16 changes: 5 additions & 11 deletions src/components/Table/LayersRow.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ const LayersRow = ({ data }: Props) => {
const store = useStore();

const [stats, setStats] = useState({});
const [isFetching, setIsFetching] = useState(true);

useEffect(() => {
const fetchData = async () => {
if (data && data.length > 0) {
setIsFetching(true);
try {
const promises = data.map(async (item) => {
const response = await fetch(`${store.statsApiUrl}/stats/layer/${item.number}`);
const response = await fetch(`${store.statsApiUrl}/layer/${item.number}`);
if (!response.ok) {
throw new Error(`Error fetching data for item ${item.number}`);
}
Expand All @@ -42,18 +40,12 @@ const LayersRow = ({ data }: Props) => {
setStats(combinedStats);
} catch (error) {
console.error(error);
} finally {
setIsFetching(false);
}
}
};
fetchData();
}, [data]);

if (isFetching) {
return <Loader size={100} />;
}

return data && data.map((item: Spacemeshv2alpha1Layer) => (
<div key={nanoid()} className="tr">
<div className="td">
Expand All @@ -66,12 +58,14 @@ const LayersRow = ({ data }: Props) => {
</div>
<div className="td">
<Link to={`/${LAYERS}/${item.number}/${TXNS}`}>
{`${stats[item.number].transactions_count} Transactions (${formatSmidge(stats[item.number].transactions_sum)})`}
{ stats[item.number] ? (
`${stats[item.number].transactions_count} Transactions (${formatSmidge(stats[item.number].transactions_sum)})`
) : <Loader size={20} /> }
</Link>
</div>
<div className="td">
<Link to={`/${LAYERS}/${item.number}/${REWARDS}`}>
{formatSmidge(stats[item.number].rewards_sum)}
{stats[item.number] ? formatSmidge(stats[item.number].rewards_sum) : <Loader size={20} />}
</Link>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/components/Table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ const Table = ({ name, subPage, id, epochs }: Props) => {
getData(nextOffset);
};

if (isFetching) {
return <Loader size={100} />;
}

return (
<>
<div className="table">
Expand Down
29 changes: 12 additions & 17 deletions src/routes/accounts/account.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { Link, useParams } from 'react-router-dom';
import { Spacemeshv2alpha1Account } from 'api';
import TitleBlock from '../../components/TitleBlock';
import { getColorByPageName } from '../../helper/getColorByPageName';

import {
ACCOUNTS, REWARDS, TXNS,
} from '../../config/constants';
import RightSideBlock from '../../components/CountBlock/RightSideBlock';
import { useStore } from '../../store';
import Loader from '../../components/Loader';
import { parseSmidge } from '../../helper/converter';
import { formatSmidge, parseSmidge } from '../../helper/converter';
import CopyButton from '../../components/CopyButton';
import Table from '../../components/Table';

Expand All @@ -24,6 +23,7 @@ const Account = () => {
const [data, setData] = useState <Spacemeshv2alpha1Account>();
const [totalRewards, setTotalRewards] = useState(0);
const [totalTransactions, setTotalTransactions] = useState(0);
const [rewardsSum, setRewardsSum] = useState(0);
const [smidge, setSmidge] = useState({ value: 0, unit: 'SMH' });

useEffect(() => {
Expand All @@ -37,21 +37,12 @@ const Account = () => {
}, [params.id]);

useEffect(() => {
store.api.reward.rewardServiceList({
coinbase: params.id,
limit: 1,
}).then((res) => {
setTotalRewards(res.total);
});
}, [params.id]);

useEffect(() => {
store.api.transaction.transactionServiceList({
address: params.id,
limit: 1,
}).then((res) => {
setTotalTransactions(res.total);
});
fetch(`${store.statsApiUrl}/account/${params.id}`).then((res) => res.json())
.then((res) => {
setRewardsSum(formatSmidge(res.rewards_sum));
setTotalRewards(res.rewards_count);
setTotalTransactions(res.transactions_count);
});
}, [params.id]);

return (
Expand Down Expand Up @@ -89,6 +80,10 @@ const Account = () => {
<span className="item-value">
<Link to={`/${ACCOUNTS}/${data.address}/${REWARDS}`}>
{totalRewards}
{' '}
(
{rewardsSum}
)
</Link>
</span>
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/accounts/accounts.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Accounts = () => {
/>
<RightSideBlock
color={getColorByPageName(ACCOUNTS)}
number={store.overview.accounts_count}
number={store.overview?.accounts_count || 0}
unit="accounts"
startTime={recentActivity || 0}
label="Recent activity"
Expand Down
2 changes: 1 addition & 1 deletion src/routes/epochs/epoch-rewards.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const EpochRewards = () => {

useEffect(() => {
if (store.netInfo === null) return;
fetch(`${store.statsApiUrl}/stats/epoch/${params.id}`).then((res) => res.json()).then((res) => {
fetch(`${store.statsApiUrl}/epoch/${params.id}`).then((res) => res.json()).then((res) => {
setStats(res);
});

Expand Down
2 changes: 1 addition & 1 deletion src/routes/epochs/epoch-smeshers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const EpochSmeshers = () => {

useEffect(() => {
if (store.netInfo === null) return;
fetch(`${store.statsApiUrl}/stats/epoch/${params.id}`).then((res) => res.json()).then((res) => {
fetch(`${store.statsApiUrl}/epoch/${params.id}`).then((res) => res.json()).then((res) => {
setStats(res);
const epochStart = params.id * store.netInfo.layersPerEpoch;
setStart(epochStart);
Expand Down
2 changes: 1 addition & 1 deletion src/routes/epochs/epoch-txns.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const EpochTxns = () => {

useEffect(() => {
if (store.netInfo === null) return;
fetch(`${store.statsApiUrl}/stats/epoch/${params.id}`).then((res) => res.json()).then((res) => {
fetch(`${store.statsApiUrl}/epoch/${params.id}`).then((res) => res.json()).then((res) => {
setStats(res);
});

Expand Down
17 changes: 9 additions & 8 deletions src/routes/epochs/epoch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useStore } from '../../store';
import { formatSmidge } from '../../helper/converter';
import CustomTimeAgo from '../../components/CustomTimeAgo';
import { fullDate } from '../../helper/formatter';
import Loader from '../../components/Loader';

const Epoch = () => {
const store = useStore();
Expand All @@ -19,11 +20,11 @@ const Epoch = () => {

const [start, setStart] = useState(0);
const [end, setEnd] = useState(0);
const [stats, setStats] = useState({});
const [stats, setStats] = useState(null);

useEffect(() => {
if (store.netInfo === null) return;
fetch(`${store.statsApiUrl}/stats/epoch/${params.id}`).then((res) => res.json()).then((res) => {
if (store.netInfo === null || store.netInfo.layersPerEpoch === null) return;
fetch(`${store.statsApiUrl}/epoch/${params.id}`).then((res) => res.json()).then((res) => {
setStats(res);
});

Expand All @@ -43,7 +44,7 @@ const Epoch = () => {
/>
<RightSideBlock
color={getColorByPageName(name)}
number={store.netInfo.layersPerEpoch}
number={store.netInfo?.layersPerEpoch || 0}
unit="layers"
startTime={store.layerTimestamp(start)}
/>
Expand Down Expand Up @@ -75,27 +76,27 @@ const Epoch = () => {
<li className="item">
<span className="item-name">Layers</span>
<span className="item-value">
<Link to={`/${EPOCHS}/${params.id}/${LAYERS}`}>{store.netInfo.layersPerEpoch}</Link>
<Link to={`/${EPOCHS}/${params.id}/${LAYERS}`}>{store.netInfo?.layersPerEpoch || 0}</Link>
</span>
</li>
<li className="item">
<span className="item-name">Rewards</span>
<span className="item-value">
<Link to={`/${EPOCHS}/${params.id}/${REWARDS}`}>
{`${stats.rewards_count} (${formatSmidge(stats.rewards_sum)})`}
{stats ? `${stats.rewards_count} (${formatSmidge(stats.rewards_sum)})` : <Loader size={20} />}
</Link>
</span>
</li>
<li className="item">
<span className="item-name">Smeshers</span>
<span className="item-value">
<Link to={`/${EPOCHS}/${params.id}/${SMESHER}`}>{stats.smeshers_count}</Link>
<Link to={`/${EPOCHS}/${params.id}/${SMESHER}`}>{stats ? stats.smeshers_count : <Loader size={20} />}</Link>
</span>
</li>
<li className="item">
<span className="item-name">Transactions</span>
<span className="item-value">
<Link to={`/${EPOCHS}/${params.id}/${TXNS}`}>{stats.transactions_count}</Link>
<Link to={`/${EPOCHS}/${params.id}/${TXNS}`}>{stats ? stats.transactions_count : <Loader size={20} />}</Link>
</span>
</li>
</ul>
Expand Down
Loading

0 comments on commit 5cfa8f8

Please sign in to comment.