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

175 lines
4.9 KiB
TypeScript
Raw Normal View History

2021-04-21 05:51:52 +00:00
import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id';
2021-05-03 05:22:30 +00:00
import { useEffect, useState } from 'react';
2021-04-14 04:44:12 +00:00
import './App.css';
2021-05-04 06:54:32 +00:00
import { ChatMessage } from './ChatMessage';
import { ChatMessage as WakuChatMessage } from 'waku/chat_message';
2021-04-21 05:51:52 +00:00
import { WakuMessage } from 'waku/waku_message';
import { StoreCodec } from 'waku/waku_store';
import handleCommand from './command';
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';
import { ThemeProvider } from '@livechat/ui-kit';
import { generate } from 'server-name-generator';
2021-04-14 04:44:12 +00:00
const themes = {
AuthorName: {
css: {
fontSize: '1.1em',
},
},
Message: {
css: {
margin: '0em',
padding: '0em',
fontSize: '0.83em',
},
},
MessageText: {
css: {
margin: '0em',
padding: '0.1em',
paddingLeft: '1em',
fontSize: '1.1em',
},
},
MessageGroup: {
css: {
margin: '0em',
padding: '0.2em',
},
},
};
2021-04-21 05:51:52 +00:00
export const ChatContentTopic = 'dingpu';
export default function App() {
let [newMessages, setNewMessages] = useState<ChatMessage[]>([]);
let [archivedMessages, setArchivedMessages] = useState<ChatMessage[]>([]);
2021-04-23 04:40:28 +00:00
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
let [nick, setNick] = useState<string>(generate());
2021-04-19 01:34:13 +00:00
2021-04-21 06:11:07 +00:00
useEffect(() => {
2021-05-10 02:38:27 +00:00
const handleRelayMessage = (wakuMsg: WakuMessage) => {
if (wakuMsg.payload) {
const chatMsg = ChatMessage.fromWakuChatMessage(
WakuChatMessage.decode(wakuMsg.payload)
);
if (chatMsg) {
setNewMessages([chatMsg]);
}
}
};
const handleProtocolChange = async (
waku: Waku,
{ peerId, protocols }: { peerId: PeerId; protocols: string[] }
) => {
if (protocols.includes(StoreCodec)) {
2021-05-07 00:37:53 +00:00
console.log(`${peerId.toB58String()}: retrieving archived messages}`);
try {
const response = await waku.store.queryHistory(peerId, [
ChatContentTopic,
]);
console.log(`${peerId.toB58String()}: messages retrieved:`, response);
if (response) {
const messages = response
.map((wakuMsg) => wakuMsg.payload)
.filter((payload) => !!payload)
.map((payload) => WakuChatMessage.decode(payload as Uint8Array))
.map((wakuChatMessage) =>
ChatMessage.fromWakuChatMessage(wakuChatMessage)
);
setArchivedMessages(messages);
}
} catch (e) {
console.log(
`${peerId.toB58String()}: error encountered when retrieving archived messages`,
e
);
}
}
};
2021-04-23 04:40:28 +00:00
if (!stateWaku) {
2021-04-27 01:05:35 +00:00
initWaku(setWaku)
2021-04-21 06:11:07 +00:00
.then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e));
2021-04-22 00:58:56 +00:00
} else {
stateWaku.relay.addObserver(handleRelayMessage, [ChatContentTopic]);
stateWaku.libp2p.peerStore.on(
'change:protocols',
handleProtocolChange.bind({}, stateWaku)
);
// To clean up listener when component unmounts
return () => {
stateWaku?.libp2p.peerStore.removeListener(
'change:protocols',
handleProtocolChange.bind({}, stateWaku)
);
};
2021-04-21 06:11:07 +00:00
}
}, [stateWaku]);
2021-04-21 05:51:52 +00:00
2021-04-21 06:11:07 +00:00
return (
2021-04-27 04:39:34 +00:00
<div
className="chat-app"
style={{ height: '100vh', width: '100vw', overflow: 'hidden' }}
>
<WakuContext.Provider value={{ waku: stateWaku }}>
<ThemeProvider theme={themes}>
<Room
nick={nick}
newMessages={newMessages}
archivedMessages={archivedMessages}
commandHandler={(input: string) => {
const { command, response } = handleCommand(
input,
stateWaku,
setNick
);
const commandMessages = response.map((msg) => {
2021-05-04 06:54:32 +00:00
return new ChatMessage(new Date(), new Date(), command, msg);
});
setNewMessages(commandMessages);
}}
/>
</ThemeProvider>
</WakuContext.Provider>
2021-04-21 06:11:07 +00:00
</div>
);
2021-04-14 04:44:12 +00:00
}
2021-04-27 01:05:35 +00:00
async function initWaku(setter: (waku: Waku) => void) {
try {
const waku = await Waku.create({
config: {
pubsub: {
enabled: true,
emitSelf: true,
},
},
});
setter(waku);
waku.libp2p.peerStore.addressBook.add(
PeerId.createFromB58String(
2021-05-02 23:52:49 +00:00
'16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ'
2021-04-27 01:05:35 +00:00
),
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7010/wss')]
2021-04-27 01:05:35 +00:00
);
2021-05-07 00:38:08 +00:00
waku.libp2p.peerStore.addressBook.add(
PeerId.createFromB58String(
'16Uiu2HAmSyrYVycqBCWcHyNVQS6zYQcdQbwyov1CDijboVRsQS37'
),
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/7009/wss')]
);
2021-04-27 01:05:35 +00:00
} catch (e) {
console.log('Issue starting waku ', e);
}
}