'use client'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { ColumnDef } from '@tanstack/react-table'; import { format } from 'date-fns'; import { Copy, MoreHorizontal, Trash2 } from 'lucide-react'; import { useState } from 'react'; import { DashboardLayout } from '@/components/dashboard-layout'; 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 { KeygenToken } from '@/lib/types'; import { toast } from 'sonner'; export default function UsersPage() { const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [selectedToken, setSelectedToken] = useState(null); const queryClient = useQueryClient(); const { data, isLoading } = useQuery({ queryKey: ['tokens'], queryFn: () => apiClient.getTokens(1, 100), }); const copyLicenseKey = (key: string, message: string = 'Copied to clipboard') => { navigator.clipboard.writeText(key); toast(message); }; const deleteMutation = useMutation({ mutationFn: async (id: string) => await apiClient.deleteToken(id), onSuccess:async () => { await queryClient.invalidateQueries({ queryKey: ['tokens'] }); setIsDeleteDialogOpen(false); setSelectedToken(null); toast.success('Token deleted successfully'); }, onError: (error) => { toast.error(error.message); }, }); const handleDelete = async () => { if (selectedToken) { await deleteMutation.mutateAsync(selectedToken.id); } }; const columns: ColumnDef[] = [ { accessorKey: 'id', header: 'Token', cell: ({ row }) => { const id = row.original.id; return (
{ id ? '***********' : 'N/A'} {id && ( )}
); }, }, { accessorKey: 'attributes.kind', header: 'kind', cell: ({ row }) => { const kind = row.original.attributes.kind; return ( {kind} ); }, }, { accessorKey: 'relationships.account.data.id', header: 'Account id', cell: ({ row }) => { const accountId = row.original.relationships?.account?.data?.id; return (
{ accountId ? '***********' : 'N/A'} {accountId && ( )}
); }, }, { accessorKey: 'relationships.bearer.data.id', header: 'User ID', cell: ({ row }) => { const bearerId = row.original.relationships?.bearer?.data?.id; return (
{ bearerId ? '***********' : 'N/A'} {bearerId && ( )}
); }, }, { accessorKey: 'attributes.expiry', header: 'Expiry', cell: ({ row }) => row.original.attributes.expiry ? format(new Date(row.original.attributes.expiry), 'MMM d, yyyy hh:mm a') : 'N/A', }, { accessorKey: 'attributes.updated', header: 'Updated', cell: ({ row }) => format(new Date(row.original.attributes.updated), 'MMM d, yyyy hh:mm a'), }, { accessorKey: 'attributes.created', header: 'Created', cell: ({ row }) => format(new Date(row.original.attributes.created), 'MMM d, yyyy hh:mm a'), }, { id: 'actions', cell: ({ row }) => { const token = row.original; return ( { setSelectedToken(token); setIsDeleteDialogOpen(true); }} className="text-red-600" > Delete ); }, }, ]; if (isLoading) { return (

Loading tokens...

); } return ( <>

Users

Manage your registered users and their access

{/* Delete User Dialog */} Delete User Are you sure you want to delete {selectedToken?.attributes.token}? This action cannot be undone.
); }