1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 05:19:39 +02:00
portainer/app/react/kubernetes/namespaces/queries/useNamespacesQuery.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.4 KiB
TypeScript
Raw Normal View History

import { useQuery } from '@tanstack/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { withGlobalError } from '@/react-tools/react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { PortainerNamespace } from '../types';
import { queryKeys } from './queryKeys';
export function useNamespacesQuery<T = PortainerNamespace[]>(
environmentId: EnvironmentId,
options?: {
autoRefreshRate?: number;
withResourceQuota?: boolean;
select?: (namespaces: PortainerNamespace[]) => T;
}
) {
return useQuery(
queryKeys.list(environmentId, {
withResourceQuota: !!options?.withResourceQuota,
}),
async () => getNamespaces(environmentId, options?.withResourceQuota),
{
...withGlobalError('Unable to get namespaces.'),
refetchInterval() {
return options?.autoRefreshRate ?? false;
},
select: options?.select,
}
);
}
// getNamespaces is used to retrieve namespaces using the Portainer backend with caching
export async function getNamespaces(
environmentId: EnvironmentId,
withResourceQuota?: boolean
) {
const params = withResourceQuota ? { withResourceQuota } : {};
try {
const { data: namespaces } = await axios.get<PortainerNamespace[]>(
`kubernetes/${environmentId}/namespaces`,
{ params }
);
return namespaces;
} catch (e) {
throw parseAxiosError(e, 'Unable to retrieve namespaces');
}
}