import { useEffect, useState } from 'react' import { Link } from '../types/api' import { getAllLinks, deleteLink } from '../api/client' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Button } from "@/components/ui/button" import { useToast } from "@/hooks/use-toast" import { Copy, Trash2, BarChart2 } from "lucide-react" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/components/ui/dialog" import { StatisticsModal } from "./StatisticsModal" interface LinkListProps { refresh?: number; } export function LinkList({ refresh = 0 }: LinkListProps) { const [links, setLinks] = useState([]) const [loading, setLoading] = useState(true) const [deleteModal, setDeleteModal] = useState<{ isOpen: boolean; linkId: number | null }>({ isOpen: false, linkId: null, }) const [statsModal, setStatsModal] = useState<{ isOpen: boolean; linkId: number | null }>({ isOpen: false, linkId: null, }); const { toast } = useToast() const fetchLinks = async () => { try { setLoading(true) const data = await getAllLinks() setLinks(data) } catch (err) { toast({ title: "Error", description: "Failed to load links", variant: "destructive", }) } finally { setLoading(false) } } useEffect(() => { fetchLinks() }, [refresh]) // Re-fetch when refresh counter changes const handleDelete = async () => { if (!deleteModal.linkId) return try { await deleteLink(deleteModal.linkId) await fetchLinks() setDeleteModal({ isOpen: false, linkId: null }) toast({ description: "Link deleted successfully", }) } catch (err) { toast({ title: "Error", description: "Failed to delete link", variant: "destructive", }) } } const handleCopy = (shortCode: string) => { // Use import.meta.env.VITE_BASE_URL or fall back to window.location.origin const baseUrl = import.meta.env.VITE_API_URL || window.location.origin navigator.clipboard.writeText(`${baseUrl}/${shortCode}`) toast({ description: "Link copied to clipboard", }) } if (loading && !links.length) { return