import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useGit } from '@/hooks/useGit'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useFSPaths } from '@/hooks/useFSPaths'; import { useNostr } from '@nostrify/react'; import { useNostrPublish } from '@/hooks/useNostrPublish'; import { useFS } from '@/hooks/useFS'; import { Link } from 'react-router-dom'; import { nip19 } from 'nostr-tools'; import { createRepositoryAnnouncementEvent } from '@/lib/announceRepository'; import { DotAI } from '@/lib/DotAI'; import type { ConfigureRepoStepProps } from '../types'; import type { NostrEvent } from '@nostrify/nostrify'; export function ConfigureRepoStep({ projectId, selectedProvider, onBack, onSuccess, onClose, }: ConfigureRepoStepProps) { const [repositoryUrl, setRepositoryUrl] = useState(''); const [isPushing, setIsPushing] = useState(false); const [error, setError] = useState(null); const { user } = useCurrentUser(); const { git } = useGit(); const { projectsPath } = useFSPaths(); const { data: gitStatus, refetch: refetchGitStatus } = useGitStatus(projectId); const { nostr } = useNostr(); const { mutateAsync: publishEvent } = useNostrPublish(); const { fs } = useFS(); const handlePushInitial = async () => { setIsPushing(true); setError(null); try { const dir = `${projectsPath}/${projectId}`; if (selectedProvider.id === 'nostr') { if (!user) { throw new Error('User is not logged in'); } // Check if a NIP-34 repo announcement already exists for this project const existingRepos = await nostr.query( [{ kinds: [30617], authors: [user.pubkey], '#d': [projectId], limit: 1 }], { signal: AbortSignal.timeout(5000) } ); let repoEvent: NostrEvent; if (existingRepos.length > 0) { // Use existing repo announcement repoEvent = existingRepos[0]; } else { // Create and publish new NIP-34 repository announcement // Get the template name const dotAI = new DotAI(fs, `${projectsPath}/${projectId}`); const template = await dotAI.readTemplate(); const templateName = template?.name; // Build t-tags array const tTags = ['shakespeare']; if (templateName) { tTags.push(templateName); } // Create the repository announcement event const eventTemplate = createRepositoryAnnouncementEvent({ repoId: projectId, name: projectId, cloneUrls: [], // Will be populated by git servers tTags, }); // Publish the event repoEvent = await publishEvent(eventTemplate); // Wait 5 seconds for Nostr git servers to update await new Promise(resolve => setTimeout(resolve, 5000)); } // Construct Nostr URI const nostrUri = `nostr://${nip19.npubEncode(repoEvent.pubkey)}/${projectId}`; // Configure the Nostr URI as the origin await git.addRemote({ dir, remote: 'origin', url: nostrUri, force: true, }); // Push to Nostr (the rest of the Nostr logic is handled internally by git.push) await git.push({ dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', }); } else { // Validate repository URL if (!repositoryUrl.trim()) { throw new Error('Repository URL is required'); } // Add remote await git.addRemote({ dir, remote: 'origin', url: repositoryUrl, force: true, // Override if exists }); // Push to remote await git.push({ dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', }); } // Refresh git status before showing success await refetchGitStatus(); // Show success state onSuccess(); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to push'; setError(message); } finally { setIsPushing(false); } }; // "Other" provider - show link to git settings if (selectedProvider.id === 'other') { return (
{selectedProvider.icon}

{selectedProvider.name}

Configure additional git providers in{' '} Git Settings .

); } // Nostr provider - check if user is logged in if (selectedProvider.id === 'nostr') { if (!user) { return (
{selectedProvider.icon}

{selectedProvider.name}

You need to be logged in with Nostr to use this feature. Please visit{' '} Nostr Settings {' '} to log in.

); } return (
{selectedProvider.icon}

{selectedProvider.name}

Push your code to Nostr with one click. Your repository will be stored on public Nostr relays.

{error && ( {error} )}
); } // Regular git provider - show repository URL input return (
{selectedProvider.icon}

{selectedProvider.name}

setRepositoryUrl(e.target.value)} />

First create the repository on {selectedProvider.name}, then paste the URL here.

{error && ( {error} )}
); }