From 1e1ad0332cb2470681701f550e67ec142f8b4785 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 22 Apr 2021 10:58:56 +1000 Subject: [PATCH] Messages do not replace each other --- web-chat/src/App.tsx | 41 ++++++++++++++--------------------------- web-chat/src/Room.tsx | 23 ++++++++++++++++++----- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/web-chat/src/App.tsx b/web-chat/src/App.tsx index c7e78be74a..a18260238c 100644 --- a/web-chat/src/App.tsx +++ b/web-chat/src/App.tsx @@ -13,7 +13,7 @@ import { WakuContext } from './WakuContext'; export const ChatContentTopic = 'dingpu'; interface State { - messages: string[], + messages: ChatMessage[], waku?: Waku } @@ -25,20 +25,9 @@ export default function App() { async function initWaku() { try { const waku = await Waku.create({}); - setState(({ messages }) => ( - { waku, messages } - )); - waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => { - const wakuMsg = WakuMessage.decode(event.data); - if (wakuMsg.payload) { - const chatMsg = ChatMessage.decode(wakuMsg.payload); - const msgStr = printMessage(chatMsg); - - const messages = state.messages.slice(); - messages.push(msgStr); - setState({ messages, waku }); - } + setState(({ messages }) => { + return {waku, messages}; }); waku.libp2p.peerStore.addressBook.add( @@ -54,10 +43,20 @@ export default function App() { initWaku() .then(() => console.log('Waku init done')) .catch((e) => console.log('Waku init failed ', e)); + } else { + state.waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => { + const wakuMsg = WakuMessage.decode(event.data); + if (wakuMsg.payload) { + const chatMsg = ChatMessage.decode(wakuMsg.payload); + const messages = state.messages.slice(); + messages.push(chatMsg); + console.log("setState on ", messages); + setState({ messages, waku: state.waku }); + } + }); } }); - return (
@@ -70,15 +69,3 @@ export default function App() {
); } - -// TODO: Make it a proper component -function printMessage(chatMsg: ChatMessage) { - const timestamp = chatMsg.timestamp.toLocaleString([], { - month: 'short', - day: 'numeric', - hour: 'numeric', - minute: '2-digit', - hour12: false - }); - return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.message}`; -} diff --git a/web-chat/src/Room.tsx b/web-chat/src/Room.tsx index 96a5bace22..8290724a8a 100644 --- a/web-chat/src/Room.tsx +++ b/web-chat/src/Room.tsx @@ -6,11 +6,12 @@ import { ListItemText } from '@material-ui/core'; import React from 'react'; +import { ChatMessage } from 'waku-chat/chat_message'; import MessageInput from './MessageInput'; import Send from './Send'; interface Props { - lines: string[], + lines: ChatMessage[], } interface State { @@ -58,16 +59,16 @@ export default class Room extends React.Component { } interface LinesProps { - messages: string[] + messages: ChatMessage[] } const Lines = (props: LinesProps) => { const renderedLines = []; - for (const line of props.messages) { + for (const i in props.messages) { renderedLines.push( - ); } @@ -78,3 +79,15 @@ const Lines = (props: LinesProps) => { ); }; + +// TODO: Make it a proper component +function printMessage(chatMsg: ChatMessage) { + const timestamp = chatMsg.timestamp.toLocaleString([], { + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: '2-digit', + hour12: false + }); + return `<${timestamp}> ${chatMsg.nick}: ${chatMsg.message}`; +}