import { useQuery, UseQueryOptions } from '@tanstack/react-query'; import axios, { parseAxiosError } from '@/portainer/services/axios/axios'; import { withGlobalError } from '@/react-tools/react-query'; import { Stack, StackId } from '../types'; import { queryKeys } from './query-keys'; import { buildStackUrl } from './buildUrl'; export function useStack( stackId?: StackId, queryOptions?: UseQueryOptions ) { return useQuery({ queryKey: queryKeys.stack(stackId), queryFn: () => getStack(stackId), enabled: !!stackId, ...withGlobalError('Unable to retrieve stack'), ...queryOptions, }); } async function getStack(stackId?: StackId) { if (!stackId) { throw new Error('Stack ID is required'); } try { const { data } = await axios.get(buildStackUrl(stackId)); return data; } catch (e) { throw parseAxiosError(e, 'Unable to retrieve stack'); } }