import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query' import { get } from 'lib/common/fetch' import { API_URL } from 'lib/constants' import { useCallback } from 'react' import { Project } from 'types' import { projectKeys } from './keys' export type ProjectsVariables = { ref?: string } export async function getProjects(signal?: AbortSignal) { const data = await get(`${API_URL}/projects`, { signal }) if (data.error) throw data.error return data as Project[] } export type ProjectsData = Awaited> export type ProjectsError = unknown export const useProjectsQuery = ({ enabled = true, ...options }: UseQueryOptions = {}) => useQuery( projectKeys.list(), ({ signal }) => getProjects(signal), { enabled: enabled, ...options } ) export const useProjectsPrefetch = () => { const client = useQueryClient() return useCallback(() => { client.prefetchQuery(projectKeys.list(), ({ signal }) => getProjects(signal)) }, []) }