Messages do not replace each other

This commit is contained in:
Franck Royer 2021-04-22 10:58:56 +10:00
parent 09fa8a0447
commit 1e1ad0332c
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 32 additions and 32 deletions

View File

@ -13,7 +13,7 @@ import { WakuContext } from './WakuContext';
export const ChatContentTopic = 'dingpu'; export const ChatContentTopic = 'dingpu';
interface State { interface State {
messages: string[], messages: ChatMessage[],
waku?: Waku waku?: Waku
} }
@ -25,20 +25,9 @@ export default function App() {
async function initWaku() { async function initWaku() {
try { try {
const waku = await Waku.create({}); const waku = await Waku.create({});
setState(({ messages }) => (
{ waku, messages }
));
waku.libp2p.pubsub.on(RelayDefaultTopic, (event) => { setState(({ messages }) => {
const wakuMsg = WakuMessage.decode(event.data); return {waku, messages};
if (wakuMsg.payload) {
const chatMsg = ChatMessage.decode(wakuMsg.payload);
const msgStr = printMessage(chatMsg);
const messages = state.messages.slice();
messages.push(msgStr);
setState({ messages, waku });
}
}); });
waku.libp2p.peerStore.addressBook.add( waku.libp2p.peerStore.addressBook.add(
@ -54,10 +43,20 @@ export default function App() {
initWaku() initWaku()
.then(() => console.log('Waku init done')) .then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e)); .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 ( return (
<div className='App'> <div className='App'>
<div className='chat-room'> <div className='chat-room'>
@ -70,15 +69,3 @@ export default function App() {
</div> </div>
); );
} }
// 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}`;
}

View File

@ -6,11 +6,12 @@ import {
ListItemText ListItemText
} from '@material-ui/core'; } from '@material-ui/core';
import React from 'react'; import React from 'react';
import { ChatMessage } from 'waku-chat/chat_message';
import MessageInput from './MessageInput'; import MessageInput from './MessageInput';
import Send from './Send'; import Send from './Send';
interface Props { interface Props {
lines: string[], lines: ChatMessage[],
} }
interface State { interface State {
@ -58,16 +59,16 @@ export default class Room extends React.Component<Props, State> {
} }
interface LinesProps { interface LinesProps {
messages: string[] messages: ChatMessage[]
} }
const Lines = (props: LinesProps) => { const Lines = (props: LinesProps) => {
const renderedLines = []; const renderedLines = [];
for (const line of props.messages) { for (const i in props.messages) {
renderedLines.push(<ListItem> renderedLines.push(<ListItem>
<ListItemText <ListItemText key={"chat-message-" + i}
primary={line} primary={printMessage(props.messages[i])}
/> />
</ListItem>); </ListItem>);
} }
@ -78,3 +79,15 @@ const Lines = (props: LinesProps) => {
</List> </List>
); );
}; };
// 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}`;
}