import { CodeEditor } from "@/components/shared/code-editor"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { api } from "@/utils/api"; import { zodResolver } from "@hookform/resolvers/zod"; import copy from "copy-to-clipboard"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; const formSchema = z.object({ name: z.string().min(1, "Name is required"), prefix: z.string().optional(), expiresIn: z.number().nullable(), organizationId: z.string().min(1, "Organization is required"), // Rate limiting fields rateLimitEnabled: z.boolean().optional(), rateLimitTimeWindow: z.number().nullable(), rateLimitMax: z.number().nullable(), // Request limiting fields remaining: z.number().nullable().optional(), refillAmount: z.number().nullable().optional(), refillInterval: z.number().nullable().optional(), }); type FormValues = z.infer; const EXPIRATION_OPTIONS = [ { label: "Never", value: "0" }, { label: "1 day", value: String(60 * 60 * 24) }, { label: "7 days", value: String(60 * 60 * 24 * 7) }, { label: "30 days", value: String(60 * 60 * 24 * 30) }, { label: "90 days", value: String(60 * 60 * 24 * 90) }, { label: "1 year", value: String(60 * 60 * 24 * 365) }, ]; const TIME_WINDOW_OPTIONS = [ { label: "1 minute", value: String(60 * 1000) }, { label: "5 minutes", value: String(5 * 60 * 1000) }, { label: "15 minutes", value: String(15 * 60 * 1000) }, { label: "30 minutes", value: String(30 * 60 * 1000) }, { label: "1 hour", value: String(60 * 60 * 1000) }, { label: "1 day", value: String(24 * 60 * 60 * 1000) }, ]; const REFILL_INTERVAL_OPTIONS = [ { label: "1 hour", value: String(60 * 60 * 1000) }, { label: "6 hours", value: String(6 * 60 * 60 * 1000) }, { label: "12 hours", value: String(12 * 60 * 60 * 1000) }, { label: "1 day", value: String(24 * 60 * 60 * 1000) }, { label: "7 days", value: String(7 * 24 * 60 * 60 * 1000) }, { label: "30 days", value: String(30 * 24 * 60 * 60 * 1000) }, ]; export const AddApiKey = () => { const [open, setOpen] = useState(false); const [showSuccessModal, setShowSuccessModal] = useState(false); const [newApiKey, setNewApiKey] = useState(""); const { refetch } = api.user.get.useQuery(); const { data: organizations } = api.organization.all.useQuery(); const createApiKey = api.user.createApiKey.useMutation({ onSuccess: (data) => { if (!data) return; setNewApiKey(data.key); setOpen(false); setShowSuccessModal(true); form.reset(); void refetch(); }, onError: () => { toast.error("Failed to generate API key"); }, }); const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { name: "", prefix: "", expiresIn: null, organizationId: "", rateLimitEnabled: false, rateLimitTimeWindow: null, rateLimitMax: null, remaining: null, refillAmount: null, refillInterval: null, }, }); const rateLimitEnabled = form.watch("rateLimitEnabled"); const onSubmit = async (values: FormValues) => { createApiKey.mutate({ name: values.name, expiresIn: values.expiresIn || undefined, prefix: values.prefix || undefined, metadata: { organizationId: values.organizationId, }, // Rate limiting rateLimitEnabled: values.rateLimitEnabled, rateLimitTimeWindow: values.rateLimitTimeWindow || undefined, rateLimitMax: values.rateLimitMax || undefined, // Request limiting remaining: values.remaining || undefined, refillAmount: values.refillAmount || undefined, refillInterval: values.refillInterval || undefined, }); }; return ( <> Generate API Key Create a new API key for accessing the API. You can set an expiration date and a custom prefix for better organization.
( Name )} /> ( Prefix )} /> ( Expiration )} /> ( Organization )} /> {/* Rate Limiting Section */}

Rate Limiting

(
Enable Rate Limiting Limit the number of requests within a time window
)} /> {rateLimitEnabled && ( <> ( Time Window The duration in which requests are counted )} /> ( Maximum Requests field.onChange( e.target.value ? Number.parseInt(e.target.value, 10) : null, ) } /> Maximum number of requests allowed within the time window )} /> )}
{/* Request Limiting Section */}

Request Limiting

( Total Request Limit field.onChange( e.target.value ? Number.parseInt(e.target.value, 10) : null, ) } /> Total number of requests allowed (leave empty for unlimited) )} /> ( Refill Amount field.onChange( e.target.value ? Number.parseInt(e.target.value, 10) : null, ) } /> Number of requests to add on each refill )} /> ( Refill Interval How often to refill the request limit )} />
API Key Generated Successfully Please copy your API key now. You won't be able to see it again!
); };