mirror of https://github.com/waku-org/js-waku.git
parent
f3738b1eac
commit
6e22897f74
|
@ -76,7 +76,7 @@ async function retrieveStoreMessages(
|
||||||
export default function App() {
|
export default function App() {
|
||||||
let [newMessages, setNewMessages] = useState<Message[]>([]);
|
let [newMessages, setNewMessages] = useState<Message[]>([]);
|
||||||
let [archivedMessages, setArchivedMessages] = useState<Message[]>([]);
|
let [archivedMessages, setArchivedMessages] = useState<Message[]>([]);
|
||||||
let [stateWaku, setWaku] = useState<Waku | undefined>(undefined);
|
let [waku, setWaku] = useState<Waku | undefined>(undefined);
|
||||||
let [nick, setNick] = useState<string>(() => {
|
let [nick, setNick] = useState<string>(() => {
|
||||||
const persistedNick = window.localStorage.getItem('nick');
|
const persistedNick = window.localStorage.getItem('nick');
|
||||||
return persistedNick !== null ? persistedNick : generate();
|
return persistedNick !== null ? persistedNick : generate();
|
||||||
|
@ -87,15 +87,15 @@ export default function App() {
|
||||||
}, [nick]);
|
}, [nick]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (stateWaku) return;
|
if (waku) return;
|
||||||
|
|
||||||
initWaku(setWaku)
|
initWaku(setWaku)
|
||||||
.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));
|
||||||
}, [stateWaku]);
|
}, [waku]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!stateWaku) return;
|
if (!waku) return;
|
||||||
|
|
||||||
const handleRelayMessage = (wakuMsg: WakuMessage) => {
|
const handleRelayMessage = (wakuMsg: WakuMessage) => {
|
||||||
console.log('Message received: ', wakuMsg);
|
console.log('Message received: ', wakuMsg);
|
||||||
|
@ -105,23 +105,23 @@ export default function App() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
stateWaku.relay.addObserver(handleRelayMessage, [ChatContentTopic]);
|
waku.relay.addObserver(handleRelayMessage, [ChatContentTopic]);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}, [stateWaku]);
|
}, [waku]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!stateWaku) return;
|
if (!waku) return;
|
||||||
|
|
||||||
const handleProtocolChange = async (
|
const handleProtocolChange = async (
|
||||||
waku: Waku,
|
_waku: Waku,
|
||||||
{ peerId, protocols }: { peerId: PeerId; protocols: string[] }
|
{ peerId, protocols }: { peerId: PeerId; protocols: string[] }
|
||||||
) => {
|
) => {
|
||||||
if (protocols.includes(StoreCodec)) {
|
if (protocols.includes(StoreCodec)) {
|
||||||
console.log(`${peerId.toB58String()}: retrieving archived messages}`);
|
console.log(`${peerId.toB58String()}: retrieving archived messages}`);
|
||||||
try {
|
try {
|
||||||
const length = await retrieveStoreMessages(
|
const length = await retrieveStoreMessages(
|
||||||
waku,
|
_waku,
|
||||||
peerId,
|
peerId,
|
||||||
setArchivedMessages
|
setArchivedMessages
|
||||||
);
|
);
|
||||||
|
@ -135,37 +135,33 @@ export default function App() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
stateWaku.libp2p.peerStore.on(
|
waku.libp2p.peerStore.on(
|
||||||
'change:protocols',
|
'change:protocols',
|
||||||
handleProtocolChange.bind({}, stateWaku)
|
handleProtocolChange.bind({}, waku)
|
||||||
);
|
);
|
||||||
|
|
||||||
// To clean up listener when component unmounts
|
// To clean up listener when component unmounts
|
||||||
return () => {
|
return () => {
|
||||||
stateWaku?.libp2p.peerStore.removeListener(
|
waku?.libp2p.peerStore.removeListener(
|
||||||
'change:protocols',
|
'change:protocols',
|
||||||
handleProtocolChange.bind({}, stateWaku)
|
handleProtocolChange.bind({}, waku)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}, [stateWaku]);
|
}, [waku]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="chat-app"
|
className="chat-app"
|
||||||
style={{ height: '100vh', width: '100vw', overflow: 'hidden' }}
|
style={{ height: '100vh', width: '100vw', overflow: 'hidden' }}
|
||||||
>
|
>
|
||||||
<WakuContext.Provider value={{ waku: stateWaku }}>
|
<WakuContext.Provider value={{ waku: waku }}>
|
||||||
<ThemeProvider theme={themes}>
|
<ThemeProvider theme={themes}>
|
||||||
<Room
|
<Room
|
||||||
nick={nick}
|
nick={nick}
|
||||||
newMessages={newMessages}
|
newMessages={newMessages}
|
||||||
archivedMessages={archivedMessages}
|
archivedMessages={archivedMessages}
|
||||||
commandHandler={(input: string) => {
|
commandHandler={(input: string) => {
|
||||||
const { command, response } = handleCommand(
|
const { command, response } = handleCommand(input, waku, setNick);
|
||||||
input,
|
|
||||||
stateWaku,
|
|
||||||
setNick
|
|
||||||
);
|
|
||||||
const commandMessages = response.map((msg) => {
|
const commandMessages = response.map((msg) => {
|
||||||
return Message.fromUtf8String(command, msg);
|
return Message.fromUtf8String(command, msg);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue