'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(strings.logsCalculatingPostCt); (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(strings.errorsFetchTotalPostCtErr); setPostCardError(true); throw new Error(`${strings.errorsFetchTotalPostCtErr}: ${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(strings.errorsUnknownError); setPostCardError(true); setPostCardLoading(false); throw new Error(strings.errorsUnknownError); } } else if (data.count) { console.log(strings.logsTotalPosts, data.count); setTotalPosts(data.count); setPostCardLoading(false); } } catch (error) { alert(strings.errorsFetchTotalPostCtErr); setPostCardError(true); setPostCardLoading(false); console.error(strings.errorsFetchTotalPostCtErrFancy, error); } })(); console.log(strings.logsCalculatingUserCt); (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(strings.errorsFetchTotalUserCtErr); setUserCtCardError(true); throw new Error(`${strings.errorsFetchTotalUserCtErr}: ${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(strings.errorsUnknownError); setUserCtCardError(true); setUserCtCardLoading(false); throw new Error(strings.errorsUnknownError); } } else if (data.count) { console.log(strings.logsTotalUsers, data.count); setTotalUsers(data.count); setUserCtCardLoading(false); } } catch (error) { alert(strings.errorsFetchTotalUserCtErr); setUserCtCardError(true); setUserCtCardLoading(false); console.error(strings.errorsFetchTotalUserCtErrFancy, error); } })(); }, []); return (

{strings.adminHeader}

{strings.totalUsersCardTitle} {userCtCardLoading ? (
) : userCtCardError ? (

{strings.errorsSuperGeneric}

) : ( totalUsers )}
{strings.totalPostsCardTitle} {postCardLoading ? (
) : postCardError ? (

{strings.errorsSuperGeneric}

) : ( totalPosts )}
{strings.adminQuickActionsCardTitle}
) }