EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
useAgentData.js
Go to the documentation of this file.
1import { useState, useEffect } from 'react';
2import { apiFetch } from '../lib/api';
3
4export function useAgentData(agentId) {
5 const [agent, setAgent] = useState(null);
6 const [loading, setLoading] = useState(false);
7 const [error, setError] = useState(null);
8
9 useEffect(() => {
10 if (!agentId) return;
11 setLoading(true);
12 setError(null);
13 apiFetch(`/agent/${agentId}`, {
14 credentials: 'include',
15 })
16 .then(res => {
17 if (!res.ok) throw new Error('Agent not found');
18 return res.json();
19 })
20 .then(data => {
21 setAgent(data);
22 setError(null);
23 })
24 .catch(err => {
25 setAgent(null);
26 setError(err.message || 'Failed to load agent');
27 })
28 .finally(() => setLoading(false));
29 }, [agentId]);
30
31 return { agent, loading, error };
32}