import { Navbar } from '@/App' import { ThemeProvider } from '@/components/theme-provider' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table' import { useParams } from 'react-router-dom' import { useEffect, useState } from 'react' import { Bar, BarChart, ResponsiveContainer, XAxis, YAxis } from 'recharts' import { ArrowRight, Copy } from 'lucide-react' import { useToast } from '@/components/ui/use-toast' import { Toaster } from '@/components/ui/toaster' type Shortener = { id: number link: string code: string visitors: { visited_at: Date country: string }[] } const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', ] as const type VisitorData = { name: string; total: number } type CountryData = { country_code: string country: string visitor_count: string } let visitor_data: VisitorData[] = [] months.forEach((months) => { visitor_data.push({ name: months, total: 0 }) }) const backend_url = import.meta.env.VITE_BACKEND_URL ?? 'http://192.168.100.40:3000' export default function Dashboard() { const [shorteners, setShorteners] = useState([]) const [isLoading, setIsLoading] = useState(false) const { shortenerId } = useParams() const [countryVisitor, setCountryVisitor] = useState([]) const [visitorData, setVisitorData] = useState(visitor_data) const getShorteners = async () => { setIsLoading(true) const response = await fetch(backend_url + '/link/' + shortenerId, { method: 'GET', }) const data = await response.json() const shortenersData = data.shorteners as Shortener[] const countryData = data.visitors as CountryData[] setShorteners(shortenersData) setCountryVisitor(countryData) calculateShortenerData(shortenersData[0]) setIsLoading(false) } const calculateShortenerData = (shortener: Shortener) => { const visitors = shortener.visitors let data: VisitorData[] = [] months.forEach((months) => { data.push({ name: months, total: 0 }) }) let visitor_data_copy = data visitors.forEach((visitor) => { const month = new Date(visitor.visited_at).getMonth() visitor_data_copy[month] = { ...visitor_data_copy[month], total: visitor_data_copy[month].total + 1, } setVisitorData(visitor_data_copy) }) console.log(visitorData) } const { toast } = useToast() const copyLinkToClipboard = async (code: string) => { await navigator.clipboard.writeText(backend_url + '/' + code) toast({ title: 'Link Copied', description: `Copied ${backend_url + '/' + code} To Clipboard`, }) } useEffect(() => { if (!shorteners.length) { getShorteners() } }, []) const thisMonth = new Date().getMonth() const getVisitorGrowth = (visitorData: VisitorData[]) => { const growth = visitorData[thisMonth]?.total ? visitorData[thisMonth].total - visitorData[thisMonth - 1]?.total ?? 0 : 0 if (growth < 0) { return `- ${Math.abs(growth)}` } return `+ ${growth}` } return (
copyLinkToClipboard(shorteners[0].code) }>
{backend_url + '/' + shortenerId}
{shorteners[0]?.link}
This Month's Visitors
{visitorData[thisMonth]?.total ?? 0}

{`${getVisitorGrowth( visitorData )} since last month`}

Visitors Top Countries By Vistors Country Visitor(s) {countryVisitor.map((country) => { return (
{ country.country }
{ country.visitor_count }
) })}
) } export function Overview({ visitorData }: { visitorData: VisitorData[] }) { return ( ) }