2024-04-14 17:54:25 +03:00
|
|
|
import { useQuery } from '@tanstack/react-query';
|
2023-09-07 15:14:03 +01:00
|
|
|
import { SystemVersion } from 'docker-types/generated/1.41';
|
2023-09-04 19:07:29 +01:00
|
|
|
|
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
|
2024-06-10 20:54:31 +02:00
|
|
|
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
|
2023-09-04 19:07:29 +01:00
|
|
|
|
|
|
|
export async function getVersion(environmentId: EnvironmentId) {
|
|
|
|
try {
|
2023-09-07 15:14:03 +01:00
|
|
|
const { data } = await axios.get<SystemVersion>(
|
2024-06-10 20:54:31 +02:00
|
|
|
buildDockerProxyUrl(environmentId, 'version')
|
2023-09-04 19:07:29 +01:00
|
|
|
);
|
|
|
|
return data;
|
|
|
|
} catch (err) {
|
2024-05-13 15:34:00 +12:00
|
|
|
throw parseAxiosError(err, 'Unable to retrieve version');
|
2023-09-04 19:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:14:03 +01:00
|
|
|
export function useVersion<TSelect = SystemVersion>(
|
2024-04-11 09:29:30 +03:00
|
|
|
environmentId?: EnvironmentId,
|
2023-09-07 15:14:03 +01:00
|
|
|
select?: (info: SystemVersion) => TSelect
|
2023-09-04 19:07:29 +01:00
|
|
|
) {
|
|
|
|
return useQuery(
|
2024-04-11 09:29:30 +03:00
|
|
|
['environment', environmentId!, 'docker', 'version'],
|
|
|
|
() => getVersion(environmentId!),
|
2023-09-04 19:07:29 +01:00
|
|
|
{
|
|
|
|
select,
|
2024-04-11 09:29:30 +03:00
|
|
|
enabled: !!environmentId,
|
2023-09-04 19:07:29 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-09-07 15:14:03 +01:00
|
|
|
|
2024-04-11 09:29:30 +03:00
|
|
|
export function useApiVersion(environmentId?: EnvironmentId) {
|
2023-09-07 15:14:03 +01:00
|
|
|
const query = useVersion(environmentId, (info) => info.ApiVersion);
|
|
|
|
return query.data ? parseFloat(query.data) : 0;
|
|
|
|
}
|