/**
* Access tokens card (mcp-ai-spec §6.2.3):
* token table (label, prefix, scope, created, last used, revoke), create modal
* with show-once secret, legacy WP-CLI token migration nag.
*/
import React, { useState } from 'react';
import { AlertTriangle, KeyRound, Loader2 } from 'lucide-react';
import type { McpCreatedToken, McpTokenInfo, McpTokenScope } from './types';
import { createToken, revokeToken } from './api';
import { McpCard } from './card';
import { ConfirmDialog, McpModal } from './modal';
import { CopyButton } from './copy-button';
import { Button } from '@/components/ui/button';
import { trackMcpTokenCreated, trackMcpTokenRevoked } from './tracking';
import { __ } from '../../lib/i18n';
function formatTs(ts: number): string {
if (!ts) return '—';
return new Date(ts * 1000).toLocaleString();
}
export function TokenManager({
tokens,
hasLegacyToken,
onTokensChanged,
onTokenCreated,
}: {
tokens: McpTokenInfo[];
hasLegacyToken: boolean;
/** Refetch tokens + status after any mutation. */
onTokensChanged: () => Promise
{__('Legacy token (set via WP-CLI) is active', 'llmagnet-llm-txt-generator')}
{__(
'It has full access and is stored in plaintext. Replace it with a managed token: create a named token above, update your clients, then delete the legacy one.',
'llmagnet-llm-txt-generator',
)}
{error}
{__('No tokens yet. Create one to connect an AI client.', 'llmagnet-llm-txt-generator')}
{tokens.map((token) => (
{__('Label', 'llmagnet-llm-txt-generator')}
{__('Token', 'llmagnet-llm-txt-generator')}
{__('Scope', 'llmagnet-llm-txt-generator')}
{__('Created', 'llmagnet-llm-txt-generator')}
{__('Last used', 'llmagnet-llm-txt-generator')}
{__('Actions', 'llmagnet-llm-txt-generator')}
))}
{token.label}
{token.prefix}…
{formatTs(token.created_at)}
{token.last_used
? formatTs(token.last_used)
: __('Never', 'llmagnet-llm-txt-generator')}
{revokeTarget.prefix}…){' '}
{__(
'will immediately lose access. This cannot be undone.',
'llmagnet-llm-txt-generator',
)}
{__('Copy your new token now —', 'llmagnet-llm-txt-generator')}{' '} {__("you won't be able to see it again.", 'llmagnet-llm-txt-generator')}
{created.label} ·{' '} {created.scope === 'write' ? __('Read-write', 'llmagnet-llm-txt-generator') : __('Read', 'llmagnet-llm-txt-generator')}{' '} {__('scope · stored hashed on the server.', 'llmagnet-llm-txt-generator')}