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-27 00:48:08 +00:00
|
|
|
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';
|
2021-04-29 06:13:09 +00:00
|
|
|
import { ThemeProvider } from '@livechat/ui-kit';
|
2021-04-29 05:30:00 +00:00
|
|
|
import { generate } from 'server-name-generator';
|
2021-04-14 04:44:12 +00:00
|
|
|
|
2021-04-21 05:51:52 +00:00
|
|
|
export const ChatContentTopic = 'dingpu';
|
|
|
|
|
2021-04-22 00:39:05 +00:00
|
|
|
export default function App() {
|
2021-04-23 04:40:28 +00:00
|
|
|
let [stateMessages, setMessages] = useState<ChatMessage[]>([]);
|
|
|
|
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
|
2021-04-29 05:30:00 +00:00
|
|
|
let [nick, setNick] = useState<string>(generate());
|
2021-04-19 01:34:13 +00:00
|
|
|
|
2021-04-21 06:11:07 +00:00
|
|
|
useEffect(() => {
|
2021-04-22 01:09:10 +00:00
|
|
|
const handleNewMessages = (event: { data: Uint8Array }) => {
|
2021-04-27 01:00:09 +00:00
|
|
|
const chatMsg = decodeWakuMessage(event.data);
|
|
|
|
if (chatMsg) {
|
|
|
|
copyAndReplace([chatMsg], stateMessages, setMessages);
|
2021-04-22 01:09:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 {
|
2021-04-23 04:40:28 +00:00
|
|
|
stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleNewMessages);
|
2021-04-22 01:09:10 +00:00
|
|
|
|
|
|
|
// To clean up listener when component unmounts
|
|
|
|
return () => {
|
2021-04-23 04:40:28 +00:00
|
|
|
stateWaku?.libp2p.pubsub.removeListener(
|
2021-04-22 07:03:33 +00:00
|
|
|
RelayDefaultTopic,
|
|
|
|
handleNewMessages
|
|
|
|
);
|
2021-04-22 01:09:10 +00:00
|
|
|
};
|
2021-04-21 06:11:07 +00:00
|
|
|
}
|
2021-04-27 01:05:35 +00:00
|
|
|
}, [stateWaku, stateMessages]);
|
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' }}
|
|
|
|
>
|
2021-04-26 07:33:12 +00:00
|
|
|
<WakuContext.Provider value={{ waku: stateWaku }}>
|
2021-04-29 06:13:09 +00:00
|
|
|
<ThemeProvider>
|
|
|
|
<Room
|
|
|
|
nick={nick}
|
|
|
|
lines={stateMessages}
|
|
|
|
commandHandler={(input: string) => {
|
|
|
|
const { command, response } = handleCommand(
|
|
|
|
input,
|
|
|
|
stateWaku,
|
|
|
|
setNick
|
|
|
|
);
|
|
|
|
const commandMessages = response.map((msg) => {
|
|
|
|
return new ChatMessage(new Date(), command, msg);
|
|
|
|
});
|
|
|
|
copyAndReplace(commandMessages, stateMessages, setMessages);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ThemeProvider>
|
2021-04-26 07:33:12 +00:00
|
|
|
</WakuContext.Provider>
|
2021-04-21 06:11:07 +00:00
|
|
|
</div>
|
|
|
|
);
|
2021-04-14 04:44:12 +00:00
|
|
|
}
|
2021-04-27 01:00:09 +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-04-29 03:31:44 +00:00
|
|
|
'16Uiu2HAm7gwDpb2SQkWv69V7YND1v9tzs12eu1YyqNYmtEZwvC8D'
|
2021-04-27 01:05:35 +00:00
|
|
|
),
|
2021-04-29 03:31:44 +00:00
|
|
|
[multiaddr('/dns4/node-01.do-ams3.jdev.misc.statusim.net/tcp/443/wss')]
|
2021-04-27 01:05:35 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Issue starting waku ', e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 01:00:09 +00:00
|
|
|
function decodeWakuMessage(data: Uint8Array): null | ChatMessage {
|
|
|
|
const wakuMsg = WakuMessage.decode(data);
|
|
|
|
if (!wakuMsg.payload) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return ChatMessage.decode(wakuMsg.payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyAndReplace<T>(
|
|
|
|
newValues: Array<T>,
|
|
|
|
currentValues: Array<T>,
|
|
|
|
setter: (val: Array<T>) => void
|
|
|
|
) {
|
|
|
|
const copy = currentValues.slice();
|
|
|
|
setter(copy.concat(newValues));
|
|
|
|
}
|