fix: state reload

This commit is contained in:
2025-03-05 10:59:49 +00:00
parent 072e791d2c
commit c5d45b0843
2 changed files with 11 additions and 22 deletions

View File

@ -35,7 +35,7 @@ export default function VpsPayment({
} }
}, 2_000); }, 2_000);
return () => clearInterval(tx); return () => clearInterval(tx);
}, [login]); }, [login, onPaid]);
return ( return (
<div className="flex flex-col gap-4 rounded-xl p-3 bg-neutral-900 items-center"> <div className="flex flex-col gap-4 rounded-xl p-3 bg-neutral-900 items-center">

View File

@ -18,17 +18,18 @@ import SSHKeySelector from "../components/ssh-keys";
const fit = new FitAddon(); const fit = new FitAddon();
export default function VmPage() { export default function VmPage() {
const { state } = useLocation() as { state?: VmInstance }; const location = useLocation() as { state?: VmInstance };
const { action } = useParams(); const { action } = useParams();
const login = useLogin(); const login = useLogin();
const navigate = useNavigate(); const navigate = useNavigate();
const [state, setState] = useState<VmInstance | undefined>(location?.state);
const [payment, setPayment] = useState<VmPayment>(); const [payment, setPayment] = useState<VmPayment>();
const [term] = useState<Terminal>(); const [term] = useState<Terminal>();
const termRef = useRef<HTMLDivElement | null>(null); const termRef = useRef<HTMLDivElement | null>(null);
const [editKey, setEditKey] = useState(false); const [editKey, setEditKey] = useState(false);
const [editReverse, setEditReverse] = useState<VmIpAssignment>(); const [editReverse, setEditReverse] = useState<VmIpAssignment>();
const [error, setError] = useState<string>(); const [error, setError] = useState<string>();
const [key, setKey] = useState(state?.ssh_key.id ?? -1); const [key, setKey] = useState(state?.ssh_key?.id ?? -1);
const renew = useCallback( const renew = useCallback(
async function () { async function () {
@ -42,9 +43,7 @@ export default function VmPage() {
async function reloadVmState() { async function reloadVmState() {
if (!state) return; if (!state) return;
const newState = await login?.api.getVm(state.id); const newState = await login?.api.getVm(state.id);
navigate("/vm", { setState(newState);
state: newState,
});
} }
function ipRow(a: VmIpAssignment, reverse: boolean) { function ipRow(a: VmIpAssignment, reverse: boolean) {
@ -162,12 +161,11 @@ export default function VmPage() {
<VpsPayment <VpsPayment
payment={payment} payment={payment}
onPaid={async () => { onPaid={async () => {
if (!login?.api || !state) return;
const newState = await login?.api.getVm(state.id);
navigate("/vm", {
state: newState,
});
setPayment(undefined); setPayment(undefined);
if (!login?.api || !state) return;
navigate("/vm", {
state
})
}} }}
/> />
)} )}
@ -187,11 +185,7 @@ export default function VmPage() {
await login.api.patchVm(state.id, { await login.api.patchVm(state.id, {
ssh_key_id: key, ssh_key_id: key,
}); });
const ns = await login.api.getVm(state?.id); await reloadVmState();
navigate(".", {
state: ns,
replace: true,
});
setEditKey(false); setEditKey(false);
} catch (e) { } catch (e) {
if (e instanceof Error) { if (e instanceof Error) {
@ -225,12 +219,7 @@ export default function VmPage() {
await login.api.patchVm(state.id, { await login.api.patchVm(state.id, {
reverse_dns: editReverse.reverse_dns, reverse_dns: editReverse.reverse_dns,
}); });
await reloadVmState();
const ns = await login.api.getVm(state?.id);
navigate(".", {
state: ns,
replace: true,
});
setEditReverse(undefined); setEditReverse(undefined);
} catch (e) { } catch (e) {
if (e instanceof Error) { if (e instanceof Error) {