mcgrizz/islands/statusManager.tsx
2023-10-04 04:09:01 -06:00

52 lines
1.4 KiB
TypeScript

import { JSX } from "preact";
import { Button } from "../components/Button.tsx";
import { ManageAction } from "../routes/api/manage.ts";
import { useEffect, useState } from "preact/hooks";
import { IS_BROWSER } from "$fresh/runtime.ts";
export function StatusManager(
props: JSX.HTMLAttributes<HTMLDivElement> & {
onAction?: (res: string) => void;
},
) {
const sendCommand = async (action: ManageAction) => {
console.log(action);
const res = await fetch("/api/manage", {
method: "POST",
body: action,
});
const body = await res.text();
props.onAction && props.onAction(body);
};
const [status, setStatus] = useState('');
const getStatus = async () => {
const res = await fetch('/api/manage');
const body = await res.text();
setStatus(body);
}
useEffect(() => {
if (IS_BROWSER) getStatus();
},[])
return (
<div {...props}>
{!!status && <small>Server is {status}</small>}
<div class="flex gap-4">
<Button color="wasabi" disabled={!status || status === 'running'} onClick={() => sendCommand(ManageAction.start)}>
Start
</Button>
<Button color="fire" disabled={!status || status === 'stopped'} onClick={() => sendCommand(ManageAction.stop)}>
Stop
</Button>
<Button color="sky" disabled={!status} onClick={() => sendCommand(ManageAction.restart)}>
Restart
</Button>
</div>
</div>
);
}