Files
2026-10-29 17:39:02 -03:00

54 lines
1.4 KiB
TypeScript

import { Signal, useSignal, useSignalEffect } from "@preact/signals";
import Button from "@/components/Button.tsx";
import InputField from "@/components/InputField.tsx";
import Form from "@/islands/Form.tsx";
import { post_json } from "@/src/client.ts";
interface AskCodeProps {
page: Signal<string>;
}
export default function AskCode({ page }: AskCodeProps) {
const checking = useSignal(false);
const on_success = (data: { code: string }) => {
localStorage.setItem("code", data.code);
page.value = "keysnew";
};
useSignalEffect(() => {
const fn = async () => {
const code = localStorage.getItem("code");
if (!code) {
return;
}
checking.value = true;
const result = await post_json<{ code: string }>("/api/verify_code", { code });
checking.value = false;
if (result.ok) {
on_success(result.data);
} else if (result.status !== 0) {
// Network errors keep the code so it can be retried on the next load
localStorage.removeItem("code");
}
};
fn();
});
if (checking.value) {
return <p class="text-center text-gray-800 dark:text-gray-400">Verifying code...</p>;
}
return (
<Form class="mx-auto w-full max-w-md py-8" action="/api/verify_code" on_success={on_success}>
<div class="flex flex-col gap-4">
<InputField type="text" name="code" placeholder="Code" />
<Button type="submit" variant="primary">Enter</Button>
</div>
</Form>
);
}