import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { useQuery, useMutation } from "@tanstack/react-query"; import { Loader2, FlaskConical, X, AlertCircle, BookOpen, } from "lucide-react"; import { getTemplateById } from "../api/test-templates"; import { createTestFromTemplate } from "../api/tests"; import type { TestTemplate } from "../types/models"; // ── Props ────────────────────────────────────────────────────────── interface TestFromTemplateFormProps { templateId: string; /** If provided, pre-selects the technique (from TechniqueDetailPage). */ techniqueId?: string; onClose: () => void; } // ── Component ────────────────────────────────────────────────────── export default function TestFromTemplateForm({ templateId, techniqueId: propTechniqueId, onClose, }: TestFromTemplateFormProps) { const navigate = useNavigate(); // ── Load template details ────────────────────────────────────── const { data: template, isLoading, error, } = useQuery({ queryKey: ["test-template", templateId], queryFn: () => getTemplateById(templateId), enabled: !!templateId, }); // ── Form state (pre-filled from template) ────────────────────── const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [technique, setTechnique] = useState(propTechniqueId || ""); const [platform, setPlatform] = useState(""); const [procedureText, setProcedureText] = useState(""); const [toolUsed, setToolUsed] = useState(""); const [expectedDetection, setExpectedDetection] = useState(""); // Hydrate form when template loads useEffect(() => { if (template) { setName(template.name || ""); setDescription(template.description || ""); setTechnique(propTechniqueId || template.mitre_technique_id || ""); setPlatform(template.platform || ""); setProcedureText(template.attack_procedure || ""); setToolUsed(template.tool_suggested || ""); setExpectedDetection(template.expected_detection || ""); } }, [template, propTechniqueId]); // ── Submit ───────────────────────────────────────────────────── const createMutation = useMutation({ mutationFn: () => createTestFromTemplate(templateId, technique), onSuccess: (test) => { navigate(`/tests/${test.id}`); }, }); const canSubmit = name.trim().length > 0 && technique.trim().length > 0; // ── Render ───────────────────────────────────────────────────── return (
{/* Header */}

Create Test from Template

{/* Body */} {isLoading ? (
) : error ? (

Failed to load template

) : (
{/* Template info banner */}

Pre-filled from template: {template?.name} {template?.source && ( {template.source} )}

{/* Name */}
setName(e.target.value)} className="w-full rounded-lg border border-gray-700 bg-gray-800 px-3 py-2 text-sm text-gray-200 placeholder-gray-500 focus:border-cyan-500 focus:outline-none focus:ring-1 focus:ring-cyan-500" placeholder="Test name" />
{/* Description */}