fixed server status client

This commit is contained in:
Vomitblood 2024-11-12 00:55:04 +08:00
parent 36f00fc1d7
commit 7232aed4ef
4 changed files with 63 additions and 37 deletions

View file

@ -31,6 +31,8 @@ PGPASSWORD=asdfpassword
## Server ## Server
!only listening on localhost is supported. DO NOT run this on a public ip.
- `/setup-demo-db` - `/setup-demo-db`
- `/nuke-db` - `/nuke-db`
- `/fetch-all-users` - `/fetch-all-users`

View file

@ -18,7 +18,7 @@
"url": "https://*.vomitblood.com" "url": "https://*.vomitblood.com"
}, },
{ {
"url": "http://localhost" "url": "http://localhost:*"
} }
], ],
"identifier": "http:default" "identifier": "http:default"

View file

@ -1,15 +1,12 @@
import { Box, Button, Chip, CircularProgress, Popover, Stack, useTheme } from "@mui/material"; import { Box, Button, Chip, CircularProgress, Popover, Stack } from "@mui/material";
import { fetch } from "@tauri-apps/plugin-http"; import { fetch } from "@tauri-apps/plugin-http";
import { useAtom } from "jotai"; import { useAtom } from "jotai";
import { MouseEvent, useState } from "react"; import { MouseEvent, useEffect, useState } from "react";
import { serverConnectionAtom, serverUrlAtom } from "../../lib/jotai"; import { serverConnectionAtom, serverUrlAtom } from "../../lib/jotai";
import { defaultSettings } from "../../lib/settings"; import { defaultSettings } from "../../lib/settings";
import { ServerUrlInput } from "./ServerUrlInput"; import { ServerUrlInput } from "./ServerUrlInput";
export const ServerStatus = () => { export const ServerStatus = () => {
// contexts
const theme = useTheme();
// atoms // atoms
const [serverConnection, setServerConnection] = useAtom(serverConnectionAtom); const [serverConnection, setServerConnection] = useAtom(serverConnectionAtom);
const [serverUrl, setServerUrl] = useAtom(serverUrlAtom); const [serverUrl, setServerUrl] = useAtom(serverUrlAtom);
@ -27,24 +24,48 @@ export const ServerStatus = () => {
// function to check server health // function to check server health
const checkServerConnection = async () => { const checkServerConnection = async () => {
fetch(serverUrl + "/health") // remove trailing slash
.then((response) => { setServerUrl(serverUrl.replace(/\/$/, ""));
if (response.ok) {
setServerConnection("connected");
} else {
setServerConnection("disconnected");
}
})
.catch(() => {
setServerConnection("disconnected");
});
// if the server is connected then continue to ping every 5 seconds try {
if (serverConnection === "connected") { const response = await fetch(serverUrl + "/health");
setTimeout(checkServerConnection, 5000); if (response.ok) {
console.log("connected");
setServerConnection("connected");
} else {
console.log("disconnected");
setServerConnection("disconnected");
}
} catch (e) {
console.log("disconnected", e);
setServerConnection("disconnected");
} }
}; };
const chipProps = {
color: serverConnection === "connected" ? "success" : serverConnection === "disconnected" ? "error" : "warning",
label:
serverConnection === "connected"
? "Server connected"
: serverConnection === "disconnected"
? "Server disconnected"
: "Connecting...",
};
useEffect(() => {
// only start interval if server is connected
if (serverConnection === "connected") {
const intervalId = setInterval(checkServerConnection, 2000);
console.log("Started server ping interval");
// cleanup interval on disconnection or component unmount
return () => {
clearInterval(intervalId);
console.log("Stopped server ping interval");
};
}
}, [serverConnection, serverUrl]);
return ( return (
<Box <Box
sx={{ sx={{
@ -62,8 +83,7 @@ export const ServerStatus = () => {
)} )}
{/* @ts-ignore */} {/* @ts-ignore */}
<Chip <Chip
color='error' {...chipProps}
label='Server disconnected'
onClick={clickEvent} onClick={clickEvent}
size='small' size='small'
/> />
@ -80,36 +100,40 @@ export const ServerStatus = () => {
horizontal: "center", horizontal: "center",
}} }}
sx={{ sx={{
"transform": "translate(0px, 4px)", "transform": "translate(0px, 8px)",
"& .MuiPaper-root": { "& .MuiPaper-root": {
borderRadius: defaultSettings.style.radius + "px", borderRadius: defaultSettings.style.radius + "px",
}, },
}} }}
> >
<Stack <Box
alignItems='center'
display='flex'
direction='row'
spacing={1}
sx={{ sx={{
backgroundColor: "#434446", backgroundColor: "#434446",
p: 2, p: 2,
}} }}
> >
<form <form
onSubmit={() => { onSubmit={(e) => {
checkServerConnection(); checkServerConnection();
e.preventDefault();
}} }}
> >
<ServerUrlInput /> <Stack
<Button alignItems='center'
type='submit' display='flex'
variant='outlined' direction='row'
spacing={1}
> >
Connect <ServerUrlInput />
</Button> <Button
type='submit'
variant='outlined'
>
Connect
</Button>
</Stack>
</form> </form>
</Stack> </Box>
</Popover> </Popover>
</Box> </Box>
); );

View file

@ -9,7 +9,7 @@ export const ServerUrlInput = () => {
return ( return (
<TextField <TextField
fullWidth fullWidth
label='Paste your backend URL here (default: http://localhost:5000)' label='Backend server URL'
onChange={(event) => setServerUrl(event.target.value)} onChange={(event) => setServerUrl(event.target.value)}
size='small' size='small'
value={serverUrl} value={serverUrl}