Skip to content

Commit ec93d33

Browse files
authored
fix: stopping status workload (#394)
1 parent 7d2353b commit ec93d33

File tree

2 files changed

+24
-86
lines changed

2 files changed

+24
-86
lines changed

renderer/src/features/mcp-servers/components/actions-mcp-server.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export function ActionsMcpServer({
3232
<div onClick={(e) => e.preventDefault()}>
3333
<Switch
3434
aria-label="Mutate server"
35-
checked={isRunning || isPending}
36-
disabled={isStarting}
35+
checked={isRunning || isStarting}
36+
disabled={isStarting || isPending}
3737
onCheckedChange={() => mutate()}
3838
/>
3939
</div>

renderer/src/features/mcp-servers/hooks/use-mutation-stop-server.ts

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import type {
44
} from '@/common/api/generated'
55
import {
66
postApiV1BetaWorkloadsByNameStopMutation,
7-
getApiV1BetaWorkloadsByNameQueryKey,
87
getApiV1BetaWorkloadsQueryKey,
8+
getApiV1BetaWorkloadsByNameOptions,
99
} from '@/common/api/generated/@tanstack/react-query.gen'
1010
import { useToastMutation } from '@/common/hooks/use-toast-mutation'
11+
import { pollBatchServerStatus } from '@/common/lib/polling'
1112
import { useQueryClient } from '@tanstack/react-query'
1213

1314
const getMutationData = (name: string) => ({
@@ -20,9 +21,6 @@ const getMutationData = (name: string) => ({
2021
export function useMutationStopServerList({ name }: { name: string }) {
2122
const queryClient = useQueryClient()
2223
const queryKey = getApiV1BetaWorkloadsQueryKey({ query: { all: true } })
23-
const serverQueryKey = getApiV1BetaWorkloadsByNameQueryKey({
24-
path: { name },
25-
})
2624
return useToastMutation({
2725
...getMutationData(name),
2826

@@ -31,18 +29,7 @@ export function useMutationStopServerList({ name }: { name: string }) {
3129
queryKey,
3230
})
3331

34-
const previousServer = queryClient.getQueryData(serverQueryKey)
35-
36-
queryClient.setQueryData(
37-
serverQueryKey,
38-
(oldData: WorkloadsWorkload | undefined) => {
39-
if (!oldData) return oldData
40-
return {
41-
...oldData,
42-
status: 'stopping',
43-
}
44-
}
45-
)
32+
const previousServersList = queryClient.getQueryData(queryKey)
4633

4734
queryClient.setQueryData(
4835
queryKey,
@@ -59,79 +46,30 @@ export function useMutationStopServerList({ name }: { name: string }) {
5946
}
6047
)
6148

62-
return { previousServer }
49+
return { previousServersList }
6350
},
64-
onSuccess: () => {
65-
queryClient.setQueryData(
66-
queryKey,
67-
(oldData: V1WorkloadListResponse | undefined) => {
68-
if (!oldData) return oldData
69-
return { ...oldData, status: 'stopped' }
70-
}
71-
)
72-
queryClient.setQueryData(
73-
serverQueryKey,
74-
(oldData: WorkloadsWorkload | undefined) => {
75-
if (!oldData) return oldData
76-
return { ...oldData, status: 'stopped' }
77-
}
51+
onSuccess: async () => {
52+
// Poll until server stopped
53+
await pollBatchServerStatus(
54+
async (names) => {
55+
const servers = await Promise.all(
56+
names.map((name) =>
57+
queryClient.fetchQuery(
58+
getApiV1BetaWorkloadsByNameOptions({ path: { name } })
59+
)
60+
)
61+
)
62+
return servers
63+
},
64+
[name],
65+
'stopped'
7866
)
67+
queryClient.invalidateQueries({ queryKey })
7968
},
8069
onError: (_error, _variables, context) => {
81-
if (context?.previousServer) {
82-
queryClient.setQueryData(queryKey, context.previousServer)
70+
if (context?.previousServersList) {
71+
queryClient.setQueryData(queryKey, context.previousServersList)
8372
}
8473
},
8574
})
8675
}
87-
88-
export function useMutationStopServer({ name }: { name: string }) {
89-
const queryClient = useQueryClient()
90-
const queryKey = getApiV1BetaWorkloadsByNameQueryKey({ path: { name } })
91-
92-
return useToastMutation({
93-
...getMutationData(name),
94-
95-
onMutate: async () => {
96-
await queryClient.cancelQueries({ queryKey })
97-
98-
const previousServerData = queryClient.getQueryData(queryKey)
99-
100-
queryClient.setQueryData(
101-
queryKey,
102-
(oldData: WorkloadsWorkload | undefined) => {
103-
if (!oldData) return oldData
104-
105-
const updatedData = {
106-
...oldData,
107-
status: 'stopping',
108-
} as WorkloadsWorkload
109-
return updatedData
110-
}
111-
)
112-
113-
return { previousServerData }
114-
},
115-
116-
onError: (_error, _variables, context) => {
117-
if (context?.previousServerData) {
118-
queryClient.setQueryData(queryKey, context.previousServerData)
119-
}
120-
},
121-
onSuccess: () => {
122-
queryClient.setQueryData(
123-
queryKey,
124-
(oldData: WorkloadsWorkload | undefined) => {
125-
if (!oldData) return oldData
126-
127-
return { ...oldData, status: 'stopped' }
128-
}
129-
)
130-
},
131-
onSettled: () => {
132-
queryClient.invalidateQueries({
133-
queryKey: getApiV1BetaWorkloadsQueryKey({ query: { all: true } }),
134-
})
135-
},
136-
})
137-
}

0 commit comments

Comments
 (0)