js-waku/web-chat/src/App.tsx

91 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-04-19 04:54:39 +00:00
import { Paper } from '@material-ui/core';
2021-04-21 05:51:52 +00:00
import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
2021-04-21 06:11:07 +00:00
import React, { useEffect, useState } from 'react';
2021-04-14 04:44:12 +00:00
import './App.css';
2021-04-21 05:51:52 +00:00
import { ChatMessage } from 'waku-chat/chat_message';
import { WakuMessage } from 'waku/waku_message';
import { RelayDefaultTopic } from 'waku/waku_relay';
2021-04-16 01:32:00 +00:00
import Room from './Room';
2021-04-21 05:51:52 +00:00
import Waku from 'waku/waku';
2021-04-19 01:34:13 +00:00
import { WakuContext } from './WakuContext';
2021-04-14 04:44:12 +00:00
2021-04-21 05:51:52 +00:00
export const ChatContentTopic = 'dingpu';
2021-04-14 05:23:00 +00:00
interface Props {
}
interface State {
2021-04-19 01:34:13 +00:00
messages: string[],
2021-04-21 05:51:52 +00:00
waku?: Waku
2021-04-14 05:23:00 +00:00
}
2021-04-21 06:11:07 +00:00
export default function App () {
let [state, setState] = useState<State>({ messages: [] });
2021-04-19 01:34:13 +00:00
2021-04-14 05:23:00 +00:00
2021-04-21 06:11:07 +00:00
useEffect(() => {
async function initWaku() {
try {
const waku = await Waku.create({});
setState(({ messages }) => (
{ waku, messages }
));
2021-04-19 00:36:37 +00:00
2021-04-21 06:11:07 +00:00
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);
2021-04-21 05:51:52 +00:00
2021-04-21 06:11:07 +00:00
const messages = state.messages.slice();
messages.push(msgStr);
setState({ messages, waku });
}
});
2021-04-21 05:51:52 +00:00
2021-04-21 06:11:07 +00:00
try {
await waku.dial('/ip4/127.0.0.1/tcp/7777/ws/p2p/QmUJKveCpfwA4cY1zRybMEt5z64FRtMHLFFQwndWrSfMmf');
console.log('Remote node dialed');
} catch (e) {
console.log('Error when dialing peer ', e);
2021-04-21 05:51:52 +00:00
}
2021-04-21 06:11:07 +00:00
} catch (e) {
console.log('Issue starting waku ', e);
}
}
2021-04-19 01:34:13 +00:00
2021-04-21 06:11:07 +00:00
if (!state.waku) {
initWaku()
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
}
}, [state.waku]);
2021-04-21 05:51:52 +00:00
2021-04-14 05:23:00 +00:00
2021-04-21 06:11:07 +00:00
return (
<div className='App'>
<div className='chat-room'>
<WakuContext.Provider value={{ waku: state.waku }}>
<Paper>
<Room lines={state.messages} />
</Paper>
</WakuContext.Provider>
2021-04-14 05:13:55 +00:00
</div>
2021-04-21 06:11:07 +00:00
</div>
);
2021-04-14 04:44:12 +00:00
}
2021-04-21 05:51:52 +00:00
// 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}`;
}