"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 { logsSchema } from "@/lib/validations/validation"; import { z } from "zod"; import { useRouter } from "next/navigation"; import { useToast } from "@/components/ui/use-toast"; import dynamic from "next/dynamic"; import { Dialog, DialogContent, DialogHeader, DialogFooter, DialogTitle, } from "@/components/ui/dialog"; const MarkdownEditor = dynamic(() => import("@uiw/react-md-editor"), { ssr: false, }); interface LogEntry { _id: string; version: string; date: string; bullets: { point: string }[]; } interface PageEntry { title: string; slug: string; content: string; // Add content to the PageEntry interface } type LogsFormValues = z.infer; const AdminLogPage = () => { const [logs, setLogs] = useState([]); const [pages, setPages] = useState([]); const [error, setError] = useState(""); const { toast } = useToast(); const router = useRouter(); const [loading, setLoading] = useState(false); const [open, setOpen] = useState(false); const [pageTitle, setPageTitle] = useState(""); const [selectedPage, setSelectedPage] = useState(null); const form = useForm({ resolver: zodResolver(logsSchema), defaultValues: { version: "", date: "", bullets: [{ point: "" }], }, }); const { fields, append, remove } = useFieldArray({ control: form.control, name: "bullets", }); const fetchLogs = async () => { try { const response = await fetch("/api/logs", { method: "GET" }); if (response.ok) { const data: LogEntry[] = 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 fetchPages = async () => { try { const response = await fetch("/api/mdx/pages", { method: "GET" }); if (response.ok) { const data: PageEntry[] = await response.json(); setPages(data); } else { throw new Error(`HTTP error! status: ${response.status}`); } } catch (error: any) { setError(error.message || "Failed to fetch pages"); } }; useEffect(() => { fetchLogs(); fetchPages(); const interval = setInterval(() => { fetchLogs(); fetchPages(); }, 10000); return () => clearInterval(interval); }, []); const onSubmit: SubmitHandler = async (data) => { setLoading(true); const response = await fetch("/api/uploadlogs", { 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/logs/${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"); } }; const createNewPage = async () => { setLoading(true); const response = await fetch(`/api/mdx/pages`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: pageTitle }), }); if (response.ok) { fetchPages(); setLoading(false); toast({ description: "Page successfully created" }); setOpen(false); setPageTitle(""); } else { setLoading(false); toast({ description: "Page creation Failed", variant: "destructive" }); } }; const handlePageSelect = async (slug: string) => { try { const response = await fetch(`/api/mdx/pages/${slug}`); if (response.ok) { const data: PageEntry = await response.json(); setSelectedPage(data); } else { toast({ description: "Failed to fetch page data", variant: "destructive", }); } } catch (error: any) { toast({ description: error.message || "Failed to fetch page data", variant: "destructive", }); } }; return (

Server Logs Form

( Version Name )} /> ( Date )} /> {fields.map((field, index) => ( ( Key Point {index + 1} )} /> ))} {/* Section to create new page */}

Multi Log page

Enter Page Title setPageTitle(e.target.value)} placeholder="Page Title" />
{/* Section to list and delete pages */}

Existing Pages

Total Pages: {pages.length}

Slug Actions {pages.map((page) => ( {page.slug} ))}
{/* Section to list and delete logs */}

Existing Logs

{error &&

{error}

} Version Date Actions {logs .slice() .reverse() .map((log) => ( {log.version} {log.date} ))}
{/* Section to edit selected page */} {selectedPage && (

Edit Page

{ if (value !== undefined) { setSelectedPage((prev) => ({ ...prev!, content: value, })); } }} />
)}
); }; export default AdminLogPage;