"use client"; import Image from "next/image"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Overview } from "@/components/ui/overview"; import { RecentFunds } from "@/components/recent-funds"; import { useEffect, useState } from "react"; import { createSupabaseClient } from "@/lib/supabase/clientComponentClient"; import useSession from "@/lib/supabase/useSession"; import { getProjectByUserId } from "@/lib/data/projectQuery"; import { Loader } from "@/components/loading/loader"; import { getInvestmentByProjectsIds } from "@/lib/data/investmentQuery"; import { useQuery } from "@supabase-cache-helpers/postgrest-react-query"; import { overAllGraphData, fourYearGraphData, dayOftheWeekData } from "../portfolio/[uid]/query"; import CountUp from "react-countup"; import { Button } from "@/components/ui/button"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Modal } from "@/components/modal"; export default function Dashboard() { const supabase = createSupabaseClient(); const router = useRouter(); const { session, loading: isLoadingSession } = useSession(); const userId = session?.user.id; const [projects, setProjects] = useState< { id: number; project_name: string; business_id: { user_id: number }[]; dataroom_id: number }[] >([]); const [latestInvestment, setLatestInvestment] = useState< { avatarUrl: string; createdTime: Date; dealAmount: number; dealStatus: string; investorId: string; username: string; }[] >([]); const tabOptions = ["daily", "monthly", "yearly"]; const [activeTab, setActiveTab] = useState("daily"); const [graphType, setGraphType] = useState("line"); const [isLoadingProjects, setIsLoadingProjects] = useState(true); const [currentProjectId, setCurrentProjectId] = useState(projects[0]?.id); const investmentDetail = useQuery( getInvestmentByProjectsIds( supabase, projects.map((item) => { return item.id.toString(); }) ) ); let graphData = []; const filteredProject = (investmentDetail?.data || []).filter((deal) => deal.project_id === currentProjectId); const handleTabChange = (tab: string) => { setActiveTab(tab); }; if (activeTab === "daily") { graphData = dayOftheWeekData(filteredProject); } else if (activeTab === "yearly") { graphData = fourYearGraphData(filteredProject); } else { graphData = overAllGraphData(filteredProject); } useEffect(() => { const fetchProjects = async () => { if (!userId) return; const { data, error } = await getProjectByUserId(supabase, userId); if (error) console.error("Error fetching projects"); setProjects(data || []); setIsLoadingProjects(false); }; fetchProjects(); }, [supabase, userId]); useEffect(() => { if (projects.length > 0 && !currentProjectId) { setCurrentProjectId(projects[0].id); } }, [projects, currentProjectId]); useEffect(() => { const setTopLatestInvestment = () => { if (investmentDetail?.data) { setLatestInvestment( investmentDetail.data .slice(0, 8) .map((item) => { if (item.project_id === currentProjectId) { return { avatarUrl: item.avatar_url, createdTime: item.created_time, dealAmount: item.deal_amount, dealStatus: item.deal_status, investorId: item.investor_id, username: item.username, }; } return undefined; }) .filter((item) => item !== undefined) as { avatarUrl: string; createdTime: Date; dealAmount: number; dealStatus: string; investorId: string; username: string; }[] ); } }; setTopLatestInvestment(); }, [currentProjectId, investmentDetail?.data]); return (
{" "}

Business Dashboard

{projects && projects.length > 0 && ( {projects.map((project) => ( setCurrentProjectId(project.id)} > {project.project_name} ))} {projects.map((project) => (
Total Funds Raised
$ project.deal_status === "Completed") .reduce((sum, current) => sum + current.deal_amount, 0)} duration={1} />
Profile Views
+
{/*

+180.1% from last month

*/}
Total Followers
+
{/*

+19% from last month

*/}
Overview {tabOptions.map((tab) => ( {tab.charAt(0).toUpperCase() + tab.slice(1)} ))} setGraphType("line")}> Line setGraphType("bar")}> Bar Recent Funds { return { name: item.username, amount: item.dealAmount, avatar: item.avatarUrl, date: new Date(item.createdTime), status: item.dealStatus, profile_url: `/profile/${item.investorId}`, }; })} />
{filteredProject && filteredProject.length > 1 ? ( { return { date: item.created_time, name: item.username, amount: item.deal_amount, status: item.deal_status, logoURL: Array.isArray(item.avatar_url) ? item.avatar_url[0] : item.avatar_url, profileURL: `/profile/${item.investor_id}`, }; })} /> ) : undefined}
))}
)}
); }