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