import React, { useState, useEffect, useMemo } from 'react'; import { Download, UserPlus } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { toast } from '@/hooks/useToast'; import { useLoginActions } from '@/hooks/useLoginActions'; import { generateSecretKey, nip19 } from 'nostr-tools'; import { cn } from '@/lib/utils'; type Step = 'key' | 'keygen'; interface SimpleSignupDialogProps { isOpen: boolean; onClose: () => void; onComplete?: () => void; onLogin?: () => void; } const SimpleSignupDialog: React.FC = ({ isOpen, onClose, onComplete, onLogin }) => { const [step, setStep] = useState('key'); const [isLoading, setIsLoading] = useState(false); const [downloaded, setDownloaded] = useState(false); const login = useLoginActions(); // Generate key once and keep it stable const secretKey = useMemo(() => generateSecretKey(), []); const nsec = useMemo(() => nip19.nsecEncode(secretKey), [secretKey]); // Reset state when dialog opens/closes useEffect(() => { if (isOpen) { // Always start with key generation as the default experience setStep('key'); setIsLoading(false); setDownloaded(false); } }, [isOpen]); const handleExtensionSignup = async () => { setIsLoading(true); try { if (!('nostr' in window)) { throw new Error('Nostr extension not found. Please install a NIP-07 extension.'); } await login.extension(); onClose(); if (onComplete) { setTimeout(() => { onComplete(); }, 600); } } catch (e: unknown) { const error = e as Error; toast({ title: 'Extension signup failed', description: error instanceof Error ? error.message : 'Extension signup failed', variant: 'destructive', }); } finally { setIsLoading(false); } }; const handleDownload = () => { try { const blob = new Blob([nsec], { type: 'text/plain; charset=utf-8' }); const url = globalThis.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'secret-key.txt'; a.style.display = 'none'; document.body.appendChild(a); a.click(); globalThis.URL.revokeObjectURL(url); document.body.removeChild(a); setDownloaded(true); toast({ title: 'Secret Key Saved!', description: 'Your key has been safely stored.', }); } catch { toast({ title: 'Download failed', description: 'Could not download the key file. Please copy it manually.', variant: 'destructive', }); } }; const handleNext = async () => { setIsLoading(true); try { login.nsec(nsec); // Mark signup completion time for fallback welcome modal localStorage.setItem('signup_completed', Date.now().toString()); onClose(); if (onComplete) { setTimeout(() => { onComplete(); }, 600); } } catch { toast({ title: 'Login Failed', description: 'Failed to login with the generated key. Please try again.', variant: 'destructive', }); } finally { setIsLoading(false); } }; const handleLoginClick = () => { onClose(); if (onLogin) { onLogin(); } }; if (step === 'key') { return ( Sign up

Welcome to Shakespeare

Create your account to get started

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

)}
); } return ( Your new key
🔑

Back up your secret key in a secure place. If lost, your account cannot be recovered. Never share your secret key with anyone.

); }; export default SimpleSignupDialog;