diff --git a/src/App.tsx b/src/App.tsx index d4e4a1a2..d74f4ffe 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -21,6 +21,7 @@ import { DeploySettingsProvider } from '@/components/DeploySettingsProvider'; import { SessionManagerProvider } from '@/components/SessionManagerProvider'; import { FSProvider } from '@/components/FSProvider'; import { ConsoleErrorProvider } from '@/components/ConsoleErrorProvider'; +import { GitSyncProvider } from '@/components/GitSyncProvider'; import { LightningFSAdapter } from '@/lib/LightningFSAdapter'; import { ElectronFSAdapter } from '@/lib/ElectronFSAdapter'; import { cleanupTmpDirectory } from '@/lib/tmpCleanup'; @@ -202,17 +203,19 @@ export function App() { - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/src/components/GitSyncProvider.tsx b/src/components/GitSyncProvider.tsx new file mode 100644 index 00000000..78bf0770 --- /dev/null +++ b/src/components/GitSyncProvider.tsx @@ -0,0 +1,32 @@ +import { useState, useCallback, ReactNode } from 'react'; +import { GitSyncContext, GitOperation, GitSyncState } from '@/contexts/GitSyncContext'; + +export function GitSyncProvider({ children }: { children: ReactNode }) { + const [syncStates, setSyncStates] = useState>(new Map()); + + const startOperation = useCallback((dir: string, operation: GitOperation) => { + setSyncStates(prev => { + const next = new Map(prev); + next.set(dir, { dir, operation, isActive: true }); + return next; + }); + }, []); + + const endOperation = useCallback((dir: string) => { + setSyncStates(prev => { + const next = new Map(prev); + next.delete(dir); + return next; + }); + }, []); + + const getState = useCallback((dir: string) => { + return syncStates.get(dir); + }, [syncStates]); + + return ( + + {children} + + ); +} diff --git a/src/components/git-sync/steps/SyncStep.tsx b/src/components/git-sync/steps/SyncStep.tsx index 3a64cf13..5855faf2 100644 --- a/src/components/git-sync/steps/SyncStep.tsx +++ b/src/components/git-sync/steps/SyncStep.tsx @@ -22,20 +22,16 @@ import { useGitSettings } from '@/hooks/useGitSettings'; import { useGit } from '@/hooks/useGit'; import { useFSPaths } from '@/hooks/useFSPaths'; import { useAppContext } from '@/hooks/useAppContext'; +import { useGitSyncState } from '@/hooks/useGitSyncState'; import type { SyncStepProps } from '../types'; import { cn } from '@/lib/utils'; import { NostrURI } from '@/lib/NostrURI'; import { ngitWebUrl } from '@/lib/ngitWebUrl'; -import { useQueryClient } from '@tanstack/react-query'; import { findCredentialsForRepo } from '@/lib/gitCredentials'; -type GitOperation = 'sync' | 'pull' | 'push' | 'force-pull' | 'force-push'; - export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { const queryClient = useQueryClient(); - const [isSyncing, setIsSyncing] = useState(false); - const [currentOperation, setCurrentOperation] = useState(null); const [error, setError] = useState(null); const [copiedUrl, setCopiedUrl] = useState(false); const [syncSuccess, setSyncSuccess] = useState(false); @@ -51,10 +47,16 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { const { git } = useGit(); const { projectsPath } = useFSPaths(); const { config } = useAppContext(); + const { getState, startOperation, endOperation } = useGitSyncState(); const dir = `${projectsPath}/${projectId}`; const ref = gitStatus?.currentBranch || 'main'; + // Get unified sync state + const syncState = getState(dir); + const isSyncing = syncState?.isActive || false; + const currentOperation = syncState?.operation || null; + const hasRemoteChanges = gitStatus?.remoteBranchExists && ((gitStatus?.ahead ?? 0) > 0 || (gitStatus?.behind ?? 0) > 0); const commitsAhead = gitStatus?.ahead ?? 0; const commitsBehind = gitStatus?.behind ?? 0; @@ -100,13 +102,14 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { // Generic git operation handler const executeGitOperation = useCallback(async ( - operation: GitOperation, + operation: 'pull' | 'push' | 'force-pull' | 'force-push', action: () => Promise ) => { if (!remoteUrl) return; - setIsSyncing(true); - setCurrentOperation(operation); + const gitOp = operation === 'force-pull' ? 'pull' : operation === 'force-push' ? 'push' : operation; + + startOperation(dir, gitOp); setError(null); setSyncSuccess(false); @@ -121,23 +124,42 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { const message = err instanceof Error ? err : new Error(`Failed to ${operation}`); setError(message); } finally { - setIsSyncing(false); - setCurrentOperation(null); + endOperation(dir); } // Invalidate git status to refresh UI queryClient.invalidateQueries({ queryKey: ['git-status', projectId] }); - }, [projectId, queryClient, remoteUrl]); + }, [projectId, queryClient, remoteUrl, dir, startOperation, endOperation]); const handleSync = async () => { - await executeGitOperation('sync', async () => { + if (!remoteUrl) return; + + setError(null); + setSyncSuccess(false); + + try { // Pull first to get latest changes - setCurrentOperation('pull'); + startOperation(dir, 'pull'); await git.pull({ dir, ref, singleBranch: true }); + endOperation(dir); + // Then push local changes - setCurrentOperation('push'); + startOperation(dir, 'push'); await git.push({ dir, remote: 'origin', ref }); - }); + endOperation(dir); + + // Show success feedback + setSyncSuccess(true); + setTimeout(() => setSyncSuccess(false), 2500); + } catch (err) { + endOperation(dir); + console.warn(err); + const message = err instanceof Error ? err : new Error('Failed to sync'); + setError(message); + } + + // Invalidate git status to refresh UI + queryClient.invalidateQueries({ queryKey: ['git-status', projectId] }); }; const handlePull = async () => { @@ -275,18 +297,14 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { if (!isSyncing) return 'Sync'; switch (currentOperation) { + case 'fetch': + return 'Fetching'; case 'pull': - return 'Pull'; + return 'Pulling'; case 'push': - return 'Push'; - case 'force-pull': - return 'Force Pull'; - case 'force-push': - return 'Force Push'; - case 'sync': - return 'Sync'; + return 'Pushing'; default: - return 'Sync'; + return 'Syncing'; } }; @@ -432,7 +450,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { disabled={isSyncing} className="w-full" > - {currentOperation === 'pull' || currentOperation === 'force-pull' ? ( + {currentOperation === 'pull' ? ( ) : ( @@ -447,7 +465,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) { disabled={isSyncing} className="w-full" > - {currentOperation === 'push' || currentOperation === 'force-push' ? ( + {currentOperation === 'push' ? ( ) : ( diff --git a/src/contexts/GitSyncContext.ts b/src/contexts/GitSyncContext.ts new file mode 100644 index 00000000..4e4affaa --- /dev/null +++ b/src/contexts/GitSyncContext.ts @@ -0,0 +1,18 @@ +import { createContext } from 'react'; + +export type GitOperation = 'fetch' | 'pull' | 'push'; + +export interface GitSyncState { + dir: string; + operation: GitOperation; + isActive: boolean; +} + +export interface GitSyncContextValue { + syncStates: Map; + startOperation: (dir: string, operation: GitOperation) => void; + endOperation: (dir: string) => void; + getState: (dir: string) => GitSyncState | undefined; +} + +export const GitSyncContext = createContext(null); diff --git a/src/hooks/useGitSync.ts b/src/hooks/useGitSync.ts index 43a096b7..39a36e35 100644 --- a/src/hooks/useGitSync.ts +++ b/src/hooks/useGitSync.ts @@ -1,5 +1,6 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useGit } from './useGit'; +import { useGitSyncState } from './useGitSyncState'; interface GitSyncResult { defaultBranch: string | null; @@ -12,6 +13,7 @@ interface GitSyncResult { export function useGitSync(dir: string | undefined, remote = 'origin') { const { git } = useGit(); const queryClient = useQueryClient(); + const { startOperation, endOperation } = useGitSyncState(); const query = useQuery({ queryKey: ['git-sync', dir], @@ -35,10 +37,12 @@ export function useGitSync(dir: string | undefined, remote = 'origin') { }); // Always fetch to update remote tracking + startOperation(dir, 'fetch'); const fetchResult = await git.fetch({ dir, remote, }); + endOperation(dir); let didPull = false; let didPush = false; @@ -48,17 +52,20 @@ export function useGitSync(dir: string | undefined, remote = 'origin') { try { // Get current branch const currentBranch = await git.currentBranch({ dir }); - + if (currentBranch) { // Pull changes from remote try { - await git.pull({ - dir, + startOperation(dir, 'pull'); + await git.pull({ + dir, ref: currentBranch, singleBranch: true, }); didPull = true; + endOperation(dir); } catch (pullError) { + endOperation(dir); // Pull might fail if there are conflicts or diverged branches // Don't throw - just log and continue console.warn('Auto-pull failed:', pullError); @@ -66,13 +73,16 @@ export function useGitSync(dir: string | undefined, remote = 'origin') { // Push local changes to remote try { - await git.push({ - dir, - remote, + startOperation(dir, 'push'); + await git.push({ + dir, + remote, ref: currentBranch, }); didPush = true; + endOperation(dir); } catch (pushError) { + endOperation(dir); // Push might fail if there are conflicts or no changes // Don't throw - just log and continue console.warn('Auto-push failed:', pushError); @@ -97,6 +107,7 @@ export function useGitSync(dir: string | undefined, remote = 'origin') { didPush, }; } catch (error) { + endOperation(dir); // Fetch/sync failed, but don't throw - just log and return null console.warn('Git sync failed:', error); return { diff --git a/src/hooks/useGitSyncState.ts b/src/hooks/useGitSyncState.ts new file mode 100644 index 00000000..b98695f1 --- /dev/null +++ b/src/hooks/useGitSyncState.ts @@ -0,0 +1,10 @@ +import { useContext } from 'react'; +import { GitSyncContext } from '@/contexts/GitSyncContext'; + +export function useGitSyncState() { + const context = useContext(GitSyncContext); + if (!context) { + throw new Error('useGitSyncState must be used within GitSyncProvider'); + } + return context; +} diff --git a/src/hooks/useGitSyncStatus.ts b/src/hooks/useGitSyncStatus.ts new file mode 100644 index 00000000..45087fe1 --- /dev/null +++ b/src/hooks/useGitSyncStatus.ts @@ -0,0 +1,25 @@ +import { useGitSyncState } from './useGitSyncState'; +import { useFSPaths } from './useFSPaths'; + +/** + * Hook to get the current git sync status for a project + */ +export function useGitSyncStatus(projectId: string | undefined) { + const { getState } = useGitSyncState(); + const { projectsPath } = useFSPaths(); + + if (!projectId) { + return { + isActive: false, + operation: null, + }; + } + + const dir = `${projectsPath}/${projectId}`; + const state = getState(dir); + + return { + isActive: state?.isActive || false, + operation: state?.operation || null, + }; +}