Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

3.4 KB
Raw Blame
import { useContext } from 'react';
import { dataService, QueryKeys } from 'librechat-data-provider';
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
import type {
  ProjectAvailableFilesParams,
  ProjectAvailableFilesResponse,
  ProjectListParams,
  ProjectListResponse,
  TChatProject,
  TChatProjectFile,
} from 'librechat-data-provider';
import type {
  UseInfiniteQueryOptions,
  QueryObserverResult,
  UseQueryOptions,
} from '@tanstack/react-query';

import { ProjectNamesContext } from '~/Providers/ProjectNamesContext';

export const useProjectsInfiniteQuery = (
  params: ProjectListParams = {},
  config?: UseInfiniteQueryOptions<ProjectListResponse, unknown>,
) => {
  const { sortBy, sortDirection, search, limit } = params;

  return useInfiniteQuery<ProjectListResponse>({
    queryKey: [QueryKeys.projects, { sortBy, sortDirection, search, limit }],
    queryFn: ({ pageParam }) =>
      dataService.listProjects({
        sortBy,
        sortDirection,
        search,
        limit,
        cursor: pageParam?.toString(),
      }),
    getNextPageParam: (lastPage) => lastPage?.nextCursor ?? undefined,
    keepPreviousData: true,
    staleTime: 5 * 60 * 1000,
    cacheTime: 30 * 60 * 1000,
    ...config,
  });
};

export const useProjectQuery = (
  projectId?: string | null,
  config?: UseQueryOptions<TChatProject>,
): QueryObserverResult<TChatProject, unknown> => {
  return useQuery<TChatProject>(
    [QueryKeys.project, projectId],
    () => dataService.getProjectById(projectId ?? ''),
    {
      enabled: Boolean(projectId),
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,
      refetchOnMount: false,
      ...config,
    },
  );
};
/**
 * A project's name for a row that only carries its id. The sidebar already holds the
 * recent projects, so a name found there costs no request; only a project past that
 * list is fetched, once per id. A record written by id (a rename) still wins.
 */
export const useProjectName = (projectId?: string | null): string | undefined => {
  const listed = useContext(ProjectNamesContext);
  const listedName = projectId ? listed?.names.get(projectId) : undefined;
  const { data: project } = useProjectQuery(projectId, {
    enabled: Boolean(projectId) && !listed?.isPending && listedName == null,
  });
  return project?.name ?? listedName;
};
export const useProjectFilesQuery = (
  projectId?: string | null,
  config?: UseQueryOptions<TChatProjectFile[]>,
): QueryObserverResult<TChatProjectFile[], unknown> => {
  return useQuery<TChatProjectFile[]>(
    [QueryKeys.projectFiles, projectId],
    () => dataService.getProjectFiles(projectId ?? ''),
    {
      enabled: Boolean(projectId),
      refetchOnWindowFocus: false,
      ...config,
    },
  );
};
export const useProjectAvailableFilesInfiniteQuery = (
  projectId?: string | null,
  params: ProjectAvailableFilesParams = {},
  config?: UseInfiniteQueryOptions<ProjectAvailableFilesResponse, unknown>,
) => {
  const queryParams = { limit: params.limit, search: params.search };

  return useInfiniteQuery<ProjectAvailableFilesResponse>({
    queryKey: [QueryKeys.projectAvailableFiles, projectId, queryParams],
    queryFn: ({ pageParam }) =>
      dataService.getAvailableProjectFiles(projectId ?? '', {
        ...queryParams,
        cursor: pageParam?.toString(),
      }),
    getNextPageParam: (lastPage) => lastPage?.nextCursor ?? undefined,
    enabled: Boolean(projectId),
    refetchOnWindowFocus: false,
    ...config,
  });
};