2024-10-31 19:18:21 +01:00
|
|
|
|
import {
|
|
|
|
|
|
Cell,
|
|
|
|
|
|
Row,
|
|
|
|
|
|
Table,
|
|
|
|
|
|
TabSortState,
|
|
|
|
|
|
} from "@codex-storage/marketplace-ui-components";
|
2024-10-10 10:34:59 +02:00
|
|
|
|
import { getMapJSON } from "dotted-map";
|
|
|
|
|
|
import DottedMap from "dotted-map/without-countries";
|
|
|
|
|
|
import { Promises } from "../../utils/promises";
|
|
|
|
|
|
import { useQuery } from "@tanstack/react-query";
|
|
|
|
|
|
import { PeerCountryCell } from "../../components/Peers/PeerCountryCell";
|
2024-10-31 19:18:21 +01:00
|
|
|
|
import { useCallback, useRef, useState } from "react";
|
2024-10-10 10:34:59 +02:00
|
|
|
|
import { PeerPin } from "../../components/Peers/types";
|
|
|
|
|
|
import "./peers.css";
|
|
|
|
|
|
import { CodexSdk } from "../../sdk/codex";
|
2024-10-15 13:51:33 +02:00
|
|
|
|
import { ErrorBoundary } from "@sentry/react";
|
|
|
|
|
|
import { ErrorPlaceholder } from "../../components/ErrorPlaceholder/ErrorPlaceholder";
|
2024-10-30 19:29:14 +01:00
|
|
|
|
import { PeersIcon } from "../../components/Menu/PeersIcon";
|
|
|
|
|
|
import { SuccessCheckIcon } from "../../components/SuccessCheckIcon/SuccessCheckIcon";
|
|
|
|
|
|
import { ErrorCircleIcon } from "../../components/ErrorCircleIcon/ErrorCircleIcon";
|
2024-10-31 10:40:07 +01:00
|
|
|
|
import { createLazyFileRoute } from "@tanstack/react-router";
|
2024-10-31 19:18:21 +01:00
|
|
|
|
import { Network } from "../../utils/network";
|
2024-10-10 10:34:59 +02:00
|
|
|
|
|
|
|
|
|
|
// This function accepts the same arguments as DottedMap in the example above.
|
|
|
|
|
|
const mapJsonString = getMapJSON({ height: 60, grid: "diagonal" });
|
|
|
|
|
|
|
2024-10-30 19:29:14 +01:00
|
|
|
|
type CustomCSSProperties = React.CSSProperties & {
|
|
|
|
|
|
"--codex-peers-percent": number;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
type Node = {
|
|
|
|
|
|
nodeId: string;
|
|
|
|
|
|
peerId: string;
|
|
|
|
|
|
record: string;
|
|
|
|
|
|
address: string;
|
|
|
|
|
|
seen: boolean;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
type SortFn = (a: Node, b: Node) => number;
|
|
|
|
|
|
|
|
|
|
|
|
const sortByBooleanValue = (state: TabSortState) => {
|
|
|
|
|
|
return (a: Node, b: Node) => {
|
|
|
|
|
|
const order = state === "desc" ? 1 : -1;
|
|
|
|
|
|
return a?.seen === b?.seen ? 0 : b?.seen ? order : -order;
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-10-11 17:58:13 +02:00
|
|
|
|
const Peers = () => {
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const ips = useRef<Record<string, string>>({});
|
2024-10-11 17:58:13 +02:00
|
|
|
|
const [pins, setPins] = useState<[PeerPin, number][]>([]);
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const [sortFn, setSortFn] = useState<SortFn | null>(() =>
|
|
|
|
|
|
sortByBooleanValue("desc")
|
|
|
|
|
|
);
|
2024-10-11 17:58:13 +02:00
|
|
|
|
const { data } = useQuery({
|
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
|
CodexSdk.debug()
|
|
|
|
|
|
.info()
|
|
|
|
|
|
.then((s) => Promises.rejectOnError(s)),
|
|
|
|
|
|
queryKey: ["debug"],
|
|
|
|
|
|
|
|
|
|
|
|
// No need to retry because if the connection to the node
|
|
|
|
|
|
// is back again, all the queries will be invalidated.
|
|
|
|
|
|
retry: false,
|
|
|
|
|
|
|
|
|
|
|
|
// The client node should be local, so display the cache value while
|
|
|
|
|
|
// making a background request looks good.
|
|
|
|
|
|
staleTime: 0,
|
2024-10-10 10:34:59 +02:00
|
|
|
|
|
2024-10-11 17:58:13 +02:00
|
|
|
|
// Refreshing when focus returns can be useful if a user comes back
|
|
|
|
|
|
// to the UI after performing an operation in the terminal.
|
|
|
|
|
|
refetchOnWindowFocus: true,
|
|
|
|
|
|
|
|
|
|
|
|
// Throw the error to the error boundary
|
|
|
|
|
|
throwOnError: true,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const onPinAdd = useCallback(
|
|
|
|
|
|
({
|
|
|
|
|
|
countryIso,
|
|
|
|
|
|
ip,
|
|
|
|
|
|
...pin
|
|
|
|
|
|
}: PeerPin & { countryIso: string; ip: string }) => {
|
|
|
|
|
|
setPins((val) => {
|
|
|
|
|
|
const [, quantity = 0] =
|
|
|
|
|
|
val.find(([p]) => p.lat === pin.lat && p.lng == pin.lng) || [];
|
|
|
|
|
|
return [...val, [pin, quantity + 1]];
|
|
|
|
|
|
});
|
|
|
|
|
|
ips.current[ip] = countryIso;
|
|
|
|
|
|
},
|
|
|
|
|
|
[]
|
|
|
|
|
|
);
|
2024-10-11 17:58:13 +02:00
|
|
|
|
|
|
|
|
|
|
// It’s safe to re-create the map at each render, because of the
|
|
|
|
|
|
// pre-computation it’s super fast ⚡️
|
|
|
|
|
|
const map = new DottedMap({ map: JSON.parse(mapJsonString) });
|
|
|
|
|
|
|
|
|
|
|
|
pins.map(([pin, quantity]) =>
|
|
|
|
|
|
map.addPin({
|
|
|
|
|
|
lat: pin.lat,
|
|
|
|
|
|
lng: pin.lng,
|
|
|
|
|
|
svgOptions: { color: "#d6ff79", radius: 0.8 * quantity },
|
|
|
|
|
|
})
|
|
|
|
|
|
);
|
2024-10-10 10:34:59 +02:00
|
|
|
|
|
2024-10-11 17:58:13 +02:00
|
|
|
|
const svgMap = map.getSVG({
|
2024-10-30 19:29:14 +01:00
|
|
|
|
radius: 0.32,
|
|
|
|
|
|
color: "#969696",
|
2024-10-11 17:58:13 +02:00
|
|
|
|
shape: "circle",
|
2024-10-30 19:29:14 +01:00
|
|
|
|
backgroundColor: "#141414",
|
2024-10-11 17:58:13 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const onSortByCountry = (state: TabSortState) => {
|
|
|
|
|
|
if (!state) {
|
|
|
|
|
|
setSortFn(null);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setSortFn(() => (a: Node, b: Node) => {
|
|
|
|
|
|
const countryA = ips.current[Network.getIp(a.address)] || "";
|
|
|
|
|
|
const countryB = ips.current[Network.getIp(b.address)] || "";
|
|
|
|
|
|
|
|
|
|
|
|
return state === "desc"
|
|
|
|
|
|
? countryA.localeCompare(countryB)
|
|
|
|
|
|
: countryB.localeCompare(countryA);
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
2024-10-11 17:58:13 +02:00
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const onSortActive = (state: TabSortState) => {
|
|
|
|
|
|
console.info("fdf");
|
|
|
|
|
|
if (!state) {
|
|
|
|
|
|
setSortFn(null);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setSortFn(() => sortByBooleanValue(state));
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const headers = [
|
|
|
|
|
|
["Country", onSortByCountry],
|
|
|
|
|
|
["PeerId"],
|
|
|
|
|
|
["Active", onSortActive],
|
|
|
|
|
|
] satisfies [string, ((state: TabSortState) => void)?][];
|
|
|
|
|
|
|
|
|
|
|
|
const nodes = data?.table?.nodes || [];
|
|
|
|
|
|
const sorted = sortFn ? nodes.slice().sort(sortFn) : nodes;
|
|
|
|
|
|
|
|
|
|
|
|
const rows = sorted.map((node) => (
|
|
|
|
|
|
<Row
|
|
|
|
|
|
cells={[
|
|
|
|
|
|
<PeerCountryCell
|
|
|
|
|
|
onPinAdd={onPinAdd}
|
|
|
|
|
|
address={node.address}></PeerCountryCell>,
|
|
|
|
|
|
<Cell>{node.peerId}</Cell>,
|
|
|
|
|
|
<Cell>
|
|
|
|
|
|
{node.seen ? (
|
|
|
|
|
|
<div className="status--active">
|
|
|
|
|
|
<SuccessCheckIcon variant="primary"></SuccessCheckIcon> Active
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div className="status--inactive">
|
|
|
|
|
|
<ErrorCircleIcon></ErrorCircleIcon> Inactive
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Cell>,
|
|
|
|
|
|
]}></Row>
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
const actives = sorted.reduce((acc, cur) => acc + (cur.seen ? 1 : 0), 0) || 0;
|
2024-10-30 19:29:14 +01:00
|
|
|
|
const total = data?.table.nodes.length || 1;
|
2024-10-11 17:58:13 +02:00
|
|
|
|
|
2024-10-30 19:29:14 +01:00
|
|
|
|
const styles: CustomCSSProperties = {
|
|
|
|
|
|
"--codex-peers-percent": (actives / total) * 180,
|
|
|
|
|
|
};
|
2024-10-11 17:58:13 +02:00
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
const good = actives > 0;
|
|
|
|
|
|
|
2024-10-30 19:29:14 +01:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="peers">
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div dangerouslySetInnerHTML={{ __html: svgMap }}></div>
|
2024-10-31 19:18:21 +01:00
|
|
|
|
<div>
|
|
|
|
|
|
<ul>
|
|
|
|
|
|
<li>Legend</li>
|
|
|
|
|
|
<li>1-3</li>
|
|
|
|
|
|
<li>3-5</li>
|
|
|
|
|
|
<li>5 +</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
<div className="connections">
|
|
|
|
|
|
<header>
|
|
|
|
|
|
<PeersIcon></PeersIcon>
|
|
|
|
|
|
<span>Connections</span>
|
|
|
|
|
|
</header>
|
|
|
|
|
|
<main style={styles}>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div></div>
|
|
|
|
|
|
<span>{actives}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<footer>
|
|
|
|
|
|
{good ? (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<SuccessCheckIcon variant="primary"></SuccessCheckIcon>
|
|
|
|
|
|
<span>Peer connections in good standing. </span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<>
|
|
|
|
|
|
<ErrorCircleIcon />
|
|
|
|
|
|
<span>No peer connection active. </span>
|
|
|
|
|
|
</>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</footer>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2024-10-30 19:29:14 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2024-10-31 19:18:21 +01:00
|
|
|
|
<Table headers={headers} rows={rows} defaultSortIndex={2} />
|
2024-10-11 17:58:13 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-10-31 10:40:07 +01:00
|
|
|
|
export const Route = createLazyFileRoute("/dashboard/peers")({
|
2024-10-15 13:51:33 +02:00
|
|
|
|
component: () => (
|
|
|
|
|
|
<ErrorBoundary
|
|
|
|
|
|
fallback={({ error }) => (
|
|
|
|
|
|
<ErrorPlaceholder error={error} subtitle="Cannot retrieve the data." />
|
|
|
|
|
|
)}>
|
|
|
|
|
|
<Peers />
|
|
|
|
|
|
</ErrorBoundary>
|
|
|
|
|
|
),
|
2024-10-10 10:34:59 +02:00
|
|
|
|
});
|