import { useNavigate, useSearchParams } from "react-router-dom"; import { useMutation, useQueryClient, useQuery } from "@tanstack/react-query"; import { ArrowLeft, FlaskConical } from "lucide-react"; import TestForm, { type TestFormData } from "../components/TestForm"; import { createTest } from "../api/tests"; import { getTechniqueByMitreId } from "../api/techniques"; export default function TestCreatePage() { const navigate = useNavigate(); const queryClient = useQueryClient(); const [searchParams] = useSearchParams(); // Get technique ID from URL query param (UUID format) const techniqueId = searchParams.get("technique"); // If we have a technique ID, try to get its mitre_id for the back link const { data: technique } = useQuery({ queryKey: ["techniqueById", techniqueId], queryFn: async () => { // We need to find the mitre_id from the technique list // This is a workaround since we get UUID but need mitre_id const apiBase = import.meta.env.VITE_API_URL || "/api/v1"; const response = await fetch(`${apiBase}/techniques`, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, }, }); const techniques = await response.json(); return techniques.find((t: { id: string }) => t.id === techniqueId); }, enabled: !!techniqueId, }); const createMutation = useMutation({ mutationFn: createTest, onSuccess: (data) => { queryClient.invalidateQueries({ queryKey: ["techniques"] }); queryClient.invalidateQueries({ queryKey: ["technique"] }); // Navigate back to the technique detail if we came from there if (technique?.mitre_id) { navigate(`/techniques/${technique.mitre_id}`); } else { navigate("/tests"); } }, }); const handleSubmit = (data: TestFormData) => { createMutation.mutate({ technique_id: data.technique_id, name: data.name, description: data.description || undefined, platform: data.platform || undefined, procedure_text: data.procedure_text || undefined, tool_used: data.tool_used || undefined, }); }; const handleBack = () => { if (technique?.mitre_id) { navigate(`/techniques/${technique.mitre_id}`); } else { navigate("/tests"); } }; return (
Document a security test for technique validation
Failed to create test: {(createMutation.error as Error)?.message || "Unknown error"}