mirror of https://github.com/waku-org/js-waku.git
Display received messages
This commit is contained in:
parent
d643735296
commit
dae4ca4345
|
@ -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>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue