feat: backend api

This commit is contained in:
2024-11-26 15:29:18 +00:00
parent daa640068b
commit 3c7736dae7
34 changed files with 1147 additions and 697 deletions

66
src/pages/vm.tsx Normal file
View File

@ -0,0 +1,66 @@
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { LNVpsApi, VmInstance, VmPayment } from "../api";
import VpsInstanceRow from "../components/vps-instance";
import useLogin from "../hooks/login";
import { ApiUrl } from "../const";
import { EventPublisher } from "@snort/system";
import { useCallback, useEffect, useState } from "react";
import VpsPayment from "../components/vps-payment";
export default function VmPage() {
const { state } = useLocation() as { state?: VmInstance };
const { action } = useParams();
const login = useLogin();
const navigate = useNavigate();
const [payment, setPayment] = useState<VmPayment>();
const renew = useCallback(
async function () {
if (!login?.signer || !state) return;
const api = new LNVpsApi(
ApiUrl,
new EventPublisher(login.signer, login.pubkey),
);
const p = await api.renewVm(state.id);
setPayment(p);
},
[login, state],
);
useEffect(() => {
switch (action) {
case "renew":
renew();
}
}, [renew, action]);
if (!state) {
return <h2>No VM selected</h2>;
}
return (
<div className="flex flex-col gap-4">
<VpsInstanceRow vm={state} />
{action === "renew" && (
<>
<h3>Renew VPS</h3>
{payment && (
<VpsPayment
payment={payment}
onPaid={async () => {
if (!login?.signer || !state) return;
const api = new LNVpsApi(
ApiUrl,
new EventPublisher(login.signer, login.pubkey),
);
const newState = await api.getVm(state.id);
navigate("/vm", {
state: newState,
});
}}
/>
)}
</>
)}
</div>
);
}