2024-04-14 14:54:25 +00:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2024-10-01 01:15:51 +00:00
|
|
|
import { withGlobalError } from '@/react-tools/react-query';
|
2023-10-11 19:32:02 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
2022-11-07 06:03:11 +00:00
|
|
|
|
2024-10-01 01:15:51 +00:00
|
|
|
import { PortainerNamespace } from '../types';
|
|
|
|
|
|
|
|
import { queryKeys } from './queryKeys';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
export function useNamespacesQuery(
|
2023-06-11 21:46:48 +00:00
|
|
|
environmentId: EnvironmentId,
|
2024-10-01 01:15:51 +00:00
|
|
|
options?: { autoRefreshRate?: number; withResourceQuota?: boolean }
|
2023-06-11 21:46:48 +00:00
|
|
|
) {
|
2022-09-21 04:49:42 +00:00
|
|
|
return useQuery(
|
2024-10-01 01:15:51 +00:00
|
|
|
queryKeys.list(environmentId, {
|
|
|
|
withResourceQuota: !!options?.withResourceQuota,
|
|
|
|
}),
|
|
|
|
async () => getNamespaces(environmentId, options?.withResourceQuota),
|
2022-09-21 04:49:42 +00:00
|
|
|
{
|
2024-10-01 01:15:51 +00:00
|
|
|
...withGlobalError('Unable to get namespaces.'),
|
2023-06-11 21:46:48 +00:00
|
|
|
refetchInterval() {
|
|
|
|
return options?.autoRefreshRate ?? false;
|
2022-09-21 04:49:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-11 19:32:02 +00:00
|
|
|
// getNamespaces is used to retrieve namespaces using the Portainer backend with caching
|
2024-10-01 01:15:51 +00:00
|
|
|
export async function getNamespaces(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
withResourceQuota?: boolean
|
|
|
|
) {
|
|
|
|
const params = withResourceQuota ? { withResourceQuota } : {};
|
2023-10-11 19:32:02 +00:00
|
|
|
try {
|
2024-10-01 01:15:51 +00:00
|
|
|
const { data: namespaces } = await axios.get<PortainerNamespace[]>(
|
|
|
|
`kubernetes/${environmentId}/namespaces`,
|
|
|
|
{ params }
|
2023-10-11 19:32:02 +00:00
|
|
|
);
|
|
|
|
return namespaces;
|
|
|
|
} catch (e) {
|
2024-10-01 01:15:51 +00:00
|
|
|
throw parseAxiosError(e, 'Unable to retrieve namespaces');
|
2023-10-11 19:32:02 +00:00
|
|
|
}
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|