Display received messages

This commit is contained in:
Franck Royer 2022-01-07 17:15:34 +11:00
parent d643735296
commit dae4ca4345
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 18 additions and 5 deletions

View File

@ -17,6 +17,7 @@ function App() {
const [wakuStatus, setWakuStatus] = React.useState('None'); const [wakuStatus, setWakuStatus] = React.useState('None');
// Using a counter just for the messages to be different // Using a counter just for the messages to be different
const [sendCounter, setSendCounter] = React.useState(0); const [sendCounter, setSendCounter] = React.useState(0);
const [messages, setMessages] = React.useState([]);
React.useEffect(() => { React.useEffect(() => {
if (!!waku) return; if (!!waku) return;
@ -34,18 +35,19 @@ function App() {
}, [waku, wakuStatus]); }, [waku, wakuStatus]);
const processIncomingMessage = React.useCallback((wakuMessage) => { const processIncomingMessage = React.useCallback((wakuMessage) => {
// Empty message?
if (!wakuMessage.payload) return; if (!wakuMessage.payload) return;
// Decode the protobuf payload const { text, timestamp } = proto.SimpleChatMessage.decode(
const { timestamp, text } = proto.SimpleChatMessage.decode(
wakuMessage.payload wakuMessage.payload
); );
const time = new Date(); const time = new Date();
time.setTime(timestamp); time.setTime(timestamp);
const message = { text, timestamp: time };
// For now, just log new messages on the console setMessages((messages) => {
console.log(`message received at ${time.toString()}: ${text}`); return [message].concat(messages);
});
}, []); }, []);
React.useEffect(() => { React.useEffect(() => {
@ -79,6 +81,17 @@ function App() {
<button onClick={sendMessageOnClick} disabled={wakuStatus !== 'Ready'}> <button onClick={sendMessageOnClick} disabled={wakuStatus !== 'Ready'}>
Send Message Send Message
</button> </button>
<ul>
{messages.map((msg) => {
return (
<li>
<p>
{msg.timestamp.toString()}: {msg.text}
</p>
</li>
);
})}
</ul>
</header> </header>
</div> </div>
); );