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';
import Translate from '@/components/elements/Translate';
import { useTranslation, type Translation } from '@/sdk/localization';
const grantedCount = (apiKey: AdminApiKey): number =>
API_KEY_RESOURCES.filter((resource) => permissionValue(apiKey, resource) > 0).length;
const ApiKeyActionsCell = ({ apiKey }: { apiKey: AdminApiKey }) => {
const { t } = useTranslation();
const deleteApiKey = useDeleteAdminApiKey();
const isSubmitting = deleteApiKey.isPending;
return (
(
)}
onConfirmed={(_event, close) =>
deleteApiKey.mutate(deleteAdminApiKeyInput(apiKey.attributes.identifier), { onSuccess: close })
}
>
}}
/>
);
};
export const apiKeyColumns = (t: Translation['t']) =>
[
{
id: 'memo',
accessorFn: (apiKey) => apiKey.attributes.memo,
header: ({ column }) => ,
cell: ({ row }) => (
{row.original.attributes.memo || t('No description provided')}
{row.original.attributes.identifier}
),
enableSorting: true,
meta: { headerClassName: 'min-w-48', cellClassName: 'min-w-48' },
},
{
id: 'permissions',
header: t('Permissions'),
cell: ({ row }) => (
{t('{{count}} granted', { count: grantedCount(row.original) })}
),
enableSorting: false,
meta: { headerClassName: 'hidden w-28 @md:table-cell', cellClassName: 'hidden w-28 @md:table-cell' },
},
{
id: 'created_by',
header: t('Created by'),
cell: ({ row }) => {
const createdBy = row.original.attributes.created_by;
return createdBy ? (
{createdBy.username}
) : (
{t('Unknown')}
);
},
enableSorting: false,
meta: { headerClassName: 'hidden w-32 @xl:table-cell', cellClassName: 'hidden w-32 @xl:table-cell' },
},
{
id: 'last_used_at',
header: t('Last used'),
cell: ({ row }) => {
const lastUsedAt = row.original.attributes.last_used_at;
return (
{lastUsedAt ? dayjs(lastUsedAt).format('ll') : t('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[];