-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathindex.tsx
More file actions
139 lines (133 loc) · 5.14 KB
/
index.tsx
File metadata and controls
139 lines (133 loc) · 5.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import Head from 'next/head'
import Nav from '@/components/nav'
import { useLibp2pContext } from '@/context/ctx'
import type { PeerUpdate } from '@libp2p/interface'
import { useCallback, useEffect, useState } from 'react'
import Image from 'next/image'
import { Multiaddr, multiaddr } from '@multiformats/multiaddr'
import { connectToMultiaddr } from '../lib/libp2p'
import Spinner from '@/components/spinner'
import PeerList from '@/components/peer-list'
import { peerIdFromString } from '@libp2p/peer-id'
export default function Home() {
const { libp2p, connections } = useLibp2pContext()
const [listenAddresses, setListenAddresses] = useState<Multiaddr[]>([])
const [maddr, setMultiaddr] = useState('')
const [dialling, setDialling] = useState(false)
const [err, setErr] = useState('')
useEffect(() => {
const onPeerUpdate = (evt: CustomEvent<PeerUpdate>) => {
const maddrs = evt.detail.peer.addresses?.map((p) => p.multiaddr)
setListenAddresses(maddrs ?? [])
}
libp2p.addEventListener('self:peer:update', onPeerUpdate)
return () => {
libp2p.removeEventListener('self:peer:update', onPeerUpdate)
}
}, [libp2p, setListenAddresses])
const handleConnectToMultiaddr = useCallback(
async (e: React.MouseEvent<HTMLButtonElement>) => {
setErr('')
if (!maddr) {
return
}
setDialling(true)
try {
if (maddr.startsWith('/')) {
await connectToMultiaddr(libp2p)(multiaddr(maddr))
setMultiaddr('')
} else {
const peerId = peerIdFromString(maddr.trim())
await libp2p.dial(peerId)
setMultiaddr('')
}
} catch (e: any) {
setErr(e?.message ?? 'Error connecting')
} finally {
setDialling(false)
}
},
[libp2p, maddr],
)
const handleMultiaddrChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
setMultiaddr(e.target.value)
},
[setMultiaddr],
)
return (
<>
<Head>
<title>Universal Connectivity</title>
<meta name="description" content="universal connectivity" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main className="min-h-full">
<Nav />
<div className="py-10">
<header>
<div className="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8">
<h1 className="text-3xl font-bold leading-tight tracking-tight text-gray-900 flex flex-row">
<p className="mr-4">Universal Connectivity</p>
<Image src="/libp2p-hero.svg" alt="libp2p logo" height="46" width="46" />
</h1>
</div>
</header>
<main>
<div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
<ul className="my-2 space-y-2 break-all">
<li className="">This PeerID: {libp2p.peerId.toString()}</li>
</ul>
Addresses:
<ul className="my-2 space-y-2 break-all">
{listenAddresses.map((ma, index) => (
<li className="text-xs text-gray-700" key={`ma-${index}`}>
{ma.toString()}
</li>
))}
</ul>
<div className="my-6 w-1/2">
<label htmlFor="peer-id" className="block text-sm font-medium leading-6 text-gray-900">
multiaddr or PeerID to connect to
</label>
<div className="mt-2">
<input
value={maddr}
type="text"
name="peer-id"
id="peer-id"
className="block w-full rounded-md border-0 py-1.5 px-3 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
placeholder="/ip4/127.0.0.1/.. or 12D3Koo..."
aria-describedby="multiaddr-id-description"
onChange={handleMultiaddrChange}
/>
</div>
<button
type="button"
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' : '')
}
onClick={handleConnectToMultiaddr}
disabled={dialling}
>
{dialling && <Spinner />} Connect{dialling && 'ing'}
</button>
{err && <p className="text-red-500">{err}</p>}
</div>
<div>
{connections.length > 0 ? (
<>
<h3 className="text-xl">Connections ({connections.length}):</h3>
<PeerList connections={connections} />
</>
) : null}
</div>
</div>
</main>
</div>
</main>
</>
)
}