11import {
2+ type InfiniteData ,
23 keepPreviousData ,
34 type QueryClient ,
45 useInfiniteQuery ,
@@ -276,6 +277,8 @@ export function useExecutionSnapshot(executionId: string | undefined) {
276277 } )
277278}
278279
280+ type LogsPage = { logs : WorkflowLog [ ] ; hasMore : boolean ; nextPage : number | undefined }
281+
279282export function useCancelExecution ( ) {
280283 const queryClient = useQueryClient ( )
281284 return useMutation ( {
@@ -294,6 +297,33 @@ export function useCancelExecution() {
294297 if ( ! data . success ) throw new Error ( 'Failed to cancel execution' )
295298 return data
296299 } ,
300+ onMutate : async ( { executionId } ) => {
301+ await queryClient . cancelQueries ( { queryKey : logKeys . lists ( ) } )
302+
303+ const previousQueries = queryClient . getQueriesData < InfiniteData < LogsPage > > ( {
304+ queryKey : logKeys . lists ( ) ,
305+ } )
306+
307+ queryClient . setQueriesData < InfiniteData < LogsPage > > ( { queryKey : logKeys . lists ( ) } , ( old ) => {
308+ if ( ! old ) return old
309+ return {
310+ ...old ,
311+ pages : old . pages . map ( ( page ) => ( {
312+ ...page ,
313+ logs : page . logs . map ( ( log ) =>
314+ log . executionId === executionId ? { ...log , status : 'cancelling' } : log
315+ ) ,
316+ } ) ) ,
317+ }
318+ } )
319+
320+ return { previousQueries }
321+ } ,
322+ onError : ( _err , _variables , context ) => {
323+ for ( const [ queryKey , data ] of context ?. previousQueries ?? [ ] ) {
324+ queryClient . setQueryData ( queryKey , data )
325+ }
326+ } ,
297327 onSettled : ( ) => {
298328 queryClient . invalidateQueries ( { queryKey : logKeys . lists ( ) } )
299329 queryClient . invalidateQueries ( { queryKey : logKeys . details ( ) } )
0 commit comments