diff --git a/src/components/RepositoryCard.tsx b/src/components/RepositoryCard.tsx new file mode 100644 index 00000000..3b20d838 --- /dev/null +++ b/src/components/RepositoryCard.tsx @@ -0,0 +1,158 @@ +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, + ExternalLink, + Copy, + Check, +} 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 { useToast } from '@/hooks/useToast'; +import { useNavigate } from 'react-router-dom'; + +interface RepositoryCardProps { + repo: Repository; +} + +export function RepositoryCard({ repo }: RepositoryCardProps) { + const { data: authorData } = useAuthor(repo.pubkey); + const { toast } = useToast(); + const navigate = useNavigate(); + const [copiedUrl, setCopiedUrl] = useState(null); + + const displayName = authorData?.metadata?.name || genUserName(repo.pubkey); + const profileImage = authorData?.metadata?.picture; + + // Get the first clone URL (prefer HTTPS) + const primaryCloneUrl = repo.cloneUrls.find(url => url.startsWith('https://')) || repo.cloneUrls[0]; + const webUrl = repo.webUrls[0]; + + const handleCopyUrl = async (url: string, e: React.MouseEvent) => { + e.stopPropagation(); + try { + await navigator.clipboard.writeText(url); + setCopiedUrl(url); + toast({ + title: 'Copied to clipboard', + description: 'Repository URL copied successfully', + }); + setTimeout(() => setCopiedUrl(null), 2000); + } catch (error) { + console.error('Failed to copy URL:', error); + toast({ + title: 'Failed to copy', + description: 'Could not copy URL to clipboard', + variant: 'destructive', + }); + } + }; + + const handleClone = () => { + if (primaryCloneUrl) { + // Navigate to clone page with URL parameter + navigate(`/clone?url=${encodeURIComponent(primaryCloneUrl)}`); + } + }; + + 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 */} +
+ {primaryCloneUrl && ( + <> + + + + )} + {webUrl && ( + + )} +
+
+
+ ); +} diff --git a/src/hooks/useUserRepositories.ts b/src/hooks/useUserRepositories.ts new file mode 100644 index 00000000..388cb9ca --- /dev/null +++ b/src/hooks/useUserRepositories.ts @@ -0,0 +1,90 @@ +import { useNostr } from '@nostrify/react'; +import { useQuery } from '@tanstack/react-query'; +import type { NostrEvent } from '@nostrify/nostrify'; + +// Kind 30617 for repository announcements (NIP-34) +const REPOSITORY_KIND = 30617; + +export interface Repository extends NostrEvent { + repoId: string; + name: string; + description: string; + webUrls: string[]; + cloneUrls: string[]; + relays: string[]; + maintainers: string[]; + repoTags: string[]; + earliestUniqueCommit?: string; + isPersonalFork: boolean; +} + +export function useUserRepositories(userPubkey?: string) { + const { nostr } = useNostr(); + + return useQuery({ + queryKey: ['nostr', 'user-repositories', userPubkey], + queryFn: async (): Promise => { + if (!userPubkey) { + return []; + } + + // Get all repository announcements for this user + const events = await nostr.query([{ + kinds: [REPOSITORY_KIND], + authors: [userPubkey], + limit: 100, + }], { signal: AbortSignal.timeout(3000) }); + + // Process repository events + const repositories: Repository[] = []; + const repoMap = new Map(); + + // Group by d-tag (repo-id) to get latest version + for (const event of events) { + const dTag = event.tags.find(tag => tag[0] === 'd')?.[1]; + if (!dTag) continue; + + const existing = repoMap.get(dTag); + if (!existing || event.created_at > existing.created_at) { + repoMap.set(dTag, event); + } + } + + // Convert to Repository objects + for (const [dTag, event] of repoMap) { + try { + const name = event.tags.find(tag => tag[0] === 'name')?.[1] || dTag; + const description = event.tags.find(tag => tag[0] === 'description')?.[1] || ''; + const webUrls = event.tags.filter(tag => tag[0] === 'web').map(tag => tag[1]).filter(Boolean); + const cloneUrls = event.tags.filter(tag => tag[0] === 'clone').map(tag => tag[1]).filter(Boolean); + const relays = event.tags.filter(tag => tag[0] === 'relays').map(tag => tag[1]).filter(Boolean); + const maintainers = event.tags.filter(tag => tag[0] === 'maintainers').map(tag => tag[1]).filter(Boolean); + const repoTags = event.tags.filter(tag => tag[0] === 't' && tag[1] !== 'personal-fork').map(tag => tag[1]); + const earliestUniqueCommit = event.tags.find(tag => tag[0] === 'r' && tag[2] === 'euc')?.[1]; + const isPersonalFork = event.tags.some(tag => tag[0] === 't' && tag[1] === 'personal-fork'); + + repositories.push({ + ...event, + repoId: dTag, + name, + description, + webUrls, + cloneUrls, + relays, + maintainers, + repoTags, + earliestUniqueCommit, + isPersonalFork, + }); + } catch (error) { + console.warn('Failed to parse repository:', error); + } + } + + return repositories; + }, + enabled: !!userPubkey, + staleTime: 30000, // 30 seconds + refetchInterval: 60000, // 1 minute + }); +} diff --git a/src/pages/Clone.tsx b/src/pages/Clone.tsx index ac4b54e6..558ede9d 100644 --- a/src/pages/Clone.tsx +++ b/src/pages/Clone.tsx @@ -10,6 +10,11 @@ import { useToast } from '@/hooks/useToast'; import { ZipImportDialog } from '@/components/ZipImportDialog'; import { useSeoMeta } from '@unhead/react'; import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from '@/components/ui/dropdown-menu'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; +import { useUserRepositories } from '@/hooks/useUserRepositories'; +import { RepositoryCard } from '@/components/RepositoryCard'; +import { Skeleton } from '@/components/ui/skeleton'; +import { Card, CardContent } from '@/components/ui/card'; export default function Clone() { const { t } = useTranslation(); @@ -22,6 +27,8 @@ export default function Clone() { const { toast } = useToast(); const autoCloneInitiatedRef = useRef(false); const [isZipDialogOpen, setIsZipDialogOpen] = useState(false); + const { user } = useCurrentUser(); + const { data: repositories = [], isLoading: isLoadingRepos } = useUserRepositories(user?.pubkey); useSeoMeta({ title: `${t('importRepository')} - Shakespeare`, @@ -276,6 +283,47 @@ export default function Clone() { open={isZipDialogOpen} onOpenChange={setIsZipDialogOpen} /> + + {/* User's NIP-34 Repositories */} + {user && (isLoadingRepos || repositories.length > 0) && ( +
+ {isLoadingRepos ? ( +
+ {Array.from({ length: 3 }).map((_, i) => ( + + +
+ +
+ + +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ ))} +
+ ) : ( +
+ {repositories.map((repo) => ( + + ))} +
+ )} +
+ )} ); } \ No newline at end of file