import { useState } from 'react'; import { extractApiError } from '@/api/client'; import { useC2Callbacks, useC2CallbackHistory, useImportC2 } from '@/hooks/useC2'; import { C2CallbackPicker } from './C2CallbackPicker'; import { C2TaskStatusBadge } from './C2TaskStatusBadge'; import { useToast } from '@/hooks/useToast'; const PAGE_SIZE = 25; interface ImportC2HistoryModalProps { simulationId: number; engagementId: number; onClose: () => void; } export function ImportC2HistoryModal({ simulationId, engagementId, onClose, }: ImportC2HistoryModalProps): JSX.Element { const { push } = useToast(); const callbacksQuery = useC2Callbacks(engagementId, { enabled: true }); const importMutation = useImportC2(simulationId); const [selectedCallbackId, setSelectedCallbackId] = useState(null); const [page, setPage] = useState(1); const [checkedIds, setCheckedIds] = useState>(new Set()); const [submitError, setSubmitError] = useState(null); const historyQuery = useC2CallbackHistory(engagementId, selectedCallbackId, { page, pageSize: PAGE_SIZE, enabled: selectedCallbackId !== null, }); const historyTasks = historyQuery.data?.tasks ?? []; const total = historyQuery.data?.total ?? 0; const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE)); const callbacks = callbacksQuery.data?.callbacks ?? []; function handleCallbackSelect(id: number) { setSelectedCallbackId(id); setPage(1); setCheckedIds(new Set()); } function toggleCheck(displayId: number) { setCheckedIds((prev) => { const next = new Set(prev); if (next.has(displayId)) { next.delete(displayId); } else { next.add(displayId); } return next; }); } const canImport = checkedIds.size > 0 && selectedCallbackId !== null; const onImport = async () => { if (!canImport) return; setSubmitError(null); try { const result = await importMutation.mutateAsync({ callback_display_id: selectedCallbackId, task_display_ids: Array.from(checkedIds), }); const msg = result.skipped > 0 ? `Imported ${result.imported} task(s), ${result.skipped} already attached` : `Imported ${result.imported} task(s)`; push(msg, 'success'); onClose(); } catch (err) { setSubmitError(extractApiError(err, 'Could not import tasks')); } }; return (
); }