import { useState } from 'react'; import { Zap, GitBranch, Copy, Check } 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'; export function SyncStep({ projectId }: StepProps) { const [isPushing, setIsPushing] = useState(false); const [error, setError] = useState(null); const [copiedUrl, setCopiedUrl] = 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 handlePull = async () => { if (!originRemote) return; setIsPushing(true); setError(null); try { const dir = `${projectsPath}/${projectId}`; await git.pull({ dir, ref: gitStatus?.currentBranch || 'main', singleBranch: true, onAuth: () => { if (matchedCredential) { return { username: matchedCredential.username, password: matchedCredential.password, }; } return undefined; }, }); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to pull'; setError(message); } finally { setIsPushing(false); } }; const handlePush = async () => { if (!originRemote) return; setIsPushing(true); setError(null); try { const dir = `${projectsPath}/${projectId}`; await git.push({ dir, remote: 'origin', ref: gitStatus?.currentBranch || 'main', onAuth: () => { if (matchedCredential) { return { username: matchedCredential.username, password: matchedCredential.password, }; } return undefined; }, }); } catch (err) { const message = err instanceof Error ? err.message : 'Failed to push'; setError(message); } finally { setIsPushing(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} )}
); }