"use client" import type React from "react" import { useState, useEffect } from "react" import { Key, ExternalLink, ArrowRight, X, Check } from "lucide-react" import { motion, AnimatePresence } from "motion/react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog" import { TOGETHER_LINK } from "@/lib/utils" import { useApiKey } from "@/hooks/use-api-key" interface ApiKeyModalProps { isOpen: boolean onClose: () => void onSubmit: (key: string) => void } export function ApiKeyModal({ isOpen, onClose, onSubmit }: ApiKeyModalProps) { const [apiKeyInput, setApiKeyInput] = useState("") const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const [shakeKey, setShakeKey] = useState(0) const [success, setSuccess] = useState(false) const [existingKey, setApiKey] = useApiKey() useEffect(() => { if (isOpen) { setSuccess(false) setError(null) setApiKeyInput((current) => { if (existingKey && current === "") { return existingKey } return current }) } }, [isOpen, existingKey]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!apiKeyInput.trim()) return setIsLoading(true) setError(null) try { const res = await fetch("/api/validate-api-key", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ apiKey: apiKeyInput.trim() }), }) const data = await res.json() if (!data.valid) { setError("Invalid API key. Please check and try again.") setShakeKey((k) => k + 1) setIsLoading(false) return } } catch { setError("Could not validate key. Please try again.") setShakeKey((k) => k + 1) setIsLoading(false) return } setIsLoading(false) setSuccess(true) onSubmit(apiKeyInput.trim()) setTimeout(() => { setSuccess(false) setApiKeyInput("") }, 1400) } const handleDelete = () => { setApiKey(null) setApiKeyInput("") setError(null) onClose() } return ( {success && ( API key saved You're all set for unlimited generation )}
{existingKey ? "Your API key" : "Add your API key"}
{existingKey ? "Update or remove your Together AI key." : "You've used all your free credits. Add your key for unlimited use."}
{error && ( {error} )} 0 ? { x: [0, -8, 8, -6, 6, -3, 3, 0] } : {}} transition={{ duration: 0.45, ease: "easeInOut" }} > { setApiKeyInput(e.target.value); setError(null) }} placeholder="sk-••••••••••••••••" className={`bg-secondary border-border/50 text-white placeholder-muted-foreground/40 py-5 pr-10 font-mono text-sm transition-colors duration-200 ${error ? "border-red-500/60 focus-visible:ring-red-500/20" : ""}`} /> {apiKeyInput && ( )}
Get a free Together AI key

Stored locally · never sent to our servers

) }