// This file is licensed under the Fossorial Commercial License. // Unauthorized use, copying, modification, or distribution is strictly prohibited. // // Copyright (c) 2025 Fossorial LLC. All rights reserved. "use client"; import { ColumnDef } from "@tanstack/react-table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Button } from "@app/components/ui/button"; import { ArrowRight, ArrowUpDown, MoreHorizontal } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useState } from "react"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { toast } from "@app/hooks/useToast"; import { formatAxiosError } from "@app/lib/api"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import moment from "moment"; import { ApiKeysDataTable } from "./ApiKeysDataTable"; import { useTranslations } from "next-intl"; export type ApiKeyRow = { id: string; key: string; name: string; createdAt: string; }; type ApiKeyTableProps = { apiKeys: ApiKeyRow[]; }; export default function ApiKeysTable({ apiKeys }: ApiKeyTableProps) { const router = useRouter(); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selected, setSelected] = useState(null); const [rows, setRows] = useState(apiKeys); const api = createApiClient(useEnvContext()); const t = useTranslations(); const deleteSite = (apiKeyId: string) => { api.delete(`/api-key/${apiKeyId}`) .catch((e) => { console.error(t('apiKeysErrorDelete'), e); toast({ variant: "destructive", title: t('apiKeysErrorDelete'), description: formatAxiosError(e, t('apiKeysErrorDeleteMessage')) }); }) .then(() => { router.refresh(); setIsDeleteModalOpen(false); const newRows = rows.filter((row) => row.id !== apiKeyId); setRows(newRows); }); }; const columns: ColumnDef[] = [ { id: "dots", cell: ({ row }) => { const apiKeyROw = row.original; const router = useRouter(); return ( { setSelected(apiKeyROw); }} > {t('viewSettings')} { setSelected(apiKeyROw); setIsDeleteModalOpen(true); }} > {t('delete')} ); } }, { accessorKey: "name", header: ({ column }) => { return ( ); } }, { accessorKey: "key", header: "Key", cell: ({ row }) => { const r = row.original; return {r.key}; } }, { accessorKey: "createdAt", header: "Created At", cell: ({ row }) => { const r = row.original; return {moment(r.createdAt).format("lll")} ; } }, { id: "actions", cell: ({ row }) => { const r = row.original; return (
); } } ]; return ( <> {selected && ( { setIsDeleteModalOpen(val); setSelected(null); }} dialog={

{t('apiKeysQuestionRemove', {selectedApiKey: selected?.name || selected?.id})}

{t('apiKeysMessageRemove')}

{t('apiKeysMessageConfirm')}

} buttonText={t('apiKeysDeleteConfirm')} onConfirm={async () => deleteSite(selected!.id)} string={selected.name} title={t('apiKeysDelete')} /> )} { router.push(`/admin/api-keys/create`); }} /> ); }