2024-04-14 17:54:25 +03:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2023-09-21 05:31:00 +03:00
|
|
|
import { Volume } from 'docker-types/generated/1.41';
|
|
|
|
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
|
|
|
|
import { queryKeys } from './query-keys';
|
2024-04-11 09:29:30 +03:00
|
|
|
import { buildUrl } from './build-url';
|
2023-09-21 05:31:00 +03:00
|
|
|
|
|
|
|
export function useVolumes<T = Volume[]>(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
{ select }: { select?: (data: Volume[]) => T } = {}
|
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
queryKeys.base(environmentId),
|
|
|
|
() => getVolumes(environmentId),
|
|
|
|
{ select }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface VolumesResponse {
|
|
|
|
Volumes: Volume[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getVolumes(environmentId: EnvironmentId) {
|
|
|
|
try {
|
2024-04-11 09:29:30 +03:00
|
|
|
const { data } = await axios.get<VolumesResponse>(buildUrl(environmentId));
|
2023-09-21 05:31:00 +03:00
|
|
|
|
|
|
|
return data.Volumes;
|
|
|
|
} catch (error) {
|
2024-05-13 15:34:00 +12:00
|
|
|
throw parseAxiosError(error, 'Unable to retrieve volumes');
|
2023-09-21 05:31:00 +03:00
|
|
|
}
|
|
|
|
}
|