2021-04-21 05:51:52 +00:00
|
|
|
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-14 03:23:44 +00:00
|
|
|
import {
|
|
|
|
ChatMessage,
|
2021-05-18 03:47:16 +00:00
|
|
|
getStatusFleetNodes,
|
|
|
|
Environment,
|
2021-05-14 03:23:44 +00:00
|
|
|
StoreCodec,
|
|
|
|
Waku,
|
2021-05-18 03:47:16 +00:00
|
|
|
WakuMessage,
|
2021-05-14 03:23:44 +00:00
|
|
|
} from 'js-waku';
|
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-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-05-03 21:53:26 +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';
|
|
|
|
|
2021-05-17 06:19:50 +00:00
|
|
|
async function retrieveStoreMessages(
|
|
|
|
waku: Waku,
|
|
|
|
peerId: PeerId,
|
|
|
|
setArchivedMessages: (value: ChatMessage[]) => void
|
|
|
|
): Promise<number> {
|
|
|
|
const callback = (wakuMessages: WakuMessage[]): void => {
|
|
|
|
const messages = wakuMessages
|
|
|
|
.map((wakuMsg) => wakuMsg.payload)
|
|
|
|
.filter((payload) => !!payload)
|
|
|
|
.map((payload) => ChatMessage.decode(payload as Uint8Array));
|
|
|
|
setArchivedMessages(messages);
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await waku.store.queryHistory({
|
|
|
|
peerId,
|
|
|
|
contentTopics: [ChatContentTopic],
|
|
|
|
pageSize: 5,
|
|
|
|
callback,
|
|
|
|
});
|
|
|
|
|
|
|
|
return res ? res.length : 0;
|
|
|
|
}
|
|
|
|
|
2021-04-22 00:39:05 +00:00
|
|
|
export default function App() {
|
2021-05-05 04:29:00 +00:00
|
|
|
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);
|
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-05-10 02:38:27 +00:00
|
|
|
const handleRelayMessage = (wakuMsg: WakuMessage) => {
|
|
|
|
if (wakuMsg.payload) {
|
2021-05-10 06:35:55 +00:00
|
|
|
const chatMsg = ChatMessage.decode(wakuMsg.payload);
|
2021-05-10 02:38:27 +00:00
|
|
|
if (chatMsg) {
|
|
|
|
setNewMessages([chatMsg]);
|
|
|
|
}
|
2021-04-30 05:25:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2021-05-17 06:19:50 +00:00
|
|
|
const length = await retrieveStoreMessages(
|
|
|
|
waku,
|
2021-05-07 06:10:58 +00:00
|
|
|
peerId,
|
2021-05-17 06:19:50 +00:00
|
|
|
setArchivedMessages
|
|
|
|
);
|
|
|
|
console.log(`${peerId.toB58String()}: messages retrieved:`, length);
|
2021-05-07 00:37:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(
|
|
|
|
`${peerId.toB58String()}: error encountered when retrieving archived messages`,
|
|
|
|
e
|
|
|
|
);
|
2021-04-30 05:25:58 +00:00
|
|
|
}
|
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-05-10 04:54:08 +00:00
|
|
|
stateWaku.relay.addObserver(handleRelayMessage, [ChatContentTopic]);
|
2021-04-22 01:09:10 +00:00
|
|
|
|
2021-05-03 21:55:35 +00:00
|
|
|
stateWaku.libp2p.peerStore.on(
|
2021-04-30 05:25:58 +00:00
|
|
|
'change:protocols',
|
|
|
|
handleProtocolChange.bind({}, stateWaku)
|
|
|
|
);
|
|
|
|
|
2021-04-22 01:09:10 +00:00
|
|
|
// To clean up listener when component unmounts
|
|
|
|
return () => {
|
2021-04-30 05:25:58 +00:00
|
|
|
stateWaku?.libp2p.peerStore.removeListener(
|
|
|
|
'change:protocols',
|
|
|
|
handleProtocolChange.bind({}, stateWaku)
|
|
|
|
);
|
2021-04-22 01:09:10 +00:00
|
|
|
};
|
2021-04-21 06:11:07 +00:00
|
|
|
}
|
2021-05-05 04:29:00 +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' }}
|
|
|
|
>
|
2021-04-26 07:33:12 +00:00
|
|
|
<WakuContext.Provider value={{ waku: stateWaku }}>
|
2021-05-03 21:53:26 +00:00
|
|
|
<ThemeProvider theme={themes}>
|
2021-04-29 06:13:09 +00:00
|
|
|
<Room
|
|
|
|
nick={nick}
|
2021-05-05 04:29:00 +00:00
|
|
|
newMessages={newMessages}
|
|
|
|
archivedMessages={archivedMessages}
|
2021-04-29 06:13:09 +00:00
|
|
|
commandHandler={(input: string) => {
|
|
|
|
const { command, response } = handleCommand(
|
|
|
|
input,
|
|
|
|
stateWaku,
|
|
|
|
setNick
|
|
|
|
);
|
|
|
|
const commandMessages = response.map((msg) => {
|
2021-05-10 06:35:55 +00:00
|
|
|
return ChatMessage.fromUtf8String(new Date(), command, msg);
|
2021-04-29 06:13:09 +00:00
|
|
|
});
|
2021-05-05 04:29:00 +00:00
|
|
|
setNewMessages(commandMessages);
|
2021-04-29 06:13:09 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</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);
|
|
|
|
|
2021-05-18 03:47:16 +00:00
|
|
|
const nodes = await getNodes();
|
2021-05-14 03:23:44 +00:00
|
|
|
await Promise.all(
|
|
|
|
nodes.map((addr) => {
|
|
|
|
return waku.dial(addr);
|
|
|
|
})
|
2021-05-07 00:38:08 +00:00
|
|
|
);
|
2021-04-27 01:05:35 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log('Issue starting waku ', e);
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 03:47:16 +00:00
|
|
|
|
|
|
|
function getNodes() {
|
|
|
|
// Works with react-scripts
|
|
|
|
if (process?.env?.NODE_ENV === 'development') {
|
|
|
|
return getStatusFleetNodes(Environment.Test);
|
|
|
|
} else {
|
|
|
|
return getStatusFleetNodes(Environment.Prod);
|
|
|
|
}
|
|
|
|
}
|