import React, { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { AlertCircle, Key, Loader2 } from 'lucide-react' import { useAuth } from '@/contexts/AuthContext' interface LoginDialogProps { open: boolean onOpenChange?: (open: boolean) => void } const LoginDialog: React.FC = ({ open, onOpenChange, }) => { const { login, isLoading, error, clearError } = useAuth() const [apiKey, setApiKey] = useState('') const [localLoading, setLocalLoading] = useState(false) // Clear form and errors when dialog opens/closes useEffect(() => { if (open) { setApiKey('') clearError() } }, [open, clearError]) // Clear error when user starts typing useEffect(() => { if (error && apiKey) { clearError() } }, [apiKey, error, clearError]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!apiKey.trim()) { return } setLocalLoading(true) try { await login(apiKey.trim()) // Login successful - dialog will close automatically when auth state changes setApiKey('') } catch (err) { // Error is handled by the AuthContext console.error('Login failed:', err) } finally { setLocalLoading(false) } } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !isSubmitDisabled) { // Create a synthetic FormEvent to satisfy handleSubmit's type const syntheticEvent = { preventDefault: () => {}, } as React.FormEvent; void handleSubmit(syntheticEvent) } } const isSubmitDisabled = !apiKey.trim() || isLoading || localLoading return ( Authentication Required Please enter your management API key to access the Llamactl dashboard.
{ void handleSubmit(e) }}>
{/* Error Display */} {error && (
{error}
)} {/* API Key Input */}
setApiKey(e.target.value)} onKeyDown={handleKeyDown} placeholder="sk-management-..." disabled={isLoading || localLoading} className={error ? "border-red-500" : ""} autoFocus autoComplete="off" />

Your management API key is required to access instance management features.

) } export default LoginDialog