@@ -3,17 +3,15 @@ import React, { useCallback, useEffect, useState } from 'react'
3
3
import { Message } from '@libp2p/interface-pubsub'
4
4
import { CHAT_TOPIC } from '@/lib/constants'
5
5
import { createIcon } from '@download/blockies'
6
+ import { ChatMessage , useChatContext } from '../context/chat-ctx'
6
7
7
- interface ChatMessage {
8
- msg : string
9
- from : 'me' | 'other'
10
- peerId : string
11
- }
12
8
13
- interface MessageProps extends ChatMessage { }
9
+ interface MessageProps extends ChatMessage { }
14
10
15
11
function Message ( { msg, from, peerId } : MessageProps ) {
16
12
const msgref = React . useRef < HTMLLIElement > ( null )
13
+ const { libp2p } = useLibp2pContext ( )
14
+
17
15
18
16
useEffect ( ( ) => {
19
17
const icon = createIcon ( {
@@ -33,17 +31,20 @@ function Message({ msg, from, peerId }: MessageProps) {
33
31
< li ref = { msgref } className = { `flex ${ from === 'me' ? 'justify-end' : 'justify-start' } ` } >
34
32
< div
35
33
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 "
37
35
>
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 >
39
40
</ div >
40
41
</ li >
41
42
)
42
43
}
43
44
44
45
export default function ChatContainer ( ) {
45
46
const { libp2p } = useLibp2pContext ( )
46
- const [ messages , setMessages ] = useState < ChatMessage [ ] > ( [ ] )
47
+ const { messageHistory , setMessageHistory } = useChatContext ( ) ;
47
48
const [ input , setInput ] = useState < string > ( '' )
48
49
49
50
// Effect hook to subscribe to pubsub events and update the message state hook
@@ -57,7 +58,7 @@ export default function ChatContainer() {
57
58
58
59
// Append signed messages, otherwise discard
59
60
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 ( ) } ] )
61
62
}
62
63
}
63
64
@@ -68,7 +69,7 @@ export default function ChatContainer() {
68
69
// libp2p.pubsub.unsubscribe(CHAT_TOPIC)
69
70
libp2p . pubsub . removeEventListener ( 'message' , messageCB )
70
71
}
71
- } , [ libp2p , messages , setMessages ] )
72
+ } , [ libp2p , messageHistory , setMessageHistory ] )
72
73
73
74
const sendMessage = useCallback ( async ( ) => {
74
75
if ( input === '' ) return
@@ -89,9 +90,9 @@ export default function ChatContainer() {
89
90
90
91
const myPeerId = libp2p . peerId . toString ( )
91
92
92
- setMessages ( [ ...messages , { msg : input , from : 'me' , peerId : myPeerId } ] )
93
+ setMessageHistory ( [ ...messageHistory , { msg : input , from : 'me' , peerId : myPeerId } ] )
93
94
setInput ( '' )
94
- } , [ input , messages , setInput , libp2p ] )
95
+ } , [ input , messageHistory , setInput , libp2p , setMessageHistory ] )
95
96
96
97
const handleKeyUp = useCallback (
97
98
async ( e : React . KeyboardEvent < HTMLInputElement > ) => {
@@ -136,10 +137,10 @@ export default function ChatContainer() {
136
137
Public Chat
137
138
</ span >
138
139
</ 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 " >
140
141
< ul className = "space-y-2" >
141
142
{ /* messages start */ }
142
- { messages . map ( ( { msg, from, peerId } , idx ) => (
143
+ { messageHistory . map ( ( { msg, from, peerId } , idx ) => (
143
144
< Message key = { idx } msg = { msg } from = { from } peerId = { peerId } />
144
145
) ) }
145
146
{ /* messages end */ }
@@ -156,9 +157,9 @@ export default function ChatContainer() {
156
157
stroke = "currentColor"
157
158
>
158
159
< path
159
- stroke-linecap = "round"
160
- stroke-linejoin = "round"
161
- stroke-width = "2"
160
+ strokeLinecap = "round"
161
+ strokeLinejoin = "round"
162
+ strokeWidth = "2"
162
163
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"
163
164
/>
164
165
</ svg >
@@ -172,9 +173,9 @@ export default function ChatContainer() {
172
173
stroke = "currentColor"
173
174
>
174
175
< path
175
- stroke-linecap = "round"
176
- stroke-linejoin = "round"
177
- stroke-width = "2"
176
+ strokeLinecap = "round"
177
+ strokeLinejoin = "round"
178
+ strokeWidth = "2"
178
179
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"
179
180
/>
180
181
</ svg >
@@ -199,9 +200,9 @@ export default function ChatContainer() {
199
200
stroke = "currentColor"
200
201
>
201
202
< path
202
- stroke-linecap = "round"
203
- stroke-linejoin = "round"
204
- stroke-width = "2"
203
+ strokeLinecap = "round"
204
+ strokeLinejoin = "round"
205
+ strokeWidth = "2"
205
206
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"
206
207
/>
207
208
</ svg >
@@ -233,9 +234,9 @@ export function RoomList() {
233
234
< svg
234
235
fill = "none"
235
236
stroke = "currentColor"
236
- stroke-linecap = "round"
237
- stroke-linejoin = "round"
238
- stroke-width = "2"
237
+ strokeLinecap = "round"
238
+ strokeLinejoin = "round"
239
+ strokeWidth = "2"
239
240
viewBox = "0 0 24 24"
240
241
className = "w-6 h-6 text-gray-300"
241
242
>
0 commit comments