Skip to content

Commit

Permalink
refactor: useMemo and inline function.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthnTuttle committed Aug 23, 2023
1 parent 3a1af07 commit bdbb4f0
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions apps/guardian-ui/src/admin/FederationAdmin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useEffect, useState, useMemo } from 'react';
import {
Flex,
Card,
Expand All @@ -18,9 +18,7 @@ import { CopyInput } from '@fedimint/ui';
import { useTranslation } from '@fedimint/utils';
import { useAdminContext } from '../hooks';
import {
ClientConfig,
ConfigResponse,
FederationStatus,
Gateway,
PeerStatus,
StatusResponse,
Expand All @@ -38,27 +36,6 @@ interface PeerData {
status: PeerStatus;
}

function generatePeerData(
config: ClientConfig,
federationStatus: FederationStatus
): PeerData[] {
const peerDataArray: PeerData[] = [];
for (const [id, status] of Object.entries(federationStatus.status_by_peer)) {
const numericId = parseInt(id, 10);
if (Object.prototype.hasOwnProperty.call(config.api_endpoints, numericId)) {
const endpoint = config.api_endpoints[numericId];
if (endpoint) {
peerDataArray.push({
id: numericId,
name: endpoint.name,
status,
});
}
}
}
return peerDataArray;
}

export const FederationAdmin: React.FC = () => {
const { api } = useAdminContext();
const [versions, setVersions] = useState<Versions>();
Expand All @@ -68,7 +45,6 @@ export const FederationAdmin: React.FC = () => {
const [config, setConfig] = useState<ConfigResponse>();
const [gateways, setGateways] = useState<Gateway[]>([]);
const [guardians, setGuardians] = useState<string | undefined>();
const [peerData, setPeerData] = useState<PeerData[] | undefined>();
const { t } = useTranslation();

useEffect(() => {
Expand Down Expand Up @@ -108,10 +84,23 @@ export const FederationAdmin: React.FC = () => {
setGuardians(`${online} / ${totalPeers}`);
}, [status]);

useEffect(() => {
const data = useMemo(() => {
if (status && status.federation && config) {
const data = generatePeerData(config.client_config, status.federation);
setPeerData(data);
const peerDataArray: PeerData[] = [];
for (const [id, federationStatus] of Object.entries(
status.federation.status_by_peer
)) {
const numericId = parseInt(id, 10);
const endpoint = config.client_config.api_endpoints[numericId];
if (endpoint) {
peerDataArray.push({
id: numericId,
name: endpoint.name,
status: federationStatus,
});
}
}
return peerDataArray;
}
}, [status, config]);

Expand Down Expand Up @@ -208,8 +197,8 @@ export const FederationAdmin: React.FC = () => {
</Tr>
</Thead>
<Tbody>
{peerData &&
peerData?.map((x) => (
{data &&
data?.map((x) => (
<Tr key={x.id}>
<Td>{x.id}</Td>
<Td>{x.name}</Td>
Expand Down

0 comments on commit bdbb4f0

Please sign in to comment.