mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add unified git sync state so state is shared across components
This commit is contained in:
7 files changed
+160
-43
No files matched your search
+14
-11
@@ -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() {
|
||||
<AISettingsProvider>
|
||||
<GitSettingsProvider>
|
||||
<DeploySettingsProvider>
|
||||
<SessionManagerProvider>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<DynamicFavicon />
|
||||
<OfflineIndicator />
|
||||
<PWAUpdatePrompt />
|
||||
<Suspense>
|
||||
<AppRouter />
|
||||
</Suspense>
|
||||
</TooltipProvider>
|
||||
</SessionManagerProvider>
|
||||
<GitSyncProvider>
|
||||
<SessionManagerProvider>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<DynamicFavicon />
|
||||
<OfflineIndicator />
|
||||
<PWAUpdatePrompt />
|
||||
<Suspense>
|
||||
<AppRouter />
|
||||
</Suspense>
|
||||
</TooltipProvider>
|
||||
</SessionManagerProvider>
|
||||
</GitSyncProvider>
|
||||
</DeploySettingsProvider>
|
||||
</GitSettingsProvider>
|
||||
</AISettingsProvider>
|
||||
|
||||
@@ -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<Map<string, GitSyncState>>(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 (
|
||||
<GitSyncContext.Provider value={{ syncStates, startOperation, endOperation, getState }}>
|
||||
{children}
|
||||
</GitSyncContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -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<GitOperation | null>(null);
|
||||
const [error, setError] = useState<Error | null>(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<void>
|
||||
) => {
|
||||
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' ? (
|
||||
<LoaderCircle className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<ArrowDown className="h-4 w-4" />
|
||||
@@ -447,7 +465,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
disabled={isSyncing}
|
||||
className="w-full"
|
||||
>
|
||||
{currentOperation === 'push' || currentOperation === 'force-push' ? (
|
||||
{currentOperation === 'push' ? (
|
||||
<LoaderCircle className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<ArrowUp className="h-4 w-4" />
|
||||
|
||||
@@ -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<string, GitSyncState>;
|
||||
startOperation: (dir: string, operation: GitOperation) => void;
|
||||
endOperation: (dir: string) => void;
|
||||
getState: (dir: string) => GitSyncState | undefined;
|
||||
}
|
||||
|
||||
export const GitSyncContext = createContext<GitSyncContextValue | null>(null);
|
||||
+17
-6
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user