"use client"; import React, { useEffect, useState } from "react"; import { useForm, SubmitHandler, useFieldArray } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Input } from "@/components/ui/input"; import { z } from "zod"; import { useToast } from "@/components/ui/use-toast"; import { vulnerabilitiesSchema } from "@/lib/validations/validation"; import { Select, SelectContent, SelectItem, SelectTrigger } from "@/components/ui/select"; interface VulnerabiltyEntry { _id: string; version: string; bullets: { point: string; securityAdvisoryUrl: string; }[]; } type VulnerabiltiesForm = z.infer; const AdminLogPage = () => { const [logs, setLogs] = useState([]); const [error, setError] = useState(""); const { toast } = useToast(); const [loading, setLoading] = useState(false); const form = useForm({ resolver: zodResolver(vulnerabilitiesSchema), defaultValues: { version: "", bullets: [{ point: "", securityAdvisoryUrl: "" }] } }); const { fields, append, remove } = useFieldArray({ control: form.control, name: "bullets" }); const fetchLogs = async () => { try { const response = await fetch("/api/vulnerabilities", { method: "GET" }); if (response.ok) { const data: VulnerabiltyEntry[] = await response.json(); setLogs(data); } else { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error: any) { setError(error.message || "Failed to fetch logs"); } }; const onSubmit: SubmitHandler = async (data) => { setLoading(true); const response = await fetch("/api/uploadvulnerabilities", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data) }); if (response.ok) { form.reset(); fetchLogs(); setLoading(false); toast({ description: "Logs successfully added" }); } else { setLoading(false); toast({ description: "Upload Failed", variant: "destructive" }); } }; const deleteLog = async (id: string) => { try { const response = await fetch(`/api/delete/vulnerability/${id}`, { method: "DELETE" }); if (response.ok) { fetchLogs(); } else { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error: any) { setError(error.message || "Failed to delete log"); } }; useEffect(() => { fetchLogs(); const interval = setInterval(() => { fetchLogs(); }, 10000); return () => clearInterval(interval); }, []); return (

Server Vulnerabilties Form

( Version Name )} /> {fields.map((field, index) => ( <> ( Key Point {index + 1} )} /> ( Security Advisory URL for Key Point {index + 1} )} /> ))} {/* Section to list and delete logs */}

Existing Vulnerabilties

{error &&

{error}

} Version Actions {logs .slice() .reverse() .map((log) => ( {log.version} ))}
); }; export default AdminLogPage;