mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-07 23:33:09 +00:00
make useCreateWaku hook
This commit is contained in:
parent
52e3fb4afa
commit
f59f5d11ee
@ -1,4 +1,6 @@
|
|||||||
import { useEffect, useReducer, useState } from "react";
|
/* eslint no-use-before-define: 0 */
|
||||||
|
// @ts-ignore
|
||||||
|
import React, { useEffect, useReducer, useState } from "react";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
import handleCommand from "./command";
|
import handleCommand from "./command";
|
||||||
import Room from "./Room";
|
import Room from "./Room";
|
||||||
@ -86,25 +88,70 @@ async function retrieveStoreMessages(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function App() {
|
const useCreateWaku = (options: any): undefined | WakuLight => {
|
||||||
const [messages, dispatchMessages] = useReducer(reduceMessages, []);
|
const [node, setNode] = React.useState<undefined | WakuLight>(undefined);
|
||||||
const [waku, setWaku] = useState<WakuLight | undefined>(undefined);
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
Promise.resolve().then(async () => {
|
||||||
|
const waku = await createLightNode(options);
|
||||||
|
await waku.start();
|
||||||
|
await waitForRemotePeer(waku, [
|
||||||
|
Protocols.Store,
|
||||||
|
Protocols.Filter,
|
||||||
|
Protocols.LightPush,
|
||||||
|
]);
|
||||||
|
setNode(waku);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
|
||||||
|
const usePersistentNick = (): [
|
||||||
|
string,
|
||||||
|
React.Dispatch<React.SetStateAction<string>>
|
||||||
|
] => {
|
||||||
const [nick, setNick] = useState<string>(() => {
|
const [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();
|
||||||
});
|
});
|
||||||
const [historicalMessagesRetrieved, setHistoricalMessagesRetrieved] =
|
|
||||||
useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
localStorage.setItem("nick", nick);
|
localStorage.setItem("nick", nick);
|
||||||
}, [nick]);
|
}, [nick]);
|
||||||
|
|
||||||
useEffect(() => {
|
return [nick, setNick];
|
||||||
initWaku(setWaku)
|
};
|
||||||
.then(() => console.log("Waku init done"))
|
|
||||||
.catch((e) => console.log("Waku init failed ", e));
|
export default function App() {
|
||||||
}, []);
|
const [messages, dispatchMessages] = useReducer(reduceMessages, []);
|
||||||
|
|
||||||
|
const publicKey = "AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM";
|
||||||
|
const fqdn = "test.waku.nodes.status.im";
|
||||||
|
const enrTree = `enrtree://${publicKey}@${fqdn}`;
|
||||||
|
const options = {
|
||||||
|
libp2p: {
|
||||||
|
peerDiscovery: [
|
||||||
|
wakuDnsDiscovery(enrTree, {
|
||||||
|
store: 1,
|
||||||
|
filter: 2,
|
||||||
|
lightpush: 2,
|
||||||
|
}),
|
||||||
|
wakuPeerExchangeDiscovery(),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const waku = useCreateWaku(options);
|
||||||
|
|
||||||
|
const [nick, setNick] = usePersistentNick();
|
||||||
|
|
||||||
|
const [historicalMessagesRetrieved, setHistoricalMessagesRetrieved] =
|
||||||
|
useState(false);
|
||||||
|
|
||||||
|
// useEffect(() => {
|
||||||
|
// initWaku(setWaku)
|
||||||
|
// .then(() => console.log("Waku init done"))
|
||||||
|
// .catch((e) => console.log("Waku init failed ", e));
|
||||||
|
// }, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!waku) return;
|
if (!waku) return;
|
||||||
@ -194,30 +241,35 @@ export default function App() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initWaku(setter: (waku: WakuLight) => void) {
|
// async function initWaku(setter: (waku: WakuLight) => void) {
|
||||||
try {
|
// try {
|
||||||
const publicKey = "AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM";
|
// const publicKey = "AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM";
|
||||||
const fqdn = "test.waku.nodes.status.im";
|
// const fqdn = "test.waku.nodes.status.im";
|
||||||
const enrTree = `enrtree://${publicKey}@${fqdn}`;
|
// const enrTree = `enrtree://${publicKey}@${fqdn}`;
|
||||||
const waku = await createLightNode({
|
// const waku = await createLightNode({
|
||||||
libp2p: {
|
// libp2p: {
|
||||||
peerDiscovery: [
|
// peerDiscovery: [
|
||||||
wakuDnsDiscovery(enrTree, {
|
// wakuDnsDiscovery(enrTree, {
|
||||||
store: 1,
|
// store: 1,
|
||||||
filter: 2,
|
// filter: 2,
|
||||||
lightpush: 2,
|
// lightpush: 2,
|
||||||
}),
|
// }),
|
||||||
wakuPeerExchangeDiscovery(),
|
// wakuPeerExchangeDiscovery(),
|
||||||
],
|
// ],
|
||||||
},
|
// },
|
||||||
});
|
// });
|
||||||
await waku.start();
|
// await waku.start();
|
||||||
|
// await waitForRemotePeer(waku, [
|
||||||
|
// Protocols.Store,
|
||||||
|
// Protocols.Filter,
|
||||||
|
// Protocols.LightPush,
|
||||||
|
// ]);
|
||||||
|
|
||||||
setter(waku);
|
// setter(waku);
|
||||||
} catch (e) {
|
// } catch (e) {
|
||||||
console.log("Issue starting waku ", e);
|
// console.log("Issue starting waku ", e);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
function reduceMessages(state: Message[], newMessages: Message[]) {
|
function reduceMessages(state: Message[], newMessages: Message[]) {
|
||||||
return state.concat(newMessages);
|
return state.concat(newMessages);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user