Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
39.8 KB
Raw Blame
'use client'

import React, { useEffect, useMemo, useState } from 'react'
import { useRouter } from 'next/navigation'
import {
  DataTable,
  TableHeaderCell,
  TableStateRow,
  TableViewport,
  useTableSort,
} from '@clerum/frontend-components'
import { CONTROL_ROUTES } from '@constants/routes'
import {
  type RecipeSecretItem,
  type SecretIdentity,
  apiSend,
  deleteMcpSecret,
  deleteRecipeSecret,
  getMcpServers,
  getRecipeSecrets,
  getRecipes,
  listMcpSecrets,
} from '../lib/api'
import { getProviderLabel, getProvidersWithCompleteCredentials } from '../lib/llm'
import { collectWorkflowRecipeSecretRefs } from '../lib/workflowRecipeSecretRefs'
import { useConfirmDialog } from './ConfirmDialog'
import { LlmProviderIcon } from './LlmProviderIcon'
import { RowActionsMenu } from './RowActionsMenu'
import { SecretsScopeTabs } from './SecretsScopeTabs'
import { SectionSearchInput } from './SectionSearchInput'
import { IconKey } from './Sidebar/icons'
import { TablePanelHeader } from './TablePanelHeader'
import { useToast } from './Toast'
import { IconRefresh } from './icons'

type SecretItem = {
  name?: string
  metadata?: { name?: string }
  // Data-key names already stored in the Secret (values are never returned by
  // the listing — spec R4.5.3). Lights up the "present" chips in edit mode.
  keys?: string[]
}

type SecretScope = 'llm' | 'mcp' | 'recipe'
type McpSecretRow = {
  name: string
  servers: string[]
  registryEntries: string[]
  registrySources: Array<{ name: string; version: string }>
  /** Live Secret identity for compare-and-delete; absent on older backends. */
  uid?: string
  resourceVersion?: string
  /** The provisioned listing confirmed this Secret exists. When false, the
   * row comes from a connector reference alone and "Add" creates it. */
  exists: boolean
}
type RecipeSecretStatus = 'provisioned' | 'missing'
type RecipeSecretRowOwnership =
  | { kind: 'shared' }
  | { kind: 'owner-recipe'; recipeName: string }
  | { kind: 'unlabeled' }
  | null // missing rows: not yet provisioned, ownership chosen at create time.
type RecipeSecretRow = {
  name: string
  namespace: string
  keys: string[]
  recipes: string[]
  status: RecipeSecretStatus
  ownership: RecipeSecretRowOwnership
  uid?: string
  resourceVersion?: string
}

const DEFAULT_RECIPE_SECRET_NAMESPACE = 'sandbox-recipes'

export function SecretsTable({
  activeScope = 'llm',
  items,
  onChanged,
  onRefresh,
  refreshing,
  loading,
  onCreateLlmSecret,
  onCreateMcpSecret,
  onCreateRecipeSecret,
  onCreateRecipeSecretFor,
}: {
  activeScope?: SecretScope
  items: SecretItem[]
  onChanged: () => Promise<void>
  onRefresh?: () => void
  refreshing?: boolean
  loading?: boolean
  onCreateLlmSecret: () => void
  onCreateMcpSecret: () => void
  onCreateRecipeSecret: () => void
  onCreateRecipeSecretFor: (
    name: string,
    keys: string[],
    ownerRecipe?: string,
    namespace?: string
  ) => void
}) {
  const router = useRouter()
  const { confirm, confirmDialog } = useConfirmDialog()
  const { showToast } = useToast()
  const [scope, setScopeState] = useState<SecretScope>(activeScope)
  useEffect(() => {
    setScopeState(activeScope)
  }, [activeScope])

  // LLM secrets state
  const [deletingName, setDeletingName] = useState<string | null>(null)
  const [error, setError] = useState('')
  const [llmSearchQuery, setLlmSearchQuery] = useState('')

  // connector secrets state
  const [mcpSearchQuery, setMcpSearchQuery] = useState('')
  const [mcpRows, setMcpRows] = useState<McpSecretRow[]>([])
  const [mcpLoading, setMcpLoading] = useState(false)
  const [mcpError, setMcpError] = useState('')
  const [mcpDeletingName, setMcpDeletingName] = useState<string | null>(null)

  // Recipe secrets state
  const [recipeSearchQuery, setRecipeSearchQuery] = useState('')
  const [recipeRows, setRecipeRows] = useState<RecipeSecretRow[]>([])
  const [recipeLoading, setRecipeLoading] = useState(false)
  const [recipeError, setRecipeError] = useState('')
  const [recipeDeletingName, setRecipeDeletingName] = useState<string | null>(null)

  const rows = useMemo(
    () =>
      items
        .map(item => String(item.name || item.metadata?.name || '').trim())
        .filter(Boolean)
        .sort((a, b) => a.localeCompare(b)),
    [items]
  )
  // Secret name -> stored data-key names, so the provider chips can report
  // which credentials each secret holds (names only, never values).
  const keysByName = useMemo(() => {
    const map = new Map<string, string[]>()
    for (const item of items) {
      const name = String(item.name || item.metadata?.name || '').trim()
      if (name) map.set(name, Array.isArray(item.keys) ? item.keys : [])
    }
    return map
  }, [items])
  const normalizedLlmSearch = llmSearchQuery.trim().toLowerCase()
  const filteredRows = useMemo(() => {
    if (!normalizedLlmSearch) return rows
    return rows.filter(name => {
      const providerNames = getProvidersWithCompleteCredentials(keysByName.get(name) ?? []).map(
        getProviderLabel
      )
      return [name, ...providerNames].join(' ').toLowerCase().includes(normalizedLlmSearch)
    })
  }, [keysByName, normalizedLlmSearch, rows])

  const normalizedMcpSearch = mcpSearchQuery.trim().toLowerCase()
  const filteredMcpRows = useMemo(() => {
    if (!normalizedMcpSearch) return mcpRows
    return mcpRows.filter(row => {
      return [row.name, ...row.servers, ...row.registryEntries]
        .join(' ')
        .toLowerCase()
        .includes(normalizedMcpSearch)
    })
  }, [mcpRows, normalizedMcpSearch])

  const normalizedRecipeSearch = recipeSearchQuery.trim().toLowerCase()
  const filteredRecipeRows = useMemo(() => {
    if (!normalizedRecipeSearch) return recipeRows
    return recipeRows.filter(row =>
      [row.name, ...row.keys, ...row.recipes]
        .join(' ')
        .toLowerCase()
        .includes(normalizedRecipeSearch)
    )
  }, [recipeRows, normalizedRecipeSearch])
  const llmSort = useTableSort<string, 'name' | 'providers'>({
    rows: filteredRows,
    defaultKey: 'name',
    identity: name => name,
    accessors: {
      name: name => name,
      providers: name =>
        getProvidersWithCompleteCredentials(keysByName.get(name) ?? [])
          .map(getProviderLabel)
          .join(', '),
    },
  })
  const mcpSort = useTableSort<McpSecretRow, 'name' | 'servers' | 'registry'>({
    rows: filteredMcpRows,
    defaultKey: 'name',
    identity: row => row.name,
    accessors: {
      name: row => row.name,
      servers: row => row.servers.join(', '),
      registry: row => row.registryEntries.join(', '),
    },
  })
  const recipeSort = useTableSort<RecipeSecretRow, 'name' | 'keys' | 'recipes'>({
    rows: filteredRecipeRows,
    defaultKey: 'name',
    identity: row => `${row.namespace}/${row.name}`,
    accessors: {
      name: row => `${row.namespace}/${row.name}`,
      keys: row => row.keys.join(', '),
      recipes: row => row.recipes.join(', '),
    },
  })

  function openUpdate(name: string) {
    router.push(CONTROL_ROUTES.secrets.editLlm(name))
  }

  async function loadMcpSecretReferences() {
    setMcpLoading(true)
    setMcpError('')
    try {
      // Server references carry the usage columns, but a Secret created
      // moments ago is referenced by no server yet — the provisioned
      // listing (names + keys only) keeps it visible immediately.
      const [result, provisioned] = await Promise.all([getMcpServers(), listMcpSecrets()])
      const usage = new Map<
        string,
        { servers: Set<string>; registrySources: Map<string, { name: string; version: string }> }
      >()
      const addSecret = (secretName: string) => {
        if (!usage.has(secretName)) {
          usage.set(secretName, { servers: new Set<string>(), registrySources: new Map() })
        }
        return usage.get(secretName)!
      }

      for (const item of result.items ?? []) {
        const metadata = (item.metadata ?? {}) as Record<string, unknown>
        const labels = (metadata.labels ?? {}) as Record<string, string>
        const annotations = (metadata.annotations ?? {}) as Record<string, string>
        const serverName = typeof metadata.name === 'string' ? metadata.name.trim() : ''
        const spec = (item.spec ?? {}) as Record<string, unknown>
        const envSecret = (spec.envSecret ?? {}) as Record<string, unknown>
        const secretName = typeof envSecret.name === 'string' ? envSecret.name : ''
        if (!secretName) continue

        const row = addSecret(secretName)
        if (serverName) row.servers.add(serverName)
        // catalog-id / catalog-version live in ANNOTATIONS (org-scoped names like
        // "@org/name" contain '@' and '/', illegal as label values). Fall back to
        // labels so connectors installed before this change still associate.
        const catalogId = annotations['clerum.io/catalog-id'] ?? labels['clerum.io/catalog-id']
        const catalogVersion =
          annotations['clerum.io/catalog-version'] ?? labels['clerum.io/catalog-version']
        if (catalogId && catalogVersion) {
          row.registrySources.set(`${catalogId}@${catalogVersion}`, {
            name: catalogId,
            version: catalogVersion,
          })
        }
      }

      for (const item of provisioned.items ?? []) {
        if (item.name) addSecret(item.name)
      }

      const identityByName = new Map(
        (provisioned.items ?? [])
          .filter(item => item.name && item.uid && item.resourceVersion)
          .map(item => [item.name, { uid: item.uid!, resourceVersion: item.resourceVersion! }])
      )
      const existsNames = new Set(
        (provisioned.items ?? []).map(item => item.name).filter(name => name)
      )

      const nextRows = Array.from(usage.entries())
        .map(([name, details]) => {
          const registrySources = Array.from(details.registrySources.values()).sort((a, b) =>
            `${a.name}@${a.version}`.localeCompare(`${b.name}@${b.version}`)
          )
          const identity = identityByName.get(name)
          return {
            name,
            servers: Array.from(details.servers).sort((a, b) => a.localeCompare(b)),
            registryEntries: registrySources.map(source => `${source.name}@${source.version}`),
            registrySources,
            exists: existsNames.has(name),
            ...(identity ? identity : {}),
          }
        })
        .sort((a, b) => a.name.localeCompare(b.name))

      setMcpRows(nextRows)
    } catch (e) {
      setMcpError(e instanceof Error ? e.message : 'Failed to load connector secret references')
    } finally {
      setMcpLoading(false)
    }
  }

  async function loadRecipeSecretsAndUsage() {
    setRecipeLoading(true)
    setRecipeError('')
    try {
      const [secretsResult, recipesResult] = await Promise.all([getRecipeSecrets(), getRecipes()])
      const secrets: RecipeSecretItem[] = secretsResult.items ?? []
      const provisionedNames = new Set(
        secrets.map(s => `${s.namespace || DEFAULT_RECIPE_SECRET_NAMESPACE}/${s.name}`)
      )

      // Scan all WorkflowRecipes for API-key style references. For each referenced
      // secret name, accumulate (a) which recipes reference it and (b) the union
      // of key names the recipes expect to be present.
      const usage = new Map<
        string,
        { name: string; namespace: string; recipes: Set<string>; declaredKeys: Set<string> }
      >()
      for (const recipe of recipesResult.items ?? []) {
        const meta = (recipe.metadata ?? {}) as Record<string, unknown>
        const recipeName = typeof meta.name === 'string' ? meta.name.trim() : ''
        if (!recipeName) continue
        const spec = (recipe.spec ?? {}) as Record<string, unknown>
        for (const ref of collectWorkflowRecipeSecretRefs(spec).values()) {
          const usageKey = `${ref.namespace}/${ref.secretName}`
          if (!usage.has(usageKey)) {
            usage.set(usageKey, {
              name: ref.secretName,
              namespace: ref.namespace,
              recipes: new Set<string>(),
              declaredKeys: new Set<string>(),
            })
          }
          const entry = usage.get(usageKey)!
          entry.recipes.add(recipeName)
          ref.keys.forEach(key => entry.declaredKeys.add(key))
        }
      }

      const provisionedRows: RecipeSecretRow[] = secrets.map(item => ({
        name: item.name,
        namespace: item.namespace || DEFAULT_RECIPE_SECRET_NAMESPACE,
        keys: Array.isArray(item.keys) ? item.keys : [],
        recipes: Array.from(
          usage.get(`${item.namespace || DEFAULT_RECIPE_SECRET_NAMESPACE}/${item.name}`)?.recipes ??
            []
        ).sort((a, b) => a.localeCompare(b)),
        status: 'provisioned',
        ownership: item.ownership ?? { kind: 'unlabeled' },
        uid: item.uid,
        resourceVersion: item.resourceVersion,
      }))

      const missingRows: RecipeSecretRow[] = Array.from(usage.entries())
        .filter(([key]) => !provisionedNames.has(key))
        .map(([, entry]) => ({
          name: entry.name,
          namespace: entry.namespace,
          keys: Array.from(entry.declaredKeys).sort((a, b) => a.localeCompare(b)),
          recipes: Array.from(entry.recipes).sort((a, b) => a.localeCompare(b)),
          status: 'missing',
          ownership: null,
        }))

      const nextRows = [...provisionedRows, ...missingRows].sort((a, b) => {
        // Surface missing rows first so they're not buried.
        if (a.status !== b.status) return a.status === 'missing' ? -1 : 1
        return `${a.namespace}/${a.name}`.localeCompare(`${b.namespace}/${b.name}`)
      })
      setRecipeRows(nextRows)
    } catch (e) {
      setRecipeError(e instanceof Error ? e.message : 'Failed to load recipe secrets')
    } finally {
      setRecipeLoading(false)
    }
  }

  useEffect(() => {
    if (scope === 'mcp') {
      void loadMcpSecretReferences()
    } else if (scope === 'recipe') {
      void loadRecipeSecretsAndUsage()
    }
  }, [scope])

  const isLlmInitialLoad = loading && rows.length === 0
  const isMcpInitialLoad = mcpLoading && mcpRows.length === 0
  const isRecipeInitialLoad = recipeLoading && recipeRows.length === 0
  const activeInitialLoad =
    scope === 'llm' ? isLlmInitialLoad : scope === 'mcp' ? isMcpInitialLoad : isRecipeInitialLoad
  const activeCount =
    scope === 'llm'
      ? filteredRows.length
      : scope === 'mcp'
        ? filteredMcpRows.length
        : filteredRecipeRows.length

  async function deleteSecret(name: string) {
    const ok = await confirm({
      title: 'Delete LLM Secret',
      message: `Delete LLM secret ${name}?`,
      confirmLabel: 'Delete',
      tone: 'danger',
    })
    if (!ok) return
    setDeletingName(name)
    setError('')
    try {
      await apiSend('DELETE', `/api/v1/admin/secrets/${encodeURIComponent(name)}`)
      showToast(`Secret ${name} deleted.`, { tone: 'success' })
      await onChanged()
    } catch (e) {
      setError(e instanceof Error ? e.message : 'Failed to delete secret')
    } finally {
      setDeletingName(null)
    }
  }

  function navigateToRecipeEdit(name: string, namespace: string) {
    const qs = new URLSearchParams({ namespace })
    router.push(CONTROL_ROUTES.secrets.editRecipe(name, Object.fromEntries(qs)))
  }

  async function deleteMcpSecretRow(row: McpSecretRow) {
    const { name, uid, resourceVersion } = row
    if (!uid || !resourceVersion) {
      setMcpError(
        `Secret ${name} has no current identity. Refresh the page and review the latest state before deleting it.`
      )
      return
    }
    const ok = await confirm({
      title: 'Delete connector Secret',
      message: `Delete connector secret ${name}? Connectors referencing it will fail until they are reconfigured.`,
      confirmLabel: 'Delete',
      tone: 'danger',
    })
    if (!ok) return
    setMcpDeletingName(name)
    setMcpError('')
    try {
      await deleteMcpSecret(name, { uid, resourceVersion } satisfies SecretIdentity)
      showToast(`Secret ${name} deleted.`, { tone: 'success' })
      await loadMcpSecretReferences()
    } catch (e) {
      setMcpError(e instanceof Error ? e.message : 'Failed to delete connector secret')
    } finally {
      setMcpDeletingName(null)
    }
  }

  async function deleteRecipeSecretRow(row: RecipeSecretRow) {
    const { name, namespace, uid, resourceVersion } = row
    if (!uid || !resourceVersion) {
      // Existing Secret deletes never use the legacy bodyless path. It is
      // reserved for the create form's rollback immediately after an older
      // create endpoint omitted identity fields.
      setRecipeError(
        `Secret ${name} has no current identity. Refresh the page and review the latest state before deleting it.`
      )
      return
    }
    const ok = await confirm({
      title: 'Delete Recipe Secret',
      message: `Delete recipe secret ${name} from ${namespace}?`,
      confirmLabel: 'Delete',
      tone: 'danger',
    })
    if (!ok) return
    const deleteKey = `${namespace}/${name}`
    setRecipeDeletingName(deleteKey)
    setRecipeError('')
    try {
      await deleteRecipeSecret(name, namespace, { uid, resourceVersion } satisfies SecretIdentity)
      showToast(`Secret ${name} deleted.`, { tone: 'success' })
      await loadRecipeSecretsAndUsage()
    } catch (e) {
      setRecipeError(e instanceof Error ? e.message : 'Failed to delete recipe secret')
    } finally {
      setRecipeDeletingName(null)
    }
  }

  return (
    <>
      <div className="cu-card cu-card--viewport-fill" style={{ marginBottom: '1.25rem' }}>
        <TablePanelHeader
          title={
            <>
              <IconKey />
              {activeInitialLoad ? 'Secrets' : `Secrets (${activeCount})`}
            </>
          }
          subtitle={
            activeScope === 'mcp'
              ? 'Create reusable connector Secrets here. Assign them when creating a connector or from its Credentials tab using Change assigned Secret.'
              : 'Manage LLM, connector, and recipe credentials in one place.'
          }
          primaryAction={
            <button
              type="button"
              className="cu-btn cu-btn--primary cu-btn--sm"
              onClick={() => {
                if (scope === 'llm') onCreateLlmSecret()
                else if (scope === 'mcp') onCreateMcpSecret()
                else onCreateRecipeSecret()
              }}
              disabled={activeInitialLoad}
            >
              {scope === 'llm'
                ? 'Add LLM secret'
                : scope === 'mcp'
                  ? 'Add connector secret'
                  : 'Add recipe secret'}
            </button>
          }
          refreshAction={
            <button
              type="button"
              className="cu-btn cu-btn--icon cu-btn--toolbar"
              onClick={() => {
                if (scope === 'llm') void onRefresh?.()
                else if (scope === 'mcp') void loadMcpSecretReferences()
                else void loadRecipeSecretsAndUsage()
              }}
              disabled={
                activeInitialLoad ||
                (scope === 'llm' ? refreshing : scope === 'mcp' ? mcpLoading : recipeLoading)
              }
              aria-label={
                scope === 'llm'
                  ? refreshing
                    ? 'Refreshing...'
                    : 'Reload LLM secrets'
                  : scope === 'mcp'
                    ? mcpLoading
                      ? 'Refreshing...'
                      : 'Reload connector secret references'
                    : recipeLoading
                      ? 'Refreshing...'
                      : 'Reload recipe secrets'
              }
            >
              <IconRefresh
                className={
                  (scope === 'llm' && refreshing) ||
                  (scope === 'mcp' && mcpLoading) ||
                  (scope === 'recipe' && recipeLoading)
                    ? 'cu-spin'
                    : undefined
                }
                width={18}
                height={18}
              />
            </button>
          }
          search={
            <SectionSearchInput
              value={
                scope === 'llm'
                  ? llmSearchQuery
                  : scope === 'mcp'
                    ? mcpSearchQuery
                    : recipeSearchQuery
              }
              onChange={value => {
                if (scope === 'llm') setLlmSearchQuery(value)
                else if (scope === 'mcp') setMcpSearchQuery(value)
                else setRecipeSearchQuery(value)
              }}
              placeholder="Search secrets"
              ariaLabel={
                scope === 'llm'
                  ? 'Search LLM secrets'
                  : scope === 'mcp'
                    ? 'Search connector secrets'
                    : 'Search recipe secrets'
              }
              disabled={activeInitialLoad}
            />
          }
        />

        <div className="cu-card__body cu-card__body--auto cu-secrets-strip">
          <SecretsScopeTabs activeValue={scope} />
        </div>

        {scope === 'llm' && error && (
          <div className="cu-card__body cu-card__body--auto cu-secrets-message-strip">
            <div className="cu-banner cu-banner--error">{error}</div>
          </div>
        )}

        {scope === 'mcp' && mcpError && (
          <div className="cu-card__body cu-card__body--auto cu-secrets-message-strip">
            <div className="cu-banner cu-banner--error">{mcpError}</div>
          </div>
        )}

        {scope === 'recipe' && recipeError && (
          <div className="cu-card__body cu-card__body--auto cu-secrets-message-strip">
            <div className="cu-banner cu-banner--error">{recipeError}</div>
          </div>
        )}

        {scope === 'llm' ? (
          <TableViewport className="cu-table-wrap">
            <DataTable className="eft-table cu-table cu-table--header-band">
              <thead>
                <tr>
                  <TableHeaderCell
                    activeDirection={llmSort.key === 'name' ? llmSort.direction : null}
                    label="Name"
                    onSort={() => llmSort.sortBy('name')}
                  />
                  <TableHeaderCell
                    activeDirection={llmSort.key === 'providers' ? llmSort.direction : null}
                    label="Providers"
                    onSort={() => llmSort.sortBy('providers')}
                  />
                  <th style={{ width: '8rem', textAlign: 'right' }} aria-label="Actions" />
                </tr>
              </thead>
              <tbody>
                {isLlmInitialLoad ? (
                  <TableStateRow colSpan={3} kind="loading" message="Loading LLM secrets…" />
                ) : error && filteredRows.length === 0 ? (
                  <TableStateRow colSpan={3} kind="error" message={error} />
                ) : filteredRows.length === 0 ? (
                  <TableStateRow
                    colSpan={3}
                    message={
                      normalizedLlmSearch
                        ? 'No LLM secrets match this search.'
                        : 'No LLM secrets found.'
                    }
                  />
                ) : (
                  llmSort.sortedRows.map(name => {
                    const providers = getProvidersWithCompleteCredentials(
                      keysByName.get(name) ?? []
                    )
                    return (
                      <tr key={name}>
                        <td>{name}</td>
                        <td>
                          {providers.length > 0 ? (
                            <div className="cu-chip-row" aria-label={`Providers for ${name}`}>
                              {providers.map(provider => {
                                const label = getProviderLabel(provider)
                                return (
                                  <span key={provider} className="cu-chip">
                                    <LlmProviderIcon provider={provider} label={label} />
                                    {label}
                                  </span>
                                )
                              })}
                            </div>
                          ) : (
                            <span style={{ color: 'var(--cu-text-soft)' }}>—</span>
                          )}
                        </td>
                        <td className="cu-table__cell-actions">
                          <div style={{ display: 'inline-flex', gap: '0.35rem' }}>
                            <RowActionsMenu
                              ariaLabel={`Actions for LLM secret ${name}`}
                              horizontalTrigger
                              actions={[
                                {
                                  key: 'update',
                                  label: 'Update',
                                  onClick: () => openUpdate(name),
                                },
                                {
                                  key: 'delete',
                                  label: deletingName === name ? 'Deleting…' : 'Delete',
                                  danger: true,
                                  disabled: deletingName === name,
                                  onClick: () => void deleteSecret(name),
                                },
                              ]}
                            />
                          </div>
                        </td>
                      </tr>
                    )
                  })
                )}
              </tbody>
            </DataTable>
          </TableViewport>
        ) : scope === 'mcp' ? (
          <TableViewport className="cu-table-wrap">
            <DataTable className="eft-table cu-table cu-table--header-band">
              <thead>
                <tr>
                  <TableHeaderCell
                    activeDirection={mcpSort.key === 'name' ? mcpSort.direction : null}
                    label="Name"
                    onSort={() => mcpSort.sortBy('name')}
                  />
                  <TableHeaderCell
                    activeDirection={mcpSort.key === 'servers' ? mcpSort.direction : null}
                    label="Attached connectors"
                    onSort={() => mcpSort.sortBy('servers')}
                    style={{ width: '36%' }}
                  />
                  <TableHeaderCell
                    activeDirection={mcpSort.key === 'registry' ? mcpSort.direction : null}
                    label="Marketplace Source"
                    onSort={() => mcpSort.sortBy('registry')}
                    style={{ width: '34%' }}
                  />
                  <th style={{ width: '8rem', textAlign: 'right' }} aria-label="Actions" />
                </tr>
              </thead>
              <tbody>
                {isMcpInitialLoad ? (
                  <TableStateRow colSpan={4} kind="loading" message="Loading connector secrets…" />
                ) : mcpError && filteredMcpRows.length === 0 ? (
                  <TableStateRow colSpan={4} kind="error" message={mcpError} />
                ) : filteredMcpRows.length === 0 ? (
                  <TableStateRow
                    colSpan={4}
                    message={
                      normalizedMcpSearch
                        ? 'No connector secrets match this search.'
                        : 'No connector secrets found.'
                    }
                  />
                ) : (
                  mcpSort.sortedRows.map(row => (
                    <tr key={row.name}>
                      <td>
                        {row.name === row.name.trim() ? (
                          row.name
                        ) : (
                          <code>{JSON.stringify(row.name)}</code>
                        )}
                      </td>
                      <td style={{ color: 'var(--cu-text-soft)', fontSize: '0.8125rem' }}>
                        {row.servers.length > 0
                          ? `${row.servers.length} server(s): ${row.servers.join(', ')}`
                          : 'Not yet attached to a connector.'}
                      </td>
                      <td style={{ color: 'var(--cu-text-soft)', fontSize: '0.8125rem' }}>
                        {row.registryEntries.length > 0
                          ? row.registryEntries.join(', ')
                          : 'Created manually or source unknown.'}
                      </td>
                      <td className="cu-table__cell-actions">
                        <RowActionsMenu
                          ariaLabel={`Actions for connector secret ${row.name}`}
                          horizontalTrigger
                          actions={[
                            // "Add" creates the Secret a connector references but
                            // that does not exist yet. An existing Secret must not
                            // go through the create flow — POST refuses duplicate
                            // names (409); its keys rotate through "Update".
                            ...(row.exists
                              ? []
                              : [
                                  {
                                    key: 'add',
                                    label: 'Add',
                                    disabled: row.name !== row.name.trim(),
                                    disabledReason:
                                      'The connector stores a non-canonical Secret name.',
                                    onClick: () => {
                                      const source =
                                        row.registrySources.length === 1
                                          ? row.registrySources[0]
                                          : undefined
                                      router.push(
                                        CONTROL_ROUTES.secrets.new({
                                          scope: 'mcp',
                                          name: row.name,
                                          registryEntry: source?.name,
                                          registryVersion: source?.version,
                                        })
                                      )
                                    },
                                  },
                                ]),
                            {
                              key: 'update',
                              label: 'Update',
                              onClick: () => {
                                router.push(
                                  CONTROL_ROUTES.secrets.editConnector(
                                    row.name,
                                    row.servers.length === 1
                                      ? { server: row.servers[0] }
                                      : undefined
                                  )
                                )
                              },
                            },
                            {
                              key: 'delete',
                              label: mcpDeletingName === row.name ? 'Deleting…' : 'Delete',
                              danger: true,
                              // deleteMcpSecret requires the Secret's live
                              // uid/resourceVersion as its CAS precondition.
                              // The provisioned listing carries it; without it
                              // (older backend) delete stays inert rather than
                              // issuing an unfenced delete.
                              disabled:
                                !row.uid || !row.resourceVersion || mcpDeletingName === row.name,
                              disabledReason:
                                'Refresh to load this Secret identity before deleting it.',
                              onClick: () => void deleteMcpSecretRow(row),
                            },
                          ]}
                        />
                      </td>
                    </tr>
                  ))
                )}
              </tbody>
            </DataTable>
          </TableViewport>
        ) : (
          <TableViewport className="cu-table-wrap">
            <DataTable className="eft-table cu-table cu-table--header-band">
              <thead>
                <tr>
                  <TableHeaderCell
                    activeDirection={recipeSort.key === 'name' ? recipeSort.direction : null}
                    label="Name"
                    onSort={() => recipeSort.sortBy('name')}
                  />
                  <TableHeaderCell
                    activeDirection={recipeSort.key === 'keys' ? recipeSort.direction : null}
                    label="Keys"
                    onSort={() => recipeSort.sortBy('keys')}
                    style={{ width: '32%' }}
                  />
                  <TableHeaderCell
                    activeDirection={recipeSort.key === 'recipes' ? recipeSort.direction : null}
                    label="Used by recipes"
                    onSort={() => recipeSort.sortBy('recipes')}
                    style={{ width: '32%' }}
                  />
                  <th style={{ width: '8rem', textAlign: 'right' }} aria-label="Actions" />
                </tr>
              </thead>
              <tbody>
                {isRecipeInitialLoad ? (
                  <TableStateRow colSpan={4} kind="loading" message="Loading recipe secrets…" />
                ) : recipeError && filteredRecipeRows.length === 0 ? (
                  <TableStateRow colSpan={4} kind="error" message={recipeError} />
                ) : filteredRecipeRows.length === 0 ? (
                  <TableStateRow
                    colSpan={4}
                    message={
                      normalizedRecipeSearch
                        ? 'No recipe secrets match this search.'
                        : 'No recipe secrets found.'
                    }
                  />
                ) : (
                  recipeSort.sortedRows.map(row => (
                    <tr key={`${row.status}:${row.namespace}:${row.name}`}>
                      <td>
                        <div
                          style={{ display: 'inline-flex', alignItems: 'center', gap: '0.5rem' }}
                        >
                          <span>{row.name}</span>
                          <span className="cu-chip" title={`Secret namespace: ${row.namespace}`}>
                            {row.namespace}
                          </span>
                          {row.status === 'missing' ? (
                            <span
                              className="cu-chip"
                              style={{
                                color: 'var(--cu-warn-text, var(--cu-text-soft))',
                                borderColor: 'var(--cu-warn-border, var(--cu-border-subtle))',
                              }}
                            >
                              Missing
                            </span>
                          ) : null}
                          {row.ownership?.kind === 'shared' ? (
                            <span className="cu-chip" title="Any recipe can reference this secret">
                              Shared
                            </span>
                          ) : null}
                          {row.ownership?.kind === 'owner-recipe' ? (
                            <span
                              className="cu-chip"
                              title={`Only ${row.ownership.recipeName} can reference this secret`}
                            >
                              Owner: {row.ownership.recipeName}
                            </span>
                          ) : null}
                          {row.ownership?.kind === 'unlabeled' ? (
                            <span
                              className="cu-chip"
                              style={{
                                color: 'var(--cu-warn-text, var(--cu-text-soft))',
                                borderColor: 'var(--cu-warn-border, var(--cu-border-subtle))',
                              }}
                              title="Secret has neither owner-recipe nor shared label — WRC will refuse to project it"
                            >
                              Unlabeled
                            </span>
                          ) : null}
                        </div>
                      </td>
                      <td style={{ color: 'var(--cu-text-soft)', fontSize: '0.8125rem' }}>
                        {row.keys.length > 0
                          ? row.keys.join(', ')
                          : row.status === 'missing'
                            ? 'No keys declared by recipe.'
                            : 'No keys defined.'}
                      </td>
                      <td style={{ color: 'var(--cu-text-soft)', fontSize: '0.8125rem' }}>
                        {row.recipes.length > 0
                          ? `${row.recipes.length} recipe(s): ${row.recipes.join(', ')}`
                          : 'Not yet referenced by a recipe.'}
                      </td>
                      <td className="cu-table__cell-actions">
                        {row.status === 'missing' ? (
                          <button
                            type="button"
                            className="cu-btn cu-btn--primary cu-btn--sm"
                            onClick={() =>
                              onCreateRecipeSecretFor(
                                row.name,
                                row.keys,
                                row.recipes.length === 1 ? row.recipes[0] : undefined,
                                row.namespace
                              )
                            }
                            aria-label={`Add recipe secret ${row.name}`}
                          >
                            Add
                          </button>
                        ) : (
                          <div style={{ display: 'inline-flex', gap: '0.35rem' }}>
                            <RowActionsMenu
                              ariaLabel={`Actions for recipe secret ${row.name}`}
                              horizontalTrigger
                              actions={[
                                {
                                  key: 'update',
                                  label: 'Update',
                                  onClick: () => navigateToRecipeEdit(row.name, row.namespace),
                                },
                                {
                                  key: 'delete',
                                  label:
                                    recipeDeletingName === `${row.namespace}/${row.name}`
                                      ? 'Deleting…'
                                      : 'Delete',
                                  danger: true,
                                  disabled: recipeDeletingName === `${row.namespace}/${row.name}`,
                                  // Pass the whole row, not (name, namespace): the delete is
                                  // fenced on the live object's uid + resourceVersion, which
                                  // only the row carries. Reducing it to two identifier strings
                                  // would drop the CAS precondition and let the request delete a
                                  // Secret that was replaced since the table was loaded.
                                  onClick: () => void deleteRecipeSecretRow(row),
                                },
                              ]}
                            />
                          </div>
                        )}
                      </td>
                    </tr>
                  ))
                )}
              </tbody>
            </DataTable>
          </TableViewport>
        )}

        {scope === 'recipe' && recipeRows.some(row => row.status === 'missing') ? (
          <div className="cu-card__body cu-card__body--auto cu-secrets-message-strip">
            <div className="cu-banner cu-banner--info">
              Some recipes reference secrets that don&apos;t exist yet. Click <strong>Add</strong>{' '}
              on a Missing row to provision the Secret in the namespace shown on that row.
            </div>
          </div>
        ) : null}
      </div>

      {confirmDialog}
    </>
  )
}