mirror of
https://github.com/BloopAI/vibe-kanban.git
synced 2026-11-20 22:40:25 +00:00
Re-apply the fix from PR #2196 which was accidentally reverted. This commit contains only the stop-button-related changes, excluding files that conflicted with PR #2341 (dead code cleanup). Changes: - Add CancellationToken support to executor approval flow - Fix frontend guard condition in stopExecution - Add entry reloading when process status changes - Implement graceful shutdown with cancellation token Original PR: #2196 Co-authored-by: Solomon <abcpro11051@disroot.org>
90 lines
2.8 KiB
TypeScript
90 lines
2.8 KiB
TypeScript
import { useMemo, useCallback } from 'react';
|
|
import { useQueries } from '@tanstack/react-query';
|
|
import { attemptsApi, executionProcessesApi } from '@/lib/api';
|
|
import { useTaskStopping } from '@/stores/useTaskDetailsUiStore';
|
|
import { useExecutionProcessesContext } from '@/contexts/ExecutionProcessesContext';
|
|
import type { AttemptData } from '@/lib/types';
|
|
import type { ExecutionProcess } from 'shared/types';
|
|
|
|
export function useAttemptExecution(attemptId?: string, taskId?: string) {
|
|
const { isStopping, setIsStopping } = useTaskStopping(taskId || '');
|
|
|
|
const {
|
|
executionProcessesVisible: executionProcesses,
|
|
isAttemptRunningVisible: isAttemptRunning,
|
|
isLoading: streamLoading,
|
|
} = useExecutionProcessesContext();
|
|
|
|
// Get setup script processes that need detailed info
|
|
const setupProcesses = useMemo(() => {
|
|
if (!executionProcesses.length) return [] as ExecutionProcess[];
|
|
return executionProcesses.filter((p) => p.run_reason === 'setupscript');
|
|
}, [executionProcesses]);
|
|
|
|
// Fetch details for setup processes
|
|
const processDetailQueries = useQueries({
|
|
queries: setupProcesses.map((process) => ({
|
|
queryKey: ['processDetails', process.id],
|
|
queryFn: () => executionProcessesApi.getDetails(process.id),
|
|
enabled: !!process.id,
|
|
})),
|
|
});
|
|
|
|
// Build attempt data combining processes and details
|
|
const attemptData: AttemptData = useMemo(() => {
|
|
if (!executionProcesses.length) {
|
|
return { processes: [], runningProcessDetails: {} };
|
|
}
|
|
|
|
// Build runningProcessDetails from the detail queries
|
|
const runningProcessDetails: Record<string, ExecutionProcess> = {};
|
|
|
|
setupProcesses.forEach((process, index) => {
|
|
const detailQuery = processDetailQueries[index];
|
|
if (detailQuery?.data) {
|
|
runningProcessDetails[process.id] = detailQuery.data;
|
|
}
|
|
});
|
|
|
|
return {
|
|
processes: executionProcesses,
|
|
runningProcessDetails,
|
|
};
|
|
}, [executionProcesses, setupProcesses, processDetailQueries]);
|
|
|
|
const stopExecution = useCallback(async () => {
|
|
if (!attemptId || isStopping) return;
|
|
|
|
try {
|
|
setIsStopping(true);
|
|
await attemptsApi.stop(attemptId);
|
|
} catch (error) {
|
|
console.error('Failed to stop executions:', error);
|
|
throw error;
|
|
} finally {
|
|
setIsStopping(false);
|
|
}
|
|
}, [attemptId, isStopping, setIsStopping]);
|
|
|
|
const isLoading =
|
|
streamLoading || processDetailQueries.some((q) => q.isLoading);
|
|
const isFetching =
|
|
streamLoading || processDetailQueries.some((q) => q.isFetching);
|
|
|
|
return {
|
|
// Data
|
|
processes: executionProcesses,
|
|
attemptData,
|
|
runningProcessDetails: attemptData.runningProcessDetails,
|
|
|
|
// Status
|
|
isAttemptRunning,
|
|
isLoading,
|
|
isFetching,
|
|
|
|
// Actions
|
|
stopExecution,
|
|
isStopping,
|
|
};
|
|
}
|