Skip to content

Commit cd6594a

Browse files
authored
fix: dismiss toast in case of shutting down (#636)
* fix: dismiss toast in case of shutting down * fix : wrap callback into useEffect * update test
1 parent 46face5 commit cd6594a

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

renderer/src/common/hooks/use-toast-mutation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ export function useToastMutation<
1717
successMsg,
1818
errorMsg,
1919
loadingMsg,
20+
toastId,
2021
...options
2122
}: UseMutationOptions<TData, TError, TVariables, TContext> & {
2223
successMsg?: ((variables: TVariables) => string) | string
2324
loadingMsg?: string
2425
errorMsg?: string
26+
toastId?: string
2527
}) {
2628
const {
2729
mutateAsync: originalMutateAsync,
@@ -60,9 +62,10 @@ export function useToastMutation<
6062

6163
return 'An error occurred'
6264
},
65+
...(toastId ? { id: toastId } : {}),
6366
})
6467
},
65-
[errorMsg, loadingMsg, originalMutateAsync, successMsg]
68+
[errorMsg, loadingMsg, originalMutateAsync, successMsg, toastId]
6669
)
6770

6871
return { mutateAsync, ...rest }

renderer/src/features/mcp-servers/hooks/__tests__/use-mutation-restart-server.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,27 @@ import type { WorkloadsWorkload } from '@/common/api/generated'
1010
import { server } from '@/common/mocks/node'
1111
import { http, HttpResponse } from 'msw'
1212
import { mswEndpoint } from '@/common/mocks/msw-endpoint'
13+
import { toast } from 'sonner'
14+
15+
vi.mock('sonner', () => ({
16+
toast: {
17+
dismiss: vi.fn(),
18+
promise: vi.fn((promise) => promise.catch(() => {})),
19+
success: vi.fn(),
20+
error: vi.fn(),
21+
loading: vi.fn(),
22+
},
23+
}))
1324

1425
// Mock electron API
26+
const mockOnServerShutdown = vi.fn()
1527
Object.defineProperty(window, 'electronAPI', {
1628
value: {
1729
shutdownStore: {
1830
getLastShutdownServers: vi.fn(),
1931
clearShutdownHistory: vi.fn(),
2032
},
33+
onServerShutdown: mockOnServerShutdown,
2134
},
2235
writable: true,
2336
})
@@ -44,6 +57,7 @@ const createQueryClientWrapper = () => {
4457

4558
beforeEach(() => {
4659
vi.clearAllMocks()
60+
mockOnServerShutdown.mockClear()
4761
window.electronAPI.shutdownStore.getLastShutdownServers = vi
4862
.fn()
4963
.mockResolvedValue([])
@@ -127,6 +141,24 @@ describe('useMutationRestartServerAtStartup', () => {
127141
expect(result.current.isError).toBe(true)
128142
})
129143
})
144+
145+
it('dismisses toast on server shutdown', async () => {
146+
const { Wrapper } = createQueryClientWrapper()
147+
148+
renderHook(() => useMutationRestartServerAtStartup(), {
149+
wrapper: Wrapper,
150+
})
151+
152+
expect(mockOnServerShutdown).toHaveBeenCalledTimes(1)
153+
const shutdownCallback = mockOnServerShutdown.mock.calls[0]?.[0]
154+
expect(shutdownCallback).toBeDefined()
155+
156+
shutdownCallback?.()
157+
158+
expect(vi.mocked(toast.dismiss)).toHaveBeenCalledWith(
159+
'restart-servers-startup'
160+
)
161+
})
130162
})
131163

132164
describe('useMutationRestartServer', () => {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
import { useToastMutation } from '@/common/hooks/use-toast-mutation'
1212
import { pollBatchServerStatus } from '@/common/lib/polling'
1313
import { useQueryClient } from '@tanstack/react-query'
14+
import { useEffect } from 'react'
15+
import { toast } from 'sonner'
16+
17+
const TOAST_ID = 'restart-servers-startup'
1418

1519
const getMutationData = (name: string) => ({
1620
...postApiV1BetaWorkloadsByNameRestartMutation(),
@@ -23,10 +27,19 @@ export function useMutationRestartServerAtStartup() {
2327
const queryClient = useQueryClient()
2428
const queryKey = getApiV1BetaWorkloadsQueryKey({ query: { all: true } })
2529

30+
useEffect(() => {
31+
const cleanup = window.electronAPI.onServerShutdown(() => {
32+
toast.dismiss(TOAST_ID)
33+
})
34+
35+
return cleanup
36+
}, [])
37+
2638
return useToastMutation({
2739
successMsg: 'Servers restarted successfully',
2840
errorMsg: 'Failed to restart servers',
2941
loadingMsg: 'Restarting servers...',
42+
toastId: TOAST_ID,
3043
...postApiV1BetaWorkloadsRestartMutation(),
3144
onMutate: async (variables) => {
3245
await queryClient.cancelQueries({ queryKey })

renderer/src/routes/__tests__/index.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Object.defineProperty(window, 'electronAPI', {
1616
getLastShutdownServers: vi.fn().mockResolvedValue([]),
1717
clearShutdownHistory: vi.fn().mockResolvedValue(undefined),
1818
},
19+
onServerShutdown: vi.fn().mockReturnValue(() => {}),
1920
},
2021
writable: true,
2122
})

0 commit comments

Comments
 (0)