Skip to content

Commit 0b9f97e

Browse files
committed
added ping cluster
1 parent 17b4e6b commit 0b9f97e

1 file changed

Lines changed: 37 additions & 4 deletions

File tree

apps/box/src/components/ConnectionOptionsSheet.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useBloxsStore } from '../stores';
1212
import axios from 'axios';
1313

1414
const PING_URL = 'https://pools.fx.land/ping';
15+
const PING_CLUSTER_URL = 'https://pools.fx.land/ping-cluster';
1516
const POOLS_URL = 'https://pools.fx.land';
1617

1718
type PingStatus = 'idle' | 'pinging' | 'connected' | 'disconnected' | 'error';
@@ -55,6 +56,38 @@ async function pingPeerId(peerId: string): Promise<{ status: PingStatus; message
5556
}
5657
}
5758

59+
async function pingCluster(peerId: string): Promise<{ status: PingStatus; message?: string }> {
60+
// Step 1: Check that pools.fx.land is accessible
61+
try {
62+
await axios.get(POOLS_URL, { timeout: 10000, validateStatus: () => true });
63+
} catch {
64+
return { status: 'error', message: 'Cannot reach pools.fx.land' };
65+
}
66+
67+
// Step 2: Ping the cluster using the kubo peerId
68+
try {
69+
const response = await axios.post(PING_CLUSTER_URL, { peerId }, { timeout: 60000 });
70+
const data = response.data;
71+
72+
if (data?.status === 'err') {
73+
return { status: 'error', message: data.msg || 'Rate limited' };
74+
}
75+
if (data?.success === true) {
76+
return { status: 'connected', message: `${data.latency}ms` };
77+
}
78+
return { status: 'disconnected', message: 'Not reachable' };
79+
} catch (err: any) {
80+
const data = err?.response?.data;
81+
if (data?.status === 'err') {
82+
return { status: 'error', message: data.msg || 'Rate limited' };
83+
}
84+
if (data?.success === false) {
85+
return { status: 'disconnected', message: 'Not reachable' };
86+
}
87+
return { status: 'error', message: err?.message || 'Ping failed' };
88+
}
89+
}
90+
5891
function PingStatusText({ status, message }: { status: PingStatus; message?: string }) {
5992
const { colors } = useFxTheme();
6093

@@ -102,13 +135,13 @@ export const ConnectionOptionsSheet = React.forwardRef<
102135
}, [currentBloxPeerId, bloxPingStatus]);
103136

104137
const handlePingCluster = useCallback(async () => {
105-
if (!clusterPeerId || clusterPingStatus === 'pinging') return;
138+
if (!currentBloxPeerId || clusterPingStatus === 'pinging') return;
106139
setClusterPingStatus('pinging');
107140
setClusterPingMessage(undefined);
108-
const result = await pingPeerId(clusterPeerId);
141+
const result = await pingCluster(currentBloxPeerId);
109142
setClusterPingStatus(result.status);
110143
setClusterPingMessage(result.message);
111-
}, [clusterPeerId, clusterPingStatus]);
144+
}, [currentBloxPeerId, clusterPingStatus]);
112145

113146
return (
114147
<FxBottomSheetModal ref={ref}>
@@ -142,7 +175,7 @@ export const ConnectionOptionsSheet = React.forwardRef<
142175
</FxPressableOpacity>
143176
)}
144177

145-
{clusterPeerId && (
178+
{currentBloxPeerId && (
146179
<FxPressableOpacity
147180
paddingVertical='8'
148181
paddingHorizontal='8'

0 commit comments

Comments
 (0)