fix(ui): prevent race conditions during backend switch

- Add mutation key to useUpdateBackend for tracking
- Detect backend switching with useIsMutating in ProxyStatusWidget
- Disable install/controls during backend switch (prevents wrong-backend install)
- Reduce useCliproxyVersions staleTime from 1h to 5min for faster refresh
- Enable refetchOnWindowFocus for versions query
This commit is contained in:
kaitranntt
2026-01-23 14:50:13 -05:00
parent a41fd2a093
commit 498175e9fb
2 changed files with 9 additions and 4 deletions
@@ -46,7 +46,7 @@ import {
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
import { useQuery } from '@tanstack/react-query';
import { useQuery, useIsMutating } from '@tanstack/react-query';
import { api, type CliproxyServerConfig } from '@/lib/api-client';
import {
useProxyStatus,
@@ -167,6 +167,9 @@ export function ProxyStatusWidget() {
staleTime: 30000, // 30 seconds
});
// Detect if backend switch is in progress (prevents race condition)
const isBackendSwitching = useIsMutating({ mutationKey: ['update-backend'] }) > 0;
// Determine if remote mode is enabled
const remoteConfig = cliproxyConfig?.remote;
const isRemoteMode = remoteConfig?.enabled && remoteConfig?.host;
@@ -176,7 +179,8 @@ export function ProxyStatusWidget() {
startProxy.isPending ||
stopProxy.isPending ||
restartProxy.isPending ||
installVersion.isPending;
installVersion.isPending ||
isBackendSwitching;
const hasUpdate = updateCheck?.hasUpdate ?? false;
const isUnstable = updateCheck?.isStable === false;
const currentVersion = updateCheck?.currentVersion;
+3 -2
View File
@@ -349,6 +349,7 @@ export function useUpdateBackend() {
const queryClient = useQueryClient();
return useMutation({
mutationKey: ['update-backend'], // Used by ProxyStatusWidget to detect backend switching
mutationFn: ({ backend, force = false }: { backend: 'original' | 'plus'; force?: boolean }) =>
api.cliproxyServer.updateBackend(backend, force),
onSuccess: () => {
@@ -378,8 +379,8 @@ export function useCliproxyVersions() {
return useQuery({
queryKey: ['cliproxy-versions'],
queryFn: () => api.cliproxy.versions(),
staleTime: 60 * 60 * 1000, // 1 hour (matches backend cache)
refetchOnWindowFocus: false,
staleTime: 5 * 60 * 1000, // 5 minutes (reduced for faster backend switch response)
refetchOnWindowFocus: true, // Refetch on focus to catch backend changes
});
}