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"
/>
+57
View File
@@ -0,0 +1,57 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useGit } from './useGit';
export function useGitAutosync(dir: string | undefined) {
const { git } = useGit();
const queryClient = useQueryClient();
const query = useQuery({
queryKey: ['git-autosync', dir],
queryFn: async (): Promise<boolean> => {
if (!dir) return false;
try {
const value = await git.getConfig({
dir,
path: 'shakespeare.autosync',
});
return value === 'true';
} catch {
// Config not set, default to false
return false;
}
},
enabled: !!dir,
staleTime: Infinity, // This value rarely changes, keep it cached
});
const mutation = useMutation({
mutationFn: async (enabled: boolean) => {
if (!dir) throw new Error('No directory specified');
await git.setConfig({
dir,
path: 'shakespeare.autosync',
value: String(enabled),
});
return enabled;
},
onSuccess: (enabled) => {
// Update the cached value immediately
queryClient.setQueryData(['git-autosync', dir], enabled);
// If enabling autosync, trigger an immediate sync
if (enabled) {
queryClient.invalidateQueries({ queryKey: ['git-sync', dir] });
}
},
});
return {
autosync: query.data ?? false,
isLoading: query.isLoading,
setAutosync: mutation.mutateAsync,
isUpdating: mutation.isPending,
};
}
+4
View File
@@ -27,6 +27,7 @@ import { useAutoBuild } from '@/hooks/useAutoBuild';
import { useToast } from '@/hooks/useToast';
import { useFSPaths } from '@/hooks/useFSPaths';
import { useGitSync } from '@/hooks/useGitSync';
import { useGitAutosync } from '@/hooks/useGitAutosync';
import { useQueryClient } from '@tanstack/react-query';
import { cn } from '@/lib/utils';
import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus';
@@ -65,6 +66,9 @@ export function ProjectView() {
// Automatically sync with remote to keep git status updated
useGitSync(projectId ? `${projectsPath}/${projectId}` : undefined);
// Load autosync state at the top level to prevent jarring toggle shifts
useGitAutosync(projectId ? `${projectsPath}/${projectId}` : undefined);
// Enable auto-build for previewable projects
useAutoBuild({
projectId: projectId!,