Skip to content

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

TypeScript
2.7 KB
Raw Blame
'use client'

import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import { AuthGate } from '@components/AuthGate'
import { CodexSubscriptionHub } from '@components/CodexSubscriptionHub'
import { DashboardLayout } from '@components/DashboardLayout'
import { SecretsTable } from '@components/SecretsTable'
import { CONTROL_ROUTES } from '@constants/routes'
import { apiGet, isSilentApiError } from '@lib/api'

export type SecretScope = 'llm' | 'mcp' | 'recipe'
export type LlmSecretsSubTab = 'api-key' | 'subscriptions'

type SecretItem = {
  name?: string
  metadata?: { name?: string }
  keys?: string[]
}

export function SecretsPageContent({
  activeScope,
  llmSubTab = 'api-key',
}: {
  activeScope: SecretScope
  llmSubTab?: LlmSecretsSubTab
}) {
  const router = useRouter()
  const [secrets, setSecrets] = useState<SecretItem[]>([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState('')

  async function loadAll() {
    setLoading(true)
    setError('')
    try {
      const response = (await apiGet('/api/v1/admin/secrets')) as { items?: SecretItem[] }
      setSecrets(response.items || [])
    } catch (nextError) {
      if (isSilentApiError(nextError)) return
      setError(nextError instanceof Error ? nextError.message : 'Failed to load secrets')
    } finally {
      setLoading(false)
    }
  }

  useEffect(() => {
    void loadAll()
  }, [])

  if (activeScope === 'llm' && llmSubTab === 'subscriptions') {
    return (
      <AuthGate>
        <DashboardLayout>
          <CodexSubscriptionHub />
        </DashboardLayout>
      </AuthGate>
    )
  }

  return (
    <AuthGate>
      <DashboardLayout>
        {error ? <div className="cu-banner cu-banner--error">{error}</div> : null}
        <SecretsTable
          activeScope={activeScope}
          items={secrets}
          onChanged={loadAll}
          onRefresh={loadAll}
          refreshing={loading}
          loading={loading}
          onCreateLlmSecret={() => router.push(CONTROL_ROUTES.secrets.new({ scope: 'llm' }))}
          onCreateMcpSecret={() => router.push(CONTROL_ROUTES.secrets.new({ scope: 'mcp' }))}
          onCreateRecipeSecret={() => router.push(CONTROL_ROUTES.secrets.new({ scope: 'recipe' }))}
          onCreateRecipeSecretFor={(name, keys, ownerRecipe, namespace) => {
            const params = new URLSearchParams({ scope: 'recipe', name })
            if (keys.length > 0) params.set('keys', keys.join(','))
            if (ownerRecipe) params.set('ownerRecipe', ownerRecipe)
            if (namespace) params.set('namespace', namespace)
            router.push(CONTROL_ROUTES.secrets.new(Object.fromEntries(params)))
          }}
        />
      </DashboardLayout>
    </AuthGate>
  )
}