Load autosync state at the top level to prevent jarring toggle shifts

This commit is contained in:
Alex Gleason committed 2025-12-29 16:25:11 -06:00
1 parent 1e13ed8f36
commit 71f3a08325
3 files changed
+67 -43

No files matched your search

+6 -43
View File
@@ -1,4 +1,4 @@
import { useState, useCallback, useEffect } from 'react';
import { useState, useCallback } from 'react';
import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp, LoaderCircle, History } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
@@ -23,6 +23,7 @@ import { useGit } from '@/hooks/useGit';
import { useFSPaths } from '@/hooks/useFSPaths';
import { useAppContext } from '@/hooks/useAppContext';
import { useGitSyncState } from '@/hooks/useGitSyncState';
import { useGitAutosync } from '@/hooks/useGitAutosync';
import type { SyncStepProps } from '../types';
import { cn } from '@/lib/utils';
import { NostrURI } from '@/lib/NostrURI';
@@ -39,8 +40,6 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
const [showForcePullDialog, setShowForcePullDialog] = useState(false);
const [showForcePushDialog, setShowForcePushDialog] = useState(false);
const [gitHistoryOpen, setGitHistoryOpen] = useState(false);
const [autosync, setAutosync] = useState<boolean>(false);
const [isLoadingAutosync, setIsLoadingAutosync] = useState(true);
const { data: gitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
@@ -52,6 +51,9 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
const dir = `${projectsPath}/${projectId}`;
const ref = gitStatus?.currentBranch || 'main';
// Use the new hook for autosync state
const { autosync, isLoading: isLoadingAutosync, setAutosync } = useGitAutosync(dir);
// Get unified sync state
const syncState = getState(dir);
const isSyncing = syncState?.isActive || false;
@@ -61,45 +63,6 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
const commitsAhead = gitStatus?.ahead ?? 0;
const commitsBehind = gitStatus?.behind ?? 0;
// Load autosync config on mount
useEffect(() => {
const loadAutosync = async () => {
try {
const value = await git.getConfig({
dir,
path: 'shakespeare.autosync',
});
setAutosync(value === 'true');
} catch {
// Config not set, default to false
setAutosync(false);
} finally {
setIsLoadingAutosync(false);
}
};
loadAutosync();
}, [git, dir]);
// Handler to toggle autosync
const handleAutosyncToggle = async (checked: boolean) => {
try {
await git.setConfig({
dir,
path: 'shakespeare.autosync',
value: String(checked),
});
setAutosync(checked);
// If enabling autosync, trigger an immediate sync
if (checked) {
queryClient.invalidateQueries({ queryKey: ['git-sync', dir] });
}
} catch (error) {
console.error('Failed to set autosync config:', error);
}
};
// Generic git operation handler
const executeGitOperation = useCallback(async (
operation: 'pull' | 'push' | 'force-pull' | 'force-push',
@@ -371,7 +334,7 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
<Switch
id="autosync"
checked={autosync}
onCheckedChange={handleAutosyncToggle}
onCheckedChange={setAutosync}
disabled={isLoadingAutosync}
className="scale-75"
/>