Show follows repositories in Clone tab

This commit is contained in:
Alex Gleason committed 2025-11-28 21:25:55 -06:00
1 parent 37c72391a0
commit c49e8811af
3 files changed
+250 -34

No files matched your search

+43
View File
@@ -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<string[]> => {
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
});
}
+86
View File
@@ -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<Repository[]> => {
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<string, NostrEvent>();
// 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
});
}