'use client'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { ColumnDef } from '@tanstack/react-table'; import { format } from 'date-fns'; import { Copy, CreditCard as Edit, Ellipsis, Key, 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 { KeygenLicense } from '@/lib/types'; import { toast } from 'sonner'; export default function LicensesPage() { const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [selectedLicense, setSelectedLicense] = useState(null); const queryClient = useQueryClient(); const router = useRouter(); const { data: licenses, isLoading } = useQuery({ queryKey: ['licenses'], queryFn: async () => await apiClient.getLicenses(1, 100), }); const deleteMutation = useMutation({ mutationFn: (id: string) => apiClient.deleteLicense(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['licenses'] }); setIsDeleteDialogOpen(false); setSelectedLicense(null); toast.success("License deleted successfully"); }, onError: (error) => { toast.error(error.message); }, }); const handleDelete = async () => { if (selectedLicense) { await deleteMutation.mutateAsync(selectedLicense.id); } }; const copyLicenseKey = (key: string) => { navigator.clipboard.writeText(key); toast("License key copied to clipboard"); }; const getStatusColor = (status: string) => { switch (status) { case 'active': return 'default'; case 'expired': return 'destructive'; case 'suspended': return 'secondary'; default: return 'outline'; } }; const columns: ColumnDef[] = [ { accessorKey: 'attributes.name', header: 'Name', cell: ({ row }) => { const name = row.original.attributes.name; return name ? (
{name}
) : ( Unnamed ); }, }, { accessorKey: 'attributes.key', header: 'License Key', cell: ({ row }) => { const key = row.original.attributes.key; const displayKey = `${key.substring(0, 8)}...${key.substring(key.length - 8)}`; return (
{displayKey}
); }, }, { accessorKey: 'attributes.status', header: 'Status', cell: ({ row }) => { const status = row.original.attributes.status; return ( {status.charAt(0).toUpperCase() + status.slice(1)} ); }, }, { accessorKey: 'attributes.uses', header: 'Uses', cell: ({ row }) => ( {row.original.attributes.uses} ), }, { accessorKey: 'attributes.expiry', header: 'Expiry', cell: ({ row }) => { const expiry = row.original.attributes.expiry; return expiry ? format(new Date(expiry), 'MMM d, yyyy') : 'Never'; }, }, { accessorKey: 'attributes.created', header: 'Created', cell: ({ row }) => format(new Date(row.original.attributes.created), 'MMM d, yyyy'), }, { id: 'actions', cell: ({ row }) => { const license = row.original; return ( router.push(`/dashboard/licenses/${license.id}`)} > Edit copyLicenseKey(license.attributes.key)} > Copy Key { setSelectedLicense(license); setIsDeleteDialogOpen(true); }} className="text-red-600" > Delete ); }, }, ]; if (isLoading) { return ( <>

Loading licenses...

); } return ( <>

Licenses

Manage software licenses and their assignments

{/* Delete License Dialog */} Delete License Are you sure you want to delete this license? This action cannot be undone.
); }