"use client"; import Image from "next/image"; import { Card, CardContent, CardDescription, 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 { getLatestInvestment, overAllGraphData, Deal } from "../portfolio/[uid]/query"; export default function Dashboard() { let supabase = createSupabaseClient(); const userId = useSession().session?.user.id; const [projects, setProjects] = useState< { id: number; project_name: string; business_id: { user_id: number }[]; dataroom_id: number }[] >([]); const [latestInvestment, setLatestInvestment] = useState< { projectId: number; name: any; amount: number; date: Date; logo_url: string; status: string }[] >([]); const [isSuccess, setIsSuccess] = useState(false); const [graphType, setGraphType] = useState("line"); const [currentProjectId, setCurrentProjectId] = useState(projects[0]?.id); const investmentDetail = useQuery( getInvestmentByProjectsIds( supabase, projects.map((item) => { return item.id.toString(); }) ) ); useEffect(() => { const fetchLatestInvestment = async () => { const latest = await getLatestInvestment( supabase, investmentDetail?.data?.map((deal) => { return { project_id: deal.project_id, deal_amount: deal.deal_amount, created_time: deal.created_time, }; }) || [] ); const resolvedLatest = await Promise.all( latest.map(async (investment) => ({ ...investment, logo_url: await investment.logo_url, })) ); setLatestInvestment( resolvedLatest.map((investment) => ({ projectId: investment.projectId, name: investment.name, amount: investment.amount, date: investment.date, logo_url: investment.logo_url, status: investmentDetail?.data?.find((deal) => deal.project_id === investment.projectId)?.deal_status.value, })) ); // console.table(investmentDetail); }; fetchLatestInvestment(); }, [supabase, investmentDetail]); useEffect(() => { const fetchProjects = async () => { if (userId) { const { data, error } = await getProjectByUserId(supabase, userId); // alert(JSON.stringify(data)); if (error) { console.error("Error while fetching projects"); } if (data) { setProjects(data); // console.table(data); } } else { console.error("Error with UserId while fetching projects"); } setIsSuccess(true); }; fetchProjects(); }, [supabase, userId]); // console.table(projects); // console.table(latestInvestment); return ( <>
Dashboard Dashboard

Business Dashboard

{projects.map((project) => ( setCurrentProjectId(project.id)} > {project.project_name} ))} {projects.map((project) => (
Total Funds Raised
${}
{/*

+20.1% from last month

*/}
Profile Views
+2350
{/*

+180.1% from last month

*/}
Total Followers
+12,234
{/*

+19% from last month

*/}
{/* Active Now
+573

+201 since last hour

*/}
Overview {/* { if (deal.project_id === currentProjectId) { return { deal_amount: deal.deal_amount, created_time: deal.created_time, }; } return undefined; }) .filter((deal) => deal !== undefined) as Deal[] )} /> */} {/* tab to switch between line and bar graph */} setGraphType("line")}> Line setGraphType("bar")}> Bar Recent Funds { if (item.projectId === currentProjectId) { return { name: item.name, amount: item.amount, avatar: item.logo_url, date: item.date, status: item.status, }; } return undefined; }) .filter((item) => item !== undefined)} />
))}
); }