From 47de47cec31f482ab6034f66f992ba9eb86a690d Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Wed, 12 Jun 2024 21:34:23 +0200 Subject: [PATCH] showing numPeers, nwaku version and minor improvements (#34) --- src/App.tsx | 65 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 9f510fb..3503559 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -64,6 +64,8 @@ function App() { >([]); const [communityName, setCommunityName] = useState(""); const [apiEndpoint, setApiEndpoint] = useState(SERVICE_ENDPOINT); + const [nwakuVersion, setNwakuVersion] = useState(""); + const [numPeers, setNumPeers] = useState("") // eslint-disable-next-line @typescript-eslint/no-explicit-any const updateMessage = (e: any) => setNewMessage(e.target.value); @@ -72,11 +74,6 @@ function App() { const name = GetUser(); setUsername(name); - const endpoint = localStorage.getItem("apiEndpoint"); - if (endpoint && !import.meta.env.VITE_API_ENDPOINT) { - setApiEndpoint(endpoint); - } - const localCommunity = localStorage.getItem("community"); console.log("current community", localCommunity); setCommunity(localCommunity ? JSON.parse(localCommunity) : undefined); @@ -89,6 +86,22 @@ function App() { } }, []); + useEffect(() => { + const fetchNwakuVersion = async () => { + try { + let url = `${apiEndpoint}/debug/v1/version`; + const response = await axios.get(url); + console.log("fetchNwakuVersion data:", response.data); + setNwakuVersion(response.data); + } catch (error) { + console.error("Error fetching data:", error); + } + }; + + fetchNwakuVersion() + + }, [apiEndpoint]); + useEffect(() => { const fetchAllMessages = async () => { try { @@ -132,6 +145,18 @@ function App() { } }; + const fetchNumPeers = async () => { + try{ + let url = `${apiEndpoint}/admin/v1/peers`; + const response = await axios.get(url); + console.log("getNumPeers data:", response.data); + setNumPeers(response.data.length) + console.log(`there are ${response.data.length} peers`) + } catch (error) { + console.error("Error fetching data:", error); + } + } + const handleCursor = async (baseUrl: string, data: ResponseData) => { if (data.cursor) { const url = `${baseUrl}&pubsubTopic=${data.cursor.pubsub_topic}&digest=${data.cursor.digest.data}&senderTime=${data.cursor.sender_time}&storeTime=${data.cursor.store_time}`; @@ -157,9 +182,16 @@ function App() { } }; - const intervalId = setInterval(fetchAllMessages, 5000); // Trigger fetchData every 5 seconds + fetchAllMessages() + fetchNumPeers() + + const intervalId = setInterval(fetchAllMessages, 2000); // Trigger fetchData every 2 seconds + const intervalId2 = setInterval(fetchNumPeers, 10000); // Trigger fetchNumPeers every 10 seconds - return () => clearInterval(intervalId); + return () => { + clearInterval(intervalId); + clearInterval(intervalId2); + } }, [joinedCommunities, apiEndpoint]); const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); @@ -217,6 +249,13 @@ function App() { } }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const checkForEnter = (e: any) => { + if (e.key === 'Enter') { + sendMessage() + } + } + const createUser = async () => { try { const name = await CreateUser(usernameInput); @@ -277,10 +316,6 @@ function App() { localStorage.setItem("community", JSON.stringify(joinedCommunities[index])); }; - const saveSettings = () => { - localStorage.setItem("apiEndpoint", apiEndpoint); - }; - const decodeMsg = (index: number, msg: Message) => { try { if ( @@ -343,7 +378,7 @@ function App() { - @@ -407,6 +442,11 @@ function App() {
{settingsDialog()}
+
+ + +
+ {!username && (
{logoImage()} @@ -465,6 +505,7 @@ function App() {