import React, { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { GitBranch, Loader2 } from 'lucide-react'; import type { Repository } from '@/hooks/useUserRepositories'; import { useAuthor } from '@/hooks/useAuthor'; import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar'; import { genUserName } from '@/lib/genUserName'; import { useNavigate } from 'react-router-dom'; import { NostrURI } from '@/lib/NostrURI'; import { useProjectsManager } from '@/hooks/useProjectsManager'; import { useToast } from '@/hooks/useToast'; import { useTranslation } from 'react-i18next'; import { useCurrentUser } from '@/hooks/useCurrentUser'; interface RepositoryCardProps { repo: Repository; } export function RepositoryCard({ repo }: RepositoryCardProps) { const { data: authorData } = useAuthor(repo.pubkey); const { user } = useCurrentUser(); const navigate = useNavigate(); const projectsManager = useProjectsManager(); const { toast } = useToast(); const { t } = useTranslation(); const [isCloning, setIsCloning] = useState(false); const displayName = authorData?.metadata?.name || genUserName(repo.pubkey); const profileImage = authorData?.metadata?.picture; // Construct Nostr clone URL const nostrURI = new NostrURI({ pubkey: repo.pubkey, identifier: repo.repoId }); const handleClone = async (e: React.MouseEvent) => { // Prevent card click from triggering e.stopPropagation(); setIsCloning(true); try { await projectsManager.init(); // Clone the repository using the Nostr URI const project = await projectsManager.cloneProject({ name: repo.name, repoUrl: nostrURI.toString(), fork: user?.pubkey !== repo.pubkey, }); toast({ title: t('nostrRepositoryImportedSuccessfully'), description: t('repositoryClonedFromNostr', { repoName: repo.name }), }); // Navigate to the new project with build parameter navigate(`/project/${project.id}?build`); } catch (error) { console.error('Failed to clone repository:', error); let errorMessage = t('failedToImportRepository'); if (error instanceof Error) { if (error.message.includes('Repository not found on Nostr network')) { errorMessage = t('repositoryNotFoundOnNostr'); } else if (error.message.includes('No clone URLs found')) { errorMessage = t('noCloneUrlsFound'); } else if (error.message.includes('All clone attempts failed')) { errorMessage = t('allCloneAttemptsFailed'); } else { errorMessage = error.message; } } toast({ title: t('failedToImportRepository'), description: errorMessage, variant: "destructive", }); } finally { setIsCloning(false); } }; return ( {/* Header with name and author */}

{repo.name}

{profileImage && ( )} {displayName.slice(0, 2).toUpperCase()} {displayName}
{/* Description */} {repo.description && (

{repo.description}

)} {/* Tags */} {repo.repoTags.length > 0 && (
{repo.repoTags.slice(0, 3).map((tag, index) => ( {tag} ))} {repo.repoTags.length > 3 && ( +{repo.repoTags.length - 3} )}
)} {/* Personal fork badge */} {repo.isPersonalFork && (
Personal Fork
)} {/* Actions */}
); }