import type { ColumnDef } from '@tanstack/react-table';
import { Link } from '@tanstack/react-router';
import {
API_KEY_RESOURCES,
deleteAdminApiKeyInput,
permissionValue,
type AdminApiKey,
useDeleteAdminApiKey,
} from '@/api/admin/api-keys/queries';
import { Dialog } from '@/components/elements/dialog';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import DataTableColumnHeader from '@/components/elements/table/DataTableColumnHeader';
import { actionsColumn, DeleteAction, RowActions } from '@/components/elements/table/RowActions';
import dayjs from '@/lib/dayjs';
const grantedCount = (apiKey: AdminApiKey): number =>
API_KEY_RESOURCES.filter((resource) => permissionValue(apiKey, resource) > 0).length;
const ApiKeyActionsCell = ({ apiKey }: { apiKey: AdminApiKey }) => {
const deleteApiKey = useDeleteAdminApiKey();
const isSubmitting = deleteApiKey.isPending;
return (
(
)}
onConfirmed={(_event, close) =>
deleteApiKey.mutate(deleteAdminApiKeyInput(apiKey.attributes.identifier), { onSuccess: close })
}
>
Deleting this application API key is permanent. Integrations using{' '}
{apiKey.attributes.identifier} will immediately stop working.
);
};
export const apiKeyColumns = [
{
id: 'memo',
accessorFn: (apiKey) => apiKey.attributes.memo,
header: ({ column }) => ,
cell: ({ row }) => (
{row.original.attributes.memo || 'No description provided'}
{row.original.attributes.identifier}
),
enableSorting: true,
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'permissions',
header: 'Permissions',
cell: ({ row }) => {grantedCount(row.original)} granted,
enableSorting: false,
meta: { headerClassName: 'hidden w-28 @md:table-cell', cellClassName: 'hidden w-28 @md:table-cell' },
},
{
id: 'created_by',
header: 'Created by',
cell: ({ row }) => {
const createdBy = row.original.attributes.created_by;
return createdBy ? (
{createdBy.username}
) : (
Unknown
);
},
enableSorting: false,
meta: { headerClassName: 'hidden w-32 @xl:table-cell', cellClassName: 'hidden w-32 @xl:table-cell' },
},
{
id: 'last_used_at',
header: 'Last used',
cell: ({ row }) => {
const lastUsedAt = row.original.attributes.last_used_at;
return (
{lastUsedAt ? dayjs(lastUsedAt).format('MMM D, YYYY') : 'Never'}
);
},
enableSorting: false,
meta: { headerClassName: 'hidden w-28 @2xl:table-cell', cellClassName: 'hidden w-28 @2xl:table-cell' },
},
{
id: 'created_at',
accessorFn: (apiKey) => apiKey.attributes.created_at,
header: ({ column }) => ,
cell: ({ row }) => (
),
enableSorting: true,
sortDescFirst: true,
meta: { headerClassName: 'hidden w-28 @3xl:table-cell', cellClassName: 'hidden w-28 @3xl:table-cell' },
},
actionsColumn(1, (apiKey) => ),
] satisfies ColumnDef[];