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