From fc9bd3ef02a71a9e8100fc1d72dd1afeffcd382a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 29 Dec 2025 22:14:30 -0600 Subject: [PATCH] Add useNostrRepo hook --- src/components/NostrRepoEditDialog.tsx | 20 ++----------- src/components/git-sync/steps/SyncStep.tsx | 25 ++++++++++++---- src/hooks/useNostrRepo.ts | 33 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 src/hooks/useNostrRepo.ts diff --git a/src/components/NostrRepoEditDialog.tsx b/src/components/NostrRepoEditDialog.tsx index 96dd4d67..b042424d 100644 --- a/src/components/NostrRepoEditDialog.tsx +++ b/src/components/NostrRepoEditDialog.tsx @@ -13,11 +13,10 @@ import { DialogHeader, DialogTitle, } from '@/components/ui/dialog'; -import { useNostr } from '@nostrify/react'; -import { useQuery } from '@tanstack/react-query'; import { useNostrPublish } from '@/hooks/useNostrPublish'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAppContext } from '@/hooks/useAppContext'; +import { useNostrRepo } from '@/hooks/useNostrRepo'; import { createRepositoryAnnouncementEvent } from '@/lib/announceRepository'; import { nip19 } from 'nostr-tools'; @@ -48,25 +47,12 @@ export function NostrRepoEditDialog({ const [error, setError] = useState(null); const { user } = useCurrentUser(); - const { nostr } = useNostr(); const { mutateAsync: publishEvent } = useNostrPublish(); const { config } = useAppContext(); // Load existing repository data with react-query - const { data: existingEvent, isLoading } = useQuery({ - queryKey: ['nostr-repo', user?.pubkey, repoIdentifier], - queryFn: async () => { - if (!user) return null; - - const events = await nostr.query( - [{ kinds: [30617], authors: [user.pubkey], '#d': [repoIdentifier], limit: 1 }], - { signal: AbortSignal.timeout(5000) } - ); - - return events.length > 0 ? events[0] : null; - }, - enabled: !!user && !!repoIdentifier, - staleTime: 30000, // Cache for 30 seconds + const { data: existingEvent, isLoading } = useNostrRepo({ + repoIdentifier, }); // Update form fields when event data loads diff --git a/src/components/git-sync/steps/SyncStep.tsx b/src/components/git-sync/steps/SyncStep.tsx index 5e15b226..65b038d8 100644 --- a/src/components/git-sync/steps/SyncStep.tsx +++ b/src/components/git-sync/steps/SyncStep.tsx @@ -1,4 +1,4 @@ -import { useState, useCallback } from 'react'; +import { useState, useCallback, useEffect } from 'react'; import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp, LoaderCircle, History, Edit } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; @@ -25,6 +25,7 @@ import { useFSPaths } from '@/hooks/useFSPaths'; import { useAppContext } from '@/hooks/useAppContext'; import { useGitSyncState } from '@/hooks/useGitSyncState'; import { useGitAutosync } from '@/hooks/useGitAutosync'; +import { useNostrRepo } from '@/hooks/useNostrRepo'; import type { SyncStepProps } from '../types'; import { cn } from '@/lib/utils'; import { NostrURI } from '@/lib/NostrURI'; @@ -211,6 +212,22 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { } }, [remoteUrl]); + // Warm up the Nostr repo query if this is a Nostr URI + // This prefetches the data so it's ready when the edit dialog opens + useNostrRepo({ + repoIdentifier: repoIdentifier || '', + enabled: !!repoIdentifier && remoteUrl?.protocol === 'nostr:', + }); + + // Extract and set repo identifier on mount if it's a Nostr URI + useEffect(() => { + if (remoteUrl?.protocol === 'nostr:') { + getRepoIdentifier().then(id => { + if (id) setRepoIdentifier(id); + }); + } + }, [remoteUrl, getRepoIdentifier]); + // Get the button label based on current operation const getButtonLabel = (): string => { if (!isSyncing) return 'Sync'; @@ -227,10 +244,8 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { } }; - const handleEditRepo = async () => { - const repoId = await getRepoIdentifier(); - if (repoId) { - setRepoIdentifier(repoId); + const handleEditRepo = () => { + if (repoIdentifier) { setEditRepoOpen(true); } }; diff --git a/src/hooks/useNostrRepo.ts b/src/hooks/useNostrRepo.ts new file mode 100644 index 00000000..1c7d6ddb --- /dev/null +++ b/src/hooks/useNostrRepo.ts @@ -0,0 +1,33 @@ +import { useNostr } from '@nostrify/react'; +import { useQuery } from '@tanstack/react-query'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; + +interface UseNostrRepoOptions { + repoIdentifier: string; + enabled?: boolean; +} + +/** + * Hook to fetch a Nostr repository announcement event (kind 30617) by identifier. + * The query is cached for 30 seconds and can be prefetched for better UX. + */ +export function useNostrRepo({ repoIdentifier, enabled = true }: UseNostrRepoOptions) { + const { user } = useCurrentUser(); + const { nostr } = useNostr(); + + return useQuery({ + queryKey: ['nostr-repo', user?.pubkey, repoIdentifier], + queryFn: async () => { + if (!user) return null; + + const events = await nostr.query( + [{ kinds: [30617], authors: [user.pubkey], '#d': [repoIdentifier], limit: 1 }], + { signal: AbortSignal.timeout(5000) } + ); + + return events.length > 0 ? events[0] : null; + }, + enabled: enabled && !!user && !!repoIdentifier, + staleTime: 30000, // Cache for 30 seconds + }); +}