From c49e8811af7e456202355021ef3fecbb3e6768ce Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 28 Nov 2025 21:25:55 -0600 Subject: [PATCH] Show follows repositories in Clone tab --- src/hooks/useContacts.ts | 43 ++++++++ src/hooks/useFollowedRepositories.ts | 86 +++++++++++++++ src/pages/Clone.tsx | 155 +++++++++++++++++++++------ 3 files changed, 250 insertions(+), 34 deletions(-) create mode 100644 src/hooks/useContacts.ts create mode 100644 src/hooks/useFollowedRepositories.ts diff --git a/src/hooks/useContacts.ts b/src/hooks/useContacts.ts new file mode 100644 index 00000000..e7344f3a --- /dev/null +++ b/src/hooks/useContacts.ts @@ -0,0 +1,43 @@ +import { useNostr } from '@nostrify/react'; +import { useQuery } from '@tanstack/react-query'; + +/** + * Hook to get the user's contact list (kind 3 follow list) + * Returns an array of pubkeys that the user follows + */ +export function useContacts(userPubkey?: string) { + const { nostr } = useNostr(); + + return useQuery({ + queryKey: ['nostr', 'contacts', userPubkey], + queryFn: async (): Promise => { + if (!userPubkey) { + return []; + } + + // Get the user's contact list (kind 3) + const events = await nostr.query([{ + kinds: [3], + authors: [userPubkey], + limit: 1, + }], { signal: AbortSignal.timeout(3000) }); + + if (events.length === 0) { + return []; + } + + // Get the most recent contact list event + const contactList = events.sort((a, b) => b.created_at - a.created_at)[0]; + + // Extract pubkeys from 'p' tags + const followedPubkeys = contactList.tags + .filter(tag => tag[0] === 'p' && tag[1]) + .map(tag => tag[1]); + + return followedPubkeys; + }, + enabled: !!userPubkey, + staleTime: 60000, // 1 minute + refetchInterval: 300000, // 5 minutes + }); +} diff --git a/src/hooks/useFollowedRepositories.ts b/src/hooks/useFollowedRepositories.ts new file mode 100644 index 00000000..e90c4e6c --- /dev/null +++ b/src/hooks/useFollowedRepositories.ts @@ -0,0 +1,86 @@ +import { useNostr } from '@nostrify/react'; +import { useQuery } from '@tanstack/react-query'; +import type { NostrEvent } from '@nostrify/nostrify'; +import type { Repository } from './useUserRepositories'; + +// Kind 30617 for repository announcements (NIP-34) +const REPOSITORY_KIND = 30617; + +/** + * Hook to get repositories from a list of followed users + */ +export function useFollowedRepositories(followedPubkeys: string[] = []) { + const { nostr } = useNostr(); + + return useQuery({ + queryKey: ['nostr', 'followed-repositories', followedPubkeys.sort().join(',')], + queryFn: async (): Promise => { + if (followedPubkeys.length === 0) { + return []; + } + + // Get all repository announcements from followed users + const events = await nostr.query([{ + kinds: [REPOSITORY_KIND], + authors: followedPubkeys, + limit: 500, // Increased limit to get more repos from follows + }], { signal: AbortSignal.timeout(5000) }); + + // Process repository events + const repositories: Repository[] = []; + const repoMap = new Map(); + + // Group by author+d-tag to get latest version of each repo + for (const event of events) { + const dTag = event.tags.find(tag => tag[0] === 'd')?.[1]; + if (!dTag) continue; + + const key = `${event.pubkey}:${dTag}`; + const existing = repoMap.get(key); + if (!existing || event.created_at > existing.created_at) { + repoMap.set(key, event); + } + } + + // Convert to Repository objects + for (const [, event] of repoMap) { + try { + const dTag = event.tags.find(tag => tag[0] === 'd')?.[1]; + if (!dTag) continue; + + 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); + } + } + + // Sort by created_at (newest first) + return repositories.sort((a, b) => b.created_at - a.created_at); + }, + enabled: followedPubkeys.length > 0, + staleTime: 30000, // 30 seconds + refetchInterval: 60000, // 1 minute + }); +} diff --git a/src/pages/Clone.tsx b/src/pages/Clone.tsx index 558ede9d..bc1bc44b 100644 --- a/src/pages/Clone.tsx +++ b/src/pages/Clone.tsx @@ -12,9 +12,12 @@ 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 { useContacts } from '@/hooks/useContacts'; +import { useFollowedRepositories } from '@/hooks/useFollowedRepositories'; import { RepositoryCard } from '@/components/RepositoryCard'; import { Skeleton } from '@/components/ui/skeleton'; import { Card, CardContent } from '@/components/ui/card'; +import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; export default function Clone() { const { t } = useTranslation(); @@ -29,6 +32,9 @@ export default function Clone() { const [isZipDialogOpen, setIsZipDialogOpen] = useState(false); const { user } = useCurrentUser(); const { data: repositories = [], isLoading: isLoadingRepos } = useUserRepositories(user?.pubkey); + const { data: contacts = [] } = useContacts(user?.pubkey); + const { data: followedRepositories = [], isLoading: isLoadingFollowedRepos } = useFollowedRepositories(contacts); + const [activeTab, setActiveTab] = useState<'my-projects' | 'follows'>('my-projects'); useSeoMeta({ title: `${t('importRepository')} - Shakespeare`, @@ -284,44 +290,125 @@ export default function Clone() { onOpenChange={setIsZipDialogOpen} /> - {/* User's NIP-34 Repositories */} - {user && (isLoadingRepos || repositories.length > 0) && ( + {/* NIP-34 Repositories with Tabs */} + {user && (
- {isLoadingRepos ? ( -
- {Array.from({ length: 3 }).map((_, i) => ( - - -
- -
- - -
-
-
- - -
-
- - -
-
- - + setActiveTab(value as 'my-projects' | 'follows')}> + + + My Projects + + + Follows + + + + + {isLoadingRepos ? ( +
+ {Array.from({ length: 3 }).map((_, i) => ( + + +
+ +
+ + +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ ))} +
+ ) : repositories.length > 0 ? ( +
+ {repositories.map((repo) => ( + + ))} +
+ ) : ( + + +
+

+ No repositories found. Publish your first repository to Nostr to see it here. +

- ))} -
- ) : ( -
- {repositories.map((repo) => ( - - ))} -
- )} + )} + + + + {isLoadingFollowedRepos ? ( +
+ {Array.from({ length: 6 }).map((_, i) => ( + + +
+ +
+ + +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+ ))} +
+ ) : followedRepositories.length > 0 ? ( +
+ {followedRepositories.map((repo) => ( + + ))} +
+ ) : contacts.length > 0 ? ( + + +
+

+ None of the people you follow have published repositories yet. +

+
+
+
+ ) : ( + + +
+

+ You're not following anyone yet. Follow people on Nostr to see their repositories here. +

+
+
+
+ )} +
+
)}