'use client'; import { useEffect, useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import strings from "@/strings.json" import { PlusCircle, UserPlus, CircleAlert } from "lucide-react" export default function Home() { const [totalPosts, setTotalPosts] = useState(0); const [postCardError, setPostCardError] = useState(false); const [postCardLoading, setPostCardLoading] = useState(true); const [totalUsers, setTotalUsers] = useState(0); const [userCtCardError, setUserCtCardError] = useState(false); const [userCtCardLoading, setUserCtCardLoading] = useState(true); useEffect(() => { console.log("[i] Calculating post count..."); (async () => { try { const username = document.cookie.split('; ').find(row => row.startsWith('username='))?.split('=')[1] || ''; const key = document.cookie.split('; ').find(row => row.startsWith('key='))?.split('=')[1] || ''; const res = await fetch(`http://localhost:3001/api/admin/posts/totalPosts`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, key }), cache: 'no-store', }); if (!res.ok) { alert('Failed to fetch total post count'); setPostCardError(true); throw new Error(`Failed to fetch total post count: ${res.status}`); } const data = await res.json(); if (data.success === false) { if (data.message) { alert(data.message); setPostCardError(true); setPostCardLoading(false); throw new Error(data.message); } else { alert('Unknown error occurred'); setPostCardError(true); setPostCardLoading(false); throw new Error('Unknown error occurred'); } } else if (data.count) { console.log("[✓] Total posts:", data.count); setTotalPosts(data.count); setPostCardLoading(false); } } catch (error) { alert('Error fetching total post count'); setPostCardError(true); setPostCardLoading(false); console.error('[!] Failed to fetch total post count:', error); } })(); console.log("[i] Calculating user count..."); (async () => { try { const username = document.cookie.split('; ').find(row => row.startsWith('username='))?.split('=')[1] || ''; const key = document.cookie.split('; ').find(row => row.startsWith('key='))?.split('=')[1] || ''; const res = await fetch(`http://localhost:3001/api/admin/users/totalUsers`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ username, key }), cache: 'no-store', }); if (!res.ok) { alert('Failed to fetch total user count'); setUserCtCardError(true); throw new Error(`Failed to fetch total user count: ${res.status}`); } const data = await res.json(); if (data.success === false) { if (data.message) { alert(data.message); setUserCtCardError(true); setUserCtCardLoading(false); throw new Error(data.message); } else { alert('Unknown error occurred'); setUserCtCardError(true); setUserCtCardLoading(false); throw new Error('Unknown error occurred'); } } else if (data.count) { console.log("[✓] Total users:", data.count); setTotalUsers(data.count); setUserCtCardLoading(false); } } catch (error) { alert('Error fetching total user count'); setUserCtCardError(true); setUserCtCardLoading(false); console.error('[!] Failed to fetch total user count:', error); } })(); }, []); return (
Error
Error