import { ActionIcon, Badge, Button, Card, CopyButton, Group, Stack, Text, Tooltip, } from '@mantine/core'; import { modals } from '@mantine/modals'; import { DateTime } from 'luxon'; import { useTranslation } from 'react-i18next'; import { TbCheck, TbCopy, TbPlus, TbTrash } from 'react-icons/tb'; import { useApiTokens } from '~/hooks/use_api_tokens'; import { CreateTokenModal } from './create_token_modal'; export function ApiTokens() { const { t } = useTranslation(); const { tokens, createToken, revokeToken } = useApiTokens(); const handleCreateTokenModal = () => { modals.open({ title: t('api-tokens.create-new'), children: ( createToken(name)} onClose={() => modals.closeAll()} /> ), }); }; const handleRevokeToken = async (tokenId: number, tokenName: string) => { modals.openConfirmModal({ title: t('api-tokens.revoke'), children: ( {t('api-tokens.confirm-revoke')} {tokenName}? ), labels: { confirm: t('api-tokens.revoke'), cancel: t('common.cancel'), }, confirmProps: { color: 'red' }, onConfirm: () => revokeToken(tokenId), }); }; const formatDate = (dateString: string | null) => { if (!dateString) return t('api-tokens.never'); return DateTime.fromISO(dateString).toRelative(); }; const isExpired = (expiresAt: string | null) => { if (!expiresAt) return false; return DateTime.fromISO(expiresAt) < DateTime.now(); }; return ( {t('api-tokens.title')} {tokens.length === 0 ? ( {t('api-tokens.no-tokens')} ) : ( tokens.map((token) => ( {token.name} {isExpired(token.expiresAt) && ( {t('api-tokens.expired')} )} {!token.isActive && ( {t('api-tokens.revoked')} )} {t('api-tokens.created')}: {formatDate(token.createdAt)} {t('api-tokens.last-used')}: {formatDate(token.lastUsedAt)} {token.expiresAt && ( {t('api-tokens.expires')}: {formatDate(token.expiresAt)} )} {token.isActive && ( {({ copied, copy }) => ( {copied ? ( ) : ( )} )} handleRevokeToken(token.id, token.name)} > )} )) )} ); }