"use client"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { z } from "zod"; import { useRouter } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm, SubmitErrorHandler } from "react-hook-form"; import { toast } from "@/components/ui/use-toast"; import { api } from "@/lib/trpc"; import { useState } from "react"; import { Check, Copy } from "lucide-react"; import { ActionButton } from "@/components/ui/action-button"; function ApiKeySuccess({ apiKey }: { apiKey: string }) { const [isCopied, setCopied] = useState(false); const onCopy = () => { navigator.clipboard.writeText(apiKey); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
Note: please copy the key and store it somewhere safe. Once you close the dialog, you won't be able to access it again.
); } function AddApiKeyForm({ onSuccess }: { onSuccess: (key: string) => void }) { const formSchema = z.object({ name: z.string(), }); const router = useRouter(); const mutator = api.apiKeys.create.useMutation({ onSuccess: (resp) => { onSuccess(resp.key); router.refresh(); }, onError: () => { toast({ description: "Something went wrong", variant: "destructive" }); }, }); const form = useForm>({ resolver: zodResolver(formSchema), }); async function onSubmit(value: z.infer) { mutator.mutate({ name: value.name }); } const onError: SubmitErrorHandler> = (errors) => { toast({ description: Object.values(errors) .map((v) => v.message) .join("\n"), variant: "destructive", }); }; return (
{ return ( Name Give your API key a unique name ); }} /> Create ); } export default function AddApiKey() { const [key, setKey] = useState(undefined); const [dialogOpen, setDialogOpen] = useState(false); return ( {key ? "Key was successfully created" : "Create API key"} {key ? ( ) : ( )} ); }