import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; 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 { Folder, GitBranch, Download, ExternalLink, Users, Globe, Cloud, HardDrive, RefreshCw, Upload } 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'; import type { UnifiedRepo } from '@/hooks/useRepos'; interface RepoCardProps { repo: UnifiedRepo; onRepoCloned?: () => void; className?: string; } export function RepoCard({ repo, onRepoCloned, className }: RepoCardProps) { const navigate = useNavigate(); const projectsManager = useProjectsManager(); const { toast } = useToast(); const [isCloning, setIsCloning] = useState(false); const getStatusIcon = () => { switch (repo.status) { case 'local-only': return ; case 'published': return ; case 'remote-only': return ; case 'synced': return ; } }; const getStatusText = () => { switch (repo.status) { case 'local-only': return 'Local only'; case 'published': return 'Published'; case 'remote-only': return 'Remote only'; case 'synced': return 'Synced'; } }; const getStatusColor = () => { switch (repo.status) { case 'local-only': return 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200'; case 'published': return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200'; case 'remote-only': return 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200'; case 'synced': return 'bg-emerald-100 text-emerald-800 dark:bg-emerald-900 dark:text-emerald-200'; } }; const handleCardClick = () => { if (repo.localProject) { navigate(`/project/${repo.localProject.id}`); } }; const handleCloneRepo = async () => { if (!repo.nostrRepo || !repo.cloneUrls.length) { toast({ title: "No clone URL", description: "This repository doesn't have any clone URLs configured.", variant: "destructive", }); return; } setIsCloning(true); 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.nostrRepo.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, }); toast({ title: "Repository cloned", description: `${repo.name} has been cloned to your projects.`, }); // Call the callback to refresh the repos list if (onRepoCloned) { onRepoCloned(); } } 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 { setIsCloning(false); } }; const canOpen = repo.status === 'local-only' || repo.status === 'published' || repo.status === 'synced'; const canClone = repo.status === 'remote-only' && repo.cloneUrls.length > 0; return ( { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleCardClick(); } } : undefined} >
{repo.status === 'remote-only' ? ( ) : ( )} {repo.name} {repo.description && ( {repo.description} )}
{getStatusIcon()} {getStatusText()}
Modified {repo.lastModified.toLocaleDateString()}
{/* 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.webUrls.length > 0) && (
{repo.maintainers.length > 0 && (
{repo.maintainers.length} maintainer{repo.maintainers.length !== 1 ? 's' : ''}
)} {repo.webUrls.length > 0 && (
Web
)}
)} {/* Actions */} {(canClone || repo.webUrls.length > 0) && (
e.stopPropagation()}> {canClone && ( )} {repo.webUrls.length > 0 && ( )}
)}
); }