// NOTE: This file is stable and usually should not be modified. // It is important that all functionality in this file is preserved, and should only be modified if explicitly requested. import React, { useState, useEffect, useRef } from 'react'; import { Download, Upload, Eye, EyeOff } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Textarea } from '@/components/ui/textarea'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { toast } from '@/hooks/useToast'; import { useLoginActions } from '@/hooks/useLoginActions'; import { useNostrPublish } from '@/hooks/useNostrPublish'; import { useUploadFile } from '@/hooks/useUploadFile'; import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools'; interface SignupDialogProps { isOpen: boolean; onClose: () => void; } const SignupDialog: React.FC = ({ isOpen, onClose }) => { const [step, setStep] = useState<'generate' | 'download' | 'profile'>('generate'); const [nsec, setNsec] = useState(''); const [showKey, setShowKey] = useState(false); const [profileData, setProfileData] = useState({ name: '', about: '', picture: '' }); const login = useLoginActions(); const { mutateAsync: publishEvent, isPending: isPublishing } = useNostrPublish(); const { mutateAsync: uploadFile, isPending: isUploading } = useUploadFile(); const avatarFileInputRef = useRef(null); // Generate a proper nsec key using nostr-tools const generateKey = () => { const sk = generateSecretKey(); setNsec(nip19.nsecEncode(sk)); setStep('download'); }; const downloadKey = () => { try { // Create a blob with the key text const blob = new Blob([nsec], { type: 'text/plain; charset=utf-8' }); const url = globalThis.URL.createObjectURL(blob); const decoded = nip19.decode(nsec); if (decoded.type !== 'nsec') { throw new Error('Invalid nsec key'); } const pubkey = getPublicKey(decoded.data); const npub = nip19.npubEncode(pubkey); const filename = `nostr-${location.hostname.replaceAll(/\./g, '-')}-${npub.slice(5, 9)}.nsec.txt`; // Create a temporary link element and trigger download const a = document.createElement('a'); a.href = url; a.download = filename; a.style.display = 'none'; document.body.appendChild(a); a.click(); // Clean up immediately globalThis.URL.revokeObjectURL(url); document.body.removeChild(a); // Continue to profile step login.nsec(nsec); setStep('profile'); } catch { toast({ title: 'Download failed', description: 'Could not download the key file. Please copy it manually.', variant: 'destructive', }); } }; const handleAvatarUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; // Reset file input e.target.value = ''; // Validate file type if (!file.type.startsWith('image/')) { toast({ title: 'Invalid file type', description: 'Please select an image file for your avatar.', variant: 'destructive', }); return; } // Validate file size (max 5MB) if (file.size > 5 * 1024 * 1024) { toast({ title: 'File too large', description: 'Avatar image must be smaller than 5MB.', variant: 'destructive', }); return; } try { const tags = await uploadFile(file); // Get the URL from the first tag const url = tags[0]?.[1]; if (url) { setProfileData(prev => ({ ...prev, picture: url })); } } catch { toast({ title: 'Upload failed', description: 'Failed to upload avatar. Please try again.', variant: 'destructive', }); } }; const finishSignup = async (skipProfile = false) => { try { // Publish profile if user provided information if (!skipProfile && (profileData.name || profileData.about || profileData.picture)) { const metadata: Record = {}; if (profileData.name) metadata.name = profileData.name; if (profileData.about) metadata.about = profileData.about; if (profileData.picture) metadata.picture = profileData.picture; await publishEvent({ kind: 0, content: JSON.stringify(metadata), }); } } catch { toast({ title: 'Profile Setup Failed', description: 'Your account was created but profile setup failed. You can update it later.', variant: 'destructive', }); } finally { onClose(); } }; const getTitle = () => { if (step === 'generate') return 'Sign up'; if (step === 'download') return 'Secret Key'; if (step === 'profile') return 'Create Your Profile'; }; // Reset state when dialog opens useEffect(() => { if (isOpen) { setStep('generate'); setNsec(''); setShowKey(false); setProfileData({ name: '', about: '', picture: '' }); } }, [isOpen]); return ( {getTitle()}
{/* Generate Step */} {step === 'generate' && (
🔑
)} {/* Download Step */} {step === 'download' && (
🔑
Important Warning

This key is your primary and only means of accessing your account. Store it safely and securely. Please download your key to continue.

)} {/* Profile Step */} {step === 'profile' && (
{/* Publishing status indicator */} {isPublishing && (
Publishing your profile...
)} {/* Profile form */}
setProfileData(prev => ({ ...prev, name: e.target.value }))} placeholder='Your name' disabled={isPublishing} />