mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-03 04:17:54 +00:00
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:
@@ -46,7 +46,7 @@ import {
|
|||||||
AlertDialogTitle,
|
AlertDialogTitle,
|
||||||
} from '@/components/ui/alert-dialog';
|
} from '@/components/ui/alert-dialog';
|
||||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
|
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 { api, type CliproxyServerConfig } from '@/lib/api-client';
|
||||||
import {
|
import {
|
||||||
useProxyStatus,
|
useProxyStatus,
|
||||||
@@ -167,6 +167,9 @@ export function ProxyStatusWidget() {
|
|||||||
staleTime: 30000, // 30 seconds
|
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
|
// Determine if remote mode is enabled
|
||||||
const remoteConfig = cliproxyConfig?.remote;
|
const remoteConfig = cliproxyConfig?.remote;
|
||||||
const isRemoteMode = remoteConfig?.enabled && remoteConfig?.host;
|
const isRemoteMode = remoteConfig?.enabled && remoteConfig?.host;
|
||||||
@@ -176,7 +179,8 @@ export function ProxyStatusWidget() {
|
|||||||
startProxy.isPending ||
|
startProxy.isPending ||
|
||||||
stopProxy.isPending ||
|
stopProxy.isPending ||
|
||||||
restartProxy.isPending ||
|
restartProxy.isPending ||
|
||||||
installVersion.isPending;
|
installVersion.isPending ||
|
||||||
|
isBackendSwitching;
|
||||||
const hasUpdate = updateCheck?.hasUpdate ?? false;
|
const hasUpdate = updateCheck?.hasUpdate ?? false;
|
||||||
const isUnstable = updateCheck?.isStable === false;
|
const isUnstable = updateCheck?.isStable === false;
|
||||||
const currentVersion = updateCheck?.currentVersion;
|
const currentVersion = updateCheck?.currentVersion;
|
||||||
|
|||||||
@@ -349,6 +349,7 @@ export function useUpdateBackend() {
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
|
mutationKey: ['update-backend'], // Used by ProxyStatusWidget to detect backend switching
|
||||||
mutationFn: ({ backend, force = false }: { backend: 'original' | 'plus'; force?: boolean }) =>
|
mutationFn: ({ backend, force = false }: { backend: 'original' | 'plus'; force?: boolean }) =>
|
||||||
api.cliproxyServer.updateBackend(backend, force),
|
api.cliproxyServer.updateBackend(backend, force),
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
@@ -378,8 +379,8 @@ export function useCliproxyVersions() {
|
|||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ['cliproxy-versions'],
|
queryKey: ['cliproxy-versions'],
|
||||||
queryFn: () => api.cliproxy.versions(),
|
queryFn: () => api.cliproxy.versions(),
|
||||||
staleTime: 60 * 60 * 1000, // 1 hour (matches backend cache)
|
staleTime: 5 * 60 * 1000, // 5 minutes (reduced for faster backend switch response)
|
||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: true, // Refetch on focus to catch backend changes
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user