mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Hold errors in GitSyncProvider
This commit is contained in:
5 files changed
+98
-19
No files matched your search
@@ -3,6 +3,8 @@ import { CloudUpload } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
|
||||
import { useGitStatus } from '@/hooks/useGitStatus';
|
||||
import { useGitSyncState } from '@/hooks/useGitSyncState';
|
||||
import { useFSPaths } from '@/hooks/useFSPaths';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { GitSyncSteps } from './GitSyncSteps';
|
||||
|
||||
@@ -15,9 +17,19 @@ export function GitSyncButton({ projectId, className }: GitSyncButtonProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const { data: gitStatus, isLoading: isGitStatusLoading } = useGitStatus(projectId);
|
||||
const { getError } = useGitSyncState();
|
||||
const { projectsPath } = useFSPaths();
|
||||
|
||||
// Determine which indicator to show (only one at a time)
|
||||
const dir = `${projectsPath}/${projectId}`;
|
||||
const hasError = !!getError(dir);
|
||||
|
||||
// Determine which indicator to show (only one at a time, in priority order)
|
||||
const renderIndicator = () => {
|
||||
// Red indicator: error state - highest priority
|
||||
if (hasError) {
|
||||
return <IndicatorDot color="red" />;
|
||||
}
|
||||
|
||||
if (isGitStatusLoading || !gitStatus) return null;
|
||||
|
||||
const originRemote = gitStatus.remotes.find(r => r.name === 'origin');
|
||||
@@ -25,13 +37,13 @@ export function GitSyncButton({ projectId, className }: GitSyncButtonProps) {
|
||||
|
||||
const { remoteBranchExists, ahead, behind, totalCommits } = gitStatus;
|
||||
|
||||
// Yellow indicator: unsynced changes (ahead or behind remote) - highest priority
|
||||
// Yellow indicator: unsynced changes (ahead or behind remote) - second priority
|
||||
const hasUnsyncedChanges = hasRemote && remoteBranchExists && (ahead > 0 || behind > 0);
|
||||
if (hasUnsyncedChanges) {
|
||||
return <IndicatorDot color="yellow" />;
|
||||
}
|
||||
|
||||
// Purple indicator: no remote configured, but has commits - lower priority
|
||||
// Purple indicator: no remote configured, but has commits - lowest priority
|
||||
const needsRemote = !hasRemote && totalCommits >= 2;
|
||||
if (needsRemote) {
|
||||
return <IndicatorDot color="primary" />;
|
||||
@@ -64,13 +76,14 @@ export function GitSyncButton({ projectId, className }: GitSyncButtonProps) {
|
||||
);
|
||||
}
|
||||
|
||||
function IndicatorDot({ color }: { color: 'primary' | 'yellow' }) {
|
||||
function IndicatorDot({ color }: { color: 'primary' | 'yellow' | 'red' }) {
|
||||
return (
|
||||
<span className={cn(
|
||||
"absolute top-1 right-1 h-2 w-2 rounded-full",
|
||||
{
|
||||
'bg-primary': color === 'primary',
|
||||
'bg-yellow-500': color === 'yellow',
|
||||
'bg-red-500': color === 'red',
|
||||
},
|
||||
)} />
|
||||
);
|
||||
|
||||
@@ -33,7 +33,6 @@ import { findCredentialsForRepo } from '@/lib/gitCredentials';
|
||||
export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
const [copiedUrl, setCopiedUrl] = useState(false);
|
||||
const [syncSuccess, setSyncSuccess] = useState(false);
|
||||
const [isExpanded, setIsExpanded] = useState(false);
|
||||
@@ -46,7 +45,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
const { git } = useGit();
|
||||
const { projectsPath } = useFSPaths();
|
||||
const { config } = useAppContext();
|
||||
const { getState, startOperation, endOperation } = useGitSyncState();
|
||||
const { getState, getError, startOperation, endOperation, setError: setGlobalError } = useGitSyncState();
|
||||
|
||||
const dir = `${projectsPath}/${projectId}`;
|
||||
const ref = gitStatus?.currentBranch || 'main';
|
||||
@@ -54,10 +53,16 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
// Use the new hook for autosync state
|
||||
const { autosync, isLoading: isLoadingAutosync, setAutosync } = useGitAutosync(dir);
|
||||
|
||||
// Get unified sync state
|
||||
// Get unified sync state and error
|
||||
const syncState = getState(dir);
|
||||
const isSyncing = syncState?.isActive || false;
|
||||
const currentOperation = syncState?.operation || null;
|
||||
const error = getError(dir) || null;
|
||||
|
||||
// Helper to set error for this directory
|
||||
const setError = useCallback((err: Error | null) => {
|
||||
setGlobalError(dir, err);
|
||||
}, [dir, setGlobalError]);
|
||||
|
||||
const hasRemoteChanges = gitStatus?.remoteBranchExists && ((gitStatus?.ahead ?? 0) > 0 || (gitStatus?.behind ?? 0) > 0);
|
||||
const commitsAhead = gitStatus?.ahead ?? 0;
|
||||
@@ -82,17 +87,19 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
// Show success feedback
|
||||
setSyncSuccess(true);
|
||||
setTimeout(() => setSyncSuccess(false), 2500);
|
||||
|
||||
// Clear error on success
|
||||
endOperation(dir);
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
const message = err instanceof Error ? err : new Error(`Failed to ${operation}`);
|
||||
endOperation(dir, message);
|
||||
setError(message);
|
||||
} finally {
|
||||
endOperation(dir);
|
||||
}
|
||||
|
||||
// Invalidate git status to refresh UI
|
||||
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
|
||||
}, [projectId, queryClient, remoteUrl, dir, startOperation, endOperation]);
|
||||
}, [projectId, queryClient, remoteUrl, dir, startOperation, endOperation, setError]);
|
||||
|
||||
const handleSync = async () => {
|
||||
if (!remoteUrl) return;
|
||||
@@ -115,9 +122,9 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
setSyncSuccess(true);
|
||||
setTimeout(() => setSyncSuccess(false), 2500);
|
||||
} catch (err) {
|
||||
endOperation(dir);
|
||||
console.warn(err);
|
||||
const message = err instanceof Error ? err : new Error('Failed to sync');
|
||||
endOperation(dir, message);
|
||||
setError(message);
|
||||
}
|
||||
|
||||
@@ -199,6 +206,8 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
|
||||
|
||||
try {
|
||||
await git.deleteRemote({ dir, remote: 'origin' });
|
||||
// Clear any errors when disconnecting
|
||||
setError(null);
|
||||
queryClient.invalidateQueries({ queryKey: ['git-status', projectId] });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err : new Error('Failed to disconnect');
|
||||
|
||||
Reference in new issue
Block a user