Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

2.1 KB
Raw Blame
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { getProxyBaseUrl, getGlobalLitellmHeaderName, deriveErrorMessage, handleError } from "@/components/networking";
import useAuthorized from "@/app/(dashboard)/hooks/useAuthorized";
import { canReadProjects } from "./projectAccess";
import { ProjectResponse, projectKeys } from "./useProjects";

// ── Fetch function ───────────────────────────────────────────────────────────

const fetchProjectDetails = async (accessToken: string, projectId: string): Promise<ProjectResponse> => {
  const baseUrl = getProxyBaseUrl();
  const url = `${baseUrl}/project/info?project_id=${encodeURIComponent(projectId)}`;

  const response = await fetch(url, {
    method: "GET",
    headers: {
      [getGlobalLitellmHeaderName()]: `Bearer ${accessToken}`,
      "Content-Type": "application/json",
    },
  });

  if (!response.ok) {
    const errorData = await response.json();
    const errorMessage = deriveErrorMessage(errorData);
    handleError(errorMessage);
    throw new Error(errorMessage);
  }

  return response.json();
};

// ── Hook ─────────────────────────────────────────────────────────────────────

export const useProjectDetails = (projectId?: string) => {
  const { accessToken, userRole } = useAuthorized();
  const queryClient = useQueryClient();

  return useQuery<ProjectResponse>({
    queryKey: projectKeys.detail(projectId!),
    queryFn: async () => fetchProjectDetails(accessToken!, projectId!),
    enabled: Boolean(accessToken && projectId) && canReadProjects(userRole),

    // Seed from the list cache when available
    initialData: () => {
      if (!projectId) return undefined;

      const projects = queryClient.getQueryData<ProjectResponse[]>(projectKeys.list({}));

      return projects?.find((p) => p.project_id === projectId);
    },
  });
};