Skip to content

Commit 999de90

Browse files
committed
fix: remove unused code + burger menu + persist Libp2p main state (#35)
1 parent aaf87f8 commit 999de90

File tree

7 files changed

+25
-139
lines changed

7 files changed

+25
-139
lines changed

packages/frontend/src/components/chat.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ function Message({ msg, from, peerId }: MessageProps) {
3131
<li ref={msgref} className={`flex ${from === 'me' ? 'justify-end' : 'justify-start'}`}>
3232
<div
3333

34-
className="flex relative max-w-xl px-4 py-2 text-gray-700 rounded shadow"
34+
className="flex relative max-w-xl px-4 py-2 text-gray-700 rounded shadow bg-white"
3535
>
36-
<span className="flex relative mx-3 px-4 py-4"> {peerId !== libp2p.peerId.toString() ? `from: ${peerId.slice(-4)}` : null} </span>
37-
<span className="block">{msg} </span>
36+
<div className="block">
37+
{msg}
38+
<p className="italic text-gray-400">{peerId !== libp2p.peerId.toString() ? `from: ${peerId.slice(-4)}` : null} </p>
39+
</div>
3840
</div>
3941
</li>
4042
)
@@ -90,7 +92,7 @@ export default function ChatContainer() {
9092

9193
setMessageHistory([...messageHistory, { msg: input, from: 'me', peerId: myPeerId }])
9294
setInput('')
93-
}, [input, messageHistory, setInput, libp2p])
95+
}, [input, messageHistory, setInput, libp2p, setMessageHistory])
9496

9597
const handleKeyUp = useCallback(
9698
async (e: React.KeyboardEvent<HTMLInputElement>) => {
@@ -135,7 +137,7 @@ export default function ChatContainer() {
135137
Public Chat
136138
</span>
137139
</div>
138-
<div className="relative w-full p-6 overflow-y-auto h-[40rem]">
140+
<div className="relative w-full p-6 overflow-y-auto h-[40rem] bg-gray-100">
139141
<ul className="space-y-2">
140142
{/* messages start */}
141143
{messageHistory.map(({ msg, from, peerId }, idx) => (

packages/frontend/src/components/nav.tsx

-54
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import Link from 'next/link'
55
import Image from 'next/image'
66
import { useRouter } from 'next/router'
77

8-
const user = {
9-
name: 'Juan Benet',
10-
11-
imageUrl: 'https://github.com/achingbrain.png',
12-
}
13-
148
const navigationItems = [
159
{ name: 'Connecting to a Peer', href: '/' },
1610
{ name: 'Chat', href: '/chat' },
@@ -42,16 +36,6 @@ export default function Navigation() {
4236
height="46"
4337
width="46"
4438
/>
45-
{/* <img
46-
className="block h-12 w-auto lg:hidden"
47-
src="/libp2p-logo.svg"
48-
alt="Your Company"
49-
/>
50-
<img
51-
className="hidden h-12 w-auto lg:block"
52-
src="/libp2p-logo.svg"
53-
alt="Your Company"
54-
/> */}
5539
</div>
5640
<div className="hidden sm:-my-px sm:ml-6 sm:flex sm:space-x-8">
5741
{navigationItems.map((item) => (
@@ -161,44 +145,6 @@ export default function Navigation() {
161145
</Link>
162146
))}
163147
</div>
164-
<div className="border-t border-gray-200 pt-4 pb-3">
165-
<div className="flex items-center px-4">
166-
<div className="flex-shrink-0">
167-
<img
168-
className="h-10 w-10 rounded-full"
169-
src={user.imageUrl}
170-
alt=""
171-
/>
172-
</div>
173-
<div className="ml-3">
174-
<div className="text-base font-medium text-gray-800">
175-
{user.name}
176-
</div>
177-
<div className="text-sm font-medium text-gray-500">
178-
{user.email}
179-
</div>
180-
</div>
181-
<button
182-
type="button"
183-
className="ml-auto flex-shrink-0 rounded-full bg-white p-1 text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2"
184-
>
185-
<span className="sr-only">View notifications</span>
186-
<BellIcon className="h-6 w-6" aria-hidden="true" />
187-
</button>
188-
</div>
189-
{/* <div className="mt-3 space-y-1">
190-
{userNavigation.map((item) => (
191-
<Disclosure.Button
192-
key={item.name}
193-
as="a"
194-
href={item.href}
195-
className="block px-4 py-2 text-base font-medium text-gray-500 hover:bg-gray-100 hover:text-gray-800"
196-
>
197-
{item.name}
198-
</Disclosure.Button>
199-
))}
200-
</div> */}
201-
</div>
202148
</Disclosure.Panel>
203149
</>
204150
)}

packages/frontend/src/components/stats.tsx

-30
This file was deleted.

packages/frontend/src/context/peer-ctx.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createContext, useContext, useState } from 'react';
1+
import React, { ReactElement, ReactNode, createContext, useContext, useState } from 'react';
22
import type { Connection } from '@libp2p/interface-connection'
33
import { PeerId } from '@libp2p/interface-peer-id'
44

@@ -17,7 +17,7 @@ export interface PeerContextInterface {
1717
export const peerContext = createContext<PeerContextInterface>({
1818
peerStats: {
1919
peerIds: [],
20-
connected: false,
20+
connected: true,
2121
connections: [],
2222
latency: 0
2323
},
@@ -28,7 +28,7 @@ export const usePeerContext = () => {
2828
return useContext(peerContext);
2929
};
3030

31-
export const PeerProvider = ({ children }: any) => {
31+
export const PeerProvider = ({ children }: { children: ReactNode }) => {
3232
const [peerStats, setPeerStats] = useState<PeerStats>({
3333
peerIds: [],
3434
connected: false,

packages/frontend/src/lib/constants.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ export const CHAT_TOPIC = "universal-connectivity"
22

33
export const CIRCUIT_RELAY_CODE = 290
44

5-
65
export const BOOTSTRAP_NODE = "/ip4/18.195.246.16/udp/9090/webrtc-direct/certhash/uEiBy_U1UNQ0IDvot_PKlQM_QeU3yx-zCAVaMxxVm2JxWBg/p2p/12D3KooWGTDZj1zAjMCJ8XXx9Z88zAAd6vn3krQYLwZ67S4vMUxz"

packages/frontend/src/lib/libp2p.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function startLibp2p(options: {} = {}) {
5656
// '/ip4/18.195.246.16/udp/9090/webrtc-direct/certhash/uEiBy_U1UNQ0IDvot_PKlQM_QeU3yx-zCAVaMxxVm2JxWBg/p2p/12D3KooWGTDZj1zAjMCJ8XXx9Z88zAAd6vn3krQYLwZ67S4vMUxz',
5757
// ],
5858
list: [
59-
'/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiBy_U1UNQ0IDvot_PKlQM_QeU3yx-zCAVaMxxVm2JxWBg/p2p/12D3KooWHNiikdQPqM6cyESe6x5TgBJ8NhTBM5Pg1mJdm4XGLYmD'
59+
'/ip4/127.0.0.1/udp/9090/webrtc-direct/certhash/uEiA2twAWww-g6fXsJe6JPlROwCHbRj6fNgr_WHxiQGEK3g/p2p/12D3KooWLTB1SrjyF8R5Z1MKErcV8abs26eo4LpadQKWsxMUcDBJ'
6060
],
6161
}),
6262
],

packages/frontend/src/pages/index.tsx

+14-45
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@ import { CheckCircleIcon, XCircleIcon } from '@heroicons/react/20/solid'
33
import Nav from '@/components/nav'
44
import { useLibp2pContext } from '@/context/ctx'
55
import { useInterval } from 'usehooks-ts'
6-
6+
import type { PeerId } from '@libp2p/interface-peer-id'
77
import type { Connection } from '@libp2p/interface-connection'
88
import { usePeerContext } from '../context/peer-ctx'
9+
import { useEffect } from 'react'
910

1011

1112
export default function Home() {
1213
const { libp2p } = useLibp2pContext()
1314
const { peerStats, setPeerStats } = usePeerContext()
1415

1516
useInterval(() => {
16-
const getConnectedPeers = async () => {
17-
return await libp2p.getPeers()
18-
}
19-
const getConnections = async () => {
20-
return await libp2p.getConnections()
21-
}
2217

2318
const ping = async () => {
2419
const { peerIds } = peerStats
@@ -36,16 +31,20 @@ export default function Home() {
3631
.catch((e) => {
3732
console.error(e, e?.error)
3833
})
34+
}, 5000)
3935

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+
}
4341

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])
4948

5049
const getUniqueConnections = (connections: Connection[]) => {
5150
const uniqueConnections: Connection[] = []
@@ -123,33 +122,3 @@ export default function Home() {
123122
</>
124123
)
125124
}
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

Comments
 (0)