'use client'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { ColumnDef } from '@tanstack/react-table'; import { format } from 'date-fns'; import { CreditCard as Edit, Ellipsis, Package, Plus, Trash2 } from 'lucide-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { DataTable } from '@/components/data-table'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { apiClient } from '@/lib/api-client'; import { KeygenProduct } from '@/lib/types'; import { toast } from 'sonner'; export default function ProductsPage() { const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [selectedProduct, setSelectedProduct] = useState(null); const queryClient = useQueryClient(); const router = useRouter(); const { data, isLoading } = useQuery({ queryKey: ['products'], queryFn: () => apiClient.getProducts(1, 100), }); const deleteMutation = useMutation({ mutationFn: async (id: string) => await apiClient.deleteProduct(id), onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ['products'] }); setIsDeleteDialogOpen(false); setSelectedProduct(null); toast.success("Product deleted successfully"); }, }); const handleDelete = async () => { if (selectedProduct) { await deleteMutation.mutateAsync(selectedProduct.id); setIsDeleteDialogOpen(false) } }; const columns: ColumnDef[] = [ { accessorKey: 'attributes.name', header: 'Name', filterFn: (row, columnId, filterValue) => { const name = row.original.attributes.name; return name.toLowerCase().includes(filterValue.toLowerCase()); }, cell: ({ row }) => (
{row.original.attributes.name}
), }, { accessorKey: 'attributes.distributionStrategy', header: 'Distribution', cell: ({ row }) => ( {row.original.attributes.distributionStrategy} ), }, { accessorKey: 'attributes.platforms', header: 'Platforms', cell: ({ row }) => (
{row.original.attributes.platforms?.slice(0, 3).map((platform, idx) => ( {platform} ))} {row.original.attributes.platforms?.length > 3 && ( +{row.original.attributes.platforms.length - 3} )}
), }, { accessorKey: 'attributes.url', header: 'URL', cell: ({ row }) => { const url = row.original.attributes.url; return url ? ( {url.length > 30 ? `${url.substring(0, 30)}...` : url} ) : 'N/A'; }, }, { accessorKey: 'attributes.created', header: 'Created', cell: ({ row }) => format(new Date(row.original.attributes.created), 'MMM d, yyyy'), }, { id: 'actions', cell: ({ row }) => { const product = row.original; return ( router.push(`/dashboard/products/${product.id}`)} > Edit { setSelectedProduct(product); setIsDeleteDialogOpen(true); }} className="text-red-600" > Delete ); }, }, ]; if (isLoading) { return ( <>

Loading products...

); } return ( <>

Products

Manage your software products and their distribution settings

Delete Product Are you sure you want to delete {selectedProduct?.attributes.name}? This action cannot be undone.
); }