Skip to content

Commit 8e44930

Browse files
maschadp-shahi
andauthored
feat: UI fixes, persist messages, set up cxn limit (#39)
Co-authored-by: Prithvi Shahi <[email protected]>
1 parent 24b0867 commit 8e44930

19 files changed

+310
-2748
lines changed

package-lock.json

+100-1,823
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/frontend/package.json

-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"@heroicons/react": "^2.0.16",
1616
"@libp2p/bootstrap": "^6.0.0",
1717
"@libp2p/kad-dht": "^8.0.6",
18-
"@libp2p/peer-id": "^2.0.1",
1918
"@libp2p/webrtc": "^1.1.2",
2019
"@libp2p/websockets": "^5.0.3",
2120
"@libp2p/webtransport": "^1.0.7",
@@ -24,9 +23,6 @@
2423
"@types/node": "18.14.6",
2524
"@types/react": "18.0.28",
2625
"@types/react-dom": "18.0.11",
27-
"blockstore-core": "^3.0.0",
28-
"datastore-core": "^8.0.4",
29-
"datastore-level": "^9.0.4",
3026
"debug": "^4.3.4",
3127
"eslint": "8.35.0",
3228
"eslint-config-next": "13.2.3",

packages/frontend/src/components/chat.tsx

+28-27
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ import React, { useCallback, useEffect, useState } from 'react'
33
import { Message } from '@libp2p/interface-pubsub'
44
import { CHAT_TOPIC } from '@/lib/constants'
55
import { createIcon } from '@download/blockies'
6+
import { ChatMessage, useChatContext } from '../context/chat-ctx'
67

7-
interface ChatMessage {
8-
msg: string
9-
from: 'me' | 'other'
10-
peerId: string
11-
}
128

13-
interface MessageProps extends ChatMessage {}
9+
interface MessageProps extends ChatMessage { }
1410

1511
function Message({ msg, from, peerId }: MessageProps) {
1612
const msgref = React.useRef<HTMLLIElement>(null)
13+
const { libp2p } = useLibp2pContext()
14+
1715

1816
useEffect(() => {
1917
const icon = createIcon({
@@ -33,17 +31,20 @@ function Message({ msg, from, peerId }: MessageProps) {
3331
<li ref={msgref} className={`flex ${from === 'me' ? 'justify-end' : 'justify-start'}`}>
3432
<div
3533

36-
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"
3735
>
38-
<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>
3940
</div>
4041
</li>
4142
)
4243
}
4344

4445
export default function ChatContainer() {
4546
const { libp2p } = useLibp2pContext()
46-
const [messages, setMessages] = useState<ChatMessage[]>([])
47+
const { messageHistory, setMessageHistory } = useChatContext();
4748
const [input, setInput] = useState<string>('')
4849

4950
// Effect hook to subscribe to pubsub events and update the message state hook
@@ -57,7 +58,7 @@ export default function ChatContainer() {
5758

5859
// Append signed messages, otherwise discard
5960
if (evt.detail.type === 'signed') {
60-
setMessages([...messages, { msg, from: 'other', peerId: evt.detail.from.toString() }])
61+
setMessageHistory([...messageHistory, { msg, from: 'other', peerId: evt.detail.from.toString() }])
6162
}
6263
}
6364

@@ -68,7 +69,7 @@ export default function ChatContainer() {
6869
// libp2p.pubsub.unsubscribe(CHAT_TOPIC)
6970
libp2p.pubsub.removeEventListener('message', messageCB)
7071
}
71-
}, [libp2p, messages, setMessages])
72+
}, [libp2p, messageHistory, setMessageHistory])
7273

7374
const sendMessage = useCallback(async () => {
7475
if (input === '') return
@@ -89,9 +90,9 @@ export default function ChatContainer() {
8990

9091
const myPeerId = libp2p.peerId.toString()
9192

92-
setMessages([...messages, { msg: input, from: 'me', peerId: myPeerId }])
93+
setMessageHistory([...messageHistory, { msg: input, from: 'me', peerId: myPeerId }])
9394
setInput('')
94-
}, [input, messages, setInput, libp2p])
95+
}, [input, messageHistory, setInput, libp2p, setMessageHistory])
9596

9697
const handleKeyUp = useCallback(
9798
async (e: React.KeyboardEvent<HTMLInputElement>) => {
@@ -136,10 +137,10 @@ export default function ChatContainer() {
136137
Public Chat
137138
</span>
138139
</div>
139-
<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">
140141
<ul className="space-y-2">
141142
{/* messages start */}
142-
{messages.map(({ msg, from, peerId }, idx) => (
143+
{messageHistory.map(({ msg, from, peerId }, idx) => (
143144
<Message key={idx} msg={msg} from={from} peerId={peerId} />
144145
))}
145146
{/* messages end */}
@@ -156,9 +157,9 @@ export default function ChatContainer() {
156157
stroke="currentColor"
157158
>
158159
<path
159-
stroke-linecap="round"
160-
stroke-linejoin="round"
161-
stroke-width="2"
160+
strokeLinecap="round"
161+
strokeLinejoin="round"
162+
strokeWidth="2"
162163
d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
163164
/>
164165
</svg>
@@ -172,9 +173,9 @@ export default function ChatContainer() {
172173
stroke="currentColor"
173174
>
174175
<path
175-
stroke-linecap="round"
176-
stroke-linejoin="round"
177-
stroke-width="2"
176+
strokeLinecap="round"
177+
strokeLinejoin="round"
178+
strokeWidth="2"
178179
d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"
179180
/>
180181
</svg>
@@ -199,9 +200,9 @@ export default function ChatContainer() {
199200
stroke="currentColor"
200201
>
201202
<path
202-
stroke-linecap="round"
203-
stroke-linejoin="round"
204-
stroke-width="2"
203+
strokeLinecap="round"
204+
strokeLinejoin="round"
205+
strokeWidth="2"
205206
d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z"
206207
/>
207208
</svg>
@@ -233,9 +234,9 @@ export function RoomList() {
233234
<svg
234235
fill="none"
235236
stroke="currentColor"
236-
stroke-linecap="round"
237-
stroke-linejoin="round"
238-
stroke-width="2"
237+
strokeLinecap="round"
238+
strokeLinejoin="round"
239+
strokeWidth="2"
239240
viewBox="0 0 24 24"
240241
className="w-6 h-6 text-gray-300"
241242
>

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/peer-info.tsx

-104
This file was deleted.

packages/frontend/src/components/stats.tsx

-30
This file was deleted.

0 commit comments

Comments
 (0)