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

+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,
};
}