import { Button, CopyButton, Group, Radio, Stack, Text, TextInput } from '@mantine/core'; import { IconCheck, IconCopy, IconKey } from '@/components/icons'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useEffect, useRef, useState, type FormEvent } from 'react'; import { useLocation } from 'react-router-dom'; import { sendJson } from '../api/client'; import { ErrorAlert, fmtTs } from '../components/common'; import { shortWhen } from '../domain/runName'; import { isActive, scopeWords, TOKENS_KEY, useAccessTokens, type AccessToken, type CreatedToken } from './useAccessTokens'; import './account-tokens.css'; /** One of your tokens: what it allows, when it was last used, when it expires, and Revoke (asked twice). */ function TokenRow({ t }: { t: AccessToken }) { const qc = useQueryClient(); const [sure, setSure] = useState(false); const revoke = useMutation({ mutationFn: () => sendJson('DELETE', `/api/tokens/${t.id}`), onSuccess: () => { setSure(false); void qc.invalidateQueries({ queryKey: TOKENS_KEY }); }, }); const active = isActive(t); return (
  • {t.name} {scopeWords(t)} · ends in …{t.last4} {t.revoked_at ? `Revoked ${shortWhen(t.revoked_at)}` : active ? `Expires ${shortWhen(t.expires_at)}` : `Expired ${shortWhen(t.expires_at)}`} {' · '}{t.last_used_at ? Last used {shortWhen(t.last_used_at)} : 'Never used'}
    {active && (!sure ? ( ) : ( Assistants using it lose access. ))} {revoke.error ?
    : null}
  • ); } /** The new token, once: a labelled, copyable field and a clear warning. It lives only in this component's memory. */ function ShownOnce({ created, onDone }: { created: CreatedToken; onDone: () => void }) { return (
    Copy it now: it won't be shown again. e.currentTarget.select()} spellCheck={false} autoComplete="off" /> {({ copied, copy }) => ( )} Expires {fmtTs(created.expires_at)}. Paste it into your assistant's settings, then close this.
    ); } /** Account → Access tokens for AI assistants: create a token (shown once), see and revoke your own. */ export function AccessTokensCard() { const qc = useQueryClient(); const tokens = useAccessTokens(); const [name, setName] = useState(''); const [scopes, setScopes] = useState<'read' | 'read,act'>('read'); const [days, setDays] = useState('90'); const [created, setCreated] = useState(null); const create = useMutation({ mutationFn: () => sendJson('POST', '/api/tokens', { name: name.trim(), scopes, days: Number(days) }), onSuccess: (r) => { setCreated(r); setName(''); void qc.invalidateQueries({ queryKey: TOKENS_KEY }); }, }); const onSubmit = (e: FormEvent) => { e.preventDefault(); if (name.trim()) create.mutate(); }; const items = tokens.data?.items ?? []; const loc = useLocation(); const ref = useRef(null); // arriving from Integrations ("/account#tokens"): bring the card into view useEffect(() => { if (loc.hash === '#tokens') ref.current?.scrollIntoView?.({ block: 'start' }); }, [loc.hash]); return (

    Access tokens for AI assistants

    A token lets Microsoft Copilot, Claude, ChatGPT or another assistant use Astra as you, with your permissions. Anything that changes something, such as starting a run, still asks you to confirm. Sign-off stays in Astra. {created && setCreated(null)} />}

    Your tokens

    {tokens.isLoading ? Loading your tokens… : items.length === 0 ? You have no tokens yet. :
      {items.map((t) => )}
    }

    Create a token

    setName(e.currentTarget.value)} maxLength={60} /> setScopes(v as 'read' | 'read,act')}>
    ); }