import { useEffect, useState } from 'react' import { Link } from '../types/api' import { getAllLinks, deleteLink } from '../api/client' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { useToast } from "@/hooks/use-toast" interface LinkListProps { refresh: number } export function LinkList({ refresh }: LinkListProps) { const [links, setLinks] = useState([]) const [loading, setLoading] = useState(true) const [deleteModal, setDeleteModal] = 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]) const handleDelete = async () => { if (!deleteModal.linkId) return try { await deleteLink(deleteModal.linkId) await fetchLinks() setDeleteModal({ isOpen: false, linkId: null }) toast({ title: "Link deleted", description: "The link has been successfully deleted.", }) } catch (err) { toast({ title: "Error", description: "Failed to delete link", variant: "destructive", }) } } if (loading && !links.length) { return
Loading...
} return (
setDeleteModal({ isOpen: open, linkId: null })}> Delete Link Are you sure you want to delete this link? This action cannot be undone. Short Code Original URL Clicks Created Actions {links.map((link) => ( {link.short_code} {link.original_url} {link.clicks} {new Date(link.created_at).toLocaleDateString()}
))}
) }