"use client"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useEffect } from "react"; import toast from "react-hot-toast"; import { get, post } from "../../lib/api"; import { useNotifications } from "./NotificationContext"; export default function TaskProcessingControls() { const queryClient = useQueryClient(); const { addNotification } = useNotifications(); const { data } = useQuery({ queryKey: ["tasks", "processing-status"], queryFn: () => get("/tasks/processing-status"), }); const startMutation = useMutation({ mutationFn: () => post("/tasks/start-processing"), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["tasks", "processing-status"], }); queryClient.invalidateQueries({ queryKey: ["tasks", "queue", "status"] }); toast.success("Task processing started successfully"); addNotification({ type: "success", title: "Task Processing Started", message: "The task processing queue has been started successfully.", }); }, }); const stopMutation = useMutation({ mutationFn: () => post("/tasks/stop-processing", { graceful: true }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["tasks", "processing-status"], }); queryClient.invalidateQueries({ queryKey: ["tasks", "queue", "status"] }); toast.success("Graceful stop initiated – finishing active tasks"); addNotification({ type: "info", title: "Graceful Stop Requested", message: "The queue will finish active tasks before stopping.", }); }, }); const hardStopMutation = useMutation({ mutationFn: () => post("/tasks/stop-processing", { graceful: false }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["tasks", "processing-status"], }); queryClient.invalidateQueries({ queryKey: ["tasks", "queue", "status"] }); toast.success("Task processing stopped immediately"); addNotification({ type: "warning", title: "Immediate Stop", message: "Queue scheduling and active work halted immediately.", }); }, }); // Listen for WebSocket events useEffect(() => { const handleTaskUpdate = (event: CustomEvent) => { const updateData = event.detail; if ( updateData.type === "started" || updateData.type === "stopped" || updateData.type === "progress" ) { // Invalidate and refetch the task processing status queryClient.invalidateQueries({ queryKey: ["tasks", "processing-status"], }); queryClient.invalidateQueries({ queryKey: ["tasks", "queue", "status"], }); } }; window.addEventListener("taskUpdate", handleTaskUpdate as EventListener); return () => { window.removeEventListener( "taskUpdate", handleTaskUpdate as EventListener ); }; }, [queryClient]); return (