@@ -3,6 +3,7 @@ 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 type { Connection } from '@libp2p/interface'
6
+ import { PeerId } from '@libp2p/interface'
6
7
import { usePeerContext } from '../context/peer-ctx'
7
8
import { useCallback , useEffect , useState } from 'react'
8
9
import Image from 'next/image'
@@ -25,7 +26,7 @@ export default function Home() {
25
26
26
27
setPeerStats ( {
27
28
...peerStats ,
28
- peerIds : connections . map ( conn => conn . remotePeer ) ,
29
+ peerIds : connections . map ( ( conn ) => conn . remotePeer ) ,
29
30
connections : connections ,
30
31
connected : connections . length > 0 ,
31
32
} )
@@ -42,7 +43,7 @@ export default function Home() {
42
43
43
44
setListenAddresses ( {
44
45
...listenAddresses ,
45
- multiaddrs
46
+ multiaddrs,
46
47
} )
47
48
} , 1000 )
48
49
@@ -52,24 +53,23 @@ export default function Home() {
52
53
} , [ libp2p , listenAddresses , setListenAddresses ] )
53
54
54
55
type PeerProtoTuple = {
55
- peerId : string
56
+ peerId : PeerId
56
57
protocols : string [ ]
57
58
}
58
59
59
60
const getFormattedConnections = ( connections : Connection [ ] ) : PeerProtoTuple [ ] => {
60
- const protoNames : Map < string , string [ ] > = new Map ( )
61
+ const protoNames : Map < PeerId , string [ ] > = new Map ( )
61
62
62
63
connections . forEach ( ( conn ) => {
63
- const exists = protoNames . get ( conn . remotePeer . toString ( ) )
64
+ const exists = protoNames . get ( conn . remotePeer )
64
65
const dedupedProtonames = [ ...new Set ( conn . remoteAddr . protoNames ( ) ) ]
65
66
66
67
if ( exists ?. length ) {
67
68
const namesToAdd = dedupedProtonames . filter ( ( name ) => ! exists . includes ( name ) )
68
69
// console.log('namesToAdd: ', namesToAdd)
69
- protoNames . set ( conn . remotePeer . toString ( ) , [ ...exists , ...namesToAdd ] )
70
-
70
+ protoNames . set ( conn . remotePeer , [ ...exists , ...namesToAdd ] )
71
71
} else {
72
- protoNames . set ( conn . remotePeer . toString ( ) , dedupedProtonames )
72
+ protoNames . set ( conn . remotePeer , dedupedProtonames )
73
73
}
74
74
} )
75
75
@@ -113,6 +113,10 @@ export default function Home() {
113
113
[ setMultiaddr ] ,
114
114
)
115
115
116
+ const handleDisconnectPeer = useCallback ( ( peerId : PeerId ) => {
117
+ libp2p . hangUp ( peerId )
118
+ } , [ libp2p ] )
119
+
116
120
return (
117
121
< >
118
122
< Head >
@@ -128,12 +132,7 @@ export default function Home() {
128
132
< div className = "mx-auto max-w-7xl px-2 sm:px-6 lg:px-8" >
129
133
< h1 className = "text-3xl font-bold leading-tight tracking-tight text-gray-900 flex flex-row" >
130
134
< p className = "mr-4" > Universal Connectivity</ p >
131
- < Image
132
- src = "/libp2p-hero.svg"
133
- alt = "libp2p logo"
134
- height = "46"
135
- width = "46"
136
- />
135
+ < Image src = "/libp2p-hero.svg" alt = "libp2p logo" height = "46" width = "46" />
137
136
</ h1 >
138
137
</ div >
139
138
</ header >
@@ -144,19 +143,12 @@ export default function Home() {
144
143
</ ul >
145
144
Addresses:
146
145
< ul className = "my-2 space-y-2 break-all" >
147
- {
148
- listenAddresses . multiaddrs . map ( ( ma , index ) => {
149
- return (
150
- < li key = { `ma-${ index } ` } > { ma . toString ( ) } </ li >
151
- )
152
- } )
153
- }
146
+ { listenAddresses . multiaddrs . map ( ( ma , index ) => {
147
+ return < li key = { `ma-${ index } ` } > { ma . toString ( ) } </ li >
148
+ } ) }
154
149
</ ul >
155
150
< div className = "my-6 w-1/2" >
156
- < label
157
- htmlFor = "peer-id"
158
- className = "block text-sm font-medium leading-6 text-gray-900"
159
- >
151
+ < label htmlFor = "peer-id" className = "block text-sm font-medium leading-6 text-gray-900" >
160
152
multiaddr to connect to
161
153
</ label >
162
154
< div className = "mt-2" >
@@ -173,16 +165,17 @@ export default function Home() {
173
165
</ div >
174
166
< button
175
167
type = "button"
176
- className = { "rounded-md bg-indigo-600 my-2 py-2 px-3 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" + ( dialling ? ' cursor-not-allowed' : '' ) }
168
+ className = {
169
+ 'rounded-md bg-indigo-600 my-2 py-2 px-3 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600' +
170
+ ( dialling ? ' cursor-not-allowed' : '' )
171
+ }
177
172
onClick = { handleConnectToMultiaddr }
178
173
disabled = { dialling }
179
174
>
180
- { dialling && < Spinner /> } { ' ' }
181
- Connect{ dialling && 'ing' } to multiaddr
175
+ { dialling && < Spinner /> } Connect{ dialling && 'ing' } to multiaddr
182
176
</ button >
183
177
{ err && < p className = "text-red-500" > { err } </ p > }
184
178
</ div >
185
-
186
179
< div className = "my-4 inline-flex items-center text-xl" >
187
180
Connected:{ ' ' }
188
181
{ peerStats . connected ? (
@@ -198,14 +191,17 @@ export default function Home() {
198
191
{ ' ' }
199
192
Connected peers ({ getFormattedConnections ( peerStats . connections ) . length } ) 👇
200
193
</ h3 >
201
- < pre className = "px-2" >
202
- { getFormattedConnections ( peerStats . connections )
203
- . map (
204
- ( pair ) =>
205
- `${ pair . peerId } (${ pair . protocols . join ( ', ' ) } )` ,
206
- )
207
- . join ( '\n' ) }
208
- </ pre >
194
+
195
+ < ul className = "divide-y divide-gray-100" >
196
+ { getFormattedConnections ( peerStats . connections ) . map ( ( pair ) => (
197
+ < li key = { pair . peerId . toString ( ) } className = "py-1 flex justify-between items-center" >
198
+ < span > { `${ pair . peerId } (${ pair . protocols . join ( ', ' ) } )` } </ span >
199
+ < button onClick = { ( ) => handleDisconnectPeer ( pair . peerId ) } className = "bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded flex flex-row" >
200
+ < XCircleIcon className = "w-6 h-6" /> Disconnect
201
+ </ button >
202
+ </ li >
203
+ ) ) }
204
+ </ ul >
209
205
</ >
210
206
) : null }
211
207
</ div >
0 commit comments