Hold errors in GitSyncProvider

This commit is contained in:
Alex Gleason committed 2025-12-29 16:43:47 -06:00
1 parent f9febcce1e
commit ffa65212bc
5 files changed
+98 -19

No files matched your search

+47 -3
View File
@@ -7,15 +7,55 @@ export function GitSyncProvider({ children }: { children: ReactNode }) {
const startOperation = useCallback((dir: string, operation: GitOperation) => {
setSyncStates(prev => {
const next = new Map(prev);
// Clear any previous error when starting a new operation
next.set(dir, { dir, operation, isActive: true });
return next;
});
}, []);
const endOperation = useCallback((dir: string) => {
const endOperation = useCallback((dir: string, error?: Error) => {
setSyncStates(prev => {
const next = new Map(prev);
next.delete(dir);
if (error) {
// Keep the state with the error
const currentState = prev.get(dir);
if (currentState) {
next.set(dir, { ...currentState, isActive: false, error });
}
} else {
// Remove the state if no error
next.delete(dir);
}
return next;
});
}, []);
const setError = useCallback((dir: string, error: Error | null) => {
setSyncStates(prev => {
const next = new Map(prev);
const currentState = prev.get(dir);
if (error) {
// Set or update error
if (currentState) {
next.set(dir, { ...currentState, error });
} else {
// Create a new state with just the error (no active operation)
next.set(dir, { dir, operation: 'fetch', isActive: false, error });
}
} else {
// Clear error
if (currentState) {
const { error: _, ...stateWithoutError } = currentState;
if (stateWithoutError.isActive) {
next.set(dir, stateWithoutError);
} else {
// If not active and no error, remove the state entirely
next.delete(dir);
}
}
}
return next;
});
}, []);
@@ -24,8 +64,12 @@ export function GitSyncProvider({ children }: { children: ReactNode }) {
return syncStates.get(dir);
}, [syncStates]);
const getError = useCallback((dir: string) => {
return syncStates.get(dir)?.error;
}, [syncStates]);
return (
<GitSyncContext.Provider value={{ syncStates, startOperation, endOperation, getState }}>
<GitSyncContext.Provider value={{ syncStates, startOperation, endOperation, setError, getState, getError }}>
{children}
</GitSyncContext.Provider>
);