UNPKG

plazbot

Version:

Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.

35 lines (34 loc) 1.14 kB
import { useState, useEffect } from 'react'; import { usePlazbotContext } from '../context/PlazbotContext'; export function useAgent() { const { sdk, agentId } = usePlazbotContext(); const [agent, setAgent] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [fetchCount, setFetchCount] = useState(0); useEffect(() => { let cancelled = false; setIsLoading(true); setError(null); sdk.agent .getAgentById({ id: agentId }) .then((data) => { if (!cancelled) setAgent(data); }) .catch((err) => { if (!cancelled) { setError(err instanceof Error ? err.message : 'Error al cargar agente'); } }) .finally(() => { if (!cancelled) setIsLoading(false); }); return () => { cancelled = true; }; }, [sdk, agentId, fetchCount]); const refetch = () => setFetchCount((c) => c + 1); return { agent, isLoading, error, refetch }; }