import { useState } from 'react'; import { Zap, GitBranch, Copy, Check, RefreshCw } from 'lucide-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 { ExternalFavicon } from '@/components/ExternalFavicon'; import { useGitStatus } from '@/hooks/useGitStatus'; import { useGitSettings } from '@/hooks/useGitSettings'; import { useGit } from '@/hooks/useGit'; import { useFSPaths } from '@/hooks/useFSPaths'; import type { StepProps } from '../types'; import { cn } from '@/lib/utils'; export function SyncStep({ projectId }: StepProps) { const [isSyncing, setIsSyncing] = useState(false); const [error, setError] = useState(null); const [copiedUrl, setCopiedUrl] = useState(false); const [syncSuccess, setSyncSuccess] = useState(false); const { data: gitStatus } = useGitStatus(projectId); const { settings } = useGitSettings(); const { git } = useGit(); const { projectsPath } = useFSPaths(); const originRemote = gitStatus?.remotes.find(r => r.name === 'origin'); // Match origin URL to a credential const matchedCredential = originRemote ? settings.credentials.find(c => originRemote.url.startsWith(c.origin)) : null; const handleSync = async () => { if (!originRemote) return; setIsSyncing(true); setError(null); setSyncSuccess(false); try { const dir = `${projectsPath}/${projectId}`; // Pull first to get latest changes await git.pull({ dir, ref: gitStatus?.currentBranch || 'main', singleBranch: true, }); // Then push local changes await git.push({ dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', }); // Show success feedback setSyncSuccess(true); setTimeout(() => setSyncSuccess(false), 1500); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to sync'; setError(message); } finally { setIsSyncing(false); } }; const handleCopyUrl = async () => { if (!originRemote) return; try { await navigator.clipboard.writeText(originRemote.url); setCopiedUrl(true); setTimeout(() => setCopiedUrl(false), 2000); } catch (err) { console.error('Failed to copy URL:', err); } }; const getProviderInfo = () => { if (!originRemote) return null; // Check if it's a Nostr URI if (originRemote.url.startsWith('nostr://')) { return { name: 'Nostr Git', icon: , }; } // Check if we have a matched credential if (matchedCredential) { return { name: matchedCredential.name, icon: ( } /> ), }; } // Fallback to generic Git return { name: 'Git', icon: , }; }; const providerInfo = getProviderInfo(); if (!originRemote) { return null; } return (
{providerInfo?.icon}

{providerInfo?.name}

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