Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

8.7 KB
Raw Blame
import { Key, Trash2, User, Shield } from 'lucide-react'
import { SCOPES_PREVIEW, useMcpTokensAdmin } from '../../../components/Admin/useMcpTokensAdmin'
import { useTranslation } from '../../../i18n'
import { useFormatDate } from '../../../hooks/useDateFormat'
import { MAdminCard } from './MAdminUi'
import MConfirmSheet from '../settings/MConfirmSheet'
import MSkeleton from '../../components/MSkeleton'

// Mobile MCP tokens admin section: OAuth sessions + long-lived MCP tokens, each
// as a card list with loading/empty states, expandable scope chips and a
// delete confirm sheet. Drop-in for the desktop AdminMcpTokensPanel — no props,
// the state machine lives in useMcpTokensAdmin, only the presentation is mobile.
export default function MAdminMcpTokensPanel() {
  const {
    sessions, sessionsLoading, tokens, tokensLoading, expandedScopes, toggleScopes,
    revokeConfirmId, setRevokeConfirmId, deleteConfirmId, setDeleteConfirmId, handleRevoke, handleDelete,
  } = useMcpTokensAdmin()
  const { t } = useTranslation()
  const { formatDate } = useFormatDate()

  return (
    <div className="space-y-3">
      {/* Section title */}
      <div>
        <div className="text-[0.875rem] font-extrabold text-m-ink">{t('admin.mcpTokens.title')}</div>
        <div className="mt-[2px] font-geist text-[0.625rem] leading-relaxed text-m-muted">
          {t('admin.mcpTokens.subtitle')}
        </div>
      </div>

      {/* OAuth Sessions */}
      <div>
        <div className="mb-2 px-1 font-geist text-[0.625rem] font-bold uppercase tracking-[0.06em] text-m-faint">
          {t('admin.oauthSessions.sectionTitle')}
        </div>
        <MAdminCard>
          {sessionsLoading ? (
            <MSkeleton variant="rows" count={3} framed={false} />
          ) : sessions.length === 0 ? (
            <div className="flex flex-col items-center justify-center gap-2 py-10">
              <Shield className="h-8 w-8 text-m-faint" />
              <p className="font-geist text-[0.6875rem] text-m-muted">{t('admin.oauthSessions.empty')}</p>
            </div>
          ) : (
            sessions.map((session, i) => {
              const expanded = expandedScopes.has(session.id)
              const visible = expanded ? session.scopes : session.scopes.slice(0, SCOPES_PREVIEW)
              const hidden = session.scopes.length - SCOPES_PREVIEW
              return (
                <div
                  key={session.id}
                  className={`flex items-start gap-2 py-[11px] ${i === 0 ? '' : 'border-t border-[color:var(--m-rowbr)]'}`}
                >
                  <div className="min-w-0 flex-1">
                    <div className="truncate text-[0.8125rem] font-bold text-m-ink">{session.client_name}</div>
                    <div className="mt-[3px] flex flex-wrap items-center gap-x-2 gap-y-[2px] font-geist text-[0.625rem] text-m-muted">
                      <span className="inline-flex items-center gap-1">
                        <User size={11} strokeWidth={2.2} className="flex-none" />
                        {session.username}
                      </span>
                      <span className="text-m-faint">·</span>
                      <span>{formatDate(new Date(session.created_at))}</span>
                    </div>
                    <div className="mt-[6px] flex flex-wrap gap-1">
                      {visible.map(scope => (
                        <span
                          key={scope}
                          className="inline-flex items-center rounded-md border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] px-1.5 py-0.5 font-mono text-[0.5625rem] text-m-muted"
                        >
                          {scope}
                        </span>
                      ))}
                      {!expanded && hidden > 0 && (
                        <button
                          type="button"
                          onClick={() => toggleScopes(session.id)}
                          className="inline-flex items-center rounded-md border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] px-1.5 py-0.5 font-geist text-[0.5625rem] font-bold text-m-ink"
                        >
                          +{hidden} more
                        </button>
                      )}
                      {expanded && hidden > 0 && (
                        <button
                          type="button"
                          onClick={() => toggleScopes(session.id)}
                          className="inline-flex items-center rounded-md border border-[color:var(--m-rowbr)] bg-[color:var(--m-ic)] px-1.5 py-0.5 font-geist text-[0.5625rem] font-bold text-m-ink"
                        >
                          show less
                        </button>
                      )}
                    </div>
                  </div>
                  <button
                    type="button"
                    onClick={() => setRevokeConfirmId(session.id)}
                    aria-label={t('common.delete')}
                    title={t('common.delete')}
                    className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-[color:color-mix(in_srgb,var(--m-st-danger)_12%,transparent)] text-[color:var(--m-st-danger)]"
                  >
                    <Trash2 size={14} strokeWidth={2.2} />
                  </button>
                </div>
              )
            })
          )}
        </MAdminCard>
      </div>

      {/* MCP Tokens */}
      <div>
        <div className="mb-2 px-1 font-geist text-[0.625rem] font-bold uppercase tracking-[0.06em] text-m-faint">
          {t('admin.mcpTokens.sectionTitle')}
        </div>
        <MAdminCard>
          {tokensLoading ? (
            <MSkeleton variant="rows" count={3} framed={false} />
          ) : tokens.length === 0 ? (
            <div className="flex flex-col items-center justify-center gap-2 py-10">
              <Key className="h-8 w-8 text-m-faint" />
              <p className="font-geist text-[0.6875rem] text-m-muted">{t('admin.mcpTokens.empty')}</p>
            </div>
          ) : (
            tokens.map((token, i) => (
              <div
                key={token.id}
                className={`flex items-start gap-2 py-[11px] ${i === 0 ? '' : 'border-t border-[color:var(--m-rowbr)]'}`}
              >
                <div className="min-w-0 flex-1">
                  <div className="truncate text-[0.8125rem] font-bold text-m-ink">{token.name}</div>
                  <div className="mt-[2px] font-mono text-[0.625rem] text-m-faint">{token.token_prefix}...</div>
                  <div className="mt-[3px] flex flex-wrap items-center gap-x-2 gap-y-[2px] font-geist text-[0.625rem] text-m-muted">
                    <span className="inline-flex items-center gap-1">
                      <User size={11} strokeWidth={2.2} className="flex-none" />
                      {token.username}
                    </span>
                    <span className="text-m-faint">·</span>
                    <span>{formatDate(new Date(token.created_at))}</span>
                    <span className="text-m-faint">·</span>
                    <span>
                      {token.last_used_at
                        ? formatDate(new Date(token.last_used_at))
                        : t('admin.mcpTokens.never')}
                    </span>
                  </div>
                </div>
                <button
                  type="button"
                  onClick={() => setDeleteConfirmId(token.id)}
                  aria-label={t('common.delete')}
                  title={t('common.delete')}
                  className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-[color:color-mix(in_srgb,var(--m-st-danger)_12%,transparent)] text-[color:var(--m-st-danger)]"
                >
                  <Trash2 size={14} strokeWidth={2.2} />
                </button>
              </div>
            ))
          )}
        </MAdminCard>
      </div>

      {/* Revoke OAuth session confirm */}
      <MConfirmSheet
        open={revokeConfirmId !== null}
        onClose={() => setRevokeConfirmId(null)}
        title={t('admin.oauthSessions.revokeTitle')}
        message={t('admin.oauthSessions.revokeMessage')}
        confirmLabel={t('common.delete')}
        cancelLabel={t('common.cancel')}
        danger
        onConfirm={() => revokeConfirmId !== null && handleRevoke(revokeConfirmId)}
      />

      {/* Delete MCP token confirm */}
      <MConfirmSheet
        open={deleteConfirmId !== null}
        onClose={() => setDeleteConfirmId(null)}
        title={t('admin.mcpTokens.deleteTitle')}
        message={t('admin.mcpTokens.deleteMessage')}
        confirmLabel={t('common.delete')}
        cancelLabel={t('common.cancel')}
        danger
        onConfirm={() => deleteConfirmId !== null && handleDelete(deleteConfirmId)}
      />
    </div>
  )
}