"use client"; import { useState } from "react"; import { ColumnDef } from "@tanstack/react-table"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { DataTable } from "@/components/data-table/data-table"; import { useApiTokens, useCreateApiKey, useDeleteApiKey, useTestSecure } from "@/modules/api-management/hooks"; import { ApiToken } from "@/modules/api-management/schemas"; import { useToast } from "@/hooks/use-toast"; import { Key, Trash, TestTube, MoreHorizontal, Plus, Eye, Copy } from "lucide-react"; import { formatDateTime } from "@/lib/utils"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; export default function ApiManagementPage() { const { toast } = useToast(); const { data, isLoading } = useApiTokens(); const createMutation = useCreateApiKey(); const deleteMutation = useDeleteApiKey(); const { refetch: testSecure } = useTestSecure(); const [apiKey, setApiKey] = useState(null); const [selectedToken, setSelectedToken] = useState(null); const [isDetailOpen, setIsDetailOpen] = useState(false); const [deleteToken, setDeleteToken] = useState(null); const handleCreate = async () => { try { const response = await createMutation.mutateAsync(); setApiKey(response.apiKey || response.token); toast({ title: "API Key created", description: "New API key generated successfully" }); } catch (error) { toast({ title: "Create failed", variant: "destructive" }); } }; const handleDelete = async () => { if (!deleteToken) return; try { await deleteMutation.mutateAsync(deleteToken); toast({ title: "API Key deleted", description: "API token has been deleted successfully" }); setDeleteToken(null); } catch (error) { toast({ title: "Delete failed", variant: "destructive" }); setDeleteToken(null); } }; const handleCopy = async (text: string) => { try { await navigator.clipboard.writeText(text); toast({ title: "Copied to clipboard", description: "API key has been copied successfully" }); } catch (error) { toast({ title: "Copy failed", variant: "destructive" }); } }; const handleOpenDetail = (token: ApiToken) => { setSelectedToken(token); setIsDetailOpen(true); }; const columns: ColumnDef[] = [ { accessorKey: "TokenCredential_AC", header: "Token", cell: ({ row }) => (
{row.original.TokenCredential_AC}
), }, { accessorKey: "CreatedAt_AC", header: "Created", cell: ({ row }) => formatDateTime(row.original.CreatedAt_AC), }, { accessorKey: "UpdatedAt_AC", header: "Updated", cell: ({ row }) => formatDateTime(row.original.UpdatedAt_AC), }, { id: "actions", cell: ({ row }) => ( handleOpenDetail(row.original)}> View Details setDeleteToken(row.original.TokenCredential_AC)} className="text-destructive" > Delete ), }, ]; if (isLoading) { return
Loading...
; } const tokens = data?.data?.tokens || []; const handleTest = async () => { try { await testSecure(); toast({ title: "Test successful", description: "API connection is working" }); } catch (error) { toast({ title: "Test failed", variant: "destructive" }); } }; return (

API Management

Manage API keys and tokens

{apiKey && ( New API Token Created Copy and save this token securely. You won't be able to see it again.

{apiKey}

)} API Tokens Total: {data?.data?.total || 0} tokens API Testing Test API connectivity API Token Details View complete API token information {selectedToken && (
{selectedToken.TokenCredential_AC}

{formatDateTime(selectedToken.CreatedAt_AC)}

{formatDateTime(selectedToken.UpdatedAt_AC)}

)}
!open && setDeleteToken(null)}> Delete API Key Are you sure you want to delete this API key? This action cannot be undone and will immediately revoke access for this token. Cancel Delete
); }