import { useState, useEffect, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, DialogFooter, } from '@/components/ui/dialog'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { ScrollArea } from '@/components/ui/scroll-area'; import { GitBranch, Loader2, AlertCircle, CheckCircle, Plus, X, Megaphone, } from 'lucide-react'; import { useGit } from '@/hooks/useGit'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useNostr } from '@/hooks/useNostr'; import { useToast } from '@/hooks/useToast'; import { nip19 } from 'nostr-tools'; interface AnnounceRepositoryDialogProps { projectId: string; remoteUrl?: string; open?: boolean; onOpenChange?: (open: boolean) => void; } export function AnnounceRepositoryDialog({ projectId, remoteUrl, open, onOpenChange, }: AnnounceRepositoryDialogProps) { const [isOpen, setIsOpen] = useState(open ?? false); const [repoId, setRepoId] = useState(''); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [webUrls, setWebUrls] = useState([]); const [cloneUrls, setCloneUrls] = useState([]); const [relays, setRelays] = useState(['wss://relay.nostr.band']); const [earliestCommit, setEarliestCommit] = useState(''); const [isCreating, setIsCreating] = useState(false); const [error, setError] = useState(null); const [createdNaddr, setCreatedNaddr] = useState(null); const [newWebUrl, setNewWebUrl] = useState(''); const [newCloneUrl, setNewCloneUrl] = useState(''); const [newRelay, setNewRelay] = useState(''); const { git } = useGit(); const { user } = useCurrentUser(); const { nostr } = useNostr(); const { toast } = useToast(); const projectPath = `/projects/${projectId}`; useEffect(() => { if (open !== undefined) { setIsOpen(open); } }, [open]); const parseRemoteUrl = useCallback((url: string) => { // Extract repo ID from Nostr URL if (url.startsWith('nostr://')) { const match = url.match(/^nostr:\/\/[^/]+\/(.+)$/); if (match) { const id = match[1]; setRepoId(id); setName(id.split('/').pop() || id); } } // Add the remote URL as a clone URL if (url && !cloneUrls.includes(url)) { setCloneUrls([url]); } }, [cloneUrls]); const getEarliestCommit = useCallback(async () => { try { const commits = await git.log({ dir: projectPath, ref: 'HEAD', }); if (commits.length > 0) { // Get the earliest commit (last in the log) const earliest = commits[commits.length - 1]; setEarliestCommit(earliest.oid); } } catch (err) { console.warn('Failed to get earliest commit:', err); } }, [git, projectPath]); useEffect(() => { if (isOpen && remoteUrl) { // Parse remote URL to pre-fill data parseRemoteUrl(remoteUrl); // Get earliest commit getEarliestCommit(); } }, [isOpen, remoteUrl, parseRemoteUrl, getEarliestCommit]); const addWebUrl = () => { if (newWebUrl.trim() && !webUrls.includes(newWebUrl.trim())) { setWebUrls([...webUrls, newWebUrl.trim()]); setNewWebUrl(''); } }; const removeWebUrl = (index: number) => { setWebUrls(webUrls.filter((_, i) => i !== index)); }; const addCloneUrl = () => { if (newCloneUrl.trim() && !cloneUrls.includes(newCloneUrl.trim())) { setCloneUrls([...cloneUrls, newCloneUrl.trim()]); setNewCloneUrl(''); } }; const removeCloneUrl = (index: number) => { setCloneUrls(cloneUrls.filter((_, i) => i !== index)); }; const addRelay = () => { if (newRelay.trim() && !relays.includes(newRelay.trim())) { setRelays([...relays, newRelay.trim()]); setNewRelay(''); } }; const removeRelay = (index: number) => { setRelays(relays.filter((_, i) => i !== index)); }; const announceRepository = async () => { if (!user || !user.signer) { toast({ title: 'Authentication required', description: 'Please log in with Nostr to announce repositories', variant: 'destructive', }); return; } if (!repoId.trim()) { toast({ title: 'Repository ID required', description: 'Please enter a repository identifier', variant: 'destructive', }); return; } if (cloneUrls.length === 0) { toast({ title: 'Clone URL required', description: 'Please add at least one clone URL', variant: 'destructive', }); return; } setIsCreating(true); setError(null); try { // Build tags array per NIP-34 const tags: string[][] = [ ['d', repoId.trim()], // Required: repository identifier ]; if (name.trim()) { tags.push(['name', name.trim()]); } if (description.trim()) { tags.push(['description', description.trim()]); } // Add web URLs webUrls.forEach(url => { if (url.trim()) tags.push(['web', url.trim()]); }); // Add clone URLs (required) cloneUrls.forEach(url => { if (url.trim()) tags.push(['clone', url.trim()]); }); // Add relays for patch submissions relays.forEach(relay => { if (relay.trim()) tags.push(['relays', relay.trim()]); }); // Add earliest unique commit (euc) for repository identification if (earliestCommit.trim()) { tags.push(['r', earliestCommit.trim(), 'euc']); } // Create repository announcement event (kind 30617) const repoEvent = { kind: 30617, content: '', tags, created_at: Math.floor(Date.now() / 1000), }; console.log('=== ANNOUNCING REPOSITORY ==='); console.log('Repository ID:', repoId.trim()); console.log('Owner pubkey:', user.pubkey); console.log('Creating repository announcement event:', repoEvent); // Sign the event const signedEvent = await user.signer.signEvent(repoEvent); console.log('Signed repository announcement:', signedEvent); console.log('Event coordinate:', `30617:${user.pubkey}:${repoId.trim()}`); // Publish to Nostr relays await nostr.event(signedEvent); console.log('Published repository announcement to Nostr relays'); console.log('To verify: Query for kind 30617 with authors=[' + user.pubkey + '] and #d=[' + repoId.trim() + ']'); // Create naddr for the repository const naddr = nip19.naddrEncode({ identifier: repoId.trim(), pubkey: user.pubkey, kind: 30617, relays: relays.length > 0 ? relays : undefined, }); setCreatedNaddr(naddr); toast({ title: 'Repository announced!', description: 'Your repository has been announced on Nostr', }); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Unknown error'; setError(errorMessage); toast({ title: 'Failed to announce repository', description: errorMessage, variant: 'destructive', }); } finally { setIsCreating(false); } }; const handleOpenChange = (newOpen: boolean) => { setIsOpen(newOpen); onOpenChange?.(newOpen); // Reset form when closing if (!newOpen) { setRepoId(''); setName(''); setDescription(''); setWebUrls([]); setCloneUrls([]); setRelays(['wss://relay.nostr.band']); setEarliestCommit(''); setCreatedNaddr(null); setError(null); setNewWebUrl(''); setNewCloneUrl(''); setNewRelay(''); } }; const copyNaddr = () => { if (createdNaddr) { navigator.clipboard.writeText(createdNaddr); toast({ title: 'Copied to clipboard', description: 'Repository address copied', }); } }; return ( Announce Repository on Nostr Publish a repository announcement (NIP-34) to make it discoverable on Nostr git clients {createdNaddr ? ( // Success state

Repository Announced!

Your repository is now discoverable on Nostr git clients

Share this address with others to let them find your repository

) : ( // Form
{/* Error alert */} {error && ( {error} )} {/* Repository ID */}
setRepoId(e.target.value)} disabled={isCreating} />

Unique identifier for this repository (like GitHub's owner/repo)

{/* Name */}
setName(e.target.value)} disabled={isCreating} />
{/* Description */}