import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { Agent } from '../agents/types'; import userService from '../api/services/userService'; import CopyButton from '../components/CopyButton'; import Spinner from '../components/Spinner'; import { ActiveState } from '../models/misc'; import { selectToken } from '../preferences/preferenceSlice'; import WrapperModal from './WrapperModal'; const baseURL = import.meta.env.VITE_BASE_URL; type AgentDetailsModalProps = { agent: Agent; mode: 'new' | 'edit' | 'draft'; modalState: ActiveState; setModalState: (state: ActiveState) => void; }; export default function AgentDetailsModal({ agent, mode, modalState, setModalState, }: AgentDetailsModalProps) { const { t } = useTranslation(); const token = useSelector(selectToken); const [sharedToken, setSharedToken] = useState( agent.shared_token ?? null, ); const [apiKey, setApiKey] = useState(null); const [webhookUrl, setWebhookUrl] = useState(null); const [loadingStates, setLoadingStates] = useState({ publicLink: false, apiKey: false, webhook: false, }); const setLoading = ( key: 'publicLink' | 'apiKey' | 'webhook', state: boolean, ) => { setLoadingStates((prev) => ({ ...prev, [key]: state })); }; const handleGeneratePublicLink = async () => { setLoading('publicLink', true); const response = await userService.shareAgent( { id: agent.id ?? '', shared: true }, token, ); if (!response.ok) { setLoading('publicLink', false); return; } const data = await response.json(); setSharedToken(data.shared_token); setLoading('publicLink', false); }; const handleGenerateWebhook = async () => { setLoading('webhook', true); const response = await userService.getAgentWebhook(agent.id ?? '', token); if (!response.ok) { setLoading('webhook', false); return; } const data = await response.json(); setWebhookUrl(data.webhook_url); setLoading('webhook', false); }; useEffect(() => { setSharedToken(agent.shared_token ?? null); setApiKey(agent.key ?? null); }, [agent]); if (modalState !== 'ACTIVE') return null; return ( { setModalState('INACTIVE'); }} >

{t('modals.agentDetails.title')}

{t('modals.agentDetails.publicLink')}

{sharedToken ? ( ) : ( )}

{t('modals.agentDetails.apiKey')}

{apiKey ? (
{apiKey} {!apiKey.includes('...') && ( )}
{!apiKey.includes('...') && ( {t('modals.agentDetails.test')} External link )}
) : ( )}

{t('modals.agentDetails.webhookUrl')}

{webhookUrl ? ( ) : ( )}
); }