showing numPeers, nwaku version and minor improvements (#34)

This commit is contained in:
gabrielmer 2024-06-12 21:34:23 +02:00 committed by GitHub
parent e85c9f2be5
commit 47de47cec3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -64,6 +64,8 @@ function App() {
>([]); >([]);
const [communityName, setCommunityName] = useState(""); const [communityName, setCommunityName] = useState("");
const [apiEndpoint, setApiEndpoint] = useState(SERVICE_ENDPOINT); const [apiEndpoint, setApiEndpoint] = useState(SERVICE_ENDPOINT);
const [nwakuVersion, setNwakuVersion] = useState("");
const [numPeers, setNumPeers] = useState("")
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const updateMessage = (e: any) => setNewMessage(e.target.value); const updateMessage = (e: any) => setNewMessage(e.target.value);
@ -72,11 +74,6 @@ function App() {
const name = GetUser(); const name = GetUser();
setUsername(name); setUsername(name);
const endpoint = localStorage.getItem("apiEndpoint");
if (endpoint && !import.meta.env.VITE_API_ENDPOINT) {
setApiEndpoint(endpoint);
}
const localCommunity = localStorage.getItem("community"); const localCommunity = localStorage.getItem("community");
console.log("current community", localCommunity); console.log("current community", localCommunity);
setCommunity(localCommunity ? JSON.parse(localCommunity) : undefined); 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(() => { useEffect(() => {
const fetchAllMessages = async () => { const fetchAllMessages = async () => {
try { 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) => { const handleCursor = async (baseUrl: string, data: ResponseData) => {
if (data.cursor) { 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}`; 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]); }, [joinedCommunities, apiEndpoint]);
const delay = (ms: number) => new Promise((res) => setTimeout(res, ms)); 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 () => { const createUser = async () => {
try { try {
const name = await CreateUser(usernameInput); const name = await CreateUser(usernameInput);
@ -277,10 +316,6 @@ function App() {
localStorage.setItem("community", JSON.stringify(joinedCommunities[index])); localStorage.setItem("community", JSON.stringify(joinedCommunities[index]));
}; };
const saveSettings = () => {
localStorage.setItem("apiEndpoint", apiEndpoint);
};
const decodeMsg = (index: number, msg: Message) => { const decodeMsg = (index: number, msg: Message) => {
try { try {
if ( if (
@ -343,7 +378,7 @@ function App() {
</div> </div>
<DialogFooter> <DialogFooter>
<DialogClose asChild> <DialogClose asChild>
<Button type="submit" onClick={saveSettings}> <Button type="submit">
Save Save
</Button> </Button>
</DialogClose> </DialogClose>
@ -407,6 +442,11 @@ function App() {
<div className="absolute right-16 top-16">{settingsDialog()}</div> <div className="absolute right-16 top-16">{settingsDialog()}</div>
<div className="absolute left-16 top-16 flex flex-col">
<Label className="text-md">Nwaku Version: {nwakuVersion}</Label>
<Label className="text-md">Number of Peers: {numPeers}</Label>
</div>
{!username && ( {!username && (
<div className="flex flex-col gap-5 items-center justify-center h-screen mt-[-60px]"> <div className="flex flex-col gap-5 items-center justify-center h-screen mt-[-60px]">
{logoImage()} {logoImage()}
@ -465,6 +505,7 @@ function App() {
<Input <Input
value={newMessage} value={newMessage}
onChange={updateMessage} onChange={updateMessage}
onKeyDown={checkForEnter}
placeholder="Input your message" placeholder="Input your message"
autoComplete="off" autoComplete="off"
autoCorrect="off" autoCorrect="off"