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, EllipsisVertical, Edit } from 'lucide-react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; 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'; import { useGitSettings } from '@/hooks/useGitSettings'; import { detectFork } from '@/lib/detectFork'; import { NostrRepoEditDialog } from '@/components/NostrRepoEditDialog'; import { useQueryClient } from '@tanstack/react-query'; interface RepositoryCardProps { repo: Repository; } export function RepositoryCard({ repo }: RepositoryCardProps) { const { data: authorData } = useAuthor(repo.pubkey); const { user } = useCurrentUser(); const { settings: gitSettings } = useGitSettings(); const navigate = useNavigate(); const projectsManager = useProjectsManager(); const { toast } = useToast(); const { t } = useTranslation(); const queryClient = useQueryClient(); const [isCloning, setIsCloning] = useState(false); const [editRepoOpen, setEditRepoOpen] = useState(false); const displayName = authorData?.metadata?.name || genUserName(repo.pubkey); const profileImage = authorData?.metadata?.picture; const isOwnRepo = user?.pubkey === repo.pubkey; // Construct Nostr clone URL const nostrURI = new NostrURI({ pubkey: repo.pubkey, identifier: repo.repoId, relay: repo.relays[0], }); const handleClone = async (e: React.MouseEvent) => { // Prevent card click from triggering e.stopPropagation(); setIsCloning(true); try { await projectsManager.init(); // Determine if this is a fork const repoUrl = nostrURI.toString(); const fork = await detectFork(repoUrl, user?.pubkey, gitSettings.credentials); // Clone the repository using the Nostr URI const project = await projectsManager.cloneProject({ name: repo.name, repoUrl, fork, }); 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); } }; const handleRepoEditSuccess = () => { // Refresh repositories list after successful edit queryClient.invalidateQueries({ queryKey: ['nostr', 'user-repositories', user?.pubkey] }); }; return ( {/* Header with name and menu */}

{repo.name}

{isOwnRepo && ( setEditRepoOpen(true)} className="gap-2"> Edit Repository )}
{/* Author */}
{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 */}
); }