import React, { useState, useEffect, useRef } from 'react'; import { Cloud, Upload } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; // Alert components removed as they're not used in the simplified design import { useLoginActions } from '@/hooks/useLoginActions'; import { cn } from '@/lib/utils'; // Only using key-add step now interface SimpleLoginDialogProps { isOpen: boolean; onClose: () => void; onLogin: () => void; onSignup?: () => void; } const validateNsec = (nsec: string) => { return /^nsec1[a-zA-Z0-9]{58}$/.test(nsec); }; const validateBunkerUri = (uri: string) => { return uri.startsWith('bunker://'); }; const SimpleLoginDialog: React.FC = ({ isOpen, onClose, onLogin, onSignup }) => { // Removed step state - we only have one step now const [isLoading, setIsLoading] = useState(false); const [isFileLoading, setIsFileLoading] = useState(false); const [nsec, setNsec] = useState(''); const [bunkerUri, setBunkerUri] = useState(''); const [activeTab, setActiveTab] = useState<'nsec' | 'bunker'>('nsec'); const [errors, setErrors] = useState<{ nsec?: string; bunker?: string; extension?: string; file?: string; }>({}); const fileInputRef = useRef(null); const login = useLoginActions(); // Reset state when dialog opens/closes useEffect(() => { if (isOpen) { setIsLoading(false); setIsFileLoading(false); setNsec(''); setBunkerUri(''); setActiveTab('nsec'); setErrors({}); // Reset file input if (fileInputRef.current) { fileInputRef.current.value = ''; } } }, [isOpen]); const handleExtensionLogin = async () => { setIsLoading(true); setErrors(prev => ({ ...prev, extension: undefined })); try { if (!('nostr' in window)) { throw new Error('Nostr extension not found. Please install a NIP-07 extension.'); } await login.extension(); onLogin(); onClose(); } catch (e: unknown) { const error = e as Error; setErrors(prev => ({ ...prev, extension: error instanceof Error ? error.message : 'Extension login failed' })); } finally { setIsLoading(false); } }; const handleKeyLogin = () => { if (!nsec.trim()) { setErrors(prev => ({ ...prev, nsec: 'Please enter your secret key' })); return; } if (!validateNsec(nsec)) { setErrors(prev => ({ ...prev, nsec: 'Invalid secret key format. Must be a valid nsec starting with nsec1.' })); return; } executeLogin(nsec); }; const handleBunkerLogin = async () => { if (!bunkerUri.trim()) { setErrors(prev => ({ ...prev, bunker: 'Please enter a bunker URI' })); return; } if (!validateBunkerUri(bunkerUri)) { setErrors(prev => ({ ...prev, bunker: 'Invalid bunker URI format. Must start with bunker://' })); return; } setIsLoading(true); setErrors(prev => ({ ...prev, bunker: undefined })); try { await login.bunker(bunkerUri); onLogin(); onClose(); setBunkerUri(''); } catch { setErrors(prev => ({ ...prev, bunker: 'Failed to connect to bunker. Please check the URI.' })); } finally { setIsLoading(false); } }; const executeLogin = (key: string) => { setIsLoading(true); setErrors({}); // Use a timeout to allow the UI to update before the synchronous login call setTimeout(() => { try { login.nsec(key); onLogin(); onClose(); } catch { setErrors({ nsec: "Failed to login with this key. Please check that it's correct." }); setIsLoading(false); } }, 50); }; const handleFileUpload = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setIsFileLoading(true); setErrors({}); const reader = new FileReader(); reader.onload = (event) => { setIsFileLoading(false); const content = event.target?.result as string; if (content) { const trimmedContent = content.trim(); if (validateNsec(trimmedContent)) { executeLogin(trimmedContent); } else { setErrors({ file: 'File does not contain a valid secret key.' }); } } else { setErrors({ file: 'Could not read file content.' }); } }; reader.onerror = () => { setIsFileLoading(false); setErrors({ file: 'Failed to read file.' }); }; reader.readAsText(file); }; const handleSignupClick = () => { onClose(); if (onSignup) { onSignup(); } }; return ( Log in

Welcome back to Shakespeare

Sign in to your existing account

🔑
{/* Tab selector */}
{activeTab === 'nsec' && (
{ setNsec(e.target.value); if (errors.nsec) setErrors(prev => ({ ...prev, nsec: undefined })); }} placeholder="nsec1..." className={errors.nsec ? 'border-red-500' : ''} /> {errors.nsec && (

{errors.nsec}

)}
{errors.file && (

{errors.file}

)}
)} {activeTab === 'bunker' && (
{ setBunkerUri(e.target.value); if (errors.bunker) setErrors(prev => ({ ...prev, bunker: undefined })); }} placeholder="bunker://" className={errors.bunker ? 'border-red-500' : ''} /> {errors.bunker && (

{errors.bunker}

)}
)}
or
{/* Extension indicator - only show if extension is detected */} {window.nostr && (

)}
New to Shakespeare?
); }; export default SimpleLoginDialog;