import { useEffect, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { httpClient } from '@/app/infra/http/HttpClient'; import { useCurrentWorkspace } from '@/app/infra/http'; import { Agent } from '@/app/infra/entities/api'; import { useSidebarData } from '@/app/home/components/home-sidebar/SidebarDataContext'; import ProcessorDetailWorkbench from '@/app/home/components/processor-detail/ProcessorDetailWorkbench'; import PipelineDetailContent from '@/app/home/pipelines/PipelineDetailContent'; import AgentCreateContent from './components/AgentCreateContent'; import AgentDebugPanel from './components/AgentDebugPanel'; import AgentFormComponent, { AgentFormHandle, AgentRunnerStatus, } from './components/AgentFormComponent'; export default function AgentDetailContent({ id }: { id: string }) { const isCreateMode = id === 'new'; const navigate = useNavigate(); const { t } = useTranslation(); const currentWorkspace = useCurrentWorkspace(); const canManage = currentWorkspace?.permissions.includes('resource.manage') ?? false; const canOperate = currentWorkspace?.permissions.includes('runtime.operate') ?? false; const { refreshPipelines, pipelines, setDetailEntityName } = useSidebarData(); const [agent, setAgent] = useState(null); const [loading, setLoading] = useState(!isCreateMode); const [formDirty, setFormDirty] = useState(false); const [formSaving, setFormSaving] = useState(false); const [runnerStatus, setRunnerStatus] = useState( null, ); const agentFormRef = useRef(null); useEffect(() => { if (isCreateMode) { setDetailEntityName(t('agents.create')); return () => setDetailEntityName(null); } const sidebarItem = pipelines.find((p) => p.id === id); setDetailEntityName(sidebarItem?.name ?? id); return () => setDetailEntityName(null); }, [id, isCreateMode, pipelines, setDetailEntityName, t]); useEffect(() => { setRunnerStatus(null); }, [id]); useEffect(() => { if (isCreateMode) return; let cancelled = false; setLoading(true); httpClient .getAgent(id) .then((resp) => { if (!cancelled) setAgent(resp.agent); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [id, isCreateMode]); if (isCreateMode) { return ( { refreshPipelines(); navigate(`/home/agents?id=${encodeURIComponent(newAgentId)}`); }} /> ); } if (loading || !agent) { return (
{t('common.loading')}
); } if (agent.kind === 'pipeline') { return ; } return ( { if (updatedAgent) { setAgent((current) => current ? { ...current, ...updatedAgent } : current, ); } refreshPipelines(); }} onDeleted={() => { refreshPipelines(); navigate('/home/agents'); }} onDirtyChange={setFormDirty} onSavingChange={setFormSaving} onRunnerStatusChange={setRunnerStatus} /> } debugTitle={canOperate ? t('agents.debugTab') : undefined} debugContent={ canOperate ? ( agentFormRef.current?.save() ?? false} onOpenRunnerConfig={() => agentFormRef.current?.openSection('runner_config') } supportedEventPatterns={ agent.supported_event_patterns ?? agent.capability?.supported_event_patterns ?? ['*'] } /> ) : undefined } unsavedLabel={t('pipelines.unsavedChanges')} /> ); }