You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/docker/proxy/queries/useSwarm.ts

41 lines
1.1 KiB

import { useQuery } from '@tanstack/react-query';
import { Swarm } 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';
import { useIsSwarm } from './useInfo';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
export function useSwarm<T = Swarm>(
environmentId: EnvironmentId,
{ select }: { select?(value: Swarm): T } = {}
) {
const isSwarm = useIsSwarm(environmentId);
return useQuery({
queryKey: [...queryKeys.base(environmentId), 'swarm'] as const,
queryFn: () => getSwarm(environmentId),
select,
enabled: isSwarm,
});
}
export async function getSwarm(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<Swarm>(
buildDockerProxyUrl(environmentId, 'swarm')
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve swarm information');
}
}
export function useSwarmId(environmentId: EnvironmentId) {
return useSwarm(environmentId, {
select: (swarm) => swarm.ID,
});
}