@@ -3,22 +3,17 @@ import { CheckCircleIcon, XCircleIcon } from '@heroicons/react/20/solid'
3
3
import Nav from '@/components/nav'
4
4
import { useLibp2pContext } from '@/context/ctx'
5
5
import { useInterval } from 'usehooks-ts'
6
-
6
+ import type { PeerId } from '@libp2p/interface-peer-id'
7
7
import type { Connection } from '@libp2p/interface-connection'
8
8
import { usePeerContext } from '../context/peer-ctx'
9
+ import { useEffect } from 'react'
9
10
10
11
11
12
export default function Home ( ) {
12
13
const { libp2p } = useLibp2pContext ( )
13
14
const { peerStats, setPeerStats } = usePeerContext ( )
14
15
15
16
useInterval ( ( ) => {
16
- const getConnectedPeers = async ( ) => {
17
- return await libp2p . getPeers ( )
18
- }
19
- const getConnections = async ( ) => {
20
- return await libp2p . getConnections ( )
21
- }
22
17
23
18
const ping = async ( ) => {
24
19
const { peerIds } = peerStats
@@ -36,16 +31,20 @@ export default function Home() {
36
31
. catch ( ( e ) => {
37
32
console . error ( e , e ?. error )
38
33
} )
34
+ } , 5000 )
39
35
40
- getConnectedPeers ( ) . then ( ( peerIds ) => {
41
- setPeerStats ( { ...peerStats , peerIds, connected : true } )
42
- } )
36
+ useEffect ( ( ) => {
37
+ const peerConnectedCB = ( evt : CustomEvent < Connection > ) => {
38
+ const connection = evt . detail
39
+ setPeerStats ( { ...peerStats , peerIds : [ ...peerStats . peerIds , connection . remotePeer ] , connections : [ ...peerStats . connections , connection ] , connected : true } )
40
+ }
43
41
44
- getConnections ( ) . then ( ( connections ) => {
45
- // If one of the connected peers matches the one in input we're connected
46
- setPeerStats ( { ...peerStats , connections } )
47
- } )
48
- } , 5000 )
42
+ libp2p . addEventListener ( 'peer:connect' , peerConnectedCB )
43
+
44
+ return ( ) => {
45
+ libp2p . removeEventListener ( 'peer:connect' , peerConnectedCB )
46
+ }
47
+ } , [ libp2p , peerStats , setPeerStats ] )
49
48
50
49
const getUniqueConnections = ( connections : Connection [ ] ) => {
51
50
const uniqueConnections : Connection [ ] = [ ]
@@ -123,33 +122,3 @@ export default function Home() {
123
122
</ >
124
123
)
125
124
}
126
-
127
- export function Stats (
128
- stats : { name : string ; stat : string } [ ] = [
129
- { name : 'Peer Ping Latency' , stat : '' } ,
130
- { name : 'Peer Count' , stat : '' } ,
131
- ] ,
132
- ) {
133
- return (
134
- < div >
135
- < h3 className = "text-base font-semibold leading-6 text-gray-900" >
136
- Last 30 days
137
- </ h3 >
138
- < dl className = "mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3" >
139
- { stats . map ( ( item ) => (
140
- < div
141
- key = { item . name }
142
- className = "overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6"
143
- >
144
- < dt className = "truncate text-sm font-medium text-gray-500" >
145
- { item . name }
146
- </ dt >
147
- < dd className = "mt-1 text-3xl font-semibold tracking-tight text-gray-900" >
148
- { item . stat }
149
- </ dd >
150
- </ div >
151
- ) ) }
152
- </ dl >
153
- </ div >
154
- )
155
- }
0 commit comments