"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import Button from "@/components/shared/Button"; import Input from "@/components/shared/Input"; export default function NewAPIKeyPage() { const router = useRouter(); const [name, setName] = useState(""); const [error, setError] = useState(""); const [newKey, setNewKey] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); async function handleSubmit(e) { e.preventDefault(); setIsSubmitting(true); setError(""); try { const response = await fetch( "http://localhost:8080/twirp/apikeys.APIKeysService/CreateAPIKey", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ token: localStorage.getItem("token"), name: name, expires_at: "", // Optional, you can add a date picker if needed }), }, ); const data = await response.json(); // Check for Twirp error response if (data.code || data.msg) { throw new Error(data.msg || "Failed to create API key"); } if (!data.key) { throw new Error("No API key received"); } setNewKey({ key: data.key, name: data.api_key.name, created_at: data.api_key.created_at, }); } catch (error) { setError(error.message || "Failed to create API key"); console.error("Error:", error); } finally { setIsSubmitting(false); } } return ( <> {newKey ? (

API Key Created: {newKey.name}

Please copy your API key now. You won't be able to see it again!

{newKey.key}

Created on: {new Date(newKey.created_at).toLocaleString()}

) : (

Create New API Key

Give your API key a name to help you identify it later.

{error && (
{error}
)}
setName(e.target.value)} placeholder="e.g., Development Server" required />
)} ); }