/* Copyright (C) 2023-2026 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { KeyRound, Plus } from 'lucide-react' import { useMemo, useState, type ReactNode } from 'react' import { useTranslation } from 'react-i18next' import { ConfirmDialog } from '@/components/confirm-dialog' import { EmptyState } from '@/components/empty-state' import { ErrorState } from '@/components/error-state' import { LoadingState } from '@/components/loading-state' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card' import { Separator } from '@/components/ui/separator' import { Sheet, SheetClose, SheetContent, SheetDescription, SheetHeader, SheetTitle, } from '@/components/ui/sheet' import { SecureVerificationDialog } from '@/features/auth/secure-verification' import { AuditLogViewer } from '@/features/usage-logs/audit/components/audit-log-viewer' import { toIntlLocale } from '@/i18n/languages' import type { AccessTokenItem as AccessToken } from '../api' import { useAccessTokens } from '../hooks/use-access-tokens' import { AccessTokenItem } from './access-token-item' import { AccessTokenCreateDialog } from './dialogs/access-token-create-dialog' import { AccessTokenDialog } from './dialogs/access-token-dialog' import { AccessTokenEditDialog } from './dialogs/access-token-edit-dialog' export function AccessTokensCard() { const { t, i18n } = useTranslation() const access = useAccessTokens() const locale = toIntlLocale(i18n.resolvedLanguage || i18n.language) const timeFormat = useMemo( () => new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', }), [locale] ) const formatTime = (seconds: number) => timeFormat.format(new Date(seconds * 1000)) const [createOpen, setCreateOpen] = useState(false) const [revokeTarget, setRevokeTarget] = useState< AccessToken | 'legacy' | null >(null) const [editTarget, setEditTarget] = useState(null) // undefined shows every access record; a token ref opens the viewer on it. const [records, setRecords] = useState<{ tokenRef?: string } | null>(null) const catalog = access.catalog.data const list = access.list.data const resourceLabels = useMemo( () => new Map( catalog?.groups.flatMap((group) => group.resources.map( (resource) => [resource.resource, resource.label_key] as const ) ) ?? [] ), [catalog] ) const atLimit = !!list && !!catalog && list.items.length >= catalog.max_tokens const now = Date.now() const confirmRevoke = async () => { const target = revokeTarget if (access.pending || !target) return setRevokeTarget(null) if (target === 'legacy') await access.revokeLegacy() else await access.revoke(target.id) } let content: ReactNode if (access.list.isPending) { content = (
) } else if (access.list.isError || !list) { content = (
void access.list.refetch()} />
) } else { const legacy = list.legacy content = (
{legacy && (
{t('Legacy token')} …{legacy.token_hint}

{legacy.created_at ? ( {t('Created {{time}}', { time: formatTime(legacy.created_at), })} ) : null} {legacy.last_used_at ? t('Last used {{time}}', { time: formatTime(legacy.last_used_at), }) : t('Never used')}

{t( 'The legacy token stops working on {{date}}. Create a new token to replace it.', { date: formatTime(legacy.retire_at) } )}

)} {list.items.length === 0 ? ( ) : (
{list.items.map((token, index) => (
{index > 0 && } setRecords({ tokenRef: target.token_ref }) } onRevoke={setRevokeTarget} />
))}
)}
) } return ( <> {t('Access tokens')} {t( 'Use access tokens to call the dashboard API from scripts. Each token only has the permissions you select.' )} {atLimit && catalog && (

{t('You can create up to {{count}} access tokens', { count: catalog.max_tokens, })}

)} {content}
{catalog && (