Add Nostr repos list

This commit is contained in:
Alex Gleason committed 2025-07-30 15:01:59 -05:00
1 parent 2ff99b7116
commit 56863d5672
7 files changed
+444 -9

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
import { useQuery } from '@tanstack/react-query';
import { useProjectsManager } from './useProjectsManager';
import { nip19 } from 'nostr-tools';
export function useNostrEnabledProjects() {
const projectsManager = useProjectsManager();
return useQuery({
queryKey: ['nostr-enabled-projects'],
queryFn: async () => {
try {
await projectsManager.init();
const projects = await projectsManager.getProjects();
const nostrEnabledProjects: string[] = [];
for (const project of projects) {
try {
const naddr = await projectsManager.getNostrRepoAddress(project.id);
if (naddr) {
// Extract the d tag (identifier) from the naddr
const decoded = nip19.decode(naddr);
if (decoded.type === 'naddr') {
nostrEnabledProjects.push(decoded.data.identifier);
}
}
} catch {
// Skip projects that can't be checked
}
}
return nostrEnabledProjects;
} catch {
return [];
}
},
staleTime: 30 * 1000, // 30 seconds
gcTime: 60 * 1000, // 1 minute
});
}
+103
View File
@@ -0,0 +1,103 @@
import { useQuery } from '@tanstack/react-query';
import { useNostr } from '@nostrify/react';
import { useCurrentUser } from './useCurrentUser';
import type { NostrEvent } from '@nostrify/nostrify';
export interface NostrRepo {
event: NostrEvent;
id: string; // d tag value
name: string;
description?: string;
webUrls: string[];
cloneUrls: string[];
relays: string[];
maintainers: string[];
tags: string[];
earliestCommit?: string;
}
function parseRepoEvent(event: NostrEvent): NostrRepo | null {
// Validate that this is a repository announcement event
if (event.kind !== 30617) return null;
const dTag = event.tags.find(([name]) => name === 'd')?.[1];
if (!dTag) return null;
const name = event.tags.find(([name]) => name === 'name')?.[1] || dTag;
const description = event.tags.find(([name]) => name === 'description')?.[1];
const webUrls = event.tags
.filter(([name]) => name === 'web')
.map(([, url]) => url)
.filter(Boolean);
const cloneUrls = event.tags
.filter(([name]) => name === 'clone')
.map(([, url]) => url)
.filter(Boolean);
const relays = event.tags
.filter(([name]) => name === 'relays')
.map(([, relay]) => relay)
.filter(Boolean);
const maintainers = event.tags
.filter(([name]) => name === 'maintainers')
.map(([, pubkey]) => pubkey)
.filter(Boolean);
const tags = event.tags
.filter(([name]) => name === 't')
.map(([, tag]) => tag)
.filter(Boolean);
const earliestCommit = event.tags
.find(([name, , marker]) => name === 'r' && marker === 'euc')?.[1];
return {
event,
id: dTag,
name,
description,
webUrls,
cloneUrls,
relays,
maintainers,
tags,
earliestCommit,
};
}
export function useNostrRepos() {
const { nostr } = useNostr();
const { user } = useCurrentUser();
return useQuery({
queryKey: ['nostr-repos', user?.pubkey],
queryFn: async (c) => {
if (!user?.pubkey) return [];
const signal = AbortSignal.any([c.signal, AbortSignal.timeout(3000)]);
// Query for repository announcements by the current user
const events = await nostr.query([
{
kinds: [30617], // Repository announcements
authors: [user.pubkey],
limit: 50,
}
], { signal });
// Parse and validate events
const repos = events
.map(parseRepoEvent)
.filter((repo): repo is NostrRepo => repo !== null)
.sort((a, b) => b.event.created_at - a.event.created_at);
return repos;
},
enabled: !!user?.pubkey,
staleTime: 5 * 60 * 1000, // 5 minutes
gcTime: 10 * 60 * 1000, // 10 minutes
});
}