Skip to content

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

TypeScript
3.3 KB
Raw Blame
'use client'

import React, { useEffect, useRef, useState } from 'react'
import { useRouter } from 'next/navigation'
import { AuthGate } from '@components/AuthGate'
import { CreateFlowSkeleton } from '@components/CreateFlowSkeleton'
import { CreatePageHeader } from '@components/CreatePageHeader'
import { DashboardLayout } from '@components/DashboardLayout'
import { HostWizard } from '@components/HostWizard'
import { IconRobot } from '@components/Sidebar/icons'
import { CREATE_FLOW_LOADING } from '@constants/createFlowLoading'
import { CONTROL_ROUTES } from '@constants/routes'
import { apiGet } from '@lib/api'

type ApiList = {
  items?: Array<Record<string, unknown>>
}

export default function CreateHostPage() {
  const router = useRouter()
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState('')
  const [mcpServers, setMcpServers] = useState<Array<Record<string, unknown>>>([])
  const [secrets, setSecrets] = useState<Array<Record<string, unknown>>>([])

  useEffect(() => {
    async function loadFormData() {
      setLoading(true)
      setError('')
      try {
        const [mcpResponse, secretResponse] = await Promise.all([
          apiGet('/api/v1/admin/mcp-servers') as Promise<ApiList>,
          apiGet('/api/v1/admin/secrets') as Promise<ApiList>,
        ])
        setMcpServers(mcpResponse.items || [])
        setSecrets(secretResponse.items || [])
      } catch (loadError) {
        setError(
          loadError instanceof Error ? loadError.message : 'Failed to load create-agent data'
        )
      } finally {
        setLoading(false)
      }
    }

    void loadFormData()
  }, [])

  // TASK-229: land on the new agent's detail page after creation — its header
  // shows the agent's name plus the route URL, instead of dumping the operator
  // back on the list where the new agent has to be found again.
  const createdAgentRef = useRef<string | null>(null)
  const handleCreated = async (created?: { name: string }) => {
    if (!created?.name) return
    createdAgentRef.current = created.name
    router.push(CONTROL_ROUTES.agents.tab(created.name, 'overview'))
  }

  const backToAgents = () => {
    // After a successful create the wizard still calls onClose on its way out;
    // the forward navigation above already happened, so this must not override
    // it with a backward jump to the list.
    if (createdAgentRef.current) return
    router.push(CONTROL_ROUTES.agents.root)
  }

  return (
    <AuthGate>
      <DashboardLayout isDetailPage>
        {error ? <div className="cu-banner cu-banner--error">{error}</div> : null}
        {loading ? (
          <CreateFlowSkeleton
            {...CREATE_FLOW_LOADING.createAgent}
            onBack={backToAgents}
            backDisabled={false}
          />
        ) : (
          <HostWizard
            mode="page"
            mcpServers={mcpServers as any}
            existingSecrets={secrets as any}
            onCreated={handleCreated}
            onClose={backToAgents}
            pageHeader={
              <CreatePageHeader
                icon={<IconRobot />}
                title="Create agent"
                subtitle="Provision a new agent with connectors, credentials, and access."
                backLabel="Back to agents"
                onBack={backToAgents}
              />
            }
          />
        )}
      </DashboardLayout>
    </AuthGate>
  )
}