// app/dashboard/page.tsx "use client"; import React, { useState, useEffect } from "react"; import Link from "next/link"; import { Card, CardContent } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Loader2, TrendingUp, TrendingDown, AlertTriangle } from "lucide-react"; import { axiosInstance } from "@/services/axios"; import { v4 as uuidv4 } from "uuid"; import { useRouter } from "next/navigation"; import { useToast } from "@/hooks/use-toast"; import { useDarkMode } from "@/app/context/ThemeContext"; import { AddCardModal } from "@/components/fundamental/addcard"; // Helper for delta style const getDeltaStyle = (value: number) => { if (value > 3) return { bg: "bg-green-100", text: "text-green-700", icon: , }; if (value < -3) return { bg: "bg-red-100", text: "text-red-700", icon: , }; return { bg: "bg-yellow-100", text: "text-yellow-700", icon: , }; }; export default function Dashboard() { // State declarations... const [isModalOpen, setIsModalOpen] = useState(false); const [newCardName, setNewCardName] = useState(""); const [newCardOrgName, setNewCardOrgName] = useState(""); const [adding, setAdding] = useState(false); const router = useRouter(); const { toast } = useToast(); const { darkMode } = useDarkMode(); const [users, setUsers] = useState([]); const [page, setPage] = useState(1); const [loadingUsers, setLoadingUsers] = useState(false); const limit = 10; const fetchUsers = async () => { setLoadingUsers(true); try { const response = await axiosInstance.get("/allUsers", { params: { page, limit }, }); if(response.data.data.length === 0) { toast({ title: "End of list", description: "No more users to show", variant: "default", }); return; } if (response.data && response.data.data) { setUsers(response.data.data); } else { toast({ title: "Error", description: "Failed to load users", variant: "destructive", }); } } catch (error) { console.error("Error fetching users:", error); toast({ title: "Error", description: "Failed to fetch users", variant: "destructive", }); } finally { setLoadingUsers(false); } }; useEffect(() => { fetchUsers(); }, [page]); const addCard = async () => { if (newCardName.trim() === "") return; try { setAdding(true); const respo = await axiosInstance.post("/saveInfo", { name: newCardName, userId: uuidv4(), type: "user", org_name: newCardOrgName, cluster: {}, deltas: {}, }); if (respo.data?.data?.userId) { router.push(`/user/score/${respo.data.data.userId}`); toast({ variant: "default", title: "Success", description: "Data source added successfully", }); } else { setAdding(false); throw new Error("Invalid response from server"); } } catch (e) { console.error("Error adding card:", e); toast({ title: "Error", description: "Failed to add card", variant: "destructive", }); setAdding(false); } }; const goToNextPage = () => setPage((prev) => prev + 1); const goToPrevPage = () => { if (page > 1) setPage((prev) => prev - 1); }; return ( Dashboard View all data sources and performance scores {loadingUsers ? ( ) : ( {users.map((user) => { const deltaEntries = Object.entries(user.deltas || {}); return ( {user.name} {user.org_name} {/* Responsive delta entries with flex layout */} {deltaEntries.map(([key, value]) => { const numValue = parseFloat(String(value)); const { bg, text, icon } = getDeltaStyle(numValue); return ( {key.replaceAll("_"," ")} {icon} {numValue.toFixed(2)} ); })} ); })} )} Previous Page {page} Next ); }
View all data sources and performance scores
{user.org_name}