File size: 787 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"use client";
import { Agent } from "app-types/agent";
import useSWR, { SWRConfiguration } from "swr";
import { handleErrorWithToast } from "ui/shared-toast";
import { fetcher } from "lib/utils";

interface UseAgentOptions extends SWRConfiguration {
  enabled?: boolean;
}

export function useAgent(
  agentId: string | null | undefined,
  options: UseAgentOptions = {},
) {
  const { enabled = true, ...swrOptions } = options;

  const {
    data: agent,
    error,
    isLoading,
    mutate,
  } = useSWR<Agent>(
    agentId && enabled ? `/api/agent/${agentId}` : null,
    fetcher,
    {
      errorRetryCount: 0,
      revalidateOnFocus: false,
      onError: handleErrorWithToast,
      ...swrOptions,
    },
  );

  return {
    agent,
    isLoading,
    error,
    mutate,
  };
}