-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathpeer-list.tsx
More file actions
68 lines (62 loc) · 2.14 KB
/
peer-list.tsx
File metadata and controls
68 lines (62 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { XCircleIcon } from '@heroicons/react/24/solid'
import type { PeerId, Connection } from '@libp2p/interface'
import { useCallback } from 'react'
import { useLibp2pContext } from '@/context/ctx'
interface PeerListProps {
connections: Connection[]
}
export default function PeerList({ connections }: PeerListProps) {
return (
<ul role="list" className="divide-y divide-gray-100">
{connections.map((connection) => (
<Peer key={connection.id} connection={connection} />
))}
</ul>
)
}
interface PeerProps {
connection: Connection
}
function Peer({ connection }: PeerProps) {
const { libp2p } = useLibp2pContext()
const handleDisconnectPeer = useCallback(
(peerId: PeerId) => {
libp2p.hangUp(peerId)
},
[libp2p],
)
let ipAddr
try {
const nodeAddr = connection.remoteAddr?.nodeAddress()
ipAddr = `${nodeAddr.address}:${nodeAddr.port} |`
} catch (e) {
ipAddr = null
}
return (
<li key={connection.id} className="flex justify-between flex-wrap mx-2 py-2 gap-x-6 py-5">
<div className="flex min-w-0 gap-x-4">
<div className="mt-1 flex items-center gap-x-1.5">
<div className="flex-none rounded-full bg-emerald-500/20 p-1">
<div className="h-1.5 w-1.5 rounded-full bg-emerald-500" />
</div>
</div>
<div className="min-w-0 flex-auto">
<p className="text-sm font-semibold leading-6 text-gray-900">{connection.remotePeer.toString()}</p>
<p className="mt-1 truncate text-xs leading-5 text-gray-500">{connection.remoteAddr.toString()}</p>
<p className="mt-1 text-xs leading-5 text-gray-500">
Connected: {new Date(connection.timeline.open).toLocaleString()}
</p>
</div>
</div>
<div className="hidden sm:flex sm:flex-col sm:items-end">
<button
onClick={() => handleDisconnectPeer(connection.remotePeer)}
className="bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded flex flex-row"
>
<XCircleIcon className="w-6 h-6" />
<span className="pl-1">Disconnect</span>
</button>
</div>
</li>
)
}