import { useState } from 'react'; import { useNostrRepos, type NostrRepo } from '@/hooks/useNostrRepos'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useToast } from '@/hooks/useToast'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Skeleton } from '@/components/ui/skeleton'; import { GitBranch, Download, ExternalLink, Users, Globe } from 'lucide-react'; import { cn } from '@/lib/utils'; import { nip19 } from 'nostr-tools'; import git from 'isomorphic-git'; import http from 'isomorphic-git/http/web'; interface NostrReposProps { className?: string; excludeProjectIds?: string[]; // Project IDs that are already "nostr enabled" onProjectCloned?: () => void; // Callback when a project is successfully cloned } export function NostrRepos({ className, excludeProjectIds = [], onProjectCloned }: NostrReposProps) { const { data: repos, isLoading, error } = useNostrRepos(); const projectsManager = useProjectsManager(); const { toast } = useToast(); const [cloningRepos, setCloningRepos] = useState>(new Set()); // Filter out repos that are already represented as projects const availableRepos = repos?.filter(repo => !excludeProjectIds.includes(repo.id)) || []; const handleCloneRepo = async (repo: NostrRepo) => { if (!repo.cloneUrls.length) { toast({ title: "No clone URL", description: "This repository doesn't have any clone URLs configured.", variant: "destructive", }); return; } setCloningRepos(prev => new Set(prev).add(repo.id)); try { await projectsManager.init(); // Create project directory const projectPath = `/projects/${repo.id}`; // Clone the repository const cloneUrl = repo.cloneUrls[0]; // Use the first clone URL await git.clone({ fs: projectsManager.fs, http, dir: projectPath, url: cloneUrl, singleBranch: true, depth: 1, }); // Set git config to mark this as a nostr-enabled project const naddr = nip19.naddrEncode({ identifier: repo.id, pubkey: repo.event.pubkey, kind: 30617, relays: repo.relays.length > 0 ? repo.relays : undefined, }); await git.setConfig({ fs: projectsManager.fs, dir: projectPath, path: 'nostr.repo', value: naddr, }); // No need to create project metadata - using filesystem stats and basename toast({ title: "Repository cloned", description: `${repo.name} has been cloned to your projects.`, }); // Call the callback to refresh the projects list, or fall back to page reload if (onProjectCloned) { onProjectCloned(); } else { window.location.reload(); } } catch (error) { console.error('Failed to clone repository:', error); toast({ title: "Clone failed", description: `Failed to clone ${repo.name}. Please try again.`, variant: "destructive", }); } finally { setCloningRepos(prev => { const newSet = new Set(prev); newSet.delete(repo.id); return newSet; }); } }; if (isLoading) { return (

Your Nostr Repositories

{[...Array(3)].map((_, i) => ( ))}
); } if (error) { return (

Your Nostr Repositories

Failed to load repositories

); } if (!availableRepos.length) { return null; // Don't show the section if there are no repos to display } return (

Your Nostr Repositories

Clone your NIP-34 repositories to start working on them locally.

{availableRepos.map((repo) => { const isCloning = cloningRepos.has(repo.id); return (
{repo.name} {repo.description && ( {repo.description} )}
{/* Tags */} {repo.tags.length > 0 && (
{repo.tags.slice(0, 3).map((tag) => ( {tag} ))} {repo.tags.length > 3 && ( +{repo.tags.length - 3} )}
)} {/* Repository info */}
{repo.maintainers.length > 0 && (
{repo.maintainers.length} maintainer{repo.maintainers.length !== 1 ? 's' : ''}
)} {repo.webUrls.length > 0 && (
Web
)}
{/* Actions */}
{repo.webUrls.length > 0 && ( )}
); })}
); }